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

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.UnsupportedEncodingException;
1717

1818
import org.codehaus.plexus.util.ReaderFactory;
19+
import org.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 ( reader instanceof InputStreamReader )
667+
if ( reader instanceof XmlReader ) {
668+
// encoding already detected
669+
XmlReader xsr = (XmlReader) reader;
670+
fileEncoding = xsr.getEncoding();
671+
}
672+
else if ( reader instanceof InputStreamReader )
667673
{
668674
InputStreamReader isr = (InputStreamReader) reader;
669675
if ( isr.getEncoding() != null )

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

+58
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121
import static org.junit.Assert.fail;
2222

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

32+
import org.codehaus.plexus.util.ReaderFactory;
2733
import org.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+
public void testEncodingISO_8859_1setInputReader()
858+
throws IOException
859+
{
860+
try ( Reader reader =
861+
ReaderFactory.newXmlReader( new File( "src/test/resources/xml", "test-encoding-ISO-8859-1.xml" ) ) )
862+
{
863+
MXParser parser = new MXParser();
864+
parser.setInput( reader );
865+
while ( parser.nextToken() != XmlPullParser.END_DOCUMENT )
866+
;
867+
assertTrue( true );
868+
}
869+
catch ( XmlPullParserException e )
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+
public void testEncodingISO_8859_1_setInputStream()
884+
throws IOException
885+
{
886+
try ( InputStream input =
887+
Files.newInputStream( Paths.get( "src/test/resources/xml", "test-encoding-ISO-8859-1.xml" ) ) )
888+
{
889+
MXParser parser = new MXParser();
890+
parser.setInput( input, null );
891+
while ( parser.nextToken() != XmlPullParser.END_DOCUMENT )
892+
;
893+
assertTrue( true );
894+
}
895+
catch ( XmlPullParserException e )
896+
{
897+
fail( "should not raise exception: " + e );
898+
}
899+
}
900+
843901
}

0 commit comments

Comments
 (0)