Skip to content

Commit 9ab4da0

Browse files
committed
Added new freemarker configuration model #38
new FreemarkerDocProcessConfig simplified doc chain configuration
1 parent 65b475c commit 9ab4da0

File tree

12 files changed

+431
-7
lines changed

12 files changed

+431
-7
lines changed

fj-doc-freemarker/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<groupId>org.fugerit.java</groupId>
4848
<artifactId>fj-doc-base</artifactId>
4949
</dependency>
50-
50+
5151
</dependencies>
5252

5353
<organization>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
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;
1415

1516
import freemarker.template.Configuration;
1617
import freemarker.template.TemplateExceptionHandler;
@@ -21,9 +22,10 @@ public class FreeMarkerConfigStep extends DocProcessorBasic {
2122
public static final String ATT_DEFAULT = "FreeMarkerConfigStep.DEFAULT";
2223

2324
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;
2426
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_30 = "2.3.30";
2527
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_29 = "2.3.29";
26-
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_DEFAULT = ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_30;
28+
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_DEFAULT = ConfigInitModel.DEFAULT_VERSION;
2729

2830
public static final String ATT_FREEMARKER_CONFIG_KEY_MODE = "mode";
2931
public static final String ATT_FREEMARKER_CONFIG_KEY_MODE_CLASS = "class";
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.fugerit.java.doc.freemarker.process;
2+
3+
import java.io.Serializable;
4+
import java.io.StringWriter;
5+
import java.io.Writer;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.Properties;
9+
10+
import org.fugerit.java.core.cfg.xml.IdConfigType;
11+
import org.fugerit.java.core.lang.helpers.BooleanUtils;
12+
import org.fugerit.java.core.util.collection.KeyString;
13+
import org.fugerit.java.core.util.regex.ParamFinder;
14+
import org.fugerit.java.doc.base.process.DocProcessContext;
15+
import org.fugerit.java.doc.base.process.DocProcessData;
16+
import org.fugerit.java.doc.freemarker.config.FreeMarkerConstants;
17+
import org.fugerit.java.doc.freemarker.config.FreemarkerApplyHelper;
18+
19+
import freemarker.template.Configuration;
20+
import freemarker.template.Template;
21+
import lombok.Data;
22+
23+
@Data
24+
public class ConfigInitModel implements IdConfigType, KeyString, Serializable {
25+
26+
public static final String VERSION_2_3_31 = "2.3.31";
27+
public static final String DEFAULT_VERSION = VERSION_2_3_31;
28+
29+
public static final String DEFAULT_CLASS_NAME = FreemarkerDocProcessConfigFacade.class.getName();
30+
31+
public static final String DEFAULT_MODE = "class";
32+
33+
public static final String DEFAULT_EXCEPTION_HANDLER = "RETHROW_HANDLER";
34+
35+
public static final String DEFAULT_LOG_EXCEPTION = BooleanUtils.BOOLEAN_FALSE;
36+
37+
public static final String DEFAULT_WRAP_UNCHECKED_EXCEPTION = BooleanUtils.BOOLEAN_TRUE;
38+
39+
public static final String DEFAULT_FALL_BACK_ON_NULL_LOOP_VARIABLE = BooleanUtils.BOOLEAN_FALSE;
40+
41+
private static final long serialVersionUID = -59587465058736934L;
42+
43+
private String id;
44+
45+
private String version = DEFAULT_VERSION;
46+
47+
private String path;
48+
49+
private String mode = DEFAULT_MODE;
50+
51+
private String className = DEFAULT_CLASS_NAME;
52+
53+
private String exceptionHandler = DEFAULT_EXCEPTION_HANDLER;
54+
55+
private String logException = DEFAULT_LOG_EXCEPTION;
56+
57+
private String wrapUncheckedExceptions = DEFAULT_WRAP_UNCHECKED_EXCEPTION;
58+
59+
private String fallbackOnNullLoopVariable = DEFAULT_FALL_BACK_ON_NULL_LOOP_VARIABLE;
60+
61+
private Configuration freemarkerConfiguration;
62+
63+
public static final String CHAIN_ID_PARAM = "chainId";
64+
65+
protected void process( DocChainModel model, DocProcessContext context, DocProcessData data ) throws Exception {
66+
// override template path
67+
String templatePath = model.getTemplatePath();
68+
ParamFinder finder = ParamFinder.newFinder();
69+
Properties params = new Properties();
70+
params.setProperty( CHAIN_ID_PARAM , model.getId() );
71+
templatePath = finder.substitute( templatePath , params );
72+
// map attributes
73+
Map<String, Object> map = FreeMarkerConstants.getFreeMarkerMap( context );
74+
if ( map == null ) {
75+
map = new HashMap<>();
76+
}
77+
map.putAll( context.toMap() );
78+
Template template = this.getFreemarkerConfiguration().getTemplate( templatePath );
79+
FreemarkerApplyHelper.setupFreemarkerMap( this.freemarkerConfiguration, map);
80+
Writer out = new StringWriter();
81+
template.process( map, out);
82+
data.setCurrentXmlData( out.toString() );
83+
}
84+
85+
@Override
86+
public String getKey() {
87+
return this.getId();
88+
}
89+
90+
@Override
91+
public String toString() {
92+
return "ConfigInitModel [id=" + id + ", version=" + version + ", path=" + path + ", mode=" + mode
93+
+ ", className=" + className + ", exceptionHandler=" + exceptionHandler + ", logException="
94+
+ logException + ", wrapUncheckedExceptions=" + wrapUncheckedExceptions
95+
+ ", fallbackOnNullLoopVariable=" + fallbackOnNullLoopVariable + ", freemarkerConfiguration="
96+
+ freemarkerConfiguration + "]";
97+
}
98+
99+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.fugerit.java.doc.freemarker.process;
2+
3+
import java.io.Serializable;
4+
5+
import org.fugerit.java.core.cfg.xml.IdConfigType;
6+
import org.fugerit.java.core.util.collection.KeyString;
7+
8+
import lombok.Data;
9+
10+
@Data
11+
public class DocChainModel implements IdConfigType, KeyString, Serializable {
12+
13+
public static final String DEFAULT_TEMPLATE_PATH = "${chainId}.ftl";
14+
15+
private static final long serialVersionUID = 9076457107043072322L;
16+
17+
private String id;
18+
19+
private String templatePath = DEFAULT_TEMPLATE_PATH;
20+
21+
@Override
22+
public String getKey() {
23+
return this.getId();
24+
}
25+
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.fugerit.java.doc.freemarker.process;
2+
3+
import java.io.Serializable;
4+
5+
import org.fugerit.java.core.cfg.xml.ListMapConfig;
6+
import org.fugerit.java.doc.base.process.DocProcessConfig;
7+
import org.fugerit.java.doc.base.process.DocProcessContext;
8+
import org.fugerit.java.doc.base.process.DocProcessData;
9+
10+
import lombok.Getter;
11+
12+
public class FreemarkerDocProcessConfig extends DocProcessConfig implements Serializable {
13+
14+
private static final long serialVersionUID = -6761081877582850120L;
15+
16+
@Getter
17+
private ListMapConfig<ConfigInitModel> configInitList;
18+
19+
@Getter
20+
private ListMapConfig<DocChainModel> docChainList;
21+
22+
protected FreemarkerDocProcessConfig() {
23+
this.configInitList = new ListMapConfig<>();
24+
this.docChainList = new ListMapConfig<>();
25+
}
26+
27+
public void process( String configId, String chainId, DocProcessContext context, DocProcessData data ) throws Exception {
28+
ConfigInitModel configInitModel = this.getConfigInitList().get( configId );
29+
DocChainModel docChainModel = this.getChainOrDefault(chainId);
30+
configInitModel.process(docChainModel, context, data);
31+
}
32+
33+
private DocChainModel getChainOrDefault( String id ) {
34+
DocChainModel model = this.getDocChainList().get( id );
35+
if ( model == null ) {
36+
model = new DocChainModel();
37+
model.setId( id );
38+
}
39+
return model;
40+
}
41+
42+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.fugerit.java.doc.freemarker.process;
2+
3+
import java.io.Reader;
4+
import java.util.Properties;
5+
6+
import javax.xml.parsers.DocumentBuilder;
7+
import javax.xml.parsers.DocumentBuilderFactory;
8+
9+
import org.fugerit.java.core.cfg.ConfigException;
10+
import org.fugerit.java.core.cfg.xml.XmlBeanHelper;
11+
import org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep;
12+
import org.w3c.dom.Document;
13+
import org.w3c.dom.Element;
14+
import org.w3c.dom.NodeList;
15+
import org.xml.sax.InputSource;
16+
17+
import freemarker.template.Configuration;
18+
import lombok.extern.slf4j.Slf4j;
19+
20+
@Slf4j
21+
public class FreemarkerDocProcessConfigFacade {
22+
23+
public static FreemarkerDocProcessConfig newSimpleConfig( String id, String templatePath ) throws ConfigException {
24+
FreemarkerDocProcessConfig config = new FreemarkerDocProcessConfig();
25+
ConfigInitModel model = new ConfigInitModel();
26+
model.setId(id);
27+
model.setPath( templatePath );
28+
try {
29+
addConfiguration(model);
30+
} catch (Exception e) {
31+
throw new ConfigException( "Error configuring FreemarkerDocProcessConfig : "+e , e );
32+
}
33+
config.getConfigInitList().add(model);
34+
return config;
35+
}
36+
37+
public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws ConfigException {
38+
FreemarkerDocProcessConfig result = null;
39+
try {
40+
FreemarkerDocProcessConfig config = new FreemarkerDocProcessConfig();
41+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
42+
dbf.setNamespaceAware( true );
43+
DocumentBuilder db = dbf.newDocumentBuilder();
44+
Document doc = db.parse( new InputSource( xmlReader ) );
45+
NodeList configInitList = doc.getElementsByTagName( "configInit" );
46+
for ( int k=0; k<configInitList.getLength(); k++ ) {
47+
Element currentTag = (Element) configInitList.item( k );
48+
ConfigInitModel model = new ConfigInitModel();
49+
XmlBeanHelper.setFromElement( model, currentTag );
50+
config.getConfigInitList().add(model);
51+
addConfiguration(model);
52+
}
53+
NodeList docChainLisgt = doc.getElementsByTagName( "docChain" );
54+
for ( int k=0; k<docChainLisgt.getLength(); k++ ) {
55+
Element currentTag = (Element) docChainLisgt.item( k );
56+
DocChainModel model = new DocChainModel();
57+
XmlBeanHelper.setFromElement( model, currentTag );
58+
config.getDocChainList().add(model);
59+
}
60+
result = config;
61+
log.info( "loadConfig ok : {}", result );
62+
} catch (Exception e) {
63+
throw new ConfigException( "Error configuring FreemarkerDocProcessConfig : "+e , e );
64+
}
65+
return result;
66+
}
67+
68+
private static void addConfiguration( ConfigInitModel model ) throws Exception {
69+
Properties params = new Properties();
70+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_VERSION , model.getVersion() );
71+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_MODE , model.getMode() );
72+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_PATH , model.getPath() );
73+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_CLASS , model.getClassName() );
74+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_EXCEPTION_HANDLER , model.getExceptionHandler() );
75+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_LOG_EXCEPTION , model.getLogException() );
76+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_WRAP_UNCHECKED_EXCEPTION , model.getWrapUncheckedExceptions() );
77+
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_FALLBACK_ON_NULL_LOOP_VARIABLE , model.getFallbackOnNullLoopVariable() );
78+
Configuration conf = FreemarkerConfigHelper.getConfig( model.getId(), params );
79+
model.setFreemarkerConfiguration( conf );
80+
}
81+
82+
}
83+
84+
class FreemarkerConfigHelper extends FreeMarkerConfigStep {
85+
86+
private static final long serialVersionUID = 8824282827447873553L;
87+
88+
protected static Configuration getConfig( String key, Properties config ) throws Exception {
89+
return FreeMarkerConfigStep.getConfig(key, config);
90+
}
91+
92+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
/*
5+
* @(#)freemarker-doc-process-1-0.xsd
6+
*
7+
* @project : org.fugerit.java.doc.base
8+
* @creation : 2023-07-09
9+
* @version : 1.0.0 (2023-07-09)
10+
*
11+
* XSD for Freemarker Doc Process Configuration
12+
*/
13+
14+
Sample doc :
15+
16+
<doc
17+
xmlns="https://freemarkerdocprocess.fugerit.org"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="https://freemarkerdocprocess.fugerit.org https://www.fugerit.org/data/java/doc/xsd/freemarker-doc-process-1-0.xsd" >
20+
</doc>
21+
22+
-->
23+
<xsd:schema xmlns='http://www.w3.org/2000/10/XMLSchema'
24+
targetNamespace='https://freemarkerdocprocess.fugerit.org'
25+
xmlns:fdp='https://freemarkerdocprocess.fugerit.org' xmlns:xsd="http://www.w3.org/2001/XMLSchema">
26+
27+
<xsd:element name='freemarker-doc-process-config'>
28+
<xsd:annotation>
29+
<xsd:documentation>This is the root element of freemarker-doc-process configuration file</xsd:documentation>
30+
</xsd:annotation>
31+
<xsd:complexType>
32+
<xsd:sequence>
33+
<xsd:element ref='fdp:configInit' minOccurs="0" maxOccurs="unbounded" />
34+
<xsd:element ref='fdp:docChain' minOccurs="0" maxOccurs="unbounded" />
35+
</xsd:sequence>
36+
</xsd:complexType>
37+
</xsd:element>
38+
39+
<xsd:element name='configInit' type="fdp:configInitType">
40+
<xsd:annotation>
41+
<xsd:documentation>Contains the meta informations of the document</xsd:documentation>
42+
<xsd:documentation>('meta' is kept for compatibility, better use 'metadata')</xsd:documentation>
43+
</xsd:annotation>
44+
</xsd:element>
45+
46+
47+
<xsd:element name='docChain' type="fdp:docChainType">
48+
<xsd:annotation>
49+
<xsd:documentation>Contains the meta informations of the document</xsd:documentation>
50+
<xsd:documentation>('meta' is kept for compatibility, better use 'metadata')</xsd:documentation>
51+
</xsd:annotation>
52+
</xsd:element>
53+
54+
<xsd:complexType name="configInitType">
55+
<xsd:annotation>
56+
<xsd:documentation>Contains the meta informations of the document</xsd:documentation>
57+
<xsd:documentation>(roughly comparable to a HTML 'head' element)</xsd:documentation>
58+
</xsd:annotation>
59+
<xsd:attribute name="id" type="xsd:string" use="required"></xsd:attribute>
60+
<xsd:attribute name="path" type="xsd:string" use="required"></xsd:attribute>
61+
<xsd:attribute name="useDefaultTemplatePath" type="xsd:boolean" use="optional" default="true"></xsd:attribute>
62+
<xsd:attribute name="version" type="xsd:string" use="optional" default="2.3.31"></xsd:attribute>
63+
<xsd:attribute name="mode" type="xsd:string" use="optional" default="class"></xsd:attribute>
64+
<xsd:attribute name="className" type="xsd:string" use="optional" default="org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfigß"></xsd:attribute>
65+
<xsd:attribute name="exceptionHandler" type="xsd:string" use="optional" default="RETHROW_HANDLER"></xsd:attribute>
66+
<xsd:attribute name="logException" type="xsd:boolean" use="optional" default="false"></xsd:attribute>
67+
<xsd:attribute name="wrapUncheckedExceptions" type="xsd:boolean" use="optional" default="true"></xsd:attribute>
68+
<xsd:attribute name="ifallbackOnNullLoopVariabled" type="xsd:boolean" use="optional" default="false"></xsd:attribute>
69+
</xsd:complexType>
70+
71+
<xsd:complexType name="docChainType">
72+
<xsd:annotation>
73+
<xsd:documentation>Contains the meta informations of the document</xsd:documentation>
74+
<xsd:documentation>(roughly comparable to a HTML 'head' element)</xsd:documentation>
75+
</xsd:annotation>
76+
<xsd:attribute name="id" type="xsd:string" use="required"></xsd:attribute>
77+
<xsd:attribute name="templatePath" type="xsd:string" use="optional" default="${chainId}.ftl"></xsd:attribute>
78+
</xsd:complexType>
79+
80+
</xsd:schema>

0 commit comments

Comments
 (0)