[stringtemplate-interest] Output string lengths..

Caleb Lyness caleb.lyness at ezswitch.net
Mon Dec 11 11:11:20 PST 2006


Terence Parr wrote:
> On Dec 11, 2006, at 10:14 AM, Caleb Lyness wrote:
>
>   
>> John Snyders wrote:
>>     
>>> Now that we have the format option you could do $d1;format("pad, 
>>> 20")$, $d2;format("pad,30")$ and let the render grab the width  
>>> from the format name but this is ugly.
>>>       
>> The format specifier is sufficient for doing padding:
>>
>> c.f: java.util.Formatter
>>
>> Here is an example:
>>
>>         System.out.println("result="+String.format("|%-50s|",  
>> "hello"));
>>         System.out.println("result="+String.format("|%50s|",  
>> "hello"));
>>
>> Output
>> result=|hello                                             |
>> result=|                                             hello|
>>
>>     
>
> So,  We could do the following $name; format="%50s"$, right? then  
> someone would have to make a string renderer, but that is easy.
>   
You can format a lot of things, including dates, so it makes the old
date renderer obsolete.
I find it rather smart because it a simple solution with lots of power:

class StringTemplate.FormatRenderer
  implements ...
...
    private Locale locale;

    // If locale is null - no localization is applied (preserving
original props for String.format)
    public FormatRenderer(Locale locale)
    {  
        this.locale = locale;
    }

    public FormatRenderer()
    {  
        this(Locale.getDefault());
    }

    public String toString(Object o, String formatStr) throws ...
     {
         return String.format(locale,formatStr,o)
     }

...

st.registerRenderer(String.class, new FormatRenderer(mylocale));
st.registerRenderer(Date.class, new FormatRenderer(mylocale)); 

So, yes easy :-D. For the docs just point the reader at the Java docs.
But only available for Java 1.5+ :-/

Perhaps the default format renderer? when format is specified but
no renderer has been defined...

I am sure you can fill in the missing bits above....

Cheers
     Caleb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061211/0e7b2c80/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3269 bytes
Desc: S/MIME Cryptographic Signature
Url : http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061211/0e7b2c80/attachment.bin 


More information about the stringtemplate-interest mailing list