001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thierry Delprat 018 */ 019package org.nuxeo.template.processors.jxls; 020 021import java.io.File; 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.List; 025import java.util.Map; 026 027import net.sf.jxls.exception.ParsePropertyException; 028import net.sf.jxls.transformer.XLSTransformer; 029 030import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 031import org.nuxeo.common.utils.FileUtils; 032import org.nuxeo.ecm.core.api.Blob; 033import org.nuxeo.ecm.core.api.Blobs; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.core.api.NuxeoException; 036import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry; 037import org.nuxeo.runtime.api.Framework; 038import org.nuxeo.template.api.TemplateInput; 039import org.nuxeo.template.api.TemplateProcessor; 040import org.nuxeo.template.api.adapters.TemplateBasedDocument; 041import org.nuxeo.template.context.SimpleContextBuilder; 042import org.nuxeo.template.processors.AbstractTemplateProcessor; 043 044/** 045 * JXLS {@link TemplateProcessor} 046 * 047 * @author <a href="mailto:[email protected]">Tiry</a> 048 */ 049public class JXLSTemplateProcessor extends AbstractTemplateProcessor { 050 051 public static final String TEMPLATE_TYPE = "JXLS"; 052 053 protected SimpleContextBuilder contextBuilder = new SimpleContextBuilder(); 054 055 @Override 056 public Blob renderTemplate(TemplateBasedDocument templateBasedDocument, String templateName) throws IOException { 057 058 Blob sourceTemplateBlob = getSourceTemplateBlob(templateBasedDocument, templateName); 059 List<TemplateInput> params = templateBasedDocument.getParams(templateName); 060 061 DocumentModel doc = templateBasedDocument.getAdaptedDoc(); 062 Map<String, Object> ctx = contextBuilder.build(doc, templateName); 063 064 JXLSBindingResolver resolver = new JXLSBindingResolver(); 065 066 resolver.resolve(params, ctx, templateBasedDocument); 067 068 File workingDir = getWorkingDir(); 069 File generated = new File(workingDir, "JXLSresult-" + System.currentTimeMillis()); 070 generated.createNewFile(); 071 072 File input = new File(workingDir, "JXLSInput-" + System.currentTimeMillis()); 073 input.createNewFile(); 074 075 sourceTemplateBlob.transferTo(input); 076 077 XLSTransformer transformer = new XLSTransformer(); 078 configureTransformer(transformer); 079 try { 080 transformer.transformXLS(input.getAbsolutePath(), ctx, generated.getAbsolutePath()); 081 } catch (InvalidFormatException | ParsePropertyException e) { 082 throw new NuxeoException(e); 083 } 084 085 input.delete(); 086 087 Blob newBlob = Blobs.createBlob(generated); 088 089 String templateFileName = sourceTemplateBlob.getFilename(); 090 091 // set the output file name 092 String targetFileExt = FileUtils.getFileExtension(templateFileName); 093 String targetFileName = FileUtils.getFileNameNoExt(templateBasedDocument.getAdaptedDoc().getTitle()); 094 targetFileName = targetFileName + "." + targetFileExt; 095 newBlob.setFilename(targetFileName); 096 MimetypeRegistry mimetypeRegistry = Framework.getService(MimetypeRegistry.class); 097 newBlob.setMimeType(mimetypeRegistry.getMimetypeFromExtension(targetFileExt)); 098 099 // mark the file for automatic deletion on GC 100 Framework.trackFile(generated, newBlob); 101 return newBlob; 102 103 } 104 105 protected void configureTransformer(XLSTransformer transformer) { 106 // NOP but subclass may use this to register a CellProcessor or a 107 // RowProcessor 108 } 109 110 @Override 111 public List<TemplateInput> getInitialParametersDefinition(Blob blob) { 112 return new ArrayList<TemplateInput>(); 113 } 114 115}