Skip to content

Commit c3a8348

Browse files
committed
Added detection of XML parser charset for IBM JDK
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1721094 13f79535-47bb-0310-9956-ffa450edef68
1 parent fdfbd85 commit c3a8348

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import org.w3c.dom.Document;
3333
import org.xml.sax.InputSource;
3434
import org.xml.sax.SAXException;
35+
import org.xml.sax.SAXParseException;
3536

37+
import javax.xml.parsers.DocumentBuilder;
3638
import javax.xml.parsers.DocumentBuilderFactory;
3739
import javax.xml.parsers.ParserConfigurationException;
3840

@@ -41,6 +43,7 @@
4143
*
4244
* @version $Id$
4345
*/
46+
@SuppressWarnings("ResultOfMethodCallIgnored")
4447
public class BOMInputStreamTest {
4548
//----------------------------------------------------------------------------
4649
// Support code
@@ -180,7 +183,7 @@ private void readBOMInputStreamTwice(final String resource) throws Exception {
180183
}
181184

182185
private void readFile(final BOMInputStream bomInputStream) throws Exception {
183-
int bytes = 0;
186+
int bytes;
184187
final byte[] bytesFromStream = new byte[100];
185188
do {
186189
bytes = bomInputStream.read(bytesFromStream);
@@ -367,13 +370,13 @@ public void testMarkResetBeforeReadWithoutBOM() throws Exception {
367370
public void testNoBoms() throws Exception {
368371
final byte[] data = new byte[] { 'A', 'B', 'C' };
369372
try {
370-
(new BOMInputStream(createUtf8DataStream(data, true), false, (ByteOrderMark[])null)).close();;
373+
(new BOMInputStream(createUtf8DataStream(data, true), false, (ByteOrderMark[])null)).close();
371374
fail("Null BOMs, expected IllegalArgumentException");
372375
} catch (final IllegalArgumentException e) {
373376
// expected
374377
}
375378
try {
376-
(new BOMInputStream(createUtf8DataStream(data, true), false, new ByteOrderMark[0])).close();;
379+
(new BOMInputStream(createUtf8DataStream(data, true), false, new ByteOrderMark[0])).close();
377380
fail("Null BOMs, expected IllegalArgumentException");
378381
} catch (final IllegalArgumentException e) {
379382
// expected
@@ -596,6 +599,8 @@ public void testReadWithoutBOM() throws Exception {
596599

597600
@Test
598601
public void testReadXmlWithBOMUcs2() throws Exception {
602+
Assume.assumeFalse("This test does not pass on some IBM VMs xml parsers", System.getProperty("java.vendor").contains("IBM"));
603+
599604
// UCS-2 is BE.
600605
Assume.assumeTrue(Charset.isSupported("ISO-10646-UCS-2"));
601606
final byte[] data = "<?xml version=\"1.0\" encoding=\"ISO-10646-UCS-2\"?><X/>".getBytes("ISO-10646-UCS-2");
@@ -612,6 +617,7 @@ public void testReadXmlWithBOMUcs4() throws Exception {
612617
// XML parser does not know what to do with UTF-32
613618
parseXml(new BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE));
614619
// XML parser does not know what to do with UTF-32
620+
Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE"));
615621
parseXml(createUtf32BeDataStream(data, true));
616622
}
617623

@@ -633,7 +639,7 @@ public void testReadXmlWithBOMUtf16Le() throws Exception {
633639

634640
@Test
635641
public void testReadXmlWithBOMUtf32Be() throws Exception {
636-
Assume.assumeTrue(Charset.isSupported("UTF_32BE"));
642+
Assume.assumeTrue("JVM and SAX need to support UTF_32BE for this", jvmAndSaxBothSupportCharset("UTF_32BE"));
637643
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32BE\"?><X/>".getBytes("UTF_32BE");
638644
parseXml(new BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE));
639645
// XML parser does not know what to do with UTF-32, so we warp the input stream with a XmlStreamReader
@@ -642,7 +648,7 @@ public void testReadXmlWithBOMUtf32Be() throws Exception {
642648

643649
@Test
644650
public void testReadXmlWithBOMUtf32Le() throws Exception {
645-
Assume.assumeTrue(Charset.isSupported("UTF_32LE"));
651+
Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE"));
646652
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32LE\"?><X/>".getBytes("UTF_32LE");
647653
parseXml(new BOMInputStream(createUtf32LeDataStream(data, true), ByteOrderMark.UTF_32LE));
648654
// XML parser does not know what to do with UTF-32, so we warp the input stream with a XmlStreamReader
@@ -659,15 +665,15 @@ public void testReadXmlWithBOMUtf8() throws Exception {
659665

660666
@Test
661667
public void testReadXmlWithoutBOMUtf32Be() throws Exception {
662-
Assume.assumeTrue(Charset.isSupported("UTF_32BE"));
663-
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32BE\"?><X/>".getBytes("UTF_32BE");
668+
Assume.assumeTrue("JVM and SAX need to support UTF_32BE for this", jvmAndSaxBothSupportCharset("UTF_32BE"));
669+
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF_32BE\"?><X/>".getBytes("UTF_32BE");
664670
parseXml(new BOMInputStream(createUtf32BeDataStream(data, false)));
665671
parseXml(createUtf32BeDataStream(data, false));
666672
}
667673

668674
@Test
669675
public void testReadXmlWithoutBOMUtf32Le() throws Exception {
670-
Assume.assumeTrue(Charset.isSupported("UTF_32LE"));
676+
Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE"));
671677
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32LE\"?><X/>".getBytes("UTF_32LE");
672678
parseXml(new BOMInputStream(createUtf32LeDataStream(data, false)));
673679
parseXml(createUtf32BeDataStream(data, false));
@@ -747,4 +753,21 @@ public void testSupportCode() throws Exception {
747753
new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF, 'A', 'B' },
748754
buf, len);
749755
}
756+
757+
private boolean jvmAndSaxBothSupportCharset(String charSetName) throws ParserConfigurationException, SAXException, IOException {
758+
return Charset.isSupported(charSetName) && doesSaxSupportCharacterSet(charSetName);
759+
}
760+
761+
private boolean doesSaxSupportCharacterSet(String charSetName) throws ParserConfigurationException, SAXException, IOException {
762+
final byte[] data = ("<?xml version=\"1.0\" encoding=\"" + charSetName + "\"?><Z/>").getBytes(charSetName);
763+
final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
764+
try {
765+
final InputSource is = new InputSource(new ByteArrayInputStream(data));
766+
is.setEncoding(charSetName);
767+
documentBuilder.parse(is);
768+
} catch (SAXParseException e) {
769+
if (e.getMessage().contains(charSetName)) return false;
770+
}
771+
return true;
772+
}
750773
}

0 commit comments

Comments
 (0)