[stringtemplate-interest] Problem with separator when using AttributeRenderer for Strings

Terence Parr parrt at cs.usfca.edu
Tue Dec 7 13:55:59 PST 2010


On Nov 25, 2010, at 4:59 AM, Felix Leipold wrote:

> Hello,
> 
> The context of the problem is a bit special. I started using AttributeRenderers for Strings to enforce escaping rules of the target language, e.g. ">" to ">" for html or "%" to "\%" for LaTeX. 
> 
> Now there is one quirk. StringTemplate also applies the AttributeRenderer registered for Strings to the separator string. As there is no expansion of templates happening inside the separator string I find this quite strange.

you can have separator=templateRef() too I believe.  I think the separator is a template not string.
Ter

> To me the separator is just literal output. To illustrate my expectation I included a JUnit test, that depends only on StringTemplate and JUnit below.
> 
> Do you think my expectation is legitimate? 
> 
> Best regards,
> 
> Felix Leipold
> 
> ------------------------------------------------------------------------------------------------
> public class RendererTest {
> 
>   @Test
>   public void testMe(){
>       StringTemplate template = new StringTemplate("<h1>A list</h1>$items:{$it$};separator=\"<br/>\"$");
> 
>       List<String> items = asList("a + b > c", "c < a + b ");
>       template.setAttribute("items", items);
> 
>       template.registerRenderer(String.class, new MyAttributeRenderer());
> 
>       assertEquals("<h1>A list</h1>a + b &gt; c&<br/>c &lt; a + b", template.toString());
> 	//fails, actual:   <h1>A list</h1>a + b &gt; c&lt;br/&gt;c &lt; a + b
> 
>   }
> 
> 
>   private static class MyAttributeRenderer implements AttributeRenderer {
>       public String toString(Object o) {
>           return ((String)o).replaceAll("<", "&lt;").replaceAll(">", "&gt;");
>       }
> 
>       public String toString(Object o, String s) {
>           return toString(o);
>       }
>   }
> }
> 
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list