You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Gérard BUNEL <ge...@ago.fr> on 2005/11/18 10:07:12 UTC

How to put # in the string

Hello,

I need to insert a # character in my template but doing so, like in 
example2 modified below, I get an Error:

        String s = "We are using $project $name to render this.\\#";
        w = new StringWriter();

        try
        {
            Velocity.evaluate( context, w, "mystring", s );
        }

ParseErrorException : org.apache.velocity.exception.ParseErrorException: 
Lexical
 error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error 
at line
1, column 47.  Encountered: <EOF> after : ""

So how could I insert this character in my template ?

-- 
BUNEL Gerard - ATLANTIDE
Consultant
Tel. : +33 (0)2 98 05 43 21
http://www.ago.fr/
--
Technopôle Brest Iroise
Site du Vernis - CS 23866
29238 Brest Cedex 3 - France
Tel. : +33 (0)2 98 05 43 21
Fax. : +33 (0)2 98 05 20 34
e-mail: atlantide-brest@ago.fr


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


Re: How to put # in the string

Posted by Nathan Bubna <nb...@gmail.com>.
Will has a nice article out there about securing velocity for user
templates.  i think it was on the Wiki last i checked, but it could be
on the main website somewhere now.

On 11/18/05, Florin Vancea <fv...@maxiq.ro> wrote:
> Just a thought:
>
> Is it safe enough for your use-case to let the user specify what goes
> interpreted as a Velocity template?
>
> I know I would _not_ like that in my apps. The power to do wrong or
> intentionally BAD things using the template is too big.
>
> Florin
>
> ----- Original Message -----
> From: "Gérard BUNEL" <ge...@ago.fr>
> To: "Velocity Users List" <ve...@jakarta.apache.org>
> Sent: Friday, November 18, 2005 4:39 PM
> Subject: Re: How to put # in the string
>
>
> > Ok, Thank you all.
> > In fact I've put a variable called "sharp" in my context and put a
> > ${sharp} in my template.
> > But as the string is provided by a final user, I have to pre-filter the
> > string to replace # with ${sharp}.
> > I just wanted to know if some other way was available
> >
> > Mike Kienenberger a écrit :
> >
> > >The most reliable way to do this is to use
> > >
> > >#set ($h = '#')
> > >
> > >And then ${h} whenever you want a # to appear.  (or ${esc.h} without
> > >the #set if you have the escape tool installed)
> > >
> > >This works in all cases exactly as you'd expect.   Avoid using \ to
> > >escape things -- it's not intuitively obvious how it works.
> > >
> > >On 11/18/05, Will Glass-Husain <wg...@forio.com> wrote:
> > >
> > >
> > >>Velocity's silly escaping rules insist that you put the slash before a
> > >>directive \#if
> > >>but not before a # by itself.  Just leave off the slash.
> > >>
> > >>WILL
> > >>
> > >>----- Original Message -----
> > >>From: "Gérard BUNEL" <ge...@ago.fr>
> > >>To: <ve...@jakarta.apache.org>
> > >>Sent: Friday, November 18, 2005 1:07 AM
> > >>Subject: How to put # in the string
> > >>
> > >>
> > >>
> > >>
> > >>>Hello,
> > >>>
> > >>>I need to insert a # character in my template but doing so, like in
> > >>>example2 modified below, I get an Error:
> > >>>
> > >>>       String s = "We are using $project $name to render this.\\#";
> > >>>       w = new StringWriter();
> > >>>
> > >>>       try
> > >>>       {
> > >>>           Velocity.evaluate( context, w, "mystring", s );
> > >>>       }
> > >>>
> > >>>ParseErrorException :
> org.apache.velocity.exception.ParseErrorException:
> > >>>Lexical
> > >>>error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error
> at
> > >>>line
> > >>>1, column 47.  Encountered: <EOF> after : ""
> > >>>
> > >>>So how could I insert this character in my template ?
> > >>>
> > >>>--
> > >>>BUNEL Gerard - ATLANTIDE
> > >>>Consultant
> > >>>Tel. : +33 (0)2 98 05 43 21
> > >>>http://www.ago.fr/
> > >>>--
> > >>>Technopôle Brest Iroise
> > >>>Site du Vernis - CS 23866
> > >>>29238 Brest Cedex 3 - France
> > >>>Tel. : +33 (0)2 98 05 43 21
> > >>>Fax. : +33 (0)2 98 05 20 34
> > >>>e-mail: atlantide-brest@ago.fr
> > >>>
> > >>>
> > >>>---------------------------------------------------------------------
> > >>>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
> > >>
> > >>
> > >>
> > >>
> > >
> > >---------------------------------------------------------------------
> > >To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > >For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> > >
> > >
> > >
> >
> >
> > --
> > BUNEL Gerard - ATLANTIDE
> > Consultant
> > Tel. : +33 (0)2 98 05 43 21
> > http://www.ago.fr/
> > --
> > Technopôle Brest Iroise
> > Site du Vernis - CS 23866
> > 29238 Brest Cedex 3 - France
> > Tel. : +33 (0)2 98 05 43 21
> > Fax. : +33 (0)2 98 05 20 34
> > e-mail: atlantide-brest@ago.fr
> >
> >
> > ---------------------------------------------------------------------
> > 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
>
>

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


