Skip to content

Commit

Permalink
Implemented new FreeMarkerSkipProcessStep #225 (#226)
Browse files Browse the repository at this point in the history
* Implemented new FreeMarkerSkipProcessStep #225

* Remove sonar cloud issues

* Added skipfm step documentation #225
  • Loading branch information
fugerit79 authored Oct 20, 2024
1 parent 7ba6b73 commit e5c5733
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 38 deletions.
4 changes: 2 additions & 2 deletions fj-doc-freemarker/src/main/docs/fdp_xsd_config_ref.html
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ <h1 style="font-weight: bold;">Reference xsd documentation for Venus - Fugerit
<span >sourceType</span>
</td>
<td id="cell_2_1" style=" width: 40%; border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 2px;">
<span >The source type : xml (default), json or yaml</span>
<span >The source type : xml (default), json or yaml. (since 8.9.6)</span>
</td>
<td id="cell_2_2" style=" width: 40%; border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 2px;">
<span >sourceType , base : string , enumeration : [ xml , json , yaml ]</span>
Expand Down Expand Up @@ -479,7 +479,7 @@ <h1 style="font-weight: bold;">Reference xsd documentation for Venus - Fugerit
<span >stepType</span>
</td>
<td id="cell_0_1" style=" width: 40%; border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 2px;">
<span >Can be a java type value implementing DocProcessorBasic or a fixed value for 'config', 'map', 'function', 'complex'</span>
<span >Can be a java type value implementing DocProcessorBasic or a fixed value for 'config', 'map', 'function', 'complex', 'skipfm' (skip FreeMarkerProcessing)</span>
</td>
<td id="cell_0_2" style=" width: 40%; border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 2px;">
<span >string</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ public class FreeMarkerComplexProcessStep extends FreeMarkerProcessStep {

public static final String ATT_MAP_ALL = "map-all";
public static final String ATT_MAP_ATTS = "map-atts";


public static String overrideTemplatePath( Properties atts, String chainId ) throws ConfigException {
String templatePath = atts.getProperty( ATT_TEMPLATE_PATH );
if ( StringUtils.isEmpty( templatePath ) ) {
throw new ConfigException( "Template must be provided" );
}
ParamFinder finder = ParamFinder.newFinder();
Properties params = new Properties();
params.setProperty( CHAIN_ID_PARAM , chainId );
templatePath = finder.substitute( templatePath , params );
return templatePath;
}

@Override
public int process(DocProcessContext context, DocProcessData data) throws Exception {
Properties atts = this.getCustomConfig();
// override template path
if ( StringUtils.isEmpty( this.getParam01() ) ) {
String templatePath = atts.getProperty( ATT_TEMPLATE_PATH );
if ( StringUtils.isEmpty( templatePath ) ) {
throw new ConfigException( "Template must be provided" );
}
ParamFinder finder = ParamFinder.newFinder();
Properties params = new Properties();
params.setProperty( CHAIN_ID_PARAM , this.getChainId() );
templatePath = finder.substitute( templatePath , params );
this.setParam01( templatePath );
this.setParam01( overrideTemplatePath( atts, this.getChainId() ) );
}
// map attributes
Map<String, Object> map = FreeMarkerConstants.getFreeMarkerMap( context );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.fugerit.java.doc.freemarker.config;

import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import org.fugerit.java.core.io.StreamIO;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.doc.base.process.DocProcessContext;
import org.fugerit.java.doc.base.process.DocProcessData;

import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class FreeMarkerSkipProcessStep extends FreeMarkerProcessStep {

/**
*
*/
private static final long serialVersionUID = -7009153804877056158L;

public static final String ATT_TEMPLATE_PATH = "template-path";
public static final String CHAIN_ID_PARAM = "chainId";

public static final String ATT_MAP_ALL = "map-all";
public static final String ATT_MAP_ATTS = "map-atts";

@Override
public int process(DocProcessContext context, DocProcessData data) throws Exception {
Properties atts = this.getCustomConfig();
// override template path
if ( StringUtils.isEmpty( this.getParam01() ) ) {
this.setParam01( FreeMarkerComplexProcessStep.overrideTemplatePath( atts, this.getChainId() ) );
}
Configuration cfg = (Configuration) context.getAttribute( FreeMarkerConstants.ATT_FREEMARKER_CONFIG );
TemplateLoader templateLoader = cfg.getTemplateLoader();
String path = this.getParam01();
Object templateSource = templateLoader.findTemplateSource( path );
try (Reader reader = templateLoader.getReader( templateSource, StandardCharsets.UTF_8.name() ) ) {
data.setCurrentXmlData(StreamIO.readString(reader));
}
return CONTINUE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public static void setupFreemarkerMap( Configuration cfg, Map<String, Object> ma
public static void addStaticAccess( Configuration cfg, Map<String, Object> map, String key, Class<?> c ) throws TemplateModelException {
BeansWrapper wrapper = new BeansWrapperBuilder( cfg.getIncompatibleImprovements() ).build();
TemplateHashModel staticModels = wrapper.getStaticModels();
TemplateHashModel docConfigStatis = (TemplateHashModel) staticModels.get( c.getName() );
map.put( key , docConfigStatis);
TemplateHashModel docConfigStatic = (TemplateHashModel) staticModels.get( c.getName() );
map.put( key , docConfigStatic);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import org.fugerit.java.core.xml.dom.DOMUtils;
import org.fugerit.java.doc.base.config.DocException;
import org.fugerit.java.doc.base.config.DocTypeHandler;
import org.fugerit.java.doc.freemarker.config.FreeMarkerComplexProcessStep;
import org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep;
import org.fugerit.java.doc.freemarker.config.FreeMarkerFunctionStep;
import org.fugerit.java.doc.freemarker.config.FreeMarkerMapStep;
import org.fugerit.java.doc.freemarker.config.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
Expand All @@ -47,13 +44,30 @@ private FreemarkerDocProcessConfigFacade() {}
public static final String ATT_CHAIN_STEP = "chainStep";

public static final String ATT_STEP_TYPE = "stepType";


/**
* Corresponding to type : org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep
*/
public static final String STEP_TYPE_CONFIG = "config";


/**
* Corresponding to type : org.fugerit.java.doc.freemarker.config.FreeMarkerFunctionStep
*/
public static final String STEP_TYPE_FUNCTION = "function";


/**
* Corresponding to type : org.fugerit.java.doc.freemarker.config.FreeMarkerComplexProcessStep
*/
public static final String STEP_TYPE_COMPLEX = "complex";


/**
* Corresponding to type : org.fugerit.java.doc.freemarker.config.FreeMarkerSkipProcessStep
*/
public static final String STEP_TYPE_SKIPFM = "skipfm";

/**
* Corresponding to type : org.fugerit.java.doc.freemarker.config.FreeMarkerMapStep
*/
public static final String STEP_TYPE_MAP = "map";

/**
Expand Down Expand Up @@ -278,6 +292,7 @@ private static void generalPropertiesSetup( FreemarkerDocProcessConfig config ,
BUILT_IN_STEPS.setProperty( STEP_TYPE_CONFIG , FreeMarkerConfigStep.class.getName() );
BUILT_IN_STEPS.setProperty( STEP_TYPE_FUNCTION , FreeMarkerFunctionStep.class.getName() );
BUILT_IN_STEPS.setProperty( STEP_TYPE_COMPLEX , FreeMarkerComplexProcessStep.class.getName() );
BUILT_IN_STEPS.setProperty( STEP_TYPE_SKIPFM , FreeMarkerSkipProcessStep.class.getName() );
BUILT_IN_STEPS.setProperty( STEP_TYPE_MAP , FreeMarkerMapStep.class.getName() );
BUILT_IN_STEPS.keySet().stream().forEach( k -> BUILT_IN_STEPS_REVERSE.put( BUILT_IN_STEPS.get( k ) , k ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @project : fj-doc-freemarker
* @creation : 2023-07-12
* @version : 1.0.0-rc.4 (2024-10-20)
* @version : 1.0.0-rc.5 (2024-10-20)
*
* XSD for Freemarker Doc Process Configuration
*/
Expand Down Expand Up @@ -147,9 +147,9 @@
<xsd:documentation>A chain parent (will inherits all the chainStep)</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="sourceType" type="fdp:sourceType" use="optional">
<xsd:attribute name="sourceType" type="fdp:sourceType" use="optional" default="xml">
<xsd:annotation>
<xsd:documentation>The source type : xml (default), json or yaml</xsd:documentation>
<xsd:documentation>The source type : xml (default), json or yaml. (since 8.9.6)</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
Expand All @@ -168,7 +168,7 @@
</xsd:choice>
<xsd:attribute name="stepType" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation>Can be a java type value implementing DocProcessorBasic or a fixed value for 'config', 'map', 'function', 'complex'</xsd:documentation>
<xsd:documentation>Can be a java type value implementing DocProcessorBasic or a fixed value for 'config', 'map', 'function', 'complex', 'skipfm' (skip FreeMarkerProcessing)</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="map-atts" type="xsd:string" use="optional">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.fail;

import java.io.*;
import java.util.Properties;

import org.fugerit.java.core.cfg.ConfigException;
import org.fugerit.java.core.cfg.ConfigRuntimeException;
Expand All @@ -17,6 +18,9 @@
import org.fugerit.java.doc.base.process.DocProcessContext;
import org.fugerit.java.doc.base.process.DocProcessData;
import org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep;
import org.fugerit.java.doc.freemarker.config.FreeMarkerConstants;
import org.fugerit.java.doc.freemarker.config.FreeMarkerSkipProcessStep;
import org.fugerit.java.doc.freemarker.config.FreemarkerApplyHelper;
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlFragmentTypeHandlerEscapeUTF8;
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandler;
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerEscapeUTF8;
Expand Down Expand Up @@ -76,12 +80,31 @@ public void testSource() throws Exception {
config.fullProcess( currentChainId, DocProcessContext.newContext(), type, fos );
}
}
// test template does not exist
try ( ByteArrayOutputStream os = new ByteArrayOutputStream() ) {
DocProcessContext context = DocProcessContext.newContext();
Assert.assertThrows( ConfigRuntimeException.class, () -> config.fullProcess( "not-exists", context, DocConfig.TYPE_HTML, os ) );
}
}

@Test
public void testSkipFM() throws Exception {
DocProcessContext context = DocProcessContext.newContext();
DocProcessData data = new DocProcessData();
FreeMarkerConfigStep configStep = new FreeMarkerConfigStep();
configStep.setParam01( "FJ_DOC_TEST_CUSTOM" );
Properties customConfig = new Properties();
customConfig.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_MODE, FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_MODE_CLASS );
customConfig.setProperty( FreeMarkerConfigStep.ATT_FREEMARKER_CONFIG_KEY_PATH, "/fj_doc_test/template/" );
configStep.setCustomConfig( customConfig );
configStep.process( context, data );
// test skip fm
FreeMarkerSkipProcessStep step = new FreeMarkerSkipProcessStep();
step.setParam01( "asciidoc-xml.ftl" );
step.process( context, data );
Assert.assertNotNull( data.getCurrentXmlData() );
}

@Test
public void testConfigFail01() {
Assert.assertThrows( ConfigRuntimeException.class , () -> FreemarkerDocProcessConfigFacade.loadConfigSafe( "cl://not-exists.xml" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
</docChain>

<docChain id="asciidoc-xml" parent="shared">
<chainStep stepType="complex" template-path="${chainId}.ftl"/>
<chainStep stepType="skipfm" template-path="${chainId}.ftl"/>
</docChain>

<docChain sourceType="json" id="asciidoc-json" parent="shared">
<chainStep stepType="complex" template-path="${chainId}.ftl"/>
<chainStep stepType="skipfm" template-path="${chainId}.ftl"/>
</docChain>

<docChain sourceType="yaml" id="asciidoc-yaml" parent="shared">
<chainStep stepType="complex" template-path="${chainId}.ftl"/>
<chainStep stepType="skipfm" template-path="${chainId}.ftl"/>
</docChain>

</freemarker-doc-process-config>
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
}, {
"align" : "center",
"_t" : "para",
"_v" : "${r"${currentPage}"} / ${r"${pageCount}"}"
"_v" : "${currentPage} / ${pageCount}"
}, {
"align" : "right",
"_t" : "para",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
</header-ext>
<footer-ext>
<para align="left">test</para>
<para align="center">${r"${currentPage}"} / ${r"${pageCount}"}</para>
<!--
if going throw freemarker processing should be :
${r"${currentPage}"} / ${r"${pageCount}"}
-->
<para align="center">${currentPage} / ${pageCount}</para>
<para align="right">test</para>
</footer-ext>
<bookmark-tree>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ _e:
_v: "test"
- align: "center"
_t: "para"
_v: "${r"${currentPage}"} / ${r"${pageCount}"}"
# if going throw freemarker processing should be :
# "${r"${currentPage}"} / ${r"${pageCount}"}"
_v: "${currentPage} / ${pageCount}"
- align: "right"
_t: "para"
_v: "test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,35 @@ And one or more document process step :

==== Build in step types

[cols="3*", options="header"]
[cols="1,2,3", options="header"]
|========================================================================================================================================
| name | type | description
| config | org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep | responsable for freemarker and venus configuration
| function | org.fugerit.java.doc.freemarker.config.FreeMarkerFunctionStep | add freemarker functions to the data model
| complex | org.fugerit.java.doc.freemarker.config.FreeMarkerComplexProcessStep | it apply the freemarker template and render the output
| map | org.fugerit.java.doc.freemarker.config.FreeMarkerMapStep | Venus data model to Freemarker data model mapping

| name
| type
| description

| config
| org.fugerit.java.doc.freemarker.config.&#8203;FreeMarkerConfigStep
| responsable for freemarker and venus configuration

| function
| org.fugerit.java.doc.freemarker.config.&#8203;FreeMarkerFunctionStep
| add freemarker functions to the data model

| complex
| org.fugerit.java.doc.freemarker.config.&#8203;FreeMarkerComplexProcessStep
| it apply the freemarker template and render the output

| map
| org.fugerit.java.doc.freemarker.config.&#8203;FreeMarkerMapStep
| Venus data model to Freemarker data model mapping

| skipfm
| org.fugerit.java.doc.freemarker.config.&#8203;FreeMarkerSkipProcessStep
| When using this step, freemarker apply template will be skipped. (Since _8.9.7_)

|========================================================================================================================================

NOTE: Additional step can be added setting the fully qualified class name in the type attribute.

NOTE: When using *skipfm* no FreeMarker template syntax should be used in the template.

0 comments on commit e5c5733

Please sign in to comment.