[stringtemplate-interest] problem with first(attribute) expression when attribute is a list of hashes

Terence Parr parrt at cs.usfca.edu
Wed Jan 23 18:05:04 PST 2008


Hi Matt,

weird. I get:

action parse error; template context is [anonymous]
line 1:12: unexpected token: .
	at  
org 
.antlr.stringtemplate.language.ActionParser.action(ActionParser.java: 
130)
	at  
org 
.antlr.stringtemplate.StringTemplate.parseAction(StringTemplate.java: 
879)
	at  
org 
.antlr 
.stringtemplate.language.TemplateParser.action(TemplateParser.java:162)
	at  
org 
.antlr 
.stringtemplate.language.TemplateParser.template(TemplateParser.java: 
127)

  which means that it did not understand the syntax. here is my unit  
test:

	public void testFirstWithListOfMaps() throws Exception {
		StringTemplate e = new StringTemplate(
				"$first(maps).Ter$"
			);
		e = e.getInstanceOf();
		Map m1 = new HashMap();
		Map m2 = new HashMap();
		m1.put("Ter", "x5707");
		e.setAttribute("maps", m1);
		m2.put("Tom", "x5332");
		e.setAttribute("maps", m2);
		String expecting = "x5707";
		assertEquals(expecting, e.toString());
	}

Ok, change to the action.g grammar to allow DOT after a function and  
now I get the right result. expanded unit test to check for actual  
list going in:

	public void testFirstWithListOfMaps() throws Exception {
		StringTemplate e = new StringTemplate(
				"$first(maps).Ter$"
			);
		e = e.getInstanceOf();
		final Map m1 = new HashMap();
		final Map m2 = new HashMap();
		m1.put("Ter", "x5707");
		e.setAttribute("maps", m1);
		m2.put("Tom", "x5332");
		e.setAttribute("maps", m2);
		String expecting = "x5707";
		assertEquals(expecting, e.toString());

		e = e.getInstanceOf();
		List list = new ArrayList() {{add(m1); add(m2);}};
		e.setAttribute("maps", list);
		expecting = "x5707";
		assertEquals(expecting, e.toString());

	}


Ok, I am about ready to release 3.1 :)

Ter

On Dec 15, 2007, at 12:18 PM, Matt Brown wrote:

> Yes, I am sure it's non-null, and hash_member isn't the actual  
> key...I'm
> not sure why I changed the names.  Here's the relevant part of the
> actual template (a page that lists results of a tag search):
>
> $if(index)$
> <h3>Pages matching tag '$first(index).tag_text$':</h3>
> <ul>
>  $index:{<li><a href="$contextRoot$/$it.section_id$/view/$it.page_id
> $">$it.title$</a> [$it.section_id$]</li>}$
> </ul>
> $endif$
>
> And here's the value of "index" from one of my tests (obtained via
> toDebugString):
>
> index=[{page_id=1296, title=music review ho!, tag_text=beer,
> section_id=music}, {page_id=1186, title=Animation Runner Kuromi 2,
> tag_text=beer, section_id=reviews}]
>
> If I remove the attribute reference from the "h3" block, the list  
> below
> it renders properly.
>
> Thanks,
>  - Matt Brown
>
>
>
> On Fri, 2007-12-14 at 11:56 -0800, Terence Parr wrote:
>> hmm..strange.  So you are sure that list_attribute[0] is non null?   
>> Is
>> "hash_member" a valid key, btw?
>>
>> Ter
>> On Dec 14, 2007, at 11:40 AM, matt at thataddress.com wrote:
>>
>>>
>>>
>>> Hello list (new here),
>>>
>>> I have a web page where I stuff a List
>>> of HashMap into a string template, and I want to use one of the
>>> members of
>>> the first hash in the list.  So I tried:
>>>
>>> $first(list_attribute).hash_member$
>>>
>>> This resulted in a
>>> NullPointerException in ConditionalExpr.write (line 88).
>>>
>>> Then I
>>> tried:
>>>
>>> $first(list_attribute):{ $it.hash_member$ }$
>>>
>>> This approach resulted in an empty string (and I verified that the
>>> member I want to reference is in fact passed into the template).  Is
>>> there some other way to do what I want, that I'm missing?  I'm using
>>> StringTemplate 3.1b1.
>>>
>>> - Matt Brown
>>>
>>>
>>> _______________________________________________
>>> stringtemplate-interest mailing list
>>> stringtemplate-interest at antlr.org
>>> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
>>
>
> _______________________________________________
> 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