StringTemplate 4 Object Wrapper
StringTemplate is © Copyright StringTemplate / Terence Parr 2014
Stow is © Copyright Dave Parfitt 2014
Stow generates Java wrapper classes for StringTemplate4 template groups. This allows you to easily access your templates by method instead of using String based keys, and will also catch certain template parameter problems at compile time:
- renamed parameters
- removed parameters
- changes in parameter case
<dependency>
<groupId>com.github.metadave</groupId>
<artifactId>Stow</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
The following example takes a StringTemplate group file (Greeting.stg
), and generates two classes: DemoGreeting
and DemoFancyName
. Finally, a demo program uses these classes to generate output.
Greeting.stg
Greeting(YourName, MyName) ::= <<
Hello <YourName>, my name is <MyName>!
>>
FancyName(Name) ::= <<
~~~<Name>~~~
>>
Run stow with the following parameters:
- -java_package com.foo.bar
- -class_prefix Demo
- -dest /path/to/some_directory
- -stg resources/Greeting.stg
which generates:
package com.foo.bar; import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import com.metadave.stow.AbstractStow; // generated by stow // https://github.com/metadave/stow public class DemoGreeting implements AbstractStow { ST st; public static final String templateName = "Greeting"; public DemoGreeting(STGroup g) { st = g.getInstanceOf(templateName); } public ST getST() { return st; } public DemoGreeting addYourName(AbstractStow val) { st.add("YourName", val.getST()); return this; } public DemoGreeting addYourName(Object val) { st.add("YourName", val); return this; } public DemoGreeting addMyName(AbstractStow val) { st.add("MyName", val.getST()); return this; } public DemoGreeting addMyName(Object val) { st.add("MyName", val); return this; } }
and
package com.foo.bar; import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import com.metadave.stow.AbstractStow; // generated by stow // https://github.com/metadave/stow public class DemoFancyName implements AbstractStow { ST st; public static final String templateName = "FancyName"; public DemoFancyName(STGroup g) { st = g.getInstanceOf(templateName); } public ST getST() { return st; } public DemoFancyName addName(AbstractStow val) { st.add("Name", val.getST()); return this; } public DemoFancyName addName(Object val) { st.add("Name", val); return this; } }
The snippet of code below shows basic usage of the generated code:
STGroup stg = new STGroupFile("Greeting.stg"); DemoGreeting g = new DemoGreeting(stg); g.addMyName("Dave"); DemoFancyName you = new DemoFancyName(stg); you.addName("User"); // added as an ST object using the AbstractStow interface g.addYourName(you); System.out.println(g.getST().render());
When the code above is run, the following text is displayed in the console:
Hello ~~~User~~~, my name is Dave!
This needs some cleanup, it seems like my IDE picks up the generated classes but Maven doesn't.
Change the arguments -java_package
, -class_prefix
, -dest
, and -stg
in the monsterous snippet of XML below, and add to your pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>RunStow</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>com.metadave.stow.Stow</argument>
<argument>-java_package</argument>
<argument>com.foo.bar</argument>
<argument>-class_prefix</argument>
<argument>Demo</argument>
<argument>-dest</argument>
<argument>${project.build.sourceDirectory}/com/foo/bar</argument>
<argument>-stg</argument>
<argument>${project.build.sourceDirectory}/../resources/Demo.stg</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
Fork this repo, create a branch with
git checkout -b your_branch_name
Submit a pull request when your code is ready for review.
http://www.apache.org/licenses/LICENSE-2.0.html
© 2014 Dave Parfitt