[stringtemplate-interest] support for set
John Snyders
jjsnyders at rcn.com
Thu Dec 7 07:27:23 PST 2006
In a previous thread
(http://www.antlr.org:8080/pipermail/stringtemplate-interest/2006-December/0
00836.html) Nate
had asked for support for the Java Set
"$if (someSet.moo)$ would be >> equivalent to the Java: if
(someSet.contains("moo"))"
I see no reason why Set should not be more fully supported. I agree that the
syntax get(string) should not be supported because it acts as a method call.
Because set is a collection it is already supported in that you can do this
$myset$ and it will enumerate all the keys.
$myset.key1$ should be replaced with "key1" if key1 is in the set and ""
otherwise.
Or should $myset.key1$ be replaced with "true" if it is present?
The change is very localized. Add this code to ASTExpr.java
rawGetObjectProperty just before the // try getXXX and isXXX properties
comment.
// Special case: if it's a Set then use the set value
// not the property method.
if ( o instanceof Set ) {
Set set = (Set)o;
if ( set.contains(propertyName) ) {
value = propertyName;
}
return value;
}
I tested the above with
StringTemplate test = new StringTemplate("$set : { [$it$] }$,
$set$\n"
+ "access: $set.key1$\n"
+ "$if(set.key2)$found key2$endif$\n"
+ "$if(set.key3)$found key3$else$no key 3$endif$");
HashSet s = new HashSet();
s.add("key1");
s.add("key2");
test.setAttribute("set", s);
System.out.println(test.toString());
Which produced:
[key1] [key2] , key1key2
access: key1
found key2
no key 3
Nate, As for maintaining your own ST source, that is a matter of choice. For
me, I soon found that there were bugs I needed to fix so I checked in the ST
source to my own repository and started making changes.(I think most of my
changes I posted back to this list) Also it looks like you may be using Java
1.5 and there are some things you can do to better support Iterable objects.
-John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061207/9a57c501/attachment.html
More information about the stringtemplate-interest
mailing list