Skip to content

Commit

Permalink
Version 2.2.3.1 (2019-12-01)
Browse files Browse the repository at this point in the history
----------------------------
- Changed type handler registration in
[DocHandlerFacade](src/main/java/org/fugerit/java/doc/base/facade/DocHandlerFacade.java)
  • Loading branch information
fugerit79 committed Nov 30, 2019
1 parent d38e538 commit 2abde77
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 26 deletions.
4 changes: 4 additions & 0 deletions fj-doc-base/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public interface DocTypeHandler extends KeyString {

String getType();

String getModule();

String getMime();

void handle( DocInput docInput, DocOutput docOutput ) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class DocTypeHandlerDefault implements DocTypeHandler, Serializable {

private String type;

private String key;
private String module;

private String mime;

Expand All @@ -28,32 +28,37 @@ public String getMime() {

@Override
public String getKey() {
return this.key;
return createKey( this.getType() , this.getModule() ) ;
}

@Override
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, DocTypeHandler> 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 {
Expand All @@ -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 ) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class FreeMarkerFopTypeHandler extends DocTypeHandlerDefault {

public static DocTypeHandler HANDLER = new FreeMarkerFopTypeHandler();

public static final String MODULE = "fop";

/**
*
*/
Expand All @@ -30,7 +32,7 @@ public FreeMarkerFopTypeHandler() {
}

public FreeMarkerFopTypeHandler(String type) {
super(type);
super(type, MODULE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ 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;
import org.apache.pdfbox.pdmodel.font.PDCIDFont;
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;
Expand All @@ -62,7 +62,7 @@ public class PdfBoxDocHandler implements AutoCloseable {

private static HashMap<String, PDCIDFont> 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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
19 changes: 18 additions & 1 deletion fj-doc-sample/src/main/resources/config/doc-handler-sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
<data id="html-fragment-fm" info="fhtml" type="org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlFragmentTypeHandler" />
</factory>


<!-- by default last handler registered for a type will be used by default -->
<factory id="full-facade">
<data id="xml-doc" info="xml" type="org.fugerit.java.doc.base.config.DocTypeHandlerXML" />
<!-- jxl handlers -->
<data id="xls-jxl" info="xls" type="org.fugerit.java.doc.mod.jxl.XlsTypeHandler" />
<!-- itext handlers -->
<data id="rtf-itext" info="rtf" type="org.fugerit.java.doc.mod.itext.RtfTypeHandler" />
<data id="html-itext" info="html" type="org.fugerit.java.doc.mod.itext.HtmlTypeHandler" />
<data id="pdf-itext" info="pdf" type="org.fugerit.java.doc.mod.itext.PdfTypeHandler" />
<!-- free marker handlers -->
<data id="html-fm" info="html" type="org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandler" />
<data id="html-fragment-fm" info="fhtml" type="org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlFragmentTypeHandler" />
<!-- poi handlers -->
<data id="xls-poi" info="xls" type="org.fugerit.java.doc.mod.poi.XlsPoiTypeHandler" />
<data id="xlsx-poi" info="xlsx" type="org.fugerit.java.doc.mod.poi.XlsxPoiTypeHandler" />
<data id="fo-fop" info="fo" type="org.fugerit.java.doc.mod.fop.FreeMarkerFopTypeHandler" />
<data id="pdf-fop" info="pdf" type="org.fugerit.java.doc.mod.fop.PdfFopTypeHandler" />
</factory>

</doc-handler-config>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
</chain>

<chain id="free-marker-01-fop" extends="free-marker-01">
</chain>

<chain id="full-facade-01" extends="free-marker-01">
<step id="step-data" defaultBehaviour="CONTINUE"
description="Creates data necessary to FreeMarker"
type="test.org.fugerit.java.doc.sample.freemarker.TestFreeMarker01DataStep"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
Expand Down
Loading

0 comments on commit 2abde77

Please sign in to comment.