Skip to content

Commit

Permalink
Fixes codehaus-plexus#163: Regression: encoding error when parsing a …
Browse files Browse the repository at this point in the history
…ISO-8859-1 xml

- Fixed code.
- Added tests.
  • Loading branch information
belingueres committed Aug 9, 2021
1 parent 0a18bb8 commit f999bdb
Show file tree
Hide file tree
Showing 3 changed files with 1,568 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.UnsupportedEncodingException;

import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.XmlReader;

//import java.util.Hashtable;

Expand Down Expand Up @@ -663,7 +664,12 @@ public void setInput( Reader in )
reset();
reader = in;

if ( reader instanceof InputStreamReader )
if ( reader instanceof XmlReader ) {
// encoding already detected
XmlReader xsr = (XmlReader) reader;
fileEncoding = xsr.getEncoding();
}
else if ( reader instanceof InputStreamReader )
{
InputStreamReader isr = (InputStreamReader) reader;
if ( isr.getEncoding() != null )
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
import static org.junit.Assert.fail;

import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.codehaus.plexus.util.ReaderFactory;
import org.junit.Test;

/**
Expand Down Expand Up @@ -840,4 +846,56 @@ public void testXMLDeclVersionEncodingStandaloneNoSpace()
}
}

/**
* Issue 163: https://github.com/codehaus-plexus/plexus-utils/issues/163
*
* @throws IOException if IO error.
*
* @since 3.4.1
*/
@Test
public void testEncodingISO_8859_1setInputReader()
throws IOException
{
try ( Reader reader =
ReaderFactory.newXmlReader( new File( "src/test/resources/xml", "test-encoding-ISO-8859-1.xml" ) ) )
{
MXParser parser = new MXParser();
parser.setInput( reader );
while ( parser.nextToken() != XmlPullParser.END_DOCUMENT )
;
assertTrue( true );
}
catch ( XmlPullParserException e )
{
fail( "should not raise exception: " + e );
}
}

/**
* Issue 163: https://github.com/codehaus-plexus/plexus-utils/issues/163
*
* @throws IOException if IO error.
*
* @since 3.4.1
*/
@Test
public void testEncodingISO_8859_1_setInputStream()
throws IOException
{
try ( InputStream input =
Files.newInputStream( Paths.get( "src/test/resources/xml", "test-encoding-ISO-8859-1.xml" ) ) )
{
MXParser parser = new MXParser();
parser.setInput( input, null );
while ( parser.nextToken() != XmlPullParser.END_DOCUMENT )
;
assertTrue( true );
}
catch ( XmlPullParserException e )
{
fail( "should not raise exception: " + e );
}
}

}
Loading

0 comments on commit f999bdb

Please sign in to comment.