You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Dhruva B. Reddy" <sl...@yahoo.com> on 2003/07/01 18:41:53 UTC

Can't figure out VelocityFormatter

I am trying to use the VelocityFormatter class.  I have the following
in my Java code:

    context.put("formatter", new VelocityFormatter(context));
    context.put("thingie", stuff);

In the template, I have the following:

    $formatter.limitLen(2, $thingie.getStuff())

This method call throws a StringIndexOutOfBoundsException.  Yet, when I
just have the following in the template:

    $thingie.getStuff()

I get the expected output.

Can someone tell me what I'm doing wrong?

Thanks,
Dhruva

__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Can't figure out VelocityFormatter

Posted by "Dhruva B. Reddy" <sl...@yahoo.com>.
Yep.  I tried passing 5 instead of 2 and it worked.

Thanks for your responses.

--- Shawn Church <sh...@boxity.com> wrote:
> Looking at the source, looks like a maxlen of <3 will fail.  I didn't
> try to
> reproduce it, but it looks like a bug.  As a test, please try a
> length of 3
> or greater.  The 3 max results since the default suffix in this case
> is the
> String "...".
> 
> You could also try invoking this method directly, specifying your own
> suffix
> having a shorter length, as in:
>      $formatter.limitLen(2, $thingie.getStuff(), ":")
> 
> 
>     public String limitLen(int maxlen,
>                            String string,
>                            String suffix)
>     {
>         String ret = string;
>         if (string.length() > maxlen)
>         {
>             ret = string.substring(0, maxlen - suffix.length()) +
> suffix;
>         }
>         return ret;
>     }
> 
> 
> Shawn
> 
> ----- Original Message -----
> From: "Dhruva B. Reddy" <sl...@yahoo.com>
> To: <ve...@jakarta.apache.org>
> Sent: Tuesday, July 01, 2003 11:41 AM
> Subject: Can't figure out VelocityFormatter
> 
> 
> > I am trying to use the VelocityFormatter class.  I have the
> following
> > in my Java code:
> >
> >     context.put("formatter", new VelocityFormatter(context));
> >     context.put("thingie", stuff);
> >
> > In the template, I have the following:
> >
> >     $formatter.limitLen(2, $thingie.getStuff())
> >
> > This method call throws a StringIndexOutOfBoundsException.  Yet,
> when I
> > just have the following in the template:
> >
> >     $thingie.getStuff()
> >
> > I get the expected output.
> >
> > Can someone tell me what I'm doing wrong?
> >
> > Thanks,
> > Dhruva
> >
> > __________________________________
> > Do you Yahoo!?
> > SBC Yahoo! DSL - Now only $29.95 per month!
> > http://sbc.yahoo.com
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> velocity-user-help@jakarta.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> velocity-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Can't figure out VelocityFormatter

Posted by Shawn Church <sh...@boxity.com>.
Looking at the source, looks like a maxlen of <3 will fail.  I didn't try to
reproduce it, but it looks like a bug.  As a test, please try a length of 3
or greater.  The 3 max results since the default suffix in this case is the
String "...".

You could also try invoking this method directly, specifying your own suffix
having a shorter length, as in:
     $formatter.limitLen(2, $thingie.getStuff(), ":")


    public String limitLen(int maxlen,
                           String string,
                           String suffix)
    {
        String ret = string;
        if (string.length() > maxlen)
        {
            ret = string.substring(0, maxlen - suffix.length()) + suffix;
        }
        return ret;
    }


Shawn

----- Original Message -----
From: "Dhruva B. Reddy" <sl...@yahoo.com>
To: <ve...@jakarta.apache.org>
Sent: Tuesday, July 01, 2003 11:41 AM
Subject: Can't figure out VelocityFormatter


> I am trying to use the VelocityFormatter class.  I have the following
> in my Java code:
>
>     context.put("formatter", new VelocityFormatter(context));
>     context.put("thingie", stuff);
>
> In the template, I have the following:
>
>     $formatter.limitLen(2, $thingie.getStuff())
>
> This method call throws a StringIndexOutOfBoundsException.  Yet, when I
> just have the following in the template:
>
>     $thingie.getStuff()
>
> I get the expected output.
>
> Can someone tell me what I'm doing wrong?
>
> Thanks,
> Dhruva
>
> __________________________________
> Do you Yahoo!?
> SBC Yahoo! DSL - Now only $29.95 per month!
> http://sbc.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: Can't figure out VelocityFormatter

Posted by Tim Colson <tc...@cisco.com>.
> I am trying to use the VelocityFormatter class.  
Funny. I was just looking through the Velocity source and found that
class about 10 minutes ago!

I thought to myself, why haven't I ever seen this before??? :-)
I am curious, where did you find documentation for this class?

Simple question, are you sure that stuff.getStuff() actually returns a
String? 

>     context.put("formatter", new VelocityFormatter(context));
>     context.put("thingie", stuff);
> 
> In the template, I have the following:
>     $formatter.limitLen(2, $thingie.getStuff())
Ahah!

I just had a look closer at the source, and it seems that limitLen takes
your specified length and subtracts the length of a default 'suffix'
which happens to be "..."

So the result is the error because as you can see below, the substring
fails. :-)
 
    /**
     * Limits 'string' to 'maxlen' character.  If the string gets
     * curtailed, 'suffix' is appended to it.
     *
     * @param maxlen An int with the maximum length.
     * @param string A String.
     * @param suffix A String.
     * @return A String.
     */
    public String limitLen(int maxlen,
                           String string,
                           String suffix)
    {
        String ret = string;
        if (string.length() > maxlen)
        {
            ret = string.substring(0, maxlen - suffix.length()) +
suffix;
        }
        return ret;
    }

Try using a limitlen of > 2 OR use this form of the limitLen:
$formatter.limitLen(2, $thingie.getStuff(), "")


Resources:
http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg09622.ht
ml
http://www.pipoware.com/velogui/#tryit


Cheers,
-Timo


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org