-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from fugerit-org/feature/issue_38_config_review
Feature/issue 38 config review
- Loading branch information
Showing
33 changed files
with
474 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/process/ConfigInitModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.fugerit.java.doc.freemarker.process; | ||
|
||
import java.io.Serializable; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import org.fugerit.java.core.cfg.xml.IdConfigType; | ||
import org.fugerit.java.core.lang.helpers.BooleanUtils; | ||
import org.fugerit.java.core.util.collection.KeyString; | ||
import org.fugerit.java.core.util.regex.ParamFinder; | ||
import org.fugerit.java.doc.base.process.DocProcessContext; | ||
import org.fugerit.java.doc.base.process.DocProcessData; | ||
import org.fugerit.java.doc.freemarker.config.FreeMarkerConstants; | ||
import org.fugerit.java.doc.freemarker.config.FreemarkerApplyHelper; | ||
|
||
import freemarker.template.Configuration; | ||
import freemarker.template.Template; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ConfigInitModel implements IdConfigType, KeyString, Serializable { | ||
|
||
public static final String VERSION_2_3_31 = "2.3.31"; | ||
public static final String DEFAULT_VERSION = VERSION_2_3_31; | ||
|
||
public static final String DEFAULT_CLASS_NAME = FreemarkerDocProcessConfigFacade.class.getName(); | ||
|
||
public static final String DEFAULT_MODE = "class"; | ||
|
||
public static final String DEFAULT_EXCEPTION_HANDLER = "RETHROW_HANDLER"; | ||
|
||
public static final String DEFAULT_LOG_EXCEPTION = BooleanUtils.BOOLEAN_FALSE; | ||
|
||
public static final String DEFAULT_WRAP_UNCHECKED_EXCEPTION = BooleanUtils.BOOLEAN_TRUE; | ||
|
||
public static final String DEFAULT_FALL_BACK_ON_NULL_LOOP_VARIABLE = BooleanUtils.BOOLEAN_FALSE; | ||
|
||
private static final long serialVersionUID = -59587465058736934L; | ||
|
||
private String id; | ||
|
||
private String version = DEFAULT_VERSION; | ||
|
||
private String path; | ||
|
||
private String mode = DEFAULT_MODE; | ||
|
||
private String className = DEFAULT_CLASS_NAME; | ||
|
||
private String exceptionHandler = DEFAULT_EXCEPTION_HANDLER; | ||
|
||
private String logException = DEFAULT_LOG_EXCEPTION; | ||
|
||
private String wrapUncheckedExceptions = DEFAULT_WRAP_UNCHECKED_EXCEPTION; | ||
|
||
private String fallbackOnNullLoopVariable = DEFAULT_FALL_BACK_ON_NULL_LOOP_VARIABLE; | ||
|
||
private Configuration freemarkerConfiguration; | ||
|
||
public static final String CHAIN_ID_PARAM = "chainId"; | ||
|
||
protected void process( DocChainModel model, DocProcessContext context, DocProcessData data ) throws Exception { | ||
// override template path | ||
String templatePath = model.getTemplatePath(); | ||
ParamFinder finder = ParamFinder.newFinder(); | ||
Properties params = new Properties(); | ||
params.setProperty( CHAIN_ID_PARAM , model.getId() ); | ||
templatePath = finder.substitute( templatePath , params ); | ||
// map attributes | ||
Map<String, Object> map = FreeMarkerConstants.getFreeMarkerMap( context ); | ||
if ( map == null ) { | ||
map = new HashMap<>(); | ||
} | ||
map.putAll( context.toMap() ); | ||
Template template = this.getFreemarkerConfiguration().getTemplate( templatePath ); | ||
FreemarkerApplyHelper.setupFreemarkerMap( this.freemarkerConfiguration, map); | ||
Writer out = new StringWriter(); | ||
template.process( map, out); | ||
data.setCurrentXmlData( out.toString() ); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return this.getId(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ConfigInitModel [id=" + id + ", version=" + version + ", path=" + path + ", mode=" + mode | ||
+ ", className=" + className + ", exceptionHandler=" + exceptionHandler + ", logException=" | ||
+ logException + ", wrapUncheckedExceptions=" + wrapUncheckedExceptions | ||
+ ", fallbackOnNullLoopVariable=" + fallbackOnNullLoopVariable + ", freemarkerConfiguration=" | ||
+ freemarkerConfiguration + "]"; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/process/DocChainModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.fugerit.java.doc.freemarker.process; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.fugerit.java.core.cfg.xml.IdConfigType; | ||
import org.fugerit.java.core.util.collection.KeyString; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class DocChainModel implements IdConfigType, KeyString, Serializable { | ||
|
||
public static final String DEFAULT_TEMPLATE_PATH = "${chainId}.ftl"; | ||
|
||
private static final long serialVersionUID = 9076457107043072322L; | ||
|
||
private String id; | ||
|
||
private String templatePath = DEFAULT_TEMPLATE_PATH; | ||
|
||
@Override | ||
public String getKey() { | ||
return this.getId(); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...ker/src/main/java/org/fugerit/java/doc/freemarker/process/FreemarkerDocProcessConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.fugerit.java.doc.freemarker.process; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.fugerit.java.core.cfg.xml.ListMapConfig; | ||
import org.fugerit.java.doc.base.process.DocProcessConfig; | ||
import org.fugerit.java.doc.base.process.DocProcessContext; | ||
import org.fugerit.java.doc.base.process.DocProcessData; | ||
|
||
import lombok.Getter; | ||
|
||
public class FreemarkerDocProcessConfig extends DocProcessConfig implements Serializable { | ||
|
||
private static final long serialVersionUID = -6761081877582850120L; | ||
|
||
@Getter | ||
private ListMapConfig<ConfigInitModel> configInitList; | ||
|
||
@Getter | ||
private ListMapConfig<DocChainModel> docChainList; | ||
|
||
protected FreemarkerDocProcessConfig() { | ||
this.configInitList = new ListMapConfig<>(); | ||
this.docChainList = new ListMapConfig<>(); | ||
} | ||
|
||
public void process( String configId, String chainId, DocProcessContext context, DocProcessData data ) throws Exception { | ||
ConfigInitModel configInitModel = this.getConfigInitList().get( configId ); | ||
DocChainModel docChainModel = this.getChainOrDefault(chainId); | ||
configInitModel.process(docChainModel, context, data); | ||
} | ||
|
||
private DocChainModel getChainOrDefault( String id ) { | ||
DocChainModel model = this.getDocChainList().get( id ); | ||
if ( model == null ) { | ||
model = new DocChainModel(); | ||
model.setId( id ); | ||
} | ||
return model; | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
...c/main/java/org/fugerit/java/doc/freemarker/process/FreemarkerDocProcessConfigFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.fugerit.java.doc.freemarker.process; | ||
|
||
import java.io.Reader; | ||
import java.util.Properties; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
|
||
import org.fugerit.java.core.cfg.ConfigException; | ||
import org.fugerit.java.core.cfg.xml.XmlBeanHelper; | ||
import org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.InputSource; | ||
|
||
import freemarker.template.Configuration; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class FreemarkerDocProcessConfigFacade { | ||
|
||
public static FreemarkerDocProcessConfig newSimpleConfig( String id, String templatePath ) throws ConfigException { | ||
FreemarkerDocProcessConfig config = new FreemarkerDocProcessConfig(); | ||
ConfigInitModel model = new ConfigInitModel(); | ||
model.setId(id); | ||
model.setPath( templatePath ); | ||
try { | ||
addConfiguration(model); | ||
} catch (Exception e) { | ||
throw new ConfigException( "Error configuring FreemarkerDocProcessConfig : "+e , e ); | ||
} | ||
config.getConfigInitList().add(model); | ||
return config; | ||
} | ||
|
||
public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws ConfigException { | ||
FreemarkerDocProcessConfig result = null; | ||
try { | ||
FreemarkerDocProcessConfig config = new FreemarkerDocProcessConfig(); | ||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
dbf.setNamespaceAware( true ); | ||
DocumentBuilder db = dbf.newDocumentBuilder(); | ||
Document doc = db.parse( new InputSource( xmlReader ) ); | ||
NodeList configInitList = doc.getElementsByTagName( "configInit" ); | ||
for ( int k=0; k<configInitList.getLength(); k++ ) { | ||
Element currentTag = (Element) configInitList.item( k ); | ||
ConfigInitModel model = new ConfigInitModel(); | ||
XmlBeanHelper.setFromElement( model, currentTag ); | ||
config.getConfigInitList().add(model); | ||
addConfiguration(model); | ||
} | ||
NodeList docChainLisgt = doc.getElementsByTagName( "docChain" ); | ||
for ( int k=0; k<docChainLisgt.getLength(); k++ ) { | ||
Element currentTag = (Element) docChainLisgt.item( k ); | ||
DocChainModel model = new DocChainModel(); | ||
XmlBeanHelper.setFromElement( model, currentTag ); | ||
config.getDocChainList().add(model); | ||
} | ||
result = config; | ||
log.info( "loadConfig ok : {}", result ); | ||
} catch (Exception e) { | ||
throw new ConfigException( "Error configuring FreemarkerDocProcessConfig : "+e , e ); | ||
} | ||
return result; | ||
} | ||
|
||
private static void addConfiguration( ConfigInitModel model ) throws Exception { | ||
Properties params = new Properties(); | ||
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_VERSION , model.getVersion() ); | ||
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_MODE , model.getMode() ); | ||
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_PATH , model.getPath() ); | ||
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_CLASS , model.getClassName() ); | ||
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_EXCEPTION_HANDLER , model.getExceptionHandler() ); | ||
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_LOG_EXCEPTION , model.getLogException() ); | ||
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_WRAP_UNCHECKED_EXCEPTION , model.getWrapUncheckedExceptions() ); | ||
params.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_FALLBACK_ON_NULL_LOOP_VARIABLE , model.getFallbackOnNullLoopVariable() ); | ||
Configuration conf = FreemarkerConfigHelper.getConfig( model.getId(), params ); | ||
model.setFreemarkerConfiguration( conf ); | ||
} | ||
|
||
} | ||
|
||
class FreemarkerConfigHelper extends FreeMarkerConfigStep { | ||
|
||
private static final long serialVersionUID = 8824282827447873553L; | ||
|
||
protected static Configuration getConfig( String key, Properties config ) throws Exception { | ||
return FreeMarkerConfigStep.getConfig(key, config); | ||
} | ||
|
||
} |
Oops, something went wrong.