Stringtemplate: is it ok to Apply Templates, in which HashMap uses, To Multi-Valued Attributes

Posted by user1071830 on Stack Overflow See other posts from Stack Overflow or by user1071830
Published on 2011-11-29T17:47:59Z Indexed on 2011/11/29 17:49 UTC
Read the original article Hit count: 484

There is two template in my .stg file, and both of them apply on multi-value a HashMap. The HashMap is employed as an injected object.

And I need those instance of HashMap can be injected for many times.

My trouble is, when I switch to another template, ANTLR seems to consider the second HashMap as a List -- multipul objects and null value.


Part of my .stg file shows as follows:

tpl_hash(BAR, FOO) ::= <<
<FOO:foo(); separator="\n">
<BAR:bar(); separator="\n">
>>

foo(nn) ::= <<
foo: <nn.name; null="NULL"> . <nn.national; null="NULL">
>>


bar(mm) ::= <<
bar: <mm.name> @ <mm.national>
>>

Part of my .g file shows:

HashMap hm = new HashMap();
hm.put("name", $name.text);
hm.put("national", "German");
tpl_hash.add("FOO",new HashMap(hm));
HashMap hm2 = new HashMap();
hm2.put("name", $name.text);
hm2.put("national", "German");
tpl_hash.add("BAR",new HashMap(hm2));

The result I expect is :

bar: Kant @ German
foo: Russell @ England

But, I got:

foo: NULL . NULL
foo: NULL . NULL
bar:  @ 
bar:  @ 

If we replace BAR with FOO, as is, keeping FOO and BAR with identical template, the output is right, like the following.

bar: Russell @ German
bar: Russell @ German

In docs, "synchronized ST add (String name, Object value) in org.stringtemplate.v4.ST" said:

"If you send in a List and then inject a single value element, add() copies original list and adds the new value."

What about a HashMap? Does ANTLR consider the HashMap, key/value pair access, an object purposely, as a List and as multi-value injected by mistake?

Thanks a lot in advance.

© Stack Overflow or respective owner

Related posts about java

Related posts about hashmap