[stringtemplate-interest] Issue with Multi-Valued attributes where the attribute is a Map Key/Value pair.

Terence Parr parrt at cs.usfca.edu
Mon Oct 3 09:56:45 PDT 2005


On Oct 3, 2005, at 5:15 AM, Robert Paterson wrote:

> Ter,
>
> Thanks for this. I thought it looked great when I first read it,  
> but...
>
> It doesn't seem to be finding any Paragraphs attributes to iterate  
> through
> now.

Code looks good.  Did you change the templates accordingly so they  
use $it.map$?

Ter
>
> I've adjusted the code:
>
> public class StringTemplateExample {
>
>   public static void main(String[] args) {
>     StringTemplateGroup group = new StringTemplateGroup 
> ("MyGroup",".");
>
>     // If I set the "Paragraphs" attribute with two HashMap  
> instances it
> works.
>     StringTemplate docOut = group.getInstanceOf("document");
>
>     HashMap para = new HashMap();
>     para.put("Bullet","N");
>     para.put("Indent","N");
>     para.put("ParagraphText","Now is the time for all good men...");
>
>     docOut.setAttribute("Paragraphs.{map}", para);
>
>     para = new HashMap();
>
>     para.put("Bullet","N");
>     para.put("Indent","N");
>     para.put("ParagraphText","... to come to the aid of their  
> party.");
>
>     docOut.setAttribute("Paragraphs.{map}", para);
>
>     System.out.println(docOut.toString());
>
>     // But if I only set it with one HashMap instance it fails, and  
> instead
> tries to iterate through the values of the HashMap.
>     docOut = group.getInstanceOf("document");
>
>     para = new HashMap();
>     para.put("Bullet","N");
>     para.put("Indent","N");
>     para.put("ParagraphText","Now is the time for all good men...");
>
>     docOut.setAttribute("Paragraphs.{map}", para);
>
>     System.out.println(docOut.toString());
>
>   }
> }
>
>
>
>
> But this just gives me:
>
>
> DOCUMENT {
>
>    CONTENT {
>
>
>    }
>
> }
> DOCUMENT {
>
>    CONTENT {
>
>
>    }
>
> }
>
>
>
> The Paragraphs attribute set seems to be empty. Any ideas? I forgot to
> mention before, I'm using StringTemplate 2.2b4.
>
> Regards,
>
> Rob Paterson
>
>
> -----Original Message-----
> From: Terence Parr [mailto:parrt at cs.usfca.edu]
> Sent: Sunday, 2 October 2005 11:54 PM
> To: Robert Paterson
> Subject: Re: [stringtemplate-interest] Issue with Multi-Valued  
> attributes
> where the attribute is a Map Key/Value pair.
>
>
> Hi Rob,
>
> I suggest wrapping in an aggregate object like you suggest.  Here's
> the easy way:
>
>    docOut.setAttribute("Paragraphs.{map}", para);
>
> That should work for multiple maps also.  You just have to modify
> your template to say
>
>       $Paragraphs:{p | p.map:paragraph()}; separator="\n\n"$
>
> or modify your paragraph.st to use $it.map.Bullet$ etc...
>
> Ter
>
>
>



More information about the stringtemplate-interest mailing list