77import javax .xml .parsers .DocumentBuilderFactory ;
88
99import 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 ;
1212import org .fugerit .java .core .cfg .xml .XmlBeanHelper ;
1313import org .fugerit .java .core .lang .helpers .ClassHelper ;
1414import 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