You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Paul Pantages <pp...@xtera.com> on 2002/01/08 04:16:09 UTC

Replace.java and "\n"

Hi all,

I am using ant 1.4.1 on Solaris 2.7, j2dsdk 1.3.1

I was having trouble replacing a token with a value containing newline
"\n". The replace task was just printing out the two characters
'\' & 'n' instead of newline.

E.g., 
   <replace
     file="filename"
     token="#BlaBla"
     value="\n#BlaBla"
   />

Was insertng \n#BlaBla into my file.

I looked into the source for replace task and at the Replace.processFile()
method. I see where the \n is supposed to be handled at line 325 or so:
...
String val = stringReplace(value.getText(), "\n", linesep);
String tok = stringReplace(token.getText(), "\n", linesep);
...

The prob. is, the "\n" in the replace call is converted to 0x0a, and
of course never matches the characters '\','n' in my token or val.

If I switch the replaced string to "\\n", the replace task does what I
expect, i.e., switches the "\n" to the unix newline 0x0a.

...
String val = stringReplace(value.getText(), "\\n", linesep);
String tok = stringReplace(token.getText(), "\\n", linesep);
...

So.... is this is really a bug or am I missing something obvious...? I
see that the file is dated 1999 and find it doubtful that this could
go unnoticed for so long.


Thanks, PdP

-- 
----------------------------------------------------------------------------
|Paul Pantages                       | Xtera Communications Inc.            |
|  pdp@xtera.com                     | 500 W. Bethany                       |
|                                    | Allen, Texas 75013                   |
|                                    | 972 649 5208 (voice)                 |
| Avenge September 11...             | 214 726 5332 (cell)                  |
----------------------------------------------------------------------------


*****
This email may contain confidential, privileged and protected information
from disclosure. Any review or distribution by others is strictly
prohibited. If you are not the intended recipient, please contact the
sender and delete all copies.
*****



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Replace.java and "\n"

Posted by Steve Loughran <st...@iseran.com>.
----- Original Message -----
From: "Paul Pantages" <pp...@xtera.com>
To: <an...@jakarta.apache.org>
Sent: Monday, January 07, 2002 7:16 PM
Subject: Replace.java and "\n"


> Hi all,

hello

> I am using ant 1.4.1 on Solaris 2.7, j2dsdk 1.3.1
>
> I was having trouble replacing a token with a value containing newline
> "\n". The replace task was just printing out the two characters
> '\' & 'n' instead of newline.
>
> E.g.,
>    <replace
>      file="filename"
>      token="#BlaBla"
>      value="\n#BlaBla"
>    />
>
> Was insertng \n#BlaBla into my file.
>
> I looked into the source for replace task and at the Replace.processFile()
> method. I see where the \n is supposed to be handled at line 325 or so:
> ...
> String val = stringReplace(value.getText(), "\n", linesep);
> String tok = stringReplace(token.getText(), "\n", linesep);
> ...
>
> The prob. is, the "\n" in the replace call is converted to 0x0a, and
> of course never matches the characters '\','n' in my token or val.

yup

> If I switch the replaced string to "\\n", the replace task does what I
> expect, i.e., switches the "\n" to the unix newline 0x0a.

maybe there is an expectation issue?

>
> ...
> String val = stringReplace(value.getText(), "\\n", linesep);
> String tok = stringReplace(token.getText(), "\\n", linesep);

nope

 ...
> So.... is this is really a bug or am I missing something obvious...?

you are missing something obvious. To put a newline into an xml string you
use &#10; not \n

On the positive note, I am glad that you were willing to go into the source
and fix behaviour which did not match your expectation. If you keep that
soon you will be finding genuine bugs...

-steve


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Replace.java and "\n"

Posted by Stefan Bodewig <bo...@apache.org>.
On Mon, 7 Jan 2002, Paul Pantages <pp...@xtera.com> wrote:

> I was having trouble replacing a token with a value containing
> newline "\n". The replace task was just printing out the two
> characters '\' & 'n' instead of newline.
> 
> E.g., 
>    <replace
>      file="filename"
>      token="#BlaBla"
>      value="\n#BlaBla"
>    />

\n is \n not a new line in anything but C-like programming languages.
Try the XML escape for a new line &#x0A; or the system property
line.separator which can be accessed via ${line.separator}.

Stefan

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>