Re: How to put # in the string

Posted by Florin Vancea <fv...@maxiq.ro>.
Just a thought:

Is it safe enough for your use-case to let the user specify what goes
interpreted as a Velocity template?

I know I would _not_ like that in my apps. The power to do wrong or
intentionally BAD things using the template is too big.

Florin

----- Original Message ----- 
From: "Gérard BUNEL" <ge...@ago.fr>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Friday, November 18, 2005 4:39 PM
Subject: Re: How to put # in the string


> Ok, Thank you all.
> In fact I've put a variable called "sharp" in my context and put a
> ${sharp} in my template.
> But as the string is provided by a final user, I have to pre-filter the
> string to replace # with ${sharp}.
> I just wanted to know if some other way was available
>
> Mike Kienenberger a écrit :
>
> >The most reliable way to do this is to use
> >
> >#set ($h = '#')
> >
> >And then ${h} whenever you want a # to appear.  (or ${esc.h} without
> >the #set if you have the escape tool installed)
> >
> >This works in all cases exactly as you'd expect.   Avoid using \ to
> >escape things -- it's not intuitively obvious how it works.
> >
> >On 11/18/05, Will Glass-Husain <wg...@forio.com> wrote:
> >
> >
> >>Velocity's silly escaping rules insist that you put the slash before a
> >>directive \#if
> >>but not before a # by itself.  Just leave off the slash.
> >>
> >>WILL
> >>
> >>----- Original Message -----
> >>From: "Gérard BUNEL" <ge...@ago.fr>
> >>To: <ve...@jakarta.apache.org>
> >>Sent: Friday, November 18, 2005 1:07 AM
> >>Subject: How to put # in the string
> >>
> >>
> >>
> >>
> >>>Hello,
> >>>
> >>>I need to insert a # character in my template but doing so, like in
> >>>example2 modified below, I get an Error:
> >>>
> >>>       String s = "We are using $project $name to render this.\\#";
> >>>       w = new StringWriter();
> >>>
> >>>       try
> >>>       {
> >>>           Velocity.evaluate( context, w, "mystring", s );
> >>>       }
> >>>
> >>>ParseErrorException :
org.apache.velocity.exception.ParseErrorException:
> >>>Lexical
> >>>error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error
at
> >>>line
> >>>1, column 47.  Encountered: <EOF> after : ""
> >>>
> >>>So how could I insert this character in my template ?
> >>>
> >>>--
> >>>BUNEL Gerard - ATLANTIDE
> >>>Consultant
> >>>Tel. : +33 (0)2 98 05 43 21
> >>>http://www.ago.fr/
> >>>--
> >>>Technopôle Brest Iroise
> >>>Site du Vernis - CS 23866
> >>>29238 Brest Cedex 3 - France
> >>>Tel. : +33 (0)2 98 05 43 21
> >>>Fax. : +33 (0)2 98 05 20 34
> >>>e-mail: atlantide-brest@ago.fr
> >>>
> >>>
> >>>---------------------------------------------------------------------
> >>>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
> >>
> >>
> >>
> >>
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >
> >
> >
>
>
> -- 
> BUNEL Gerard - ATLANTIDE
> Consultant
> Tel. : +33 (0)2 98 05 43 21
> http://www.ago.fr/
> --
> Technopôle Brest Iroise
> Site du Vernis - CS 23866
> 29238 Brest Cedex 3 - France
> Tel. : +33 (0)2 98 05 43 21
> Fax. : +33 (0)2 98 05 20 34
> e-mail: atlantide-brest@ago.fr
>
>
> ---------------------------------------------------------------------
> 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: How to put # in the string

Posted by Gérard BUNEL <ge...@ago.fr>.
Ok, Thank you all.
In fact I've put a variable called "sharp" in my context and put a 
${sharp} in my template.
But as the string is provided by a final user, I have to pre-filter the 
string to replace # with ${sharp}.
I just wanted to know if some other way was available

Mike Kienenberger a écrit :

>The most reliable way to do this is to use
>
>#set ($h = '#')
>
>And then ${h} whenever you want a # to appear.  (or ${esc.h} without
>the #set if you have the escape tool installed)
>
>This works in all cases exactly as you'd expect.   Avoid using \ to
>escape things -- it's not intuitively obvious how it works.
>
>On 11/18/05, Will Glass-Husain <wg...@forio.com> wrote:
>  
>
>>Velocity's silly escaping rules insist that you put the slash before a
>>directive \#if
>>but not before a # by itself.  Just leave off the slash.
>>
>>WILL
>>
>>----- Original Message -----
>>From: "Gérard BUNEL" <ge...@ago.fr>
>>To: <ve...@jakarta.apache.org>
>>Sent: Friday, November 18, 2005 1:07 AM
>>Subject: How to put # in the string
>>
>>
>>    
>>
>>>Hello,
>>>
>>>I need to insert a # character in my template but doing so, like in
>>>example2 modified below, I get an Error:
>>>
>>>       String s = "We are using $project $name to render this.\\#";
>>>       w = new StringWriter();
>>>
>>>       try
>>>       {
>>>           Velocity.evaluate( context, w, "mystring", s );
>>>       }
>>>
>>>ParseErrorException : org.apache.velocity.exception.ParseErrorException:
>>>Lexical
>>>error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at
>>>line
>>>1, column 47.  Encountered: <EOF> after : ""
>>>
>>>So how could I insert this character in my template ?
>>>
>>>--
>>>BUNEL Gerard - ATLANTIDE
>>>Consultant
>>>Tel. : +33 (0)2 98 05 43 21
>>>http://www.ago.fr/
>>>--
>>>Technopôle Brest Iroise
>>>Site du Vernis - CS 23866
>>>29238 Brest Cedex 3 - France
>>>Tel. : +33 (0)2 98 05 43 21
>>>Fax. : +33 (0)2 98 05 20 34
>>>e-mail: atlantide-brest@ago.fr
>>>
>>>
>>>---------------------------------------------------------------------
>>>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
>>
>>
>>    
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>  
>


-- 
BUNEL Gerard - ATLANTIDE
Consultant
Tel. : +33 (0)2 98 05 43 21
http://www.ago.fr/
--
Technopôle Brest Iroise
Site du Vernis - CS 23866
29238 Brest Cedex 3 - France
Tel. : +33 (0)2 98 05 43 21
Fax. : +33 (0)2 98 05 20 34
e-mail: atlantide-brest@ago.fr


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


Re: How to put # in the string

Posted by Mike Kienenberger <mk...@gmail.com>.
The most reliable way to do this is to use

#set ($h = '#')

And then ${h} whenever you want a # to appear.  (or ${esc.h} without
the #set if you have the escape tool installed)

This works in all cases exactly as you'd expect.   Avoid using \ to
escape things -- it's not intuitively obvious how it works.

On 11/18/05, Will Glass-Husain <wg...@forio.com> wrote:
> Velocity's silly escaping rules insist that you put the slash before a
> directive \#if
> but not before a # by itself.  Just leave off the slash.
>
> WILL
>
> ----- Original Message -----
> From: "Gérard BUNEL" <ge...@ago.fr>
> To: <ve...@jakarta.apache.org>
> Sent: Friday, November 18, 2005 1:07 AM
> Subject: How to put # in the string
>
>
> > Hello,
> >
> > I need to insert a # character in my template but doing so, like in
> > example2 modified below, I get an Error:
> >
> >        String s = "We are using $project $name to render this.\\#";
> >        w = new StringWriter();
> >
> >        try
> >        {
> >            Velocity.evaluate( context, w, "mystring", s );
> >        }
> >
> > ParseErrorException : org.apache.velocity.exception.ParseErrorException:
> > Lexical
> > error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at
> > line
> > 1, column 47.  Encountered: <EOF> after : ""
> >
> > So how could I insert this character in my template ?
> >
> > --
> > BUNEL Gerard - ATLANTIDE
> > Consultant
> > Tel. : +33 (0)2 98 05 43 21
> > http://www.ago.fr/
> > --
> > Technopôle Brest Iroise
> > Site du Vernis - CS 23866
> > 29238 Brest Cedex 3 - France
> > Tel. : +33 (0)2 98 05 43 21
> > Fax. : +33 (0)2 98 05 20 34
> > e-mail: atlantide-brest@ago.fr
> >
> >
> > ---------------------------------------------------------------------
> > 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
>
>

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


Re: How to put # in the string

Posted by Will Glass-Husain <wg...@forio.com>.
Velocity's silly escaping rules insist that you put the slash before a 
directive \#if
but not before a # by itself.  Just leave off the slash.

WILL

----- Original Message ----- 
From: "Gérard BUNEL" <ge...@ago.fr>
To: <ve...@jakarta.apache.org>
Sent: Friday, November 18, 2005 1:07 AM
Subject: How to put # in the string


> Hello,
>
> I need to insert a # character in my template but doing so, like in 
> example2 modified below, I get an Error:
>
>        String s = "We are using $project $name to render this.\\#";
>        w = new StringWriter();
>
>        try
>        {
>            Velocity.evaluate( context, w, "mystring", s );
>        }
>
> ParseErrorException : org.apache.velocity.exception.ParseErrorException: 
> Lexical
> error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at 
> line
> 1, column 47.  Encountered: <EOF> after : ""
>
> So how could I insert this character in my template ?
>
> -- 
> BUNEL Gerard - ATLANTIDE
> Consultant
> Tel. : +33 (0)2 98 05 43 21
> http://www.ago.fr/
> --
> Technopôle Brest Iroise
> Site du Vernis - CS 23866
> 29238 Brest Cedex 3 - France
> Tel. : +33 (0)2 98 05 43 21
> Fax. : +33 (0)2 98 05 20 34
> e-mail: atlantide-brest@ago.fr
>
>
> ---------------------------------------------------------------------
> 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