1
1
package org .fugerit .java .doc .freemarker .process ;
2
2
3
3
import java .io .Reader ;
4
- import java .util .Enumeration ;
5
4
import java .util .Properties ;
6
5
7
6
import javax .xml .parsers .DocumentBuilder ;
10
9
import org .fugerit .java .core .cfg .ConfigException ;
11
10
import org .fugerit .java .core .cfg .xml .XmlBeanHelper ;
12
11
import org .fugerit .java .core .lang .helpers .ClassHelper ;
12
+ import org .fugerit .java .core .lang .helpers .StringUtils ;
13
+ import org .fugerit .java .core .util .filterchain .MiniFilterBase ;
14
+ import org .fugerit .java .core .util .filterchain .MiniFilterChain ;
13
15
import org .fugerit .java .core .xml .dom .DOMUtils ;
16
+ import org .fugerit .java .doc .base .config .DocException ;
17
+ import org .fugerit .java .doc .freemarker .config .FreeMarkerComplexProcessStep ;
14
18
import org .fugerit .java .doc .freemarker .config .FreeMarkerConfigStep ;
19
+ import org .fugerit .java .doc .freemarker .config .FreeMarkerFunctionStep ;
20
+ import org .fugerit .java .doc .freemarker .config .FreeMarkerMapStep ;
15
21
import org .w3c .dom .Document ;
16
22
import org .w3c .dom .Element ;
17
23
import org .w3c .dom .NodeList ;
23
29
@ Slf4j
24
30
public class FreemarkerDocProcessConfigFacade {
25
31
32
+ public static final String ATT_DOC_CHAIN = "docChain" ;
33
+
34
+ public static final String ATT_CHAIN_STEP = "chainStep" ;
35
+
26
36
public static FreemarkerDocProcessConfig newSimpleConfig ( String id , String templatePath ) throws ConfigException {
27
37
FreemarkerDocProcessConfig config = new FreemarkerDocProcessConfig ();
28
- ConfigInitModel model = new ConfigInitModel ();
29
- model .setId (id );
30
- model .setPath ( templatePath );
31
- try {
32
- addConfiguration (model );
33
- } catch (Exception e ) {
34
- throw new ConfigException ( "Error configuring FreemarkerDocProcessConfig : " +e , e );
35
- }
36
- config .getConfigInitList ().add (model );
38
+ config .setDefaultChain (
39
+ new DefaultChainProvider () {
40
+ @ Override
41
+ public MiniFilterChain newDefaultChain (String id ) {
42
+ MiniFilterChain defaultChain = new MiniFilterChain ( "DEFAULT_CHAIN_" +id +"_" +System .currentTimeMillis (), MiniFilterChain .CONTINUE );
43
+ defaultChain .setChainId ( defaultChain .getKey () );
44
+ // config step
45
+ FreeMarkerConfigStep configStep = new FreeMarkerConfigStep ();
46
+ Properties configParams = new Properties ();
47
+ configParams .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_PATH , templatePath );
48
+ configStep .setParam01 ( id );
49
+ configStep .setCustomConfig ( convertConfiguration ( configParams ) );
50
+ defaultChain .getFilterChain ().add ( configStep );
51
+ // default step
52
+ FreeMarkerComplexProcessStep processStep = new FreeMarkerComplexProcessStep ();
53
+ Properties processAtts = new Properties ();
54
+ processAtts .setProperty ( "template-path" , "${chainId}.ftl" );
55
+ processAtts .setProperty ( "map-atts" , "simpleTableModel" );
56
+ processStep .setCustomConfig ( processAtts );
57
+ processStep .setChainId ( id );
58
+ defaultChain .getFilterChain ().add ( processStep );
59
+ return defaultChain ;
60
+ }
61
+ }
62
+ );
37
63
return config ;
38
64
}
39
-
65
+
40
66
public static FreemarkerDocProcessConfig loadConfig ( Reader xmlReader ) throws ConfigException {
41
67
FreemarkerDocProcessConfig result = null ;
42
68
try {
@@ -45,29 +71,8 @@ public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws C
45
71
dbf .setNamespaceAware ( true );
46
72
DocumentBuilder db = dbf .newDocumentBuilder ();
47
73
Document doc = db .parse ( new InputSource ( xmlReader ) );
48
- NodeList configInitList = doc .getElementsByTagName ( "configInit" );
49
- for ( int k =0 ; k <configInitList .getLength (); k ++ ) {
50
- Element currentTag = (Element ) configInitList .item ( k );
51
- ConfigInitModel model = new ConfigInitModel ();
52
- XmlBeanHelper .setFromElement ( model , currentTag );
53
- config .getConfigInitList ().add (model );
54
- 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
- }
69
- }
70
- NodeList docChainLisgt = doc .getElementsByTagName ( "docChain" );
74
+ // docChain reading
75
+ NodeList docChainLisgt = doc .getElementsByTagName ( ATT_DOC_CHAIN );
71
76
for ( int k =0 ; k <docChainLisgt .getLength (); k ++ ) {
72
77
Element currentTag = (Element ) docChainLisgt .item ( k );
73
78
DocChainModel model = new DocChainModel ();
@@ -78,36 +83,72 @@ public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws C
78
83
model .setMapAttsEnum ( DOMUtils .attributesToProperties ( mapAttsEnumTag ) );
79
84
log .debug ( "chain att enum {} -> {}" , model .getId (), model .getMapAttsEnum () );
80
85
}
86
+ if ( StringUtils .isNotEmpty ( model .getParent () ) ) {
87
+ DocChainModel parent = config .getDocChainList ().get ( model .getParent () );
88
+ if ( parent == null ) {
89
+ throw new DocException ( "No parent found : " +model .getParent () );
90
+ } else {
91
+ model .getChainStepList ().addAll ( parent .getChainStepList () );
92
+ }
93
+ }
81
94
// chain step
82
- NodeList chainStepList = currentTag .getElementsByTagName ( "chainStep" );
95
+ NodeList chainStepList = currentTag .getElementsByTagName ( ATT_CHAIN_STEP );
83
96
for ( int i =0 ; i <chainStepList .getLength (); i ++ ) {
84
97
Element currentChainStepTag = (Element ) chainStepList .item (i );
85
98
ChainStepModel chainStepModel = new ChainStepModel ();
86
- XmlBeanHelper .setFromElement ( chainStepModel , currentChainStepTag );
99
+ Properties atts = DOMUtils .attributesToProperties ( currentChainStepTag );
100
+ chainStepModel .setStepType ( atts .getProperty ( "stepType" ) );
101
+ atts .remove ( "stepType" );
102
+ chainStepModel .setAttributes (atts );
87
103
model .getChainStepList ().add (chainStepModel );
88
104
}
89
105
config .getDocChainList ().add (model );
90
106
}
91
107
result = config ;
92
108
log .info ( "loadConfig ok : {}" , result );
109
+ // populate mini filter chain model
110
+ for ( DocChainModel docChainModel : config .getDocChainList () ) {
111
+ MiniFilterChain chain = new MiniFilterChain ( docChainModel .getId (), MiniFilterChain .CONTINUE );
112
+ chain .setChainId ( docChainModel .getId () );
113
+ for ( ChainStepModel chainStepModel : docChainModel .getChainStepList () ) {
114
+ String type = BUILT_IN_STEPS .getProperty ( chainStepModel .getStepType (), chainStepModel .getStepType () );
115
+ MiniFilterBase step = (MiniFilterBase ) ClassHelper .newInstance ( type );
116
+ step .setCustomConfig ( chainStepModel .getAttributes () );
117
+ if ( FreeMarkerConfigStep .class .getName ().equalsIgnoreCase ( type ) ) {
118
+ step .setParam01 ( chainStepModel .getAttributes ().getProperty ( "id" ) );
119
+ Properties configProps = convertConfiguration ( chainStepModel .getAttributes () ) ;
120
+ step .setCustomConfig ( configProps );
121
+ }
122
+ step .setChainId ( chain .getChainId () );
123
+ chain .getFilterChain ().add ( step );
124
+ }
125
+ config .addAdditionalChain (chain );
126
+ }
93
127
} catch (Exception e ) {
94
128
throw new ConfigException ( "Error configuring FreemarkerDocProcessConfig : " +e , e );
95
129
}
96
130
return result ;
97
131
}
98
132
99
- private static void addConfiguration ( ConfigInitModel model ) throws Exception {
133
+ private static final Properties BUILT_IN_STEPS = new Properties ();
134
+ static {
135
+ BUILT_IN_STEPS .setProperty ( "config" , FreeMarkerConfigStep .class .getName () );
136
+ BUILT_IN_STEPS .setProperty ( "function" , FreeMarkerFunctionStep .class .getName () );
137
+ BUILT_IN_STEPS .setProperty ( "complex" , FreeMarkerComplexProcessStep .class .getName () );
138
+ BUILT_IN_STEPS .setProperty ( "map" , FreeMarkerMapStep .class .getName () );
139
+ }
140
+
141
+ private static Properties convertConfiguration ( Properties props ) {
100
142
Properties params = new Properties ();
101
- params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_VERSION , model .getVersion () );
102
- params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_MODE , model .getMode () );
103
- params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_PATH , model .getPath () );
104
- params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_CLASS , model .getClassName () );
105
- params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_EXCEPTION_HANDLER , model .getExceptionHandler () );
106
- params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_LOG_EXCEPTION , model .getLogException () );
107
- params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_WRAP_UNCHECKED_EXCEPTION , model .getWrapUncheckedExceptions () );
108
- params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_FALLBACK_ON_NULL_LOOP_VARIABLE , model .getFallbackOnNullLoopVariable () );
109
- Configuration conf = FreemarkerConfigHelper .getConfig ( model .getId (), params );
110
- model .setFreemarkerConfiguration ( conf );
143
+ params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_VERSION , props .getProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_VERSION , ConfigInitModel .DEFAULT_VERSION ) );
144
+ params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_MODE , props .getProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_MODE , ConfigInitModel .DEFAULT_MODE ) );
145
+ params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_PATH , props .getProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_PATH ) );
146
+ params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_CLASS , props .getProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_CLASS , ConfigInitModel .DEFAULT_CLASS_NAME ) );
147
+ params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_EXCEPTION_HANDLER , props .getProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_EXCEPTION_HANDLER , ConfigInitModel .DEFAULT_EXCEPTION_HANDLER ) );
148
+ params .setProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_LOG_EXCEPTION , props .getProperty ( FreeMarkerConfigStep .ATT_FREEMARKER_CONFIG_KEY_LOG_EXCEPTION , ConfigInitModel .DEFAULT_LOG_EXCEPTION ) );
149
+ 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 ) );
150
+ 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 ) );
151
+ return params ;
111
152
}
112
153
113
154
}
@@ -121,3 +162,4 @@ protected static Configuration getConfig( String key, Properties config ) throws
121
162
}
122
163
123
164
}
165
+
0 commit comments