[stringtemplate-interest] Setting variables in templates?
Brain, Jim
JBrain at Aegonusa.com
Fri Nov 13 07:38:21 PST 2009
I *think* you cannot, but I thought I'd at least show my example, as there might be a more elegant way to express this:
import org.antlr.stringtemplate.*;
public class STTest {
public static void main(String[] args) {
StringTemplateGroup stg = new StringTemplateGroup("Source");
stg.defineTemplate("reader","$if(name)$$Parser(node={get(\"$XMLName$\").getText()})$$else$$Parser(node={get(\"$XMLName$\").getAttribute()})$$endif$");
stg.defineTemplate("Parser","$parser$");
StringTemplateGroup stg2 = new StringTemplateGroup("Class");
stg2.setSuperGroup(stg);
StringTemplate st = stg2.defineTemplate("prop","$reader(parser={eval($node$)})$");
st.setAttribute("name","fName");
st.setAttribute("XMLName","XMLName");
StringTemplate st2 = stg2.defineTemplate("prop","$reader(parser={eval2($node$)})$");
st2.setAttribute("XMLName","XMLName2");
System.out.println(st.toString());
System.out.println(st2.toString());
}
}
Yields an expected:
eval(get("XMLName").getText())
eval2(get("XMLName2").getAttribute())
What bothers me is the "Parser" template, which seems trivial. It's there because I needed to set a conditional "node" variable, and the only way I could think to do it was to pass it as a parameter.
I thought about putting the "node" definition in the st2 definition, but then I am repeating it all over the place.
Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20091113/e7830b33/attachment-0001.html
More information about the stringtemplate-interest
mailing list