4

I am using String template file to generate java files. For that i am using ANTLR. Code for One of the string template file is shown below:

package framework;
public abstract class  Listener$GUIdriver.name$  {  
 $GUIdriver.commands:{ command |   
     public abstract void onNew$command.name;format="capital"$Command
           ($command.allParameter:{ param |   $param.type.name$ newValue};separator=" , "$);   
          }; separator="\n"$
$GUIdriver.allDataAccess:{ dataAccess | 
  public abstract void onNew$dataAccess.dataAccessName;format="capital"$Request(String request); 
     }; separator="\n"$
 } 

But it doesnot produce effect of format="capital".How to incorporate such changes?Should i need to include any package or file?I am new to String Template & ANTLR.

2 Answers2

1

The format string you want to use is "cap"

format="cap"

You'll need to register the StringRenderer first, however :-)

stGroup.registerRenderer(String.class, new StringRenderer());

More detail

Here's an example group file, testGroup.stg:

group testGroup;

test(text) ::= <<
<text; format="cap">
>>

and here's an example of using it:

import org.stringtemplate.v4.*;

public class Test {

    public static void main(String[] args) {
        STGroup stGroup = new STGroupFile("testGroup.stg");
        ST st = stGroup.getInstanceOf("test");
        stGroup.registerRenderer(String.class, new StringRenderer());
        st.add("text", "helloWorld"); // note lower case 'h'
        System.out.println(st.render());
    }
}

This renders:

HelloWorld

Andy Stabler
  • 1,309
  • 1
  • 15
  • 19
  • Can you please go through some hello world string template application where you can demonstrate me about how to use it ? –  Oct 11 '15 at 04:54
  • Sure, see my updated answer. For more info check the [ST docs](https://theantlrguy.atlassian.net/wiki/display/ST4/StringTemplate+4+Documentation) – Andy Stabler Oct 11 '15 at 11:46
  • I have made changes in to String template file by registering the StringRendere. But prob is i cant change double to Double, i am able to change tempvalue to Tempvalue. Can you please give me reason ? –  Oct 12 '15 at 09:12
0
method(item) ::= <<
<item.name; format="cap">Value
>>