CAMEL-7025 fixed the NPE of StaxConverter
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/644c1f0a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/644c1f0a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/644c1f0a Branch: refs/heads/camel-gora Commit: 644c1f0ab894fbd8c33050cd354a38bbb7acac2a Parents: 08a6f45 Author: Willem Jiang <willem.ji...@gmail.com> Authored: Mon Dec 2 14:38:20 2013 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Mon Dec 2 14:38:20 2013 +0800 ---------------------------------------------------------------------- .../apache/camel/converter/jaxp/StaxConverter.java | 14 ++++++++++++-- .../camel/converter/jaxp/StaxConverterTest.java | 6 +++--- 2 files changed, 15 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/644c1f0a/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxConverter.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxConverter.java b/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxConverter.java index 5469df5..ca16a11 100644 --- a/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxConverter.java +++ b/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxConverter.java @@ -167,7 +167,12 @@ public class StaxConverter { public XMLStreamReader createXMLStreamReader(InputStream in, Exchange exchange) throws XMLStreamException { XMLInputFactory factory = getInputFactory(); try { - return factory.createXMLStreamReader(IOHelper.buffered(in), IOHelper.getCharsetName(exchange, false)); + String charsetName = IOHelper.getCharsetName(exchange, false); + if (charsetName == null) { + return factory.createXMLStreamReader(IOHelper.buffered(in)); + } else { + return factory.createXMLStreamReader(IOHelper.buffered(in), charsetName); + } } finally { returnXMLInputFactory(factory); } @@ -236,7 +241,12 @@ public class StaxConverter { public XMLEventReader createXMLEventReader(InputStream in, Exchange exchange) throws XMLStreamException { XMLInputFactory factory = getInputFactory(); try { - return factory.createXMLEventReader(IOHelper.buffered(in), IOHelper.getCharsetName(exchange, false)); + String charsetName = IOHelper.getCharsetName(exchange, false); + if (charsetName == null) { + return factory.createXMLEventReader(IOHelper.buffered(in)); + } else { + return factory.createXMLEventReader(IOHelper.buffered(in), charsetName); + } } finally { returnXMLInputFactory(factory); } http://git-wip-us.apache.org/repos/asf/camel/blob/644c1f0a/camel-core/src/test/java/org/apache/camel/converter/jaxp/StaxConverterTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/converter/jaxp/StaxConverterTest.java b/camel-core/src/test/java/org/apache/camel/converter/jaxp/StaxConverterTest.java index 5fe411b..3a299af 100644 --- a/camel-core/src/test/java/org/apache/camel/converter/jaxp/StaxConverterTest.java +++ b/camel-core/src/test/java/org/apache/camel/converter/jaxp/StaxConverterTest.java @@ -58,7 +58,7 @@ public class StaxConverterTest extends ContextTestSupport { output = new ByteArrayOutputStream(); // ensure UTF-8 encoding Exchange exchange = new DefaultExchange(context); - exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.name()); + //exchange.setProperty(Exchange.CHARSET_NAME, ISO_8859_1.toString()); writer = context.getTypeConverter().mandatoryConvertTo(XMLEventWriter.class, exchange, output); while (reader.hasNext()) { writer.add(reader.nextEvent()); @@ -74,7 +74,7 @@ public class StaxConverterTest extends ContextTestSupport { assertNotNull(output); String result = new String(output.toByteArray(), UTF_8.name()); - + System.out.println(result); boolean equals = TEST_XML_WITH_XML_HEADER.equals(result) || TEST_XML_WITH_XML_HEADER_ISO_8859_1.equals(result); assertTrue("Should match header", equals); } @@ -93,7 +93,7 @@ public class StaxConverterTest extends ContextTestSupport { output = new ByteArrayOutputStream(); // ensure UTF-8 encoding Exchange exchange = new DefaultExchange(context); - exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.name()); + //exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.name()); writer = context.getTypeConverter().mandatoryConvertTo(XMLStreamWriter.class, exchange, output); // copy to writer while (reader.hasNext()) {