Skip to content

Commit fbec6e4

Browse files
committed
Tool converting old config model to new one
1 parent 9007092 commit fbec6e4

File tree

21 files changed

+462
-2
lines changed

21 files changed

+462
-2
lines changed

docgen/release-notes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
------------------
33
+ Freemarker html type handler converted to new configuration model
44
+ Configuration stub for freemarker configuration model
5+
+ Tool for stub generation (new maven module fj-doc-tool)
6+
+ Tool converting old config model to new one (new maven module fj-doc-tool)
57

68
1.3.1-rc.002 (2023-07-13)
79
------------------

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public class FreemarkerDocProcessConfigFacade {
4747

4848
public static final String STEP_TYPE_FUNCTION = "function";
4949

50+
public static final String STEP_TYPE_COMPLEX = "complex";
51+
5052
public static final String STEP_TYPE_MAP = "map";
5153

5254
public static FreemarkerDocProcessConfig newSimpleConfig( String id, String templatePath ) throws ConfigException {
@@ -200,11 +202,13 @@ public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws C
200202
}
201203

202204
private static final Properties BUILT_IN_STEPS = new Properties();
205+
public static final Properties BUILT_IN_STEPS_REVERSE = new Properties();
203206
static {
204207
BUILT_IN_STEPS.setProperty( STEP_TYPE_CONFIG , FreeMarkerConfigStep.class.getName() );
205208
BUILT_IN_STEPS.setProperty( STEP_TYPE_FUNCTION , FreeMarkerFunctionStep.class.getName() );
206-
BUILT_IN_STEPS.setProperty( "complex" , FreeMarkerComplexProcessStep.class.getName() );
207-
BUILT_IN_STEPS.setProperty( "map" , FreeMarkerMapStep.class.getName() );
209+
BUILT_IN_STEPS.setProperty( STEP_TYPE_COMPLEX , FreeMarkerComplexProcessStep.class.getName() );
210+
BUILT_IN_STEPS.setProperty( STEP_TYPE_MAP , FreeMarkerMapStep.class.getName() );
211+
BUILT_IN_STEPS.keySet().stream().forEach( k -> BUILT_IN_STEPS_REVERSE.put( BUILT_IN_STEPS.get( k ) , k ) );
208212
}
209213

210214
private static Properties convertConfiguration( Properties props ) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.fugerit.java.doc.freemarker.tool;
2+
3+
import java.io.InputStream;
4+
import java.io.Writer;
5+
import java.util.Properties;
6+
7+
import javax.xml.parsers.DocumentBuilder;
8+
import javax.xml.parsers.DocumentBuilderFactory;
9+
10+
import org.fugerit.java.core.io.StreamIO;
11+
import org.fugerit.java.doc.base.process.DocProcessContext;
12+
import org.fugerit.java.doc.base.process.DocProcessData;
13+
import org.fugerit.java.doc.freemarker.config.FreeMarkerProcessStep;
14+
import org.fugerit.java.doc.freemarker.helper.FreeMarkerDocProcess;
15+
import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfigFacade;
16+
import org.fugerit.java.doc.freemarker.tool.model.ChainModel;
17+
import org.fugerit.java.doc.freemarker.tool.model.ConfigModel;
18+
import org.fugerit.java.doc.freemarker.tool.model.StepModel;
19+
import org.w3c.dom.Attr;
20+
import org.w3c.dom.Document;
21+
import org.w3c.dom.Element;
22+
import org.w3c.dom.NamedNodeMap;
23+
import org.w3c.dom.NodeList;
24+
25+
import lombok.extern.slf4j.Slf4j;
26+
27+
@Slf4j
28+
public class ConvertConfig {
29+
30+
public static final String ATT_CONFIG_MODEL = "configModel";
31+
32+
public static void generate( InputStream is, Writer out, Properties params ) throws Exception {
33+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
34+
dbf.setNamespaceAware( true );
35+
DocumentBuilder parser = dbf.newDocumentBuilder();
36+
Document inputDoc = parser.parse( is );
37+
NodeList chainTagList = inputDoc.getDocumentElement().getElementsByTagName( "chain" );
38+
ConfigModel configModel = new ConfigModel();
39+
for ( int k=0; k<chainTagList.getLength(); k++ ) {
40+
Element currentChainTag = (Element) chainTagList.item( k );
41+
String chainId = currentChainTag.getAttribute( "id" );
42+
log.info( "current chain id {}", chainId );
43+
ChainModel chainModel = new ChainModel(chainId);
44+
configModel.getChainList().add(chainModel);
45+
NodeList stepTagList = currentChainTag.getElementsByTagName( "step" );
46+
for ( int i=0; i<stepTagList.getLength(); i++ ) {
47+
Element currentStepTag = (Element) stepTagList.item(i);
48+
String type = currentStepTag.getAttribute( "type" );
49+
String convertType = FreemarkerDocProcessConfigFacade.BUILT_IN_STEPS_REVERSE.getProperty( type, type );
50+
StepModel stepModel = new StepModel(convertType);
51+
chainModel.getStepList().add(stepModel);
52+
if ( FreemarkerDocProcessConfigFacade.STEP_TYPE_CONFIG.equalsIgnoreCase( convertType ) ) {
53+
stepModel.getAtts().put( "id" , currentStepTag.getAttribute( "param01" ) );
54+
} else if ( FreeMarkerProcessStep.class.getName().equalsIgnoreCase( convertType ) ) {
55+
stepModel.setType( FreemarkerDocProcessConfigFacade.STEP_TYPE_COMPLEX );
56+
stepModel.getAtts().put( "template-path" , currentStepTag.getAttribute( "param01" ) );
57+
}
58+
log.info( "current step type {}", stepModel.getType() );
59+
NodeList propertiesTagList = currentStepTag.getElementsByTagName( "properties" );
60+
if ( propertiesTagList.getLength() > 0 ) {
61+
Element propertyTag = (Element) propertiesTagList.item( 0 );
62+
NamedNodeMap attMap = propertyTag.getAttributes();
63+
for ( int j=0; j<attMap.getLength(); j++ ) {
64+
Attr currentAtt = (Attr)attMap.item( j );
65+
log.info( "current att {} -> {}", currentAtt.getName() , currentAtt.getValue() );
66+
stepModel.getAtts().put( currentAtt.getName() , currentAtt.getValue() );
67+
}
68+
}
69+
}
70+
}
71+
DocProcessData data = new DocProcessData();
72+
DocProcessContext context = DocProcessContext.newContext( GenerateStub.ATT_STUB_PARAMS, params ).withAtt( ATT_CONFIG_MODEL , configModel );
73+
FreeMarkerDocProcess.getInstance().process( GenerateStub.CONFIG_STUB_CHAIN_ID, context, data );
74+
StreamIO.pipeChar( data.getCurrentXmlReader() , out, StreamIO.MODE_CLOSE_IN_ONLY );
75+
}
76+
77+
}

fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/tool/GenerateStub.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ public class GenerateStub {
1313
public static final String CONFIG_STUB_CHAIN_ID = "freemarker-doc-process-config-stub";
1414

1515
public static final String ATT_STUB_PARAMS = "stubParams";
16+
17+
/**
18+
* Generate the docHandlerConfig stub ('1' = enabled, default = '1')
19+
*/
20+
public static final String PARAM_STUB_HANDLER = "stub-handler";
21+
22+
/**
23+
* Generate the docChain stub ('0' = enabled, default = '1')
24+
*/
25+
public static final String PARAM_STUB_CHAIN = "stub-chain";
1626

1727
/**
1828
* Enable FOP basic type handler in stub ('1' = enabled, default = '0')
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.fugerit.java.doc.freemarker.tool.model;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import lombok.Data;
8+
9+
@Data
10+
public class ChainModel implements Serializable {
11+
12+
private static final long serialVersionUID = 3421438389573953861L;
13+
14+
private List<StepModel> stepList;
15+
16+
private String id;
17+
18+
public ChainModel( String id ) {
19+
this.setId( id );
20+
this.stepList = new ArrayList<>();
21+
}
22+
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.fugerit.java.doc.freemarker.tool.model;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import lombok.Data;
8+
9+
@Data
10+
public class ConfigModel implements Serializable {
11+
12+
private static final long serialVersionUID = 5198896174327509127L;
13+
14+
private List<ChainModel> chainList;
15+
16+
public ConfigModel() {
17+
this.chainList = new ArrayList<>();
18+
}
19+
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.fugerit.java.doc.freemarker.tool.model;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Set;
5+
6+
import lombok.Data;
7+
8+
@Data
9+
public class StepModel {
10+
11+
private String type;
12+
13+
private LinkedHashMap<String, String> atts;
14+
15+
public StepModel(String type) {
16+
super();
17+
this.type = type;
18+
this.atts = new LinkedHashMap<>();
19+
}
20+
21+
public Set<String> getAttNames() {
22+
return this.getAtts().keySet();
23+
}
24+
25+
}

fj-doc-freemarker/src/main/resources/fj_doc_freemarker_config/fm-freemarker-doc-process-config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<docChain id="freemarker-doc-process-config-stub" parent="base-freemarker">
3737
<chainStep stepType="map">
3838
<map name="stubParams" value="stubParams"/>
39+
<map name="configModel" value="configModel"/>
3940
</chainStep>
4041
<chainStep stepType="complex" template-path="freemarker-doc-process-config-stub.ftl"/>
4142
</docChain>

fj-doc-freemarker/src/main/resources/fj_doc_freemarker_config/template/freemarker-doc-process-config-stub.ftl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
Configuration stub version : 002 (2023-07-18)
99
-->
1010

11+
<#assign stubHandler=stubParams['stub-handler']!'1'>
12+
<#if stubHandler == '1'>
13+
1114
<docHandlerConfig>
1215

1316
<!-- Type handler for markdown format -->
@@ -74,7 +77,10 @@
7477
<#if enableOpencsv != '1'>--> </#if>
7578

7679
</docHandlerConfig>
80+
</#if>
7781

82+
<#assign stubChain=stubParams['stub-chain']!'0'>
83+
<#if stubChain == '1'>
7884
<#assign configId=stubParams['config-id']!'FJ_DOC_STUB'>
7985
<#assign fmVersion=stubParams['fm-version']!'2.3.29'>
8086
<#assign fmTemplatePath=stubParams['fm-template-path']!'/free_marker/'>
@@ -99,5 +105,32 @@
99105
-->
100106
<chainStep stepType="complex" template-path="sample-chain.ftl"/>
101107
</docChain>
108+
</#if>
109+
110+
<#if (configModel)??>
111+
<#list configModel.chainList as chainModel>
112+
<docChain id="${chainModel.id}">
113+
<#list chainModel.stepList as stepModel>
114+
<chainStep stepType="${stepModel.type}"<#if stepModel.type == 'complex'><#list stepModel.attNames as currentAttName> ${currentAttName}="${stepModel.atts[currentAttName]}"</#list></#if>>
115+
<#if stepModel.type == 'map'>
116+
<#list stepModel.attNames as currentAttName>
117+
<map name="${currentAttName}" value="${stepModel.atts[currentAttName]}"/>
118+
</#list>
119+
<#elseif stepModel.type == 'config'>
120+
<config
121+
<#list stepModel.attNames as currentAttName>
122+
${currentAttName}="${stepModel.atts[currentAttName]}"
123+
</#list>
124+
/>
125+
<#elseif stepModel.type == 'complex'>
126+
<#else>
127+
<!-- custom step, additional configuration may be needed -->
128+
</#if>
129+
</chainStep>
130+
</#list>
131+
</docChain>
132+
133+
</#list>
134+
</#if>
102135

103136
</freemarker-doc-process-config>

fj-doc-tool/src/main/java/org/fugerit/java/doc/tool/DocTool.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.fugerit.java.core.cli.ArgUtils;
1111
import org.fugerit.java.core.lang.helpers.StringUtils;
1212
import org.fugerit.java.core.util.PropsIO;
13+
import org.fugerit.java.doc.tool.handler.ConvertConfigHandler;
1314
import org.fugerit.java.doc.tool.handler.GenerateStubHandler;
1415

1516
import lombok.extern.slf4j.Slf4j;
@@ -23,6 +24,8 @@ public class DocTool {
2324

2425
public static final String ARG_TOOL_GENERATE_STUB = "generate-stub";
2526

27+
public static final String ARG_TOOL_CONVERT_CONFIG = "convert-config";
28+
2629
public static void handle( Properties params ) throws Exception {
2730
String toolHandler = params.getProperty( ARG_TOOL );
2831
if ( StringUtils.isEmpty( toolHandler ) ) {
@@ -31,6 +34,8 @@ public static void handle( Properties params ) throws Exception {
3134
Consumer<Properties> handler = null;
3235
if ( ARG_TOOL_GENERATE_STUB.equalsIgnoreCase( toolHandler ) ) {
3336
handler = new GenerateStubHandler();
37+
} else if ( ARG_TOOL_CONVERT_CONFIG.equalsIgnoreCase( toolHandler ) ) {
38+
handler = new ConvertConfigHandler();
3439
} else {
3540
throw new ConfigException( "Unknown tool : "+toolHandler );
3641
}

0 commit comments

Comments
 (0)