https://github.com/antlr/stringtemplate4/blob/master/doc/introduction.md
Format of template file (*.stg) та (*.st)
import "file.stg" // importing templates from file
ID1() ::= " text <IDTemplateInFile()>" // only one line of text in ""
ID2() ::= " text <ID1()>"
// symbols << start the multi-line template and >> close
ID3() ::= <<
Text
<ID2TemplateInFile()>
Text
<ID2()>
Text
>>
'::=' divides Identificators of template and context
<ID()> serves to insert defined template. It is surrounded by "<>". Part "()" - is obligatory
Value of Attributes can not be defined in template
an this type of file template can not import any templates and be imported in file (*.stg)
Example of code:
import org.stringtemplate.v4.*;
public class StringTemplateExamle {
public static void main(String[] arg) {
//STRawGroupDir group = new STRawGroupDir("C:\\Development\\Projects\\Code database (SCID)\\Scheme (structure) of database in database (SoDiD)\\Templates");
//STGroupDir group = new STGroupDir("C:\\Development\\Projects\\Code database (SCID)\\Scheme (structure) of database in database (SoDiD)\\Templates");
//STGroupFile group = new STGroupFile("C:\\Development\\Projects\\Code database (SCID)\\Scheme (structure) of database in database (SoDiD)\\Templates\\Attributes.stg");
//STGroupFile group = new STGroupFile("C:\\Development\\Projects\\Code database (SCID)\\Scheme (structure) of database in database (SoDiD)\\Templates\\Utilities.stg");
// file (*.stg)
STGroupFile group = new STGroupFile("C:\\Development\\Projects\\Code database (SCID)\\Scheme (structure) of database in database (SoDiD)\\Templates\\SoDiD Structure Derby.stg");
ST st = group.getInstanceOf("SoDiDStructureDerby");
//ST st = group.getInstanceOf("Utilities");
//ST st = group.getInstanceOf("generatedColumnSpec");
System.out.println(st.render());
}
}
Only one unnamed template can be in one file '*.vm'
Velocity does not allow Multiple named template, but have means to define values of attributes
To get values of attributes "Formated string" can be used in text of templates
Formal notation: ${mudSlinger_9}
Shorthand notation: $mudSlinger_9
To initilize attributes are used constraction (directive)
#set( $IDName="Formated String" )
Example of template:
#set( $ID1 = "String of Text" )
#set( $ID2 = "$ID1 Some Text" )
#parse("file.vm") ## It outputs the template from file and set attributes
Text
${ID2}
Text
#parse - Renders a local template that is parsed by Velocity
Format: # [ { ] parse [ } ] ( arg )
arg - Refers to a template under TEMPLATE_ROOT.
#include - Renders local file(s) that are not parsed by Velocity
Format: # [ { ] include [ } ] ( arg [ arg2 ... argn ] )
arg - Refers to a valid file under TEMPLATE_ROOT.
Examples: String: #include( "disclaimer.txt" "opinion.txt" ) or Variable: #include( $foo $bar )
uses velocity-engine-core-2.2.jar
also needs libraries: commons-collections4-4.4.jar, commons-lang3-3.9.jar, slf4j-api-1.7.30.jar, slf4j-simple-1.7.30.jar
Example of program:
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import java.io.StringWriter;
public class VelocityExample {
public static void main(String[] arg) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(
VelocityEngine.RUNTIME_LOG_NAME, "mylog");
ve.setProperty("resource.loader.file.path", "C:\\Development\\Projects\\Code database (SCID)\\Scheme (structure) of database in database (SoDiD)\\Templates\\Velocity");
ve.init();
VelocityContext context = new VelocityContext();
Template t = ve.getTemplate("SoDiD Structure Derby.vm");
StringWriter sw = new StringWriter();
t.merge( context, sw );
System.out.println(sw);
}
}
Include very good and detailed documentation, but site is not stable and slow.
https://freemarker.apache.org/docs/dgui_misc_userdefdir.html
Multi-line templates without naming it and storing several templates in 1 file '*.ftlh' or '*.ftl'
Syntax of templates is similar on Velocity, but even more sophisticated, and can be configured as [...] instead of {...}
Access in template ${foo}, where 'foo' is string, variable or attribute.
To insert other template to parse serves directive
<#include "file.ftl">
Allows to define variables <#local ID=""> ,<#global ID = "Text">
and:
<#assign ID = "Text ${ID2} Text">
Example of template:
<#assign ID1 = "String of Text" >
<#assign ID2 = "${ID1} Some Text" >
<#include "file.ftl"> <-- It outputs the template from file and set attributes -->
Text
${ID2}
Text
For separating named elements from file in namespace
<#import "/libs/mylib.ftl" as my> <-- Defining -->
<@my.copyright date="1999-2002"/> <-- Using -->
Defining user named templates: <#macro IDOfAttribute> MultiLineText </#macro>
Invoking user defined templates (macros) in template <@IDOfAttribute/>
(need only freemarker.jar) (most other templators have complicated dependencies)
Example of Program
import freemarker.template.*;
import java.util.*;
import java.io.*;
public class FreeMarkerExample {
public static void main(String[] args) throws Exception {
/* ------------------------------------------------------------------------ */
/* You should do this ONLY ONCE in the whole application life-cycle: */
/* Create and adjust the configuration singleton */
Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
cfg.setDirectoryForTemplateLoading(new File("C:\\Development\\Projects\\Code database (SCID)\\Scheme (structure) of database in database (SoDiD)\\Templates\\Freemarker"));
// Recommended settings for new projects:
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);
cfg.setFallbackOnNullLoopVariable(false);
/* ------------------------------------------------------------------------ */
/* You usually do these for MULTIPLE TIMES in the application life-cycle: */
/* Create a data-model */
Map root = new HashMap();
// root.put("user", "Big Joe");
// Product latest = new Product();
// latest.setUrl("products/greenmouse.html");
// latest.setName("green mouse");
// root.put("latestProduct", latest);
/* Get the template (uses cache internally) */
Template temp = cfg.getTemplate("SoDiD Structure Derby.ftlh");
/* Merge data-model with template */
Writer out = new OutputStreamWriter(System.out);
temp.process(root, out);
// Note: Depending on what `out` is, you may need to call `out.close()`.
// This is usually the case for file output, but not for servlet output.
}
}
https://commons.apache.org/proper/commons-text/userguide.html
The functionality is very simple and documentation are not clear.
The default syntax of a substituted variable in template is ${variableName}
In the mode of 'Interpolator' some directive are available in a template, for instance
${file:UTF-8:PathToFile}
PathToFile -absolute path to a file or relative to some folder
In 'Interpolator' mode other variable substitutions ${variableName}
are not available, so after using 'Interpolator' substitution must be applying
Attributes are not maintained in template format and have to be stored and retrieved separately for instance in format of java.util.Properties
Java program have only one dependency: commons-text-1.8.jar
Example of program:
import org.apache.commons.text.StringSubstitutor;
import java.io.*;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class StringSubstitutorExample {
public static void main(String[] args) {
String content;
String fullnNameOfTemplateFile = "C:\\Development\\Projects\\Code database (SCID)\\Scheme (structure) of database in database (SoDiD)\\Templates\\StringSubstitutor\\SoDiD Structure Derby.ftlh";
try {
File templateFile = new File(fullnNameOfTemplateFile);
content = new String(Files.readAllBytes(templateFile.toPath()));
} catch (IOException e) {
System.out.println("Do not find template file: " + fullnNameOfTemplateFile);
return;
}
Properties prop = new Properties();
File attributeFile = new File("C:\\Development\\Projects\\Code database (SCID)\\Scheme (structure) of database in database (SoDiD)\\Templates\\StringSubstitutor\\Attributes.ftl");
try (InputStream inputStream = new FileInputStream(attributeFile)) {
prop.load(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Map valuesMap = new HashMap();
//valuesMap.put("StandardColumnsOfCatalogueWithHierarchy", "quick brown fox");
//String templateString = "The ${animal} jumped over the ${target}.";
Map valuesMap = new HashMap(prop);
// System.out.println(valuesMap);
StringSubstitutor interpolator = StringSubstitutor.createInterpolator();
interpolator.setEnableSubstitutionInVariables(true); // Allows for nested $'s.
// String resolvedString = interpolator.replace(content, valuesMap); // do not resolve file because in invoke from StringSubstitutor
String resolvedString = interpolator.replace(content);
StringSubstitutor sub = new StringSubstitutor(valuesMap);
resolvedString = sub.replace(resolvedString);
System.out.println(resolvedString);
}
}