stringtemplate-4

Combining Antlr 3.5.2 with StringTemplate 4 for code generation

怎甘沉沦 提交于 2020-01-05 13:09:37
问题 Current project I'm working on is limited to using antlr 3.5.2, but I would like to use the featureset of StringTemplate 4 for our code generation. Can antlr 3.5.2 generate a java treewalker that uses StringTemplate 4? (e.g. a tree grammer with output=template that results in a java file with ST* references instead of StringTemplate*) 回答1: The output=template option only supports StringTemplate 3. You can still support StringTemplate 4, but it would require using embedded actions or a hand

how do I iterate though a java list in stringtemplate?

允我心安 提交于 2020-01-01 04:19:12
问题 I want to iterate through a hibernate query results inside stringtemplate. I've been looking for examples but I can't find anything. can you please help? thanks 回答1: The syntax looks like <items :{ item | <item> }> Putting it together in Java: List<String> teams = Arrays.asList("Cats", "Birds", "Turtles"); ST s = new ST( "<teams :{team | <team> }>"); s.add("teams", teams); System.out.println(s.render()); In this example, I iterate over the List and print each team that is in the teams list.

StringTemplate indentation adds whitespace inside String

混江龙づ霸主 提交于 2019-12-23 21:50:07
问题 This is obviously a SSCCE. I have the following template file: xml(value)::=<< <a> <b> ^value^ </b> </a> >> ... with the following code: private static final String VALUE="alpha\nbeta"; public static void main(String args[]) throws Exception { STGroup stGroup = new STGroupFile("templates/a.xml.stg", '^', '^'); ST st = stGroup.getInstanceOf("xml"); st.add("value", VALUE); System.out.printf("--------\n%s\n--------", st.render()); } The code produces: [java] -------- [java] <a> [java] <b> [java]

Unable to catch STException in StringTemplate 4

旧城冷巷雨未停 提交于 2019-12-13 12:17:54
问题 I am unable to catch the STException thrown by the STGroupFile. This is a problem. I need to abort if the template is bad. To reproduce this problem, I have this incorrect template file called tmp.stg: temp1(param1)::=<< %if(param1)% %param1:{%temp2(p)%}; separator"\n"% %endif% >> And this groovy code to process it: #!/usr/bin/env groovy @Grab(group="org.antlr", module="ST4", version="4.0.8") import org.stringtemplate.v4.STGroupFile; import org.stringtemplate.v4.NumberRenderer; public class

custom string delimiters stringtemplate-4

怎甘沉沦 提交于 2019-12-12 20:34:15
问题 I am trying to use stringtemplate-4 engine in android. But I need starting delimiter, " {{ " while ending delimiter should be, " }} " Here, I think only char delimiters are allowed. So how to use string delimiters? Thnx in advance. 回答1: StringTemplate only supports using single characters as the delimiter. This limitation is coded in several places, including but not limited to the following. The STGroup constructors, and the delimiterStartChar and delimiterStopChar fields of the same class

Calling Java function with arguments from StringTemplate?

你离开我真会死。 提交于 2019-12-12 14:46:26
问题 StringTemplate allows programmers to fetch data through getters(a function with no arguments). I would like to know that Is it possible to call Java function with arguments from String Template? 回答1: There is a workaround by abusing dictionaries. Here is an example of implementing "function" for limiting item count in a List (issue on github). In your code add dictionary: group.defineDictionary("max", new MaxListItemsLimiter()); Usage (in this example first item in array is max. items count):

How to get StringTemplate V4 to ignore < as delimiter?

☆樱花仙子☆ 提交于 2019-12-12 11:32:12
问题 I'm using StringTemplate V4 to generate some HTML code in my project. I need to have HTML formatting in my templates, so using the default delimiters < and > would be very awkward. So, I'm creating a group passing the delimiter as argument (as recommended by this question), but it simply doesn't work. Here is my test code: public void testTemplate() { char sep = '$'; STGroup stGroup = new STGroupString("temp", "<html>hello, $name$!</html>", sep, sep); System.out.println("Group created"); ST

Custom format functions for StringTemplate4

…衆ロ難τιáo~ 提交于 2019-12-10 10:38:34
问题 I would like to know how to create a custom format function for string template. Let say I have the following code: render(attributes) :: << <html> $atributes: { <div> $customformat(atribute.name)$</div> } </html> >> customformat(name) ::= << $name; format="upper"$ >> Currently the behaviour of the function customformat is: Input: "hello world" -> Output: "HELLO WORLD" And I would like to modify the customformat function so the output is something like the following: Input: "hello world" ->

NoIndentWriter not respecting whitespace in template

守給你的承諾、 提交于 2019-12-10 08:48:54
问题 I am trying to use the NoIndentWriter class to get around a problem I am facing related to the inserting of whitespace in multiline Strings. However, it appears that NoIndentWriter does a lot more than just not auto-indenting. In particular it trims all leading (but not trailing whitespace) even if explicitly present in a template. E.g. with the following template file: $ cat src/templates/a.xml.stg ladder(value)::=<< 0 1 2 3 ^value^ >> ... and the following code: public static void main

embed java code inside a template

穿精又带淫゛_ 提交于 2019-12-08 05:02:25
问题 Is it possible to embed executable java code inside a ST4 template? eg if I want to pass a string to my template, which sometimes prints the string as-is, and sometimes prints it in caps, so I want a java code to do the necessary conversion. Without this feature, I see 3 ways to achieve the same functionality: (1) pre-compute the alternative values in java, and pass them all to the template at one shot (but this may result in too many arguments): // in the below code, if the template can