[stringtemplate-interest] Outputting java.util.Properties

John Snyders jjsnyders at rcn.com
Fri Feb 2 14:50:56 PST 2007


Properties works like a map but it is not derived from Map so you can't do
<props.keys> to enumerate the keys.

"keys" is a pseudo property of Map that returns the keys in the map.

Usually with Map or Properties you want random access to the values as in
<props.one>

If you want all the key value pairs and you can switch to Map (or wrap
Properties in a class that implements Map) then you can do this:

 <props.keys: { <it> <props.(it)> };separator=", ">

Note how I used the separator option rather than first and rest as you had
below.

The only issue with this is you get the keys in any order unles your map is
ordered like TreeMap.

Hope this helps
-John
> -----Original Message-----
> From: stringtemplate-interest-bounces at antlr.org
> [mailto:stringtemplate-interest-bounces at antlr.org]On Behalf Of Juergen
> Weber
> Sent: Friday, February 02, 2007 3:48 PM
> To: stringtemplate-interest at antlr.org
> Subject: [stringtemplate-interest] Outputting java.util.Properties
>
>
> Hi,
>
> I tried to use java.util.Properties in a template, like
>
> Properties p = new Properties();
>
> p.setProperty("one","1");
> p.setProperty("two","2");
>
> How to you program your template to get:
>
>    one 1,   two 2
>
> I only managed this by copying the Properties into a list of KeyValues
>
>         public KeyValue(Object key, Object value)
>                 {
>                         super();
>                         this.key = key;
>                         this.value = value;
>                 }
>
>                 public Object getKey()
>                 {
>                         return key;
>                 }
>
>                 public void setKey(Object key)
>                 {
>                         this.key = key;
>                 }
>
> So I could use
>
> <first(props):{<it.value> <it.key><\n>}>
> <rest(props):{,<it.value> <it.key><\n>}>
>
> But I feel there must be an easier way to do this...
> Thanks,
> Jürgen
> _______________________________________________
> 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