Skip to content

Commit

Permalink
Merge pull request #42 from fugerit-org/feature/issue_38_config_review
Browse files Browse the repository at this point in the history
Feature/issue 38 config review
  • Loading branch information
fugerit79 authored Jul 9, 2023
2 parents 65b475c + 394aee1 commit 378e4b8
Show file tree
Hide file tree
Showing 33 changed files with 474 additions and 79 deletions.
4 changes: 2 additions & 2 deletions docgen/parameters.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"title" : "Venus (Fugerit Document Generation Framework)",
"name": "Venus",
"version" : "1.1.2-rc.001",
"date" : "03/07/2023",
"version" : "1.3.0-rc.001",
"date" : "09/07/2023",
"organization" : {
"name" : "Fugerit Org",
"url" : "https://www.fugerit.org"
Expand Down
6 changes: 5 additions & 1 deletion docgen/release-notes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
1.2.0-rc.001 (2023-07-07)
1.3.0-rc.001 (2023-07-09)
------------------
+ [New freemarker configuration model](https://github.com/fugerit-org/fj-bom/issues/38)

1.2.0-rc.001 (2023-07-07)
------------------
+ Added module fj-doc-lib-simpletable-import to convert xlsx or csv to simple table

Expand Down
2 changes: 1 addition & 1 deletion fj-doc-base-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc</artifactId>
<version>1.2.0-rc.001</version>
<version>1.3.0-rc.001</version>
</parent>

<name>fj-doc-base-json</name>
Expand Down
2 changes: 1 addition & 1 deletion fj-doc-base-yaml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc</artifactId>
<version>1.2.0-rc.001</version>
<version>1.3.0-rc.001</version>
</parent>

<name>fj-doc-base-yaml</name>
Expand Down
2 changes: 1 addition & 1 deletion fj-doc-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc</artifactId>
<version>1.2.0-rc.001</version>
<version>1.3.0-rc.001</version>
</parent>

<name>fj-doc-base</name>
Expand Down
2 changes: 1 addition & 1 deletion fj-doc-bom-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc</artifactId>
<version>1.2.0-rc.001</version>
<version>1.3.0-rc.001</version>
</parent>

<name>fj-doc-bom-core</name>
Expand Down
2 changes: 1 addition & 1 deletion fj-doc-bom-fop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc</artifactId>
<version>1.2.0-rc.001</version>
<version>1.3.0-rc.001</version>
</parent>

<name>fj-doc-bom-fop</name>
Expand Down
4 changes: 2 additions & 2 deletions fj-doc-freemarker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc</artifactId>
<version>1.2.0-rc.001</version>
<version>1.3.0-rc.001</version>
</parent>

<name>fj-doc-freemarker</name>
Expand Down Expand Up @@ -47,7 +47,7 @@
<groupId>org.fugerit.java</groupId>
<artifactId>fj-doc-base</artifactId>
</dependency>

</dependencies>

<organization>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.fugerit.java.doc.base.process.DocProcessContext;
import org.fugerit.java.doc.base.process.DocProcessData;
import org.fugerit.java.doc.base.process.DocProcessorBasic;
import org.fugerit.java.doc.freemarker.process.ConfigInitModel;

import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
Expand All @@ -21,9 +22,10 @@ public class FreeMarkerConfigStep extends DocProcessorBasic {
public static final String ATT_DEFAULT = "FreeMarkerConfigStep.DEFAULT";

public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION = "version";
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_31 = ConfigInitModel.VERSION_2_3_31;
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_30 = "2.3.30";
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_29 = "2.3.29";
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_DEFAULT = ATT_FREEMARKER_CONFIG_KEY_VERSION_2_3_30;
public static final String ATT_FREEMARKER_CONFIG_KEY_VERSION_DEFAULT = ConfigInitModel.DEFAULT_VERSION;

public static final String ATT_FREEMARKER_CONFIG_KEY_MODE = "mode";
public static final String ATT_FREEMARKER_CONFIG_KEY_MODE_CLASS = "class";
Expand Down
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 + "]";
}

}
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();
}

}
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;
}

}
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);
}

}
Loading

0 comments on commit 378e4b8

Please sign in to comment.