|
| 1 | +package org.fugerit.java.doc.freemarker.process; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.io.StringWriter; |
| 5 | +import java.io.Writer; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Properties; |
| 9 | + |
| 10 | +import org.fugerit.java.core.cfg.xml.IdConfigType; |
| 11 | +import org.fugerit.java.core.lang.helpers.BooleanUtils; |
| 12 | +import org.fugerit.java.core.util.collection.KeyString; |
| 13 | +import org.fugerit.java.core.util.regex.ParamFinder; |
| 14 | +import org.fugerit.java.doc.base.process.DocProcessContext; |
| 15 | +import org.fugerit.java.doc.base.process.DocProcessData; |
| 16 | +import org.fugerit.java.doc.freemarker.config.FreeMarkerConstants; |
| 17 | +import org.fugerit.java.doc.freemarker.config.FreemarkerApplyHelper; |
| 18 | + |
| 19 | +import freemarker.template.Configuration; |
| 20 | +import freemarker.template.Template; |
| 21 | +import lombok.Data; |
| 22 | + |
| 23 | +@Data |
| 24 | +public class ConfigInitModel implements IdConfigType, KeyString, Serializable { |
| 25 | + |
| 26 | + public static final String VERSION_2_3_31 = "2.3.31"; |
| 27 | + public static final String DEFAULT_VERSION = VERSION_2_3_31; |
| 28 | + |
| 29 | + public static final String DEFAULT_CLASS_NAME = FreemarkerDocProcessConfigFacade.class.getName(); |
| 30 | + |
| 31 | + public static final String DEFAULT_MODE = "class"; |
| 32 | + |
| 33 | + public static final String DEFAULT_EXCEPTION_HANDLER = "RETHROW_HANDLER"; |
| 34 | + |
| 35 | + public static final String DEFAULT_LOG_EXCEPTION = BooleanUtils.BOOLEAN_FALSE; |
| 36 | + |
| 37 | + public static final String DEFAULT_WRAP_UNCHECKED_EXCEPTION = BooleanUtils.BOOLEAN_TRUE; |
| 38 | + |
| 39 | + public static final String DEFAULT_FALL_BACK_ON_NULL_LOOP_VARIABLE = BooleanUtils.BOOLEAN_FALSE; |
| 40 | + |
| 41 | + private static final long serialVersionUID = -59587465058736934L; |
| 42 | + |
| 43 | + private String id; |
| 44 | + |
| 45 | + private String version = DEFAULT_VERSION; |
| 46 | + |
| 47 | + private String path; |
| 48 | + |
| 49 | + private String mode = DEFAULT_MODE; |
| 50 | + |
| 51 | + private String className = DEFAULT_CLASS_NAME; |
| 52 | + |
| 53 | + private String exceptionHandler = DEFAULT_EXCEPTION_HANDLER; |
| 54 | + |
| 55 | + private String logException = DEFAULT_LOG_EXCEPTION; |
| 56 | + |
| 57 | + private String wrapUncheckedExceptions = DEFAULT_WRAP_UNCHECKED_EXCEPTION; |
| 58 | + |
| 59 | + private String fallbackOnNullLoopVariable = DEFAULT_FALL_BACK_ON_NULL_LOOP_VARIABLE; |
| 60 | + |
| 61 | + private Configuration freemarkerConfiguration; |
| 62 | + |
| 63 | + public static final String CHAIN_ID_PARAM = "chainId"; |
| 64 | + |
| 65 | + protected void process( DocChainModel model, DocProcessContext context, DocProcessData data ) throws Exception { |
| 66 | + // override template path |
| 67 | + String templatePath = model.getTemplatePath(); |
| 68 | + ParamFinder finder = ParamFinder.newFinder(); |
| 69 | + Properties params = new Properties(); |
| 70 | + params.setProperty( CHAIN_ID_PARAM , model.getId() ); |
| 71 | + templatePath = finder.substitute( templatePath , params ); |
| 72 | + // map attributes |
| 73 | + Map<String, Object> map = FreeMarkerConstants.getFreeMarkerMap( context ); |
| 74 | + if ( map == null ) { |
| 75 | + map = new HashMap<>(); |
| 76 | + } |
| 77 | + map.putAll( context.toMap() ); |
| 78 | + Template template = this.getFreemarkerConfiguration().getTemplate( templatePath ); |
| 79 | + FreemarkerApplyHelper.setupFreemarkerMap( this.freemarkerConfiguration, map); |
| 80 | + Writer out = new StringWriter(); |
| 81 | + template.process( map, out); |
| 82 | + data.setCurrentXmlData( out.toString() ); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String getKey() { |
| 87 | + return this.getId(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public String toString() { |
| 92 | + return "ConfigInitModel [id=" + id + ", version=" + version + ", path=" + path + ", mode=" + mode |
| 93 | + + ", className=" + className + ", exceptionHandler=" + exceptionHandler + ", logException=" |
| 94 | + + logException + ", wrapUncheckedExceptions=" + wrapUncheckedExceptions |
| 95 | + + ", fallbackOnNullLoopVariable=" + fallbackOnNullLoopVariable + ", freemarkerConfiguration=" |
| 96 | + + freemarkerConfiguration + "]"; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments