diff --git a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXBuilder.java b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXBuilder.java index c3abbc422ae7b..9bbdb1600232c 100644 --- a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXBuilder.java +++ b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXBuilder.java @@ -44,4 +44,24 @@ public static Expression stax(Class clazz) { public static Expression stax(String clazzName) { return new StAXJAXBIteratorExpression(clazzName); } + + /** + * Creates a {@link org.apache.camel.component.stax.StAXJAXBIteratorExpression}. + * + * @param clazz the class which has JAXB annotations to bind POJO. + * @param isNamespaceAware sets the namespace awareness of the xml reader + */ + public static Expression stax(Class clazz, boolean isNamespaceAware) { + return new StAXJAXBIteratorExpression(clazz, isNamespaceAware); + } + + /** + * Creates a {@link org.apache.camel.component.stax.StAXJAXBIteratorExpression}. + * + * @param clazzName the FQN name of the class which has JAXB annotations to bind POJO. + * @param isNamespaceAware sets the namespace awareness of the xml reader + */ + public static Expression stax(String clazzName, boolean isNamespaceAware) { + return new StAXJAXBIteratorExpression(clazzName, isNamespaceAware); + } } diff --git a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java index 01e9e2a764fe1..5a714aa53647a 100644 --- a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java +++ b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java @@ -18,12 +18,14 @@ import java.io.Closeable; import java.io.IOException; +import java.io.InputStream; import java.util.Iterator; import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.XMLEvent; @@ -49,6 +51,7 @@ public class StAXJAXBIteratorExpression extends ExpressionAdapter { private final Class handled; private final String handledName; + private final boolean isNamespaceAware; /** * Creates this expression. @@ -59,6 +62,7 @@ public StAXJAXBIteratorExpression(Class handled) { ObjectHelper.notNull(handled, "handled"); this.handled = handled; this.handledName = null; + this.isNamespaceAware = true; } /** @@ -70,6 +74,33 @@ public StAXJAXBIteratorExpression(String handledName) { ObjectHelper.notNull(handledName, "handledName"); this.handledName = handledName; this.handled = null; + this.isNamespaceAware = true; + } + + /** + * Creates this expression. + * + * @param handled the class which has JAXB annotations to bind POJO. + * @param isNamespaceAware sets the namespace awareness of the xml reader + */ + public StAXJAXBIteratorExpression(Class handled, boolean isNamespaceAware) { + ObjectHelper.notNull(handled, "handled"); + this.handled = handled; + this.handledName = null; + this.isNamespaceAware = isNamespaceAware; + } + + /** + * Creates this expression. + * + * @param handledName the FQN name of the class which has JAXB annotations to bind POJO. + * @param isNamespaceAware sets the namespace awareness of the xml reader + */ + public StAXJAXBIteratorExpression(String handledName, boolean isNamespaceAware) { + ObjectHelper.notNull(handledName, "handledName"); + this.handledName = handledName; + this.handled = null; + this.isNamespaceAware = isNamespaceAware; } private static JAXBContext jaxbContext(Class handled) throws JAXBException { @@ -89,7 +120,15 @@ private static JAXBContext jaxbContext(Class handled) throws JAXBException { @SuppressWarnings("unchecked") public Object evaluate(Exchange exchange) { try { - XMLEventReader reader = exchange.getIn().getMandatoryBody(XMLEventReader.class); + XMLEventReader reader; + if (isNamespaceAware) { + reader = exchange.getIn().getMandatoryBody(XMLEventReader.class); + } else { + InputStream inputStream = exchange.getIn().getMandatoryBody(InputStream.class); + XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); + xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); + reader = xmlInputFactory.createXMLEventReader(inputStream); + } Class clazz = handled; if (clazz == null && handledName != null) { clazz = (Class) exchange.getContext().getClassResolver().resolveMandatoryClass(handledName); @@ -104,6 +143,9 @@ public Object evaluate(Exchange exchange) { } catch (ClassNotFoundException e) { exchange.setException(e); return null; + } catch (XMLStreamException e) { + exchange.setException(e); + return null; } }