Commit 78ca443c authored by Daniel Bluhm's avatar Daniel Bluhm

Add velocity tools and use esc tool for newlines

Having access to these tools should be quite helpful for future
templates. Current templates can likely be simplified as well.
Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent d0c5995f
......@@ -108,6 +108,11 @@
<version>1.7.30</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity.tools</groupId>
<artifactId>velocity-tools-generic</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<parent>
......
......@@ -15,9 +15,9 @@ import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.ToolContext;
import org.apache.velocity.tools.ToolManager;
/**
* Abstract base class for classes that render a Java Source file through
......@@ -26,9 +26,52 @@ import org.apache.velocity.context.Context;
*/
public abstract class VelocitySourceWriter {
/**
* Tool configuration file.
*/
private static final String TOOL_CONFIG = "tools.xml";
/**
* Velocity Engine.
*/
private static VelocityEngine engine = configureVelocityEngine();
/**
* Velocity Tools.
*/
private static ToolManager tools = configureTools();
/**
* Set up the velocity engine with properties.
* @return initialized VelocityEngine
*/
private static VelocityEngine configureVelocityEngine() {
VelocityEngine engine = new VelocityEngine();
engine.init(VelocityProperties.get());
return engine;
}
/**
* Set up the velocity tool manager.
* @return
*/
private static ToolManager configureTools() {
// autoConfigure = false, includeDefaults = true
ToolManager tools = new ToolManager(false, true);
tools.configure(TOOL_CONFIG);
return tools;
}
/**
* Template for writing. Should be filled by concrete classes.
*/
protected String template;
/**
* Context for template. Should be filled by concrete classes.
*/
protected Map<String, Object> context;
public VelocitySourceWriter() {
this.context = new HashMap<>();
}
......@@ -38,13 +81,17 @@ public abstract class VelocitySourceWriter {
* @param writer to which the java source will be written
*/
public void write(Writer writer) {
// Make sure Velocity is initialized. Subsequent calls are harmless.
Velocity.init(VelocityProperties.get());
if (template == null || template.isEmpty()) {
throw new IllegalStateException("template must be set by concrete VelocitySourceWriter.");
}
// Make velocity context from generic map context.
Context velocityContext = new VelocityContext(context);
// Make tool context (subclass of velocity context) from generic map
// context. This places all tools into the template context.
ToolContext velocityContext = tools.createContext();
// Put all our values into the template context.
velocityContext.putAll(context);
// Write template from context.
Velocity.mergeTemplate(template, "UTF-8", velocityContext, writer);
engine.mergeTemplate(template, "UTF-8", velocityContext, writer);
}
}
}
\ No newline at end of file
......@@ -46,7 +46,7 @@ public class ${class} implements ${interface}, Serializable {
#foreach($field in $fields)
#fielddoc
#nonnull($tab, $newline)
#nonnull($tab, $esc.newline)
#if(!${field.Getter} || ${field.VarNameDifferent})
@Getter(AccessLevel.NONE)
#end
......@@ -179,7 +179,7 @@ public class ${class} implements ${interface}, Serializable {
retval =
#@settab(6)
#foreach($field in ${fields.Match})#@nonewline
${field.VarName}Match#if($foreach.hasNext) &&#if($foreach.count % 3 == 0)$newline#else #end#end
${field.VarName}Match#if($foreach.hasNext) &&#if($foreach.count % 3 == 0)$esc.newline#else #end#end
#end#end;
#end
}
......
......@@ -10,14 +10,12 @@
## Use as a block macro to set the tab index of all lines in body.
#macro(settab $num)
#set($tab = " ")
#set($newline = "
")
#if($num == 0)
#set($shift = "")
#else
#set($shift = "#foreach($i in [1..$num])$tab#end")
#end
#noop$shift$bodyContent.toString().replace("$tab", "").replace("$newline", "$newline$shift").trim()
#noop$shift$bodyContent.toString().replace("$tab", "").replace("$esc.newline", "$esc.newline$shift").trim()
#end
## Use as a block macro to remove the first newline in the body.
......@@ -62,12 +60,10 @@
## Get field doc comment
#macro(fielddoc)
#set($tab = " ")
#set($newline = "
")
#if(${field.DocString})
#@settab(1)
/**
* $field.DocString.trim().replace("$newline", "$newline$tab *")
* $field.DocString.trim().replace("$esc.newline", "$esc.newline$tab *")
*/
#end
#end
......
<?xml version="1.0" encoding="windows-1252"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<tools>
<data type="number" key="TOOLS_VERSION" value="3.0-SNAPSHOT"/>
<data type="boolean" key="GENERIC_TOOLS_AVAILABLE" value="true"/>
<toolbox scope="application">
<tool class="org.apache.velocity.tools.generic.ClassTool" value="classtool"/>
<tool class="org.apache.velocity.tools.generic.CollectionTool"/>
<tool class="org.apache.velocity.tools.generic.ComparisonDateTool"/>
<tool class="org.apache.velocity.tools.generic.DisplayTool"/>
<tool class="org.apache.velocity.tools.generic.EscapeTool"/>
<tool class="org.apache.velocity.tools.generic.FieldTool"/>
<tool class="org.apache.velocity.tools.generic.LogTool"/>
<tool class="org.apache.velocity.tools.generic.MathTool"/>
<tool class="org.apache.velocity.tools.generic.NumberTool"/>
<tool class="org.apache.velocity.tools.generic.ResourceTool"/>
<!-- include deprecated tools, which will only be instanciated on demand -->
<tool class="org.apache.velocity.tools.generic.AlternatorTool"/>
<tool class="org.apache.velocity.tools.generic.ConversionTool"/>
<tool class="org.apache.velocity.tools.generic.SortTool"/>
</toolbox>
<toolbox scope="request">
<tool class="org.apache.velocity.tools.generic.ContextTool"/>
<tool class="org.apache.velocity.tools.generic.ImportTool"/>
<tool class="org.apache.velocity.tools.generic.JsonTool"/>
<tool class="org.apache.velocity.tools.generic.LinkTool"/>
<tool class="org.apache.velocity.tools.generic.LoopTool"/>
<tool class="org.apache.velocity.tools.generic.RenderTool"/>
<tool class="org.apache.velocity.tools.generic.XmlTool"/>
<!--
This is not useful in its default form.
But, if it were, it'd be request-scoped.
<tool class="org.apache.velocity.tools.generic.ValueParser"/>
-->
</toolbox>
</tools>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment