Skip to content

Commit bb346ad

Browse files
authored
Fixes#163: Regression: encoding error when parsing a ISO-8859-1 xml (#164)
- Fixed code. - Added tests.
1 parent 5e6d78e commit bb346ad

File tree

3 files changed

+1568
-1
lines changed

3 files changed

+1568
-1
lines changed

‎src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
importjava.io.UnsupportedEncodingException;
1717

1818
importorg.codehaus.plexus.util.ReaderFactory;
19+
importorg.codehaus.plexus.util.xml.XmlReader;
1920

2021
//import java.util.Hashtable;
2122

@@ -663,7 +664,12 @@ public void setInput( Reader in )
663664
reset();
664665
reader = in;
665666

666-
if ( readerinstanceofInputStreamReader )
667+
if ( readerinstanceofXmlReader ){
668+
// encoding already detected
669+
XmlReaderxsr = (XmlReader) reader;
670+
fileEncoding = xsr.getEncoding();
671+
}
672+
elseif ( readerinstanceofInputStreamReader )
667673
{
668674
InputStreamReaderisr = (InputStreamReader) reader;
669675
if ( isr.getEncoding() != null )

‎src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121
importstaticorg.junit.Assert.fail;
2222

2323
importjava.io.EOFException;
24+
importjava.io.File;
2425
importjava.io.IOException;
26+
importjava.io.InputStream;
27+
importjava.io.Reader;
2528
importjava.io.StringReader;
29+
importjava.nio.file.Files;
30+
importjava.nio.file.Paths;
2631

32+
importorg.codehaus.plexus.util.ReaderFactory;
2733
importorg.junit.Test;
2834

2935
/**
@@ -840,4 +846,56 @@ public void testXMLDeclVersionEncodingStandaloneNoSpace()
840846
}
841847
}
842848

849+
/**
850+
* Issue 163: https://github.com/codehaus-plexus/plexus-utils/issues/163
851+
*
852+
* @throws IOException if IO error.
853+
*
854+
* @since 3.4.1
855+
*/
856+
@Test
857+
publicvoidtestEncodingISO_8859_1setInputReader()
858+
throwsIOException
859+
{
860+
try ( Readerreader =
861+
ReaderFactory.newXmlReader( newFile( "src/test/resources/xml", "test-encoding-ISO-8859-1.xml" ) ) )
862+
{
863+
MXParserparser = newMXParser();
864+
parser.setInput( reader );
865+
while ( parser.nextToken() != XmlPullParser.END_DOCUMENT )
866+
;
867+
assertTrue( true );
868+
}
869+
catch ( XmlPullParserExceptione )
870+
{
871+
fail( "should not raise exception: " + e );
872+
}
873+
}
874+
875+
/**
876+
* Issue 163: https://github.com/codehaus-plexus/plexus-utils/issues/163
877+
*
878+
* @throws IOException if IO error.
879+
*
880+
* @since 3.4.1
881+
*/
882+
@Test
883+
publicvoidtestEncodingISO_8859_1_setInputStream()
884+
throwsIOException
885+
{
886+
try ( InputStreaminput =
887+
Files.newInputStream( Paths.get( "src/test/resources/xml", "test-encoding-ISO-8859-1.xml" ) ) )
888+
{
889+
MXParserparser = newMXParser();
890+
parser.setInput( input, null );
891+
while ( parser.nextToken() != XmlPullParser.END_DOCUMENT )
892+
;
893+
assertTrue( true );
894+
}
895+
catch ( XmlPullParserExceptione )
896+
{
897+
fail( "should not raise exception: " + e );
898+
}
899+
}
900+
843901
}

0 commit comments

Comments
(0)