[stringtemplate-interest] Attribute not found exception? Revisited

Caleb Lyness caleb.lyness at ezswitch.net
Wed Nov 8 12:46:33 PST 2006


Ahoy!

With ref to the following thread:
http://www.antlr.org:8080/pipermail/stringtemplate-interest/2006-March/000291.html

* I am looking for a way to ask a template what attributes it is
using/wanting.
So far I have overloaded a Map class which records which keys are requested.
I pass this in as the attributes list. At the end of a template gen I
know what was
requested and what was not.

*Q:* Can I do this with out actually rendering, (via toString())... i.e.
is there another way?

* Sometimes its useful to know (say for testing) that a "required"
string is missing. What is
required and what is not is quite specific to the template. Here is how
I do it for the HTML
I am generating:

$if (checkAttributes)$
<!--
$reqAttr;null=missingAttribute("reqAttr")$
$serviceCode;null=missingAttribute("serviceCode")$
-->
$endif$

If the checkAttributes attribute is true/specified the html comment is
"rendered". If the
value is "required" by this template then it is listed in this comment.
When the comment
is rendered either the value is rendered or if the attribute is missing
it spits out a string
which can be easily found including the name of the missing attribute.

The template shown is defined in a group file as:

missingAttribute(attributeName) ::= "TEST:FAIL - Missing attribute:
\$$attributeName$\$"

The output will look something like this:

   <p>       
  
   <!--
   TEST:FAIL - Missing attribute: $reqAttr$
   1
   -->
   You have recei

The unit tests can then search for these failure messages and make the
test fall over. The comments prevent
the test stuff from breaking the final output... :-)

For maps you can use a default to generate a string which can be caught
in the test too. Its a pity
the key of the map is not treated as an attribute. That would make this
possible:

amap ::= [
  "2":     "value 2",
  "5":     "Other",
  default: "Unknown reason: $key$"
]

With this you could generate the fail message AND report the failing
value....
Perhaps someone else can think of a way to solve this one...

Cheers
     Caleb

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061108/d5affb15/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3269 bytes
Desc: S/MIME Cryptographic Signature
Url : http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061108/d5affb15/attachment.bin 


More information about the stringtemplate-interest mailing list