[stringtemplate-interest] strange iteration problems when using HashMaps

Uwe Simm uwes at cadence.com
Sat Aug 19 05:18:32 PDT 2006


hi experts,

i'm using StringTemplate for a small application and i was surprised to
see the following behaviour:


i did expect that i do iterate in testme() over the elements of a and
applying the template op() to every element.
this works fine if a has got more than one element (the attribute has
been assigned more than once). however if the attribute is only assigned
once it looks like the iterator iterates over the contents of the
aggregate (the contents of the HashMap) which was not the primary
indent.

the question now is: how can iterate over the elements of the attribute
a apply the op() function independent of the number of elements in a?

regards
/uwe

---------------templates-------------------------
op(t) ::= "this is [<t.a>]"

testme(a) ::= <<
	testme
	<a:{<op(it)>};separator="\n">
	testme
>>
-------------------------------------------------
package convert;

import java.io.FileReader;
import java.util.HashMap;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;

public class STtest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
        StringTemplateGroup group = new StringTemplateGroup(new
FileReader("templates"));
		StringTemplate pack = group.getInstanceOf("testme");

        HashMap<String,Object> h = new HashMap<String,Object>();

        h.put("a","attr_a");
        pack.setAttribute("a", h);
        pack.setAttribute("a", h); // comment this line!

        System.err.println(pack);
	}
}
-------------------------------------------------


More information about the stringtemplate-interest mailing list