Skip to content

Commit d44045b

Browse files
committed
Added pdf a support #43
1 parent bd370a5 commit d44045b

File tree

9 files changed

+413
-76
lines changed

9 files changed

+413
-76
lines changed

fj-doc-base/src/main/docs/fdp_xsd_config_ref.html

Lines changed: 206 additions & 14 deletions
Large diffs are not rendered by default.

fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public class DocConfig {
77
public static final String TYPE_XML = "xml";
88

99
public static final String TYPE_PDF = "pdf";
10+
public static final String FORMAT_PDF_A_1A = "PDF/A-1a";
11+
public static final String FORMAT_PDF_A_1B = "PDF/A-1b";
1012

1113
public static final String TYPE_RTF = "rtf";
1214

fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public interface DocTypeHandler extends KeyString {
1212

1313
String getMime();
1414

15+
String getFormat();
16+
1517
Charset getCharset();
1618

1719
void handle( DocInput docInput, DocOutput docOutput ) throws Exception;

fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/DocTypeHandlerDefault.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
import org.w3c.dom.Element;
1111
import org.w3c.dom.NodeList;
1212

13+
import lombok.AccessLevel;
14+
import lombok.Setter;
15+
import lombok.extern.slf4j.Slf4j;
16+
17+
@Slf4j
1318
public class DocTypeHandlerDefault extends XMLConfigurableObject implements DocTypeHandler, Serializable {
1419

1520
/**
@@ -19,6 +24,8 @@ public class DocTypeHandlerDefault extends XMLConfigurableObject implements DocT
1924

2025
public static final String TAG_NAME_CONFIG = "config";
2126

27+
public static final String TAG_NAME_CONFIG_ALT = "docHandlerCustomConfig";
28+
2229
public static final String ATT_NAME_CHARSET = "charset";
2330

2431
private String type;
@@ -27,6 +34,8 @@ public class DocTypeHandlerDefault extends XMLConfigurableObject implements DocT
2734

2835
private String mime;
2936

37+
@Setter(value = AccessLevel.PROTECTED) private String format;
38+
3039
private Charset charset;
3140

3241
@Override
@@ -40,7 +49,7 @@ public String getMime() {
4049

4150
@Override
4251
public String getKey() {
43-
return createKey( this.getType() , this.getModule() ) ;
52+
return createKey( this.getFormat() , this.getModule() ) ;
4453
}
4554

4655
@Override
@@ -53,6 +62,11 @@ public String getModule() {
5362
return module;
5463
}
5564

65+
@Override
66+
public String getFormat() {
67+
return StringUtils.valueWithDefault( this.format , this.getType() );
68+
}
69+
5670
@Override
5771
public Charset getCharset() {
5872
return charset;
@@ -64,10 +78,15 @@ public void handle(DocInput docInput, DocOutput docOutput) throws Exception {
6478
}
6579

6680
public DocTypeHandlerDefault(String type, String module, String mime, Charset charset) {
81+
this( type, module, mime, charset, null );
82+
}
83+
84+
public DocTypeHandlerDefault(String type, String module, String mime, Charset charset, String format) {
6785
super();
6886
this.type = type;
6987
this.module = module;
7088
this.mime = mime;
89+
this.format = format;
7190
this.charset = DocCharsetProvider.getDefaultProvider().resolveCharset(charset);
7291
}
7392

@@ -87,17 +106,37 @@ protected void handleConfigTag( Element config ) throws ConfigException {
87106

88107
}
89108

90-
@Override
91-
public void configure(Element tag) throws ConfigException {
92-
NodeList nl = tag.getElementsByTagName( TAG_NAME_CONFIG );
109+
private Element lookupConfig( Element tag, String tagName ) {
110+
Element configTag = null;
111+
NodeList nl = tag.getElementsByTagName( tagName );
93112
if ( nl.getLength() > 0 ) {
94-
Element config = (Element)nl.item( 0 );
95-
String charsetAtt = config.getAttribute( ATT_NAME_CHARSET );
113+
configTag = (Element)nl.item( 0 );
114+
String charsetAtt = configTag.getAttribute( ATT_NAME_CHARSET );
96115
if ( StringUtils.isNotEmpty( charsetAtt ) ) {
97116
this.charset = Charset.forName( charsetAtt );
98117
}
99-
this.handleConfigTag(config);
118+
}
119+
return configTag;
120+
}
121+
122+
@Override
123+
public void configure(Element tag) throws ConfigException {
124+
log.info( "configure : {}", tag.getAttribute( "id" ) );
125+
Element configTag = this.lookupConfig(tag, TAG_NAME_CONFIG_ALT );
126+
if ( configTag == null ) {
127+
configTag = this.lookupConfig(tag, TAG_NAME_CONFIG );
128+
}
129+
if ( configTag != null ) {
130+
this.handleConfigTag(configTag);
100131
}
101132
}
102133

134+
@Override
135+
public String toString() {
136+
return this.getClass().getSimpleName()+" [type=" + type + ", module=" + module + ", format=" + format + "]";
137+
}
138+
139+
140+
141+
103142
}

fj-doc-base/src/main/java/org/fugerit/java/doc/base/facade/DocHandlerFacade.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import org.fugerit.java.doc.base.config.DocInput;
1313
import org.fugerit.java.doc.base.config.DocOutput;
1414
import org.fugerit.java.doc.base.config.DocTypeHandler;
15-
import org.slf4j.Logger;
16-
import org.slf4j.LoggerFactory;
15+
16+
import lombok.extern.slf4j.Slf4j;
1717

1818
/**
1919
* DocHandlerFacade
@@ -24,10 +24,9 @@
2424
* @author fugerit
2525
*
2626
*/
27+
@Slf4j
2728
public class DocHandlerFacade implements Serializable {
2829

29-
private static final Logger logger = LoggerFactory.getLogger( DocHandlerFacade.class );
30-
3130
/**
3231
*
3332
*/
@@ -47,30 +46,33 @@ public DocHandlerFacade() {
4746
}
4847

4948
private void doRegister( DocTypeHandler handler, String id ) {
50-
logger.info( "Registering handler with id {} : {}", id, handler.getClass().getName() );
49+
log.info( "Registering handler with id {} : {}", id, handler.getClass().getName() );
5150
this.mapHandlers.put( id, handler );
52-
ListMapStringKey<DocTypeHandler> list = this.mapTypeHandlers.get( handler.getType() );
51+
ListMapStringKey<DocTypeHandler> list = this.mapTypeHandlers.get( handler.getFormat() );
5352
if ( list == null ) {
5453
list = new ListMapStringKey<DocTypeHandler>();
55-
this.mapTypeHandlers.put( handler.getType() , list );
54+
this.mapTypeHandlers.put( handler.getFormat() , list );
5655
}
5756
list.add( handler );
5857
}
5958

6059
public void registerHandler( DocTypeHandler handler, boolean registerForType, boolean errorOnDuplicate ) throws Exception {
6160
doRegister( handler, handler.getKey() );
6261
if ( registerForType ) {
62+
String format = handler.getFormat();
6363
String type = handler.getType();
64-
DocTypeHandler previous = this.mapHandlers.get( type );
64+
DocTypeHandler previous = this.mapHandlers.get( format );
6565
if ( previous != null ) {
6666
if ( errorOnDuplicate ) {
67-
throw new ConfigException( "Duplicate handler for type : "+type );
67+
throw new ConfigException( "Duplicate handler for format : "+format+" (type:"+type+")" );
6868
} else {
69-
logger.warn( "Warning duplicate handler for type, {} will replace {}", type, handler.getKey(), previous.getKey() );
69+
log.warn( "Warning duplicate handler for format, {} will replace {}", format, handler.getKey(), previous.getKey() );
7070
}
7171
}
72-
doRegister(handler, type);
72+
doRegister(handler, format);
7373
}
74+
log.info( "list keys current -> {} : list {}", handler, this.mapHandlers.keySet() );
75+
log.debug( "test" );
7476
}
7577

7678
public void registerHandler( DocTypeHandler handler ) throws Exception {

fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/config/FreeMarkerConfigStep.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.fugerit.java.doc.base.process.DocProcessContext;
1212
import org.fugerit.java.doc.base.process.DocProcessData;
1313
import org.fugerit.java.doc.base.process.DocProcessorBasic;
14-
import org.fugerit.java.doc.freemarker.process.ConfigInitModel;
1514

1615
import freemarker.template.Configuration;
1716
import freemarker.template.TemplateExceptionHandler;
@@ -22,10 +21,10 @@ public class FreeMarkerConfigStep extends DocProcessorBasic {
2221
public static final String ATT_DEFAULT = "FreeMarkerConfigStep.DEFAULT";
2322

2423
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION = "version";
25-
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_31 = ConfigInitModel.VERSION_2_3_31;
24+
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_31 = "2.3.31";
2625
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_30 = "2.3.30";
2726
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_29 = "2.3.29";
28-
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_DEFAULT = ConfigInitModel.DEFAULT_VERSION;
27+
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_DEFAULT = ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_31;
2928

3029
public static final String ATT_FREEMARKER_CONFIG_KEY_MODE = "mode";
3130
public static final String ATT_FREEMARKER_CONFIG_KEY_MODE_CLASS = "class";

fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/process/FreemarkerDocProcessConfigFacade.java

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import javax.xml.parsers.DocumentBuilderFactory;
88

99
import org.fugerit.java.core.cfg.ConfigException;
10-
import org.fugerit.java.core.cfg.xml.FactoryType;
11-
import org.fugerit.java.core.cfg.xml.FactoryTypeHelper;
10+
import org.fugerit.java.core.cfg.ConfigurableObject;
11+
import org.fugerit.java.core.cfg.helpers.UnsafeHelper;
1212
import org.fugerit.java.core.cfg.xml.XmlBeanHelper;
1313
import org.fugerit.java.core.lang.helpers.ClassHelper;
1414
import org.fugerit.java.core.lang.helpers.StringUtils;
@@ -38,14 +38,14 @@ public class FreemarkerDocProcessConfigFacade {
3838

3939
public static final String ATT_CHAIN_STEP = "chainStep";
4040

41+
public static final String ATT_STEP_TYPE = "stepType";
42+
4143
public static final String STEP_TYPE_CONFIG = "config";
4244

4345
public static final String STEP_TYPE_FUNCTION = "function";
4446

4547
public static final String STEP_TYPE_MAP = "map";
4648

47-
private static final FactoryTypeHelper<DocTypeHandler> HELPER = FactoryTypeHelper.newInstance( DocTypeHandler.class );
48-
4949
public static FreemarkerDocProcessConfig newSimpleConfig( String id, String templatePath ) throws ConfigException {
5050
FreemarkerDocProcessConfig config = new FreemarkerDocProcessConfig();
5151
config.setDefaultChain(
@@ -76,6 +76,22 @@ public MiniFilterChain newDefaultChain(String id) {
7676
return config;
7777
}
7878

79+
private static DocTypeHandler createHelper( Element docHandlerConfig ) throws ConfigException {
80+
String type = docHandlerConfig.getAttribute( "type" );
81+
log.info( "factoryType : {} , resultType : {}", docHandlerConfig, type );
82+
DocTypeHandler res = null;
83+
try {
84+
res = (DocTypeHandler)ClassHelper.newInstance( type );
85+
if ( res instanceof ConfigurableObject ) {
86+
log.info( "ConfigurableObject -> try configure()" );
87+
((ConfigurableObject)res).configure( (Element)docHandlerConfig );
88+
}
89+
} catch (Exception | NoClassDefFoundError e) {
90+
UnsafeHelper.handleUnsafe( new ConfigException( "Type cannot be loaded : "+e, e ), docHandlerConfig.getAttribute( "unsafe"), docHandlerConfig.getAttribute( "unsafeMode") );
91+
}
92+
return res;
93+
}
94+
7995
public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws ConfigException {
8096
FreemarkerDocProcessConfig result = null;
8197
try {
@@ -88,12 +104,12 @@ public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws C
88104
NodeList docHandlerConfigList = doc.getElementsByTagName( ATT_DOC_HANDLER_CONFIG );
89105
if ( docHandlerConfigList.getLength() == 1 ) {
90106
Element docHandlerConfigTag = (Element) docHandlerConfigList.item( 0 );
91-
NodeList docHandlerList = docHandlerConfigTag.getElementsByTagName( "data" );
107+
NodeList docHandlerList = docHandlerConfigTag.getElementsByTagName( "docHandler" );
92108
log.info( "docHandlerList -> {}", docHandlerList.getLength() );
93109
for ( int k=0; k<docHandlerList.getLength(); k++ ) {
94-
FactoryType current = new FactoryType();
95-
XmlBeanHelper.setFromElement( current, (Element)docHandlerList.item( k ) );
96-
config.getFacade().registerHandler( HELPER.createHelper( current ) );
110+
Element currentHandlerTag = (Element)docHandlerList.item( k );
111+
DocTypeHandler handler = createHelper( currentHandlerTag );
112+
config.getFacade().registerHandler( handler );
97113
}
98114

99115
}
@@ -117,8 +133,8 @@ public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws C
117133
Element currentChainStepTag = (Element) chainStepList.item(i);
118134
ChainStepModel chainStepModel = new ChainStepModel();
119135
Properties atts = DOMUtils.attributesToProperties( currentChainStepTag );
120-
chainStepModel.setStepType( atts.getProperty( "stepType" ) );
121-
atts.remove( "stepType" );
136+
chainStepModel.setStepType( atts.getProperty( ATT_STEP_TYPE ) );
137+
atts.remove( ATT_STEP_TYPE );
122138
chainStepModel.setAttributes(atts);
123139
if ( STEP_TYPE_CONFIG.equalsIgnoreCase( chainStepModel.getStepType() ) ) {
124140
NodeList configList = currentChainStepTag.getElementsByTagName( STEP_TYPE_CONFIG );
@@ -178,14 +194,22 @@ public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws C
178194

179195
private static Properties convertConfiguration( Properties props ) {
180196
Properties params = new Properties();
181-
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_VERSION , props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_VERSION, ConfigInitModel.DEFAULT_VERSION ) );
182-
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_MODE , props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_MODE, ConfigInitModel.DEFAULT_MODE) );
183-
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_PATH , props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_PATH ) );
184-
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_CLASS , props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_CLASS, ConfigInitModel.DEFAULT_CLASS_NAME) );
185-
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_EXCEPTION_HANDLER , props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_EXCEPTION_HANDLER, ConfigInitModel.DEFAULT_EXCEPTION_HANDLER) );
186-
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_LOG_EXCEPTION , props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_LOG_EXCEPTION, ConfigInitModel.DEFAULT_LOG_EXCEPTION) );
187-
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_WRAP_UNCHECKED_EXCEPTION , props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_WRAP_UNCHECKED_EXCEPTION, ConfigInitModel.DEFAULT_WRAP_UNCHECKED_EXCEPTION) );
188-
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_FALLBACK_ON_NULL_LOOP_VARIABLE , props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_FALLBACK_ON_NULL_LOOP_VARIABLE, ConfigInitModel.DEFAULT_FALL_BACK_ON_NULL_LOOP_VARIABLE) );
197+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_VERSION ,
198+
props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_VERSION, FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_VERSION_DEFAULT ) );
199+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_MODE ,
200+
props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_MODE, FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_MODE_CLASS ) );
201+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_PATH ,
202+
props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_PATH ) );
203+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_CLASS ,
204+
props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_CLASS, FreemarkerDocProcessConfigFacade.class.getName() ) );
205+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_EXCEPTION_HANDLER ,
206+
props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_EXCEPTION_HANDLER, FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_EXCEPTION_HANDLER_DEFAULT ) );
207+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_LOG_EXCEPTION ,
208+
props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_LOG_EXCEPTION, FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_LOG_EXCEPTION_DEFAULT ) );
209+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_WRAP_UNCHECKED_EXCEPTION ,
210+
props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_WRAP_UNCHECKED_EXCEPTION, FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_WRAP_UNCHECKED_EXCEPTION_DEFAULT ) );
211+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_FALLBACK_ON_NULL_LOOP_VARIABLE ,
212+
props.getProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_FALLBACK_ON_NULL_LOOP_VARIABLE, FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_FALLBACK_ON_NULL_LOOP_VARIABLE_DEFAULT ) );
189213
return params;
190214
}
191215

0 commit comments

Comments
 (0)