Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/main/java/org/codehaus/plexus/util/xml/XmlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,8 @@ private String calculateRawEncoding(String bomEnc, String xmlGuessEnc, String xm
}
encoding = UTF_8;
} else if (bomEnc.equals(UTF_16BE) || bomEnc.equals(UTF_16LE)) {
if (xmlGuessEnc != null && !xmlGuessEnc.equals(bomEnc)) {
throw new IOException(RAW_EX_1.format(new Object[] {bomEnc, xmlGuessEnc, xmlEnc}));
}
if (xmlEnc != null && !xmlEnc.equals(UTF_16) && !xmlEnc.equals(bomEnc)) {
if (xmlGuessEnc != null && !xmlGuessEnc.equals(bomEnc)
|| xmlEnc != null && !xmlEnc.equals(UTF_16) && !xmlEnc.equals(bomEnc)) {
throw new XmlStreamReaderException(
RAW_EX_1.format(new Object[] {bomEnc, xmlGuessEnc, xmlEnc}), bomEnc, xmlGuessEnc, xmlEnc, is);
}
Expand Down
35 changes: 13 additions & 22 deletions src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

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

// import java.util.Hashtable;

Expand Down Expand Up @@ -573,17 +573,6 @@ public Object getProperty(String name) {
public void setInput(Reader in) throws XmlPullParserException {
reset();
reader = in;

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) {
fileEncoding = isr.getEncoding().toUpperCase();
}
}
}

@Override
Expand All @@ -596,11 +585,21 @@ public void setInput(java.io.InputStream inputStream, String inputEncoding) thro
if (inputEncoding != null) {
reader = ReaderFactory.newReader(inputStream, inputEncoding);
} else {
reader = ReaderFactory.newXmlReader(inputStream);
reader = new XmlStreamReader(inputStream, false);
}
} catch (UnsupportedEncodingException une) {
throw new XmlPullParserException(
"could not create reader for encoding " + inputEncoding + " : " + une, this, une);
} catch (XmlStreamReaderException e) {
if ("UTF-8".equals(e.getBomEncoding())) {
throw new XmlPullParserException(
"UTF-8 BOM plus xml decl of " + e.getXmlEncoding() + " is incompatible", this, e);
}
if (e.getBomEncoding() != null && e.getBomEncoding().startsWith("UTF-16")) {
throw new XmlPullParserException(
"UTF-16 BOM in a " + e.getXmlEncoding() + " encoded file is incompatible", this, e);
}
throw new XmlPullParserException("could not create reader : " + e, this, e);
} catch (IOException e) {
throw new XmlPullParserException("could not create reader : " + e, this, e);
}
Expand Down Expand Up @@ -2758,14 +2757,6 @@ private void parseXmlDeclWithVersion(int versionStart, int versionEnd) throws Xm
// TODO reconcile with setInput encodingName
inputEncoding = newString(buf, encodingStart, encodingEnd - encodingStart);

if ("UTF8".equals(fileEncoding) && inputEncoding.toUpperCase().startsWith("ISO-")) {
throw new XmlPullParserException(
"UTF-8 BOM plus xml decl of " + inputEncoding + " is incompatible", this, null);
} else if ("UTF-16".equals(fileEncoding) && inputEncoding.equalsIgnoreCase("UTF-8")) {
throw new XmlPullParserException(
"UTF-16 BOM plus xml decl of " + inputEncoding + " is incompatible", this, null);
}

lastParsedAttr = "encoding";

ch = more();
Expand Down
72 changes: 72 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 @@ -20,13 +20,15 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.codehaus.plexus.util.xml.ReaderFactory;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -903,6 +905,76 @@ void encodingISO88591setStringReader() throws IOException {
}
}

/**
* Issue 163: https://github.com/codehaus-plexus/plexus-utils/issues/163
*
* Another case of bug #163: Reader generated with ReaderFactory.newReader and the right file encoding.
*
* @throws IOException if IO error.
*
* @since 3.5.2
*/
@Test
void encodingISO88591newReader() throws IOException {
// NOTE: if using Files.newBufferedReader(path, StandardCharsets.UTF-8), the reader will throw an exception
// because the decoder created by new InputStreamReader() is lenient while the one created by
// Files.newBufferedReader() is not.
try (Reader reader = new InputStreamReader(
Files.newInputStream(Paths.get("src/test/resources/xml", "test-encoding-ISO-8859-1.xml")),
StandardCharsets.UTF_8)) {
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
*
* Another case of bug #163: InputStream supplied with the right file encoding.
*
* @throws IOException if IO error.
*
* @since 3.5.2
*/
@Test
void encodingISO88591setInputStreamEncoded() 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, StandardCharsets.UTF_8.name());
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
void encodingUTF8newXmlReader() throws IOException {
try (Reader reader = new XmlStreamReader(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);
}
}

/**
* <p>
* Test custom Entity not found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
Expand Down Expand Up @@ -182,14 +181,13 @@ public void testhst_bh_006() throws IOException {
*/
@Test
void testhst_lhs_007() throws IOException {
try (FileInputStream is = new FileInputStream(new File(testResourcesDir, "007.xml"));
InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
parser.setInput(reader);
try (InputStream is = new FileInputStream(new File(testResourcesDir, "007.xml"))) {
parser.setInput(is, null);
while (parser.nextToken() != XmlPullParser.END_DOCUMENT)
;
fail("UTF-8 BOM plus xml decl of iso-8859-1 incompatible");
fail("UTF-8 BOM plus xml decl of ISO-8859-1 incompatible");
} catch (XmlPullParserException e) {
assertTrue(e.getMessage().contains("UTF-8 BOM plus xml decl of iso-8859-1 is incompatible"));
assertTrue(e.getMessage().contains("UTF-8 BOM plus xml decl of ISO-8859-1 is incompatible"));
}
}

Expand All @@ -204,14 +202,13 @@ void testhst_lhs_007() throws IOException {
*/
@Test
void testhst_lhs_008() throws IOException {
try (FileInputStream is = new FileInputStream(new File(testResourcesDir, "008.xml"));
InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_16)) {
parser.setInput(reader);
try (InputStream is = new FileInputStream(new File(testResourcesDir, "008.xml"))) {
parser.setInput(is, null);
while (parser.nextToken() != XmlPullParser.END_DOCUMENT)
;
fail("UTF-16 BOM plus xml decl of utf-8 (using UTF-16 coding) incompatible");
fail("UTF-16 BOM plus xml decl of UTF-8 (using UTF-16 coding) incompatible");
} catch (XmlPullParserException e) {
assertTrue(e.getMessage().contains("UTF-16 BOM plus xml decl of utf-8 is incompatible"));
assertTrue(e.getMessage().contains("UTF-16 BOM in a UTF-8 encoded file is incompatible"));
}
}

Expand All @@ -226,14 +223,13 @@ void testhst_lhs_008() throws IOException {
*/
@Test
void testhst_lhs_009() throws IOException {
try (FileInputStream is = new FileInputStream(new File(testResourcesDir, "009.xml"));
InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
parser.setInput(reader);
try (InputStream is = new FileInputStream(new File(testResourcesDir, "009.xml"))) {
parser.setInput(is, null);
while (parser.nextToken() != XmlPullParser.END_DOCUMENT)
;
fail("UTF-16 BOM plus xml decl of utf-8 (using UTF-8 coding) incompatible");
fail("UTF-16 BOM plus xml decl of UTF-8 (using UTF-8 coding) incompatible");
} catch (XmlPullParserException e) {
assertTrue(e.getMessage().contains("UTF-16 BOM in a UTF-8 encoded file is incompatible"));
assertTrue(e.getMessage().contains("UTF-16 BOM in a UTF-8 encoded file is incompatible"), e.getMessage());
}
}
}
Loading
Loading