Skip to content

Commit 1a4b3a0

Browse files
committed
Second configuration review
Probably this is still temporary
1 parent 3a1baf1 commit 1a4b3a0

File tree

6 files changed

+151
-24
lines changed

6 files changed

+151
-24
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.fugerit.java.doc.freemarker.process;
2+
3+
import java.io.Serializable;
4+
5+
import lombok.Data;
6+
7+
@Data
8+
public class ChainStepModel implements Serializable {
9+
10+
private static final long serialVersionUID = 622077549080786391L;
11+
12+
private String type;
13+
14+
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@
33
import java.io.Serializable;
44
import java.io.StringWriter;
55
import java.io.Writer;
6+
import java.util.Enumeration;
67
import java.util.HashMap;
78
import java.util.Map;
89
import java.util.Properties;
910

1011
import org.fugerit.java.core.cfg.xml.IdConfigType;
1112
import org.fugerit.java.core.lang.helpers.BooleanUtils;
13+
import org.fugerit.java.core.lang.helpers.ClassHelper;
1214
import org.fugerit.java.core.util.collection.KeyString;
1315
import org.fugerit.java.core.util.regex.ParamFinder;
1416
import org.fugerit.java.doc.base.process.DocProcessContext;
1517
import org.fugerit.java.doc.base.process.DocProcessData;
18+
import org.fugerit.java.doc.base.process.DocProcessorBasic;
1619
import org.fugerit.java.doc.freemarker.config.FreeMarkerConstants;
1720
import org.fugerit.java.doc.freemarker.config.FreemarkerApplyHelper;
1821

1922
import freemarker.template.Configuration;
2023
import freemarker.template.Template;
2124
import lombok.Data;
25+
import lombok.extern.slf4j.Slf4j;
2226

27+
@Slf4j
2328
@Data
2429
public class ConfigInitModel implements IdConfigType, KeyString, Serializable {
2530

@@ -59,6 +64,8 @@ public class ConfigInitModel implements IdConfigType, KeyString, Serializable {
5964
private String fallbackOnNullLoopVariable = DEFAULT_FALL_BACK_ON_NULL_LOOP_VARIABLE;
6065

6166
private Configuration freemarkerConfiguration;
67+
68+
private Map<String, Object> generalContext = new HashMap<>();
6269

6370
public static final String CHAIN_ID_PARAM = "chainId";
6471

@@ -74,7 +81,24 @@ protected void process( DocChainModel model, DocProcessContext context, DocProce
7481
if ( map == null ) {
7582
map = new HashMap<>();
7683
}
77-
map.putAll( context.toMap() );
84+
map.putAll( this.getGeneralContext() );
85+
for ( ChainStepModel step : model.getChainStepList() ) {
86+
DocProcessorBasic docStep = (DocProcessorBasic) ClassHelper.newInstance( step.getType() );
87+
int res = docStep.apply(context, data);
88+
log.debug( "docStep {} -> {}", docStep, res );
89+
}
90+
log.debug( "mapsAtt mode -> {} -> {}", model.getId(), model.getMapAtts() );
91+
if ( DocChainModel.MAP_ATTS_ENUM.equalsIgnoreCase( model.getMapAtts() ) ) {
92+
Enumeration<Object> ea = model.getMapAttsEnum().keys();
93+
while ( ea.hasMoreElements() ) {
94+
String key = ea.nextElement().toString();
95+
String value = model.getMapAttsEnum().getProperty( key );
96+
map.put( value , context.getAttribute( key ) );
97+
log.info( "map att enum -> {} -> {}", key, value );
98+
}
99+
} else {
100+
map.putAll( context.toMap() );
101+
}
78102
Template template = this.getFreemarkerConfiguration().getTemplate( templatePath );
79103
FreemarkerApplyHelper.setupFreemarkerMap( this.freemarkerConfiguration, map);
80104
Writer out = new StringWriter();

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.fugerit.java.doc.freemarker.process;
22

33
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Properties;
47

58
import org.fugerit.java.core.cfg.xml.IdConfigType;
69
import org.fugerit.java.core.util.collection.KeyString;
@@ -10,14 +13,26 @@
1013
@Data
1114
public class DocChainModel implements IdConfigType, KeyString, Serializable {
1215

16+
public static final String MAP_ATTS_ALL = "all";
17+
18+
public static final String MAP_ATTS_ENUM = "enum";
19+
20+
public static final String MAP_ATTS_DEFAULT = MAP_ATTS_ALL;
21+
1322
public static final String DEFAULT_TEMPLATE_PATH = "${chainId}.ftl";
1423

1524
private static final long serialVersionUID = 9076457107043072322L;
1625

1726
private String id;
1827

1928
private String templatePath = DEFAULT_TEMPLATE_PATH;
29+
30+
private String mapAtts = MAP_ATTS_DEFAULT;
31+
32+
private Properties mapAttsEnum;
2033

34+
private List<ChainStepModel> chainStepList = new ArrayList<>();
35+
2136
@Override
2237
public String getKey() {
2338
return this.getId();

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package org.fugerit.java.doc.freemarker.process;
22

33
import java.io.Reader;
4+
import java.util.Enumeration;
45
import java.util.Properties;
56

67
import javax.xml.parsers.DocumentBuilder;
78
import javax.xml.parsers.DocumentBuilderFactory;
89

910
import org.fugerit.java.core.cfg.ConfigException;
1011
import org.fugerit.java.core.cfg.xml.XmlBeanHelper;
12+
import org.fugerit.java.core.lang.helpers.ClassHelper;
13+
import org.fugerit.java.core.xml.dom.DOMUtils;
1114
import org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep;
1215
import org.w3c.dom.Document;
1316
import org.w3c.dom.Element;
@@ -49,12 +52,40 @@ public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws C
4952
XmlBeanHelper.setFromElement( model, currentTag );
5053
config.getConfigInitList().add(model);
5154
addConfiguration(model);
55+
// functions map
56+
NodeList functionsMap = currentTag.getElementsByTagName( "functionsMap" );
57+
if ( functionsMap.getLength() > 0 ) {
58+
for ( int i=0; i<functionsMap.getLength(); i++ ) {
59+
Element currentFM = (Element)functionsMap.item(i);
60+
Properties currentFMProps = DOMUtils.attributesToProperties( currentFM );
61+
Enumeration<Object> efm = currentFMProps.keys();
62+
while ( efm.hasMoreElements() ) {
63+
String key = efm.nextElement().toString();
64+
String value = currentFMProps.getProperty( key );
65+
model.getGeneralContext().put( key , ClassHelper.newInstance(value) );
66+
}
67+
}
68+
}
5269
}
5370
NodeList docChainLisgt = doc.getElementsByTagName( "docChain" );
5471
for ( int k=0; k<docChainLisgt.getLength(); k++ ) {
5572
Element currentTag = (Element) docChainLisgt.item( k );
5673
DocChainModel model = new DocChainModel();
5774
XmlBeanHelper.setFromElement( model, currentTag );
75+
// attributes mapping
76+
if ( DocChainModel.MAP_ATTS_ENUM.equalsIgnoreCase( model.getMapAtts() ) ) {
77+
Element mapAttsEnumTag = (Element)currentTag.getElementsByTagName( "mapAttsEnum" ).item( 0 );
78+
model.setMapAttsEnum( DOMUtils.attributesToProperties( mapAttsEnumTag ) );
79+
log.debug( "chain att enum {} -> {}", model.getId(), model.getMapAttsEnum() );
80+
}
81+
// chain step
82+
NodeList chainStepList = currentTag.getElementsByTagName( "chainStep" );
83+
for ( int i=0; i<chainStepList.getLength(); i++ ) {
84+
Element currentChainStepTag = (Element) chainStepList.item(i);
85+
ChainStepModel chainStepModel = new ChainStepModel();
86+
XmlBeanHelper.setFromElement( chainStepModel, currentChainStepTag );
87+
model.getChainStepList().add(chainStepModel);
88+
}
5889
config.getDocChainList().add(model);
5990
}
6091
result = config;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<freemarker-doc-process-config
3+
xmlns="https://freemarkerdocprocess.fugerit.org"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="https://freemarkerdocprocess.fugerit.org https://www.fugerit.org/data/java/doc/xsd/freemarker-doc-process-1-0.xsd" >
6+
>
7+
8+
<configInit
9+
id="FJ_SAMPLE_TEST"
10+
version="2.3.31"
11+
path="/free_marker/">
12+
<functionsMap
13+
messageFormat="org.fugerit.java.doc.freemarker.fun.SimpleMessageFun"
14+
sumLong="org.fugerit.java.doc.freemarker.fun.SimpleSumLongFun"/>
15+
</configInit>
16+
17+
<docChain id="free-marker-01" templatePath="test_01.ftl" mapAtts="enum">
18+
<mapAttsEnum list="userList" testBase64Img="testBase64Img"/>
19+
<chainStep type="test.org.fugerit.java.doc.sample.freemarker.TestFreeMarker01DataStep"/>
20+
</docChain>
21+
22+
<docChain id="free-marker-01-fop" templatePath="test_01.ftl" mapAtts="enum">
23+
<mapAttsEnum list="userList" testBase64Img="testBase64Img"/>
24+
<chainStep type="test.org.fugerit.java.doc.sample.freemarker.TestFreeMarker01DataStep"/>
25+
</docChain>
26+
27+
<docChain id="free-marker-test-fop-font" templatePath="test_fop_font.ftl" mapAtts="enum">
28+
<mapAttsEnum list="userList" testBase64Img="testBase64Img"/>
29+
<chainStep type="test.org.fugerit.java.doc.sample.freemarker.TestFreeMarker01DataStep"/>
30+
</docChain>
31+
32+
<docChain id="full-facade-01" templatePath="test_01.ftl" mapAtts="enum">
33+
<mapAttsEnum list="userList" testBase64Img="testBase64Img"/>
34+
<chainStep type="test.org.fugerit.java.doc.sample.freemarker.TestFreeMarker01DataStep"/>
35+
</docChain>
36+
37+
<docChain id="free-marker-02" templatePath="test_02.ftl" mapAtts="enum">
38+
<mapAttsEnum list="userList" testBase64Img="testBase64Img"/>
39+
<chainStep type="test.org.fugerit.java.doc.sample.freemarker.TestFreeMarker01DataStep"/>
40+
</docChain>
41+
42+
<docChain id="free-marker-color-test" templatePath="color_test.ftl" mapAtts="enum">
43+
<mapAttsEnum list="userList" testBase64Img="testBase64Img"/>
44+
<chainStep type="test.org.fugerit.java.doc.sample.freemarker.TestFreeMarker01DataStep"/>
45+
</docChain>
46+
47+
<docChain id="free-marker-format-test" templatePath="format_test.ftl" mapAtts="enum">
48+
<mapAttsEnum list="userList" testBase64Img="testBase64Img"/>
49+
<chainStep type="test.org.fugerit.java.doc.sample.freemarker.TestFreeMarker01DataStep"/>
50+
</docChain>
51+
52+
<docChain id="basic" templatePath="basic.ftl" mapAtts="enum">
53+
<mapAttsEnum list="userList" testBase64Img="testBase64Img"/>
54+
<chainStep type="test.org.fugerit.java.doc.sample.freemarker.TestFreeMarker01DataStep"/>
55+
</docChain>
56+
57+
</freemarker-doc-process-config>

fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/freemarker/BasicFreeMarkerTest.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package test.org.fugerit.java.doc.sample.freemarker;
22

3-
import java.io.InputStream;
3+
import java.io.InputStreamReader;
44
import java.io.Reader;
55

66
import org.fugerit.java.core.lang.helpers.ClassHelper;
7-
import org.fugerit.java.core.util.filterchain.MiniFilterChain;
87
import org.fugerit.java.doc.base.config.DocConfig;
9-
import org.fugerit.java.doc.base.process.DocProcessConfig;
108
import org.fugerit.java.doc.base.process.DocProcessContext;
119
import org.fugerit.java.doc.base.process.DocProcessData;
12-
import org.fugerit.java.doc.base.xml.DocValidator;
10+
import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfig;
11+
import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfigFacade;
1312

1413
import test.org.fugerit.java.doc.sample.facade.BasicFacadeTest;
1514

@@ -23,40 +22,27 @@ protected BasicFreeMarkerTest(String nameBase, String... typeList) {
2322
super(nameBase, typeList);
2423
}
2524

26-
private static DocProcessConfig init() {
27-
DocProcessConfig config = null;
28-
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "config/doc-process-sample.xml" ) ) {
29-
config = DocProcessConfig.loadConfig( is );
25+
private static FreemarkerDocProcessConfig init() {
26+
FreemarkerDocProcessConfig config = null;
27+
try ( InputStreamReader xmlReader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader( "config/freemarker-doc-process.xml" ) ) ) {
28+
config = FreemarkerDocProcessConfigFacade.loadConfig( xmlReader );
3029
} catch (Exception e) {
3130
throw new RuntimeException( e );
3231
}
3332
return config;
3433
}
3534

36-
private static DocProcessConfig PROCESS_CONFIG = init();
35+
private static FreemarkerDocProcessConfig PROCESS_CONFIG = init();
3736

38-
39-
4037
@Override
4138
protected Reader getXmlReader() throws Exception {
4239
return this.process( this.getNameBase() );
4340
}
4441

4542
public Reader process( String chainId ) throws Exception {
46-
long startTime = System.currentTimeMillis();
47-
// required : access to che processing chain
48-
MiniFilterChain chain = PROCESS_CONFIG.getChainCache( chainId );
4943
DocProcessContext context = new DocProcessContext();
5044
DocProcessData data = new DocProcessData();
51-
int res = chain.apply( context , data );
52-
logger.info( "RES {} ", res );
53-
this.checkpoints.addCheckpointFromStartTime( "DOC-FREEMARKER" , startTime );
54-
startTime = System.currentTimeMillis();
55-
// optional : validate and print XSD errors :
56-
try ( Reader input = data.getCurrentXmlReader() ) {
57-
DocValidator.logValidation( input , logger );
58-
this.checkpoints.addCheckpointFromStartTime( "DOC-VALIDATION" , startTime );
59-
}
45+
PROCESS_CONFIG.process( "FJ_SAMPLE_TEST" , chainId, context, data );
6046
return data.getCurrentXmlReader();
6147
}
6248

0 commit comments

Comments
 (0)