Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,24 @@ public static <T> Expression stax(Class<T> clazz) {
public static <T> Expression stax(String clazzName) {
return new StAXJAXBIteratorExpression<T>(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 <T> Expression stax(Class<T> clazz, boolean isNamespaceAware) {
return new StAXJAXBIteratorExpression<T>(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 <T> Expression stax(String clazzName, boolean isNamespaceAware) {
return new StAXJAXBIteratorExpression<T>(clazzName, isNamespaceAware);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -49,6 +51,7 @@ public class StAXJAXBIteratorExpression<T> extends ExpressionAdapter {

private final Class<T> handled;
private final String handledName;
private final boolean isNamespaceAware;

/**
* Creates this expression.
Expand All @@ -59,6 +62,7 @@ public StAXJAXBIteratorExpression(Class<T> handled) {
ObjectHelper.notNull(handled, "handled");
this.handled = handled;
this.handledName = null;
this.isNamespaceAware = true;
}

/**
Expand All @@ -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<T> 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 {
Expand All @@ -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<T> clazz = handled;
if (clazz == null && handledName != null) {
clazz = (Class<T>) exchange.getContext().getClassResolver().resolveMandatoryClass(handledName);
Expand All @@ -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;
}
}

Expand Down