[stringtemplate-interest] difficulty using format and null options together

Terence Parr parrt at cs.usfca.edu
Mon Mar 17 07:46:06 PDT 2008


On Mar 17, 2008, at 12:39 AM, Mark Wharton wrote:

> Hello,
>
> I'm having difficulty using format and null options together in  
> generating an SQL statement with quoted values and NULL keyword.  
> Here's a sample:
>
> 	String ts = "$values; format=\"quote\", null=\"NULL\", separator= 
> \", \"$";
> 	StringTemplate t = new StringTemplate(ts);
> 	t.registerRenderer(String.class, new ValueRenderer());
> 	List values = new ArrayList();
> 	values.add("x");
> 	values.add("y");
> 	values.add(null);
> 	values.add("z");
> 	values.add(null);
> 	t.setAttribute("values", values);
> 	System.out.println("t: "+t.toString());
>
> Actual Result...
>
> 	t: "x", "y", "NULL", "z", "NULL"
>
> Desired Result...
>
> 	t: "x", "y", NULL, "z", NULL
>
> It looks like the null option is applied before the format option  
> and consequently the NULL becomes quoted because it is a string  
> when the renderer processes it. I don't see how it is possible to  
> have non quoted NULL in the output for null values in the list when  
> the format option is also used in the same expression.

Hi Mark.  Hmm...good question.  Should null option be considered  
totally apart from others?  Seems like null is in fact kind of like  
an exception thing...but, what if it's a template reference that is  
complicated and you need to run the format thing on it first?  i can  
see arguments for both.  Just adjust your formatter to avoid doing it  
for NULL for now.

Ter


More information about the stringtemplate-interest mailing list