You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Michael Giroux <ml...@gmail.com> on 2007/03/05 18:02:09 UTC

How to specify '\n' in a template?

My template is generating javadocs.  The input string may have \n characters
that I wish to convert to "\n * " to preserve the intent of the comment
string.

In java, I am able to accomplish this with "xxx\nyyy".replaceAll("\n", "\n *
")

I have tried a couple of variations in the velocity template, but so far,
the replaceAll is not replacing the strings.

A hex dump of the output file shows that the result of the generation is
xxx\nyyy  (\n == 0x0a).

What am I doing wrong?

Thanks
Michael Giroux

Re: How to specify '\n' in a template?

Posted by Nathan Bubna <nb...@gmail.com>.
String literal definitions in VTL (Velocity Template Language) are not
the same as those in Java.  For example, the use of single quotes to
delineate a string do not imply a character.  Instead, they simply
mean that the string is not to be interpolated.  So, within a single
quoted string literal, there is *no* escaping at present.  Within a
double quoted string (which is by default interpolated), the escaping
rules are the same as for the rest of VTL.  This means that you can
only escape references and directives and use VTL comments.
Basically, that string becomes a mini-template of its own.

As of Velocity 1.5 (and its beta releases), VTL allows line breaks
within string literal definitions.  This means that you would put a
new line character into the string by typing that actual character,
rather than using the Java escape sequence (\n).  So, you would want
to do something like this:

#set( $new = "
" )
$foo.replaceAll( "$new", "$new * " )

To do this with Velocity 1.4, you would probably need to insert the
newline character via a tool of some sort.

On 3/5/07, Michael Giroux <ml...@gmail.com> wrote:
> OK, I managed to solve my problem by changing the replacement string:
> replaceAll("\n", "$0 * ")
>
> So the issue has changed a bit -- what am I missing in the docs about string
> processing?
> When executed from a java program, replaceAll("\n", "\n * ") does what I
> want, but when I put this same syntax into a velocity template, the
> replacement ends up as "n * "  where the newlines were replaced with the
> letter n followed by my " * " sequence.
>
> From this I conclude that velocity processing of the string literal is
> having some side effect that I'm not aware of.
>
> What am I missing about string processing?
>
> Michael
>
>
> On 3/5/07, Michael Giroux <ml...@gmail.com> wrote:
> >
> > My template is generating javadocs.  The input string may have \n
> > characters that I wish to convert to "\n * " to preserve the intent of the
> > comment string.
> >
> > In java, I am able to accomplish this with "xxx\nyyy".replaceAll("\n", "\n
> > * ")
> >
> > I have tried a couple of variations in the velocity template, but so far,
> > the replaceAll is not replacing the strings.
> >
> > A hex dump of the output file shows that the result of the generation is
> > xxx\nyyy  (\n == 0x0a).
> >
> > What am I doing wrong?
> >
> > Thanks
> > Michael Giroux
> >
>

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


Re: How to specify '\n' in a template?

Posted by Michael Giroux <ml...@gmail.com>.
OK, I managed to solve my problem by changing the replacement string:
replaceAll("\n", "$0 * ")

So the issue has changed a bit -- what am I missing in the docs about string
processing?
When executed from a java program, replaceAll("\n", "\n * ") does what I
want, but when I put this same syntax into a velocity template, the
replacement ends up as "n * "  where the newlines were replaced with the
letter n followed by my " * " sequence.

>From this I conclude that velocity processing of the string literal is
having some side effect that I'm not aware of.

What am I missing about string processing?

Michael


On 3/5/07, Michael Giroux <ml...@gmail.com> wrote:
>
> My template is generating javadocs.  The input string may have \n
> characters that I wish to convert to "\n * " to preserve the intent of the
> comment string.
>
> In java, I am able to accomplish this with "xxx\nyyy".replaceAll("\n", "\n
> * ")
>
> I have tried a couple of variations in the velocity template, but so far,
> the replaceAll is not replacing the strings.
>
> A hex dump of the output file shows that the result of the generation is
> xxx\nyyy  (\n == 0x0a).
>
> What am I doing wrong?
>
> Thanks
> Michael Giroux
>