[stringtemplate-interest] Template overloading

Florian Heidenreich florian.heidenreich at inf.tu-dresden.de
Thu Feb 9 09:57:18 PST 2006


Andrew,

thank you very much for your helpful feedback. I was looking for real 
template overloading and found a solution using the setSuperGroup method 
of the StringTemplateGroup.

This is what I'm doing:

// File 'testGeneral.stg'
group testGeneral;

general() ::= <<
generalTemplate
 >>

specific() ::= <<
generalTemplate
 >>


// File 'testSpecific.stg'
group testSpecific;

specific() ::= <<
specificTemplate
 >>


Testing template overloading:

Reader generalReader = new 
FileReader(ClassLoader.getSystemClassLoader().getResource("test/testGeneral.stg").getFile());
StringTemplateGroup general = new StringTemplateGroup(generalReader);
						
Reader specificReader = new 
FileReader(ClassLoader.getSystemClassLoader().getResource("test/testSpecific.stg").getFile());
StringTemplateGroup specific = new StringTemplateGroup(specificReader);
					
specific.setSuperGroup(general);
						
StringTemplate generalTemplate1 = specific.getInstanceOf("general");
assertEquals(generalTemplate1.toString(), "generalTemplate");
						
StringTemplate specificTemplate1 = specific.getInstanceOf("specific");
assertEquals(specificTemplate1.toString(), "specificTemplate");


Thank you!

Best regards,
~ Florian



Andrew Goodnough wrote:
> I've done exactly this for generating database-specific SQL.  We are
> switching from Sybase to Postgres so we have a need to create both.  I
> created 3 templates:
> 
> sybase.stg    //Contains sybase-specific templates
> postgres.stg //Contains postgres-specific templates
> shared.stg    //Contains db agnostic templates
> 
> You will probably want to put most of the template code in the shared
> template group file and only use the db specific template when you find
> some syntax that is not general.  The shared.stg tends to contain the
> outline of what needs to be done while the specific template does the
> real work.  Like:
> 
> -shared.stg-
> doStuff(userType) ::= <<
> --START SCRIPT--
> <userType:dropUserType()>
> --END SCRIPT--
> 
> -sybase.stg-
> delim() ::= "<\n>GO"
> 
> dropUserType() ::= <<
> EXEC sp_droptype '<it.name>'<delim()>
> 
> -postgres.stg-
> delim() ::= ";"
> 
> dropUserType() ::= <<
> DROP DOMAIN '<it.name>'<delim()>
> 
> 
> The Java code uses the standard super group idea:
> ====
>         InputStream sharedIs =
> getClass().getResourceAsStream("shared.stg");
>         sharedReader = new InputStreamReader(sharedIs);
>         StringTemplateGroup sharedTemplates = new
> StringTemplateGroup(sharedReader, AngleBracketTemplateLexer.class);
>         
>         InputStream specificIs = getClass().getResourceAsStream(dbms +
> ".stg");  // dbms can be 'sybase' or 'postgres'
>         specificReader = new InputStreamReader(specificIs);
>         StringTemplateGroup specificTemplates = new
> StringTemplateGroup(specificReader, AngleBracketTemplateLexer.class);
>         specificTemplates.setSuperGroup(sharedTemplates);
>         
>         StringTemplate fileTpl =
> specificTemplates.getInstanceOf(templateName);
> ==== 
> 
> I've implemented a lot of logic in Java Beans that I pass to my
> templates to render.  I've got quite a bit of DDL and DML working in
> production so if you start going down this road in earnest, I can send
> you my templates and code if you like.
> 
> Andy Goodnough
> Wisconsin Courts
> http://wicourts.gov/
> 
> 
>>>> Florian Heidenreich <florian.heidenreich at inf.tu-dresden.de>
> 02/09/06 8:02 am >>> 
> Hello!
> 
> I'm currently writing a SQL code generator using StringTemplate.
> 
> One idea I had is to build a StringTemplateGroup for Standard SQL and 
> overload dialect specific templates in a child group (e.g. for MySQL or
> 
> Oracle). My goal is to replace/overload the specific templates from 
> Standard SQL with the ones from the specific dialect while keeping the
> 
> templates which are equal in both versions.
> 
> Is this possible with StringTemplate and if so, how can I achieve
> that?
> 
> Thank you!
> 
> Best regards,
> ~ Florian
> _______________________________________________
> stringtemplate- interest mailing list
> stringtemplate- interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate- interest


More information about the stringtemplate-interest mailing list