[stringtemplate-interest] Treating inner classes differently than outer classes when rendering source code
Nicholas Dunn
i82much at gmail.com
Mon Apr 18 11:52:19 PDT 2011
I am using StringTemplate 3.2 in a project to generate sourcecode for
serializing/deserializing data adhering to binary packed message
specifications (e.g. DIS, STANAG 4586). The problem I'm running into is as
follows. I structure the templates roughly as follows:
http://i54.tinypic.com/29wphjl.jpg
The issue is that not all of my classes that I want to generate use the
STANAG4586Java specializations. There are two exceptions. One is inner
classes; I want all of the templates for the inner classes to use the Java
rather than STANAG4586Java templates. The second is for helper classes that
don't actually represent complete messages themselves, but instead represent
some data structure that is repeated in multiple messages. For instance, I
have a TimeStamp class that is used in many Messages, yet it is not a
message by itself. I want the Java templates to take over in this case.
I think the second problem is easy to fix; in my domain object I have a
method "isHelperClass" that I can call to determine whether to use the Java
templates or the STANAG4586Java specialization templates; for those helper
classes I would just use the plain Java.
The real tricky problem is in the inner classes. The way I'm doing inner
classes is as follows:
class(name, number, description, classAttributes, innerClasses,
classComments, package)::=<<
$packageDeclaration(package)$
$imports(...)$
$header()$
$classBody(...)$
>>
classBody(name, number, description, classAttributes, innerClasses,
classComments,innerClass)::=<<
$startBlockComment()$
$classComments; format="comment"$
$endBlockComment()$
$classDeclaration(className=name, parentClass=parentClass)$ $startBlock()$
// snip
$innerClasses(...)$
$endBlock()$
>>
where innerClasses is defined as follows:
innerClasses()::=<<
$innerClasses:{inner | $classBody(name=inner.name, number="",
classAttributes=inner.classAttributes, description=inner.comment,
innerClasses=inner.innerClasses, classComments=inner.comment,
innerClass=inner.innerClass)$}; separator="\n\n"$
>>
In other words, I recursively call the class template, which handles all of
the code generation I want to do. The problem comes from the fact that I
want these inner classes to use a different set of templates than the
enclosing stanag message. I'm getting around this by checking in the
specialized templates whether the object I'm rendering is in fact a helper
class or not:
declarations(variables)::=<<
$if(!helperClass)$
private PresenceVector presenceVector = new PresenceVector();
$super.declarations(rest(variables))$
$else$
$super.declarations(variables)$
$endif$
>>
I really don't like this approach because I'm repeating myself over and over
again with the $if(!helperClass)$ conditional template inclusion.
Am I missing something obvious? Is there a better way of accomplishing what
I'm trying to do? I would appreciate any help.
Thanks,
--
Nicholas Dunn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110418/d984e1c6/attachment.html
More information about the stringtemplate-interest
mailing list