javaantlr4stringtemplate-4

Convert First Letter to Capital using String Template


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.


Solution

  • 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