diff --git a/fj-doc-base/ChangeLog.md b/fj-doc-base/ChangeLog.md index 821e26368..8814b0611 100644 --- a/fj-doc-base/ChangeLog.md +++ b/fj-doc-base/ChangeLog.md @@ -1,3 +1,7 @@ +Version 2.2.3.1 (2019-12-01) +---------------------------- +- Changed type handler registration in [DocHandlerFacade](src/main/java/org/fugerit/java/doc/base/facade/DocHandlerFacade.java) + Version 2.2.2.4 (2019-11-30) ---------------------------- - Now one element is mandatory in element 'cell' (minOccurs='1') diff --git a/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandler.java b/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandler.java index b773f5765..6b1e0d5ff 100644 --- a/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandler.java +++ b/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandler.java @@ -6,6 +6,8 @@ public interface DocTypeHandler extends KeyString { String getType(); + String getModule(); + String getMime(); void handle( DocInput docInput, DocOutput docOutput ) throws Exception; diff --git a/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandlerDefault.java b/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandlerDefault.java index 768df8565..8bacaec32 100644 --- a/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandlerDefault.java +++ b/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandlerDefault.java @@ -13,7 +13,7 @@ public class DocTypeHandlerDefault implements DocTypeHandler, Serializable { private String type; - private String key; + private String module; private String mime; @@ -28,7 +28,7 @@ public String getMime() { @Override public String getKey() { - return this.key; + return createKey( this.getType() , this.getModule() ) ; } @Override @@ -36,24 +36,29 @@ public String getType() { return this.type; } + @Override + public String getModule() { + return module; + } + @Override public void handle(DocInput docInput, DocOutput docOutput) throws Exception { } - public DocTypeHandlerDefault(String type, String key, String mime) { + public DocTypeHandlerDefault(String type, String module, String mime) { super(); this.type = type; - this.key = key; + this.module = module; this.mime = mime; } - public DocTypeHandlerDefault(String type, String mime) { - this( type, type, mime ); + public DocTypeHandlerDefault(String type, String module ) { + this( type, module, null ); } - public DocTypeHandlerDefault(String type) { - this( type, type, null ); + public static final String createKey( String type, String mod ) { + return type+"-"+mod; } } diff --git a/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandlerXML.java b/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandlerXML.java index 0fbc8f474..a21cc13e1 100644 --- a/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandlerXML.java +++ b/fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandlerXML.java @@ -13,8 +13,12 @@ public class DocTypeHandlerXML extends DocTypeHandlerDefault { public static final DocTypeHandler HANDLER = new DocTypeHandlerXML(); + public static final String TYPE = DocConfig.TYPE_XML; + + public static final String MODULE = "doc"; + public DocTypeHandlerXML() { - super( DocConfig.TYPE_XML ); + super( TYPE, MODULE ); } @Override diff --git a/fj-doc-base/src/main/java/org/fugerit/java/doc/base/facade/DocHandlerFacade.java b/fj-doc-base/src/main/java/org/fugerit/java/doc/base/facade/DocHandlerFacade.java index e70e1e58c..da30314f9 100644 --- a/fj-doc-base/src/main/java/org/fugerit/java/doc/base/facade/DocHandlerFacade.java +++ b/fj-doc-base/src/main/java/org/fugerit/java/doc/base/facade/DocHandlerFacade.java @@ -9,22 +9,60 @@ import org.fugerit.java.doc.base.config.DocInput; import org.fugerit.java.doc.base.config.DocOutput; import org.fugerit.java.doc.base.config.DocTypeHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * DocHandlerFacade + * + * Starting from versione 2.2.3.1 registration of type handlers changes : + * Now is possible to register a type handler both for type (ex. pdf) or key (ex. pdf-fop, pdf-itext, pdf-box) + * + * @author fugerit + * + */ public class DocHandlerFacade implements Serializable { + private static final Logger logger = LoggerFactory.getLogger( DocHandlerFacade.class ); + /** * */ private static final long serialVersionUID = -8625371479549479952L; + public static final boolean DEFAULT_REGISTER_FOR_TYPE = true; + + public static final boolean DEFAULT_ERROR_ON_DUPLICATE = false; + private Map mapHandlers; public DocHandlerFacade() { this.mapHandlers = new HashMap<>(); } + + private void doRegister( DocTypeHandler handler, String id ) { + logger.info( "Registering handler with id {} : {}", id, handler.getClass().getName() ); + this.mapHandlers.put( id, handler ); + } + + public void registerHandler( DocTypeHandler handler, boolean registerForType, boolean errorOnDuplicate ) throws Exception { + doRegister( handler, handler.getKey() ); + if ( registerForType ) { + String type = handler.getType(); + DocTypeHandler previous = this.mapHandlers.get( type ); + if ( previous != null ) { + if ( errorOnDuplicate ) { + throw new ConfigException( "Duplicate handler for type : "+type ); + } else { + logger.warn( "Warning duplicate handler for type, {} will replace {}", type, handler.getKey(), previous.getKey() ); + } + } + doRegister(handler, type); + } + } public void registerHandler( DocTypeHandler handler ) throws Exception { - this.mapHandlers.put( handler.getKey(), handler ); + this.registerHandler( handler, DEFAULT_REGISTER_FOR_TYPE, DEFAULT_ERROR_ON_DUPLICATE ); } public void handle( DocInput docInput, DocOutput docOutput ) throws Exception { @@ -37,6 +75,9 @@ public void handle( DocInput docInput, DocOutput docOutput ) throws Exception { } } + public DocTypeHandler findHandler( String id ) { + return this.mapHandlers.get( id ); + } public void register( String factoryCatalogPath ) { diff --git a/fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/helper/FreeMarkerDocHelperTypeHandler.java b/fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/helper/FreeMarkerDocHelperTypeHandler.java index 5aa70b44b..2d1a63013 100644 --- a/fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/helper/FreeMarkerDocHelperTypeHandler.java +++ b/fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/helper/FreeMarkerDocHelperTypeHandler.java @@ -14,13 +14,15 @@ public class FreeMarkerDocHelperTypeHandler extends DocTypeHandlerDefault { public static final String ATT_DOCBASE = "docBase"; + public static final String MODULE = "fm"; + /** * */ private static final long serialVersionUID = -7394516771708L; public FreeMarkerDocHelperTypeHandler(String type, String fmDocChainId) { - super(type); + super(type, MODULE); this.fmDocChainId = fmDocChainId; } diff --git a/fj-doc-mod-fop/src/main/java/org/fugerit/java/doc/mod/fop/FreeMarkerFopTypeHandler.java b/fj-doc-mod-fop/src/main/java/org/fugerit/java/doc/mod/fop/FreeMarkerFopTypeHandler.java index a12d46613..51de20798 100644 --- a/fj-doc-mod-fop/src/main/java/org/fugerit/java/doc/mod/fop/FreeMarkerFopTypeHandler.java +++ b/fj-doc-mod-fop/src/main/java/org/fugerit/java/doc/mod/fop/FreeMarkerFopTypeHandler.java @@ -20,6 +20,8 @@ public class FreeMarkerFopTypeHandler extends DocTypeHandlerDefault { public static DocTypeHandler HANDLER = new FreeMarkerFopTypeHandler(); + public static final String MODULE = "fop"; + /** * */ @@ -30,7 +32,7 @@ public FreeMarkerFopTypeHandler() { } public FreeMarkerFopTypeHandler(String type) { - super(type); + super(type, MODULE); } @Override diff --git a/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/HtmlTypeHandler.java b/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/HtmlTypeHandler.java index e6c381e4c..ec2222d5d 100644 --- a/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/HtmlTypeHandler.java +++ b/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/HtmlTypeHandler.java @@ -22,7 +22,7 @@ public class HtmlTypeHandler extends DocTypeHandlerDefault { public static final DocTypeHandler HANDLER = new HtmlTypeHandler(); public HtmlTypeHandler() { - super( ITextDocHandler.DOC_OUTPUT_HTML ); + super( ITextDocHandler.DOC_OUTPUT_HTML, ITextDocHandler.MODULE ); } @Override diff --git a/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/ITextDocHandler.java b/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/ITextDocHandler.java index 00524db06..d350edbd2 100644 --- a/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/ITextDocHandler.java +++ b/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/ITextDocHandler.java @@ -35,6 +35,7 @@ The Apache Software Foundation (http://www.apache.org/). import org.fugerit.java.core.log.LogFacade; import org.fugerit.java.core.util.regex.ParamFinder; +import org.fugerit.java.doc.base.config.DocConfig; import org.fugerit.java.doc.base.helper.SourceResolverHelper; import org.fugerit.java.doc.base.model.DocBarcode; import org.fugerit.java.doc.base.model.DocBase; @@ -129,11 +130,13 @@ private static void setStyle( DocStyle parent, DocStyle current ) { private String docType; - public final static String DOC_OUTPUT_HTML = "html"; + public final static String MODULE = "itext"; - public final static String DOC_OUTPUT_PDF = "pdf"; + public final static String DOC_OUTPUT_HTML = DocConfig.TYPE_HTML; - public final static String DOC_OUTPUT_RTF = "rtf"; + public final static String DOC_OUTPUT_PDF = DocConfig.TYPE_PDF; + + public final static String DOC_OUTPUT_RTF = DocConfig.TYPE_RTF; public final static String DOC_DEFAULT_FONT_NAME = "default-font-name"; public final static String DOC_DEFAULT_FONT_SIZE = "default-font-size"; diff --git a/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/PdfTypeHandler.java b/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/PdfTypeHandler.java index 73ec7a007..c77d25ef0 100644 --- a/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/PdfTypeHandler.java +++ b/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/PdfTypeHandler.java @@ -23,7 +23,7 @@ public class PdfTypeHandler extends DocTypeHandlerDefault { public static DocTypeHandler HANDLER = new PdfTypeHandler(); public PdfTypeHandler() { - super( ITextDocHandler.DOC_OUTPUT_PDF ); + super( ITextDocHandler.DOC_OUTPUT_PDF, ITextDocHandler.MODULE ); } @Override diff --git a/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/RtfTypeHandler.java b/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/RtfTypeHandler.java index 94f0845ac..382f8ae86 100644 --- a/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/RtfTypeHandler.java +++ b/fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/RtfTypeHandler.java @@ -23,7 +23,7 @@ public class RtfTypeHandler extends DocTypeHandlerDefault { public static final DocTypeHandler HANDLER = new RtfTypeHandler(); public RtfTypeHandler() { - super( ITextDocHandler.DOC_OUTPUT_RTF ); + super( ITextDocHandler.DOC_OUTPUT_RTF, ITextDocHandler.MODULE ); } @Override diff --git a/fj-doc-mod-jxl/src/main/java/org/fugerit/java/doc/mod/jxl/XlsTypeHandler.java b/fj-doc-mod-jxl/src/main/java/org/fugerit/java/doc/mod/jxl/XlsTypeHandler.java index da0bf5153..cdca08ee0 100644 --- a/fj-doc-mod-jxl/src/main/java/org/fugerit/java/doc/mod/jxl/XlsTypeHandler.java +++ b/fj-doc-mod-jxl/src/main/java/org/fugerit/java/doc/mod/jxl/XlsTypeHandler.java @@ -53,8 +53,10 @@ public class XlsTypeHandler extends DocTypeHandlerDefault { public static DocTypeHandler HANDLER = new XlsTypeHandler(); + public static final String MODULE = "jxl"; + public XlsTypeHandler() { - super( DocConfig.TYPE_XLS ); + super( DocConfig.TYPE_XLS, MODULE ); } @Override diff --git a/fj-doc-mod-pdfbox/src/main/java/org/fugerit/java/doc/mod/pdfbox/PdfBoxDocHandler.java b/fj-doc-mod-pdfbox/src/main/java/org/fugerit/java/doc/mod/pdfbox/PdfBoxDocHandler.java index 6b21c73f3..34e85bf28 100644 --- a/fj-doc-mod-pdfbox/src/main/java/org/fugerit/java/doc/mod/pdfbox/PdfBoxDocHandler.java +++ b/fj-doc-mod-pdfbox/src/main/java/org/fugerit/java/doc/mod/pdfbox/PdfBoxDocHandler.java @@ -31,7 +31,6 @@ The Apache Software Foundation (http://www.apache.org/). import java.util.Iterator; import java.util.Properties; -import org.apache.pdfbox.contentstream.PDContentStream; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; @@ -39,6 +38,7 @@ The Apache Software Foundation (http://www.apache.org/). import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.fugerit.java.core.util.regex.ParamFinder; +import org.fugerit.java.doc.base.config.DocConfig; import org.fugerit.java.doc.base.model.DocBase; import org.fugerit.java.doc.base.model.DocElement; import org.fugerit.java.doc.base.model.DocPhrase; @@ -62,7 +62,7 @@ public class PdfBoxDocHandler implements AutoCloseable { private static HashMap fonts = new HashMap<>(); - public final static String DOC_OUTPUT_PDF = "pdf"; + public final static String DOC_OUTPUT_PDF = DocConfig.TYPE_PDF; public final static String DOC_DEFAULT_FONT_NAME = "default-font-name"; public final static String DOC_DEFAULT_FONT_SIZE = "default-font-size"; diff --git a/fj-doc-mod-pdfbox/src/main/java/org/fugerit/java/doc/mod/pdfbox/PdfBoxTypeHandler.java b/fj-doc-mod-pdfbox/src/main/java/org/fugerit/java/doc/mod/pdfbox/PdfBoxTypeHandler.java index 3003d2696..0684d31f6 100644 --- a/fj-doc-mod-pdfbox/src/main/java/org/fugerit/java/doc/mod/pdfbox/PdfBoxTypeHandler.java +++ b/fj-doc-mod-pdfbox/src/main/java/org/fugerit/java/doc/mod/pdfbox/PdfBoxTypeHandler.java @@ -11,10 +11,17 @@ public class PdfBoxTypeHandler extends DocTypeHandlerDefault { + /** + * + */ + private static final long serialVersionUID = 7188424476250673707L; + + public static final String MODULE = "box"; + public static DocTypeHandler HANDLER = new PdfBoxTypeHandler(); public PdfBoxTypeHandler() { - super( PdfBoxDocHandler.DOC_OUTPUT_PDF ); + super( PdfBoxDocHandler.DOC_OUTPUT_PDF, MODULE ); } @Override diff --git a/fj-doc-mod-poi/src/main/java/org/fugerit/java/doc/mod/poi/BasicPoiTypeHandler.java b/fj-doc-mod-poi/src/main/java/org/fugerit/java/doc/mod/poi/BasicPoiTypeHandler.java index b70f0e11e..a4ff94295 100644 --- a/fj-doc-mod-poi/src/main/java/org/fugerit/java/doc/mod/poi/BasicPoiTypeHandler.java +++ b/fj-doc-mod-poi/src/main/java/org/fugerit/java/doc/mod/poi/BasicPoiTypeHandler.java @@ -48,8 +48,10 @@ public abstract class BasicPoiTypeHandler extends DocTypeHandlerDefault { */ private static final long serialVersionUID = 1175953200917290547L; + public static final String MODULE = "poi"; + public BasicPoiTypeHandler(String type) { - super(type); + super(type, MODULE); } protected abstract Workbook newWorkbook( DocInput docInput, InputStream is ) throws Exception; diff --git a/fj-doc-sample/src/main/java/org/fugerit/java/doc/sample/facade/SampleFacade.java b/fj-doc-sample/src/main/java/org/fugerit/java/doc/sample/facade/SampleFacade.java index 9d5c47ace..ea60183bb 100644 --- a/fj-doc-sample/src/main/java/org/fugerit/java/doc/sample/facade/SampleFacade.java +++ b/fj-doc-sample/src/main/java/org/fugerit/java/doc/sample/facade/SampleFacade.java @@ -56,6 +56,8 @@ private static DocHandlerFactory init( String path ) { public static final String ALT_HTML_FM = "alternate-html-fm"; + public static final String ALT_FULL_FACADE = "full-facade"; + public static DocHandlerFacade getFacade( String id ) { return FACTORY.get( id ); } diff --git a/fj-doc-sample/src/main/resources/config/doc-handler-sample.xml b/fj-doc-sample/src/main/resources/config/doc-handler-sample.xml index 8e300bd12..dab6219b8 100644 --- a/fj-doc-sample/src/main/resources/config/doc-handler-sample.xml +++ b/fj-doc-sample/src/main/resources/config/doc-handler-sample.xml @@ -28,6 +28,23 @@ - + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/fj-doc-sample/src/main/resources/config/doc-process-sample.xml b/fj-doc-sample/src/main/resources/config/doc-process-sample.xml index 47eca297f..222224562 100644 --- a/fj-doc-sample/src/main/resources/config/doc-process-sample.xml +++ b/fj-doc-sample/src/main/resources/config/doc-process-sample.xml @@ -33,6 +33,9 @@ + + + diff --git a/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/facade/BasicFacadeTest.java b/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/facade/BasicFacadeTest.java index 823e70fa6..a3e0fd007 100644 --- a/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/facade/BasicFacadeTest.java +++ b/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/facade/BasicFacadeTest.java @@ -7,11 +7,13 @@ import java.util.ArrayList; import java.util.List; +import org.fugerit.java.core.cfg.ConfigException; import org.fugerit.java.core.util.checkpoint.CheckpointFormatHelper; import org.fugerit.java.core.util.checkpoint.Checkpoints; import org.fugerit.java.doc.base.config.DocConfig; import org.fugerit.java.doc.base.config.DocInput; import org.fugerit.java.doc.base.config.DocOutput; +import org.fugerit.java.doc.base.config.DocTypeHandler; import org.fugerit.java.doc.base.facade.DocFacade; import org.fugerit.java.doc.base.facade.DocHandlerFacade; import org.fugerit.java.doc.base.model.DocBase; @@ -80,13 +82,23 @@ protected DocBase getDocBase() throws Exception { } public void produce( File outputFolder, String facadeId, DocBase doc, Reader reader, String baseName, String type ) throws Exception { - File file = new File( outputFolder, baseName + "." + type); + DocHandlerFacade facade = SampleFacade.getFacade( facadeId ); + DocTypeHandler handler = facade.findHandler( type ); + StringBuilder append = new StringBuilder(); + if ( handler == null ) { + throw new ConfigException( "No handler with id : "+type ); + } else if ( !handler.getType().equalsIgnoreCase( type ) ) { + append.append( "_" ); + append.append( handler.getModule() ); + } + append.append( "." ); + append.append( handler.getType() ); + File file = new File( outputFolder, baseName + append.toString() ); logger.info("Create file {}", file.getCanonicalPath()); try (FileOutputStream fos = new FileOutputStream(file)) { long start = System.currentTimeMillis(); DocInput input = DocInput.newInput( type , reader ); DocOutput output = DocOutput.newOutput( fos ); - DocHandlerFacade facade = SampleFacade.getFacade( facadeId ); facade.handle( input , output ); this.checkpoints.addCheckpointFromStartTime( "PRODUCE-"+type, start ); } diff --git a/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/freemarker/TestFreeMarker01FullFacade.java b/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/freemarker/TestFreeMarker01FullFacade.java new file mode 100644 index 000000000..6cae31ae3 --- /dev/null +++ b/fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/freemarker/TestFreeMarker01FullFacade.java @@ -0,0 +1,18 @@ +package test.org.fugerit.java.doc.sample.freemarker; + +import org.fugerit.java.doc.mod.fop.FreeMarkerFopTypeHandler; +import org.fugerit.java.doc.mod.fop.PdfFopTypeHandler; +import org.fugerit.java.doc.mod.itext.PdfTypeHandler; +import org.fugerit.java.doc.sample.facade.SampleFacade; + +public class TestFreeMarker01FullFacade extends BasicFreeMarkerTest { + + public TestFreeMarker01FullFacade() { + super( "full-facade-01", + FreeMarkerFopTypeHandler.HANDLER.getKey(), + PdfFopTypeHandler.HANDLER.getKey(), + PdfTypeHandler.HANDLER.getKey() ); + this.setFacadeId( SampleFacade.ALT_FULL_FACADE ); + } + +}