[stringtemplate-interest] Problem with separator when using AttributeRenderer for Strings
Felix Leipold
felix.leipold at gmail.com
Thu Nov 25 04:59:41 PST 2010
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. 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 > c&<br/>c < a + b", template.toString());
//fails, actual: <h1>A list</h1>a + b > c<br/>c < a + b
}
private static class MyAttributeRenderer implements AttributeRenderer {
public String toString(Object o) {
return ((String)o).replaceAll("<", "<").replaceAll(">", ">");
}
public String toString(Object o, String s) {
return toString(o);
}
}
}
More information about the stringtemplate-interest
mailing list