Name: nt126004 Date: 05/22/2003
FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
If you use the possibility of java.util.prefs.Preferences
to import or export preferences into or from a xml file all
new line characters of a string will be stripped.
This bug also bothers all operating systems where
the "FileSystemPreferences" are used (at least Linux).
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. create a multiline preference value
2. export it
3. import it again
EXPECTED VERSUS ACTUAL BEHAVIOR :
It was expected that the export and import preserves the
new line characters.
One possible way would be in doing some kind of escaping or
possibly configure the xmlparser in a way to do this.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package test;
import java.io.*;
import java.util.prefs.Preferences;
public class PrefTest
{
public static void main(String[] args)
{
try
{
String value = "value\nwith\nmultiple\nlines";
System.out.println( "value before1: " + value );
Preferences.userRoot().node( "test" ).put( "key", value );
System.out.println( "value before2: " + Preferences.userRoot().node
( "test" ).get( "key", "default" ) );
Preferences.userRoot().node( "test" ).exportSubtree( new
FileOutputStream( "some_test_file.xml" ) );
Preferences.importPreferences( new FileInputStream
( "some_test_file.xml" ) );
System.out.println( "value after: " + Preferences.userRoot().node
( "test" ).get( "key", "default" ) );
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
do a escaping in the client code:
before put:
int len = value.length();
StringBuffer ret = new StringBuffer( len + 16 );
for ( int i = 0; i < len; i++ )
{
char c = value.charAt( i );
if ( c == '\n' )
{
ret.append( "\\n" );
}
else if ( c == '\\' )
{
ret.append( "\\\\" );
}
else
{
ret.append( c );
}
}
value = ret.toString();
---------------------------------------------------------
after put:
int len = value.length();
StringBuffer ret = new StringBuffer( len );
for ( int i = 0; i < len; i++ )
{
char c = value.charAt( i );
if ( c == '\\' )
{
i++;
if ( i < len )
{
c = value.charAt( i );
if ( c == 'n' )
{
ret.append( '\n' );
}
else if ( c == '\\' )
{
ret.append( '\\' );
}
else
{
throw new Error( "wrong format" );
}
}
else
{
throw new Error( "wrong format" );
}
}
else
{
ret.append( c );
}
}
value = ret.toString();
(Review ID: 163650)
======================================================================
FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
If you use the possibility of java.util.prefs.Preferences
to import or export preferences into or from a xml file all
new line characters of a string will be stripped.
This bug also bothers all operating systems where
the "FileSystemPreferences" are used (at least Linux).
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. create a multiline preference value
2. export it
3. import it again
EXPECTED VERSUS ACTUAL BEHAVIOR :
It was expected that the export and import preserves the
new line characters.
One possible way would be in doing some kind of escaping or
possibly configure the xmlparser in a way to do this.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package test;
import java.io.*;
import java.util.prefs.Preferences;
public class PrefTest
{
public static void main(String[] args)
{
try
{
String value = "value\nwith\nmultiple\nlines";
System.out.println( "value before1: " + value );
Preferences.userRoot().node( "test" ).put( "key", value );
System.out.println( "value before2: " + Preferences.userRoot().node
( "test" ).get( "key", "default" ) );
Preferences.userRoot().node( "test" ).exportSubtree( new
FileOutputStream( "some_test_file.xml" ) );
Preferences.importPreferences( new FileInputStream
( "some_test_file.xml" ) );
System.out.println( "value after: " + Preferences.userRoot().node
( "test" ).get( "key", "default" ) );
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
do a escaping in the client code:
before put:
int len = value.length();
StringBuffer ret = new StringBuffer( len + 16 );
for ( int i = 0; i < len; i++ )
{
char c = value.charAt( i );
if ( c == '\n' )
{
ret.append( "\\n" );
}
else if ( c == '\\' )
{
ret.append( "\\\\" );
}
else
{
ret.append( c );
}
}
value = ret.toString();
---------------------------------------------------------
after put:
int len = value.length();
StringBuffer ret = new StringBuffer( len );
for ( int i = 0; i < len; i++ )
{
char c = value.charAt( i );
if ( c == '\\' )
{
i++;
if ( i < len )
{
c = value.charAt( i );
if ( c == 'n' )
{
ret.append( '\n' );
}
else if ( c == '\\' )
{
ret.append( '\\' );
}
else
{
throw new Error( "wrong format" );
}
}
else
{
throw new Error( "wrong format" );
}
}
else
{
ret.append( c );
}
}
value = ret.toString();
(Review ID: 163650)
======================================================================