use of javax.xml.stream.XMLStreamReader in project camel by apache.
the class XStreamDataFormat method createHierarchicalStreamReader.
protected HierarchicalStreamReader createHierarchicalStreamReader(Exchange exchange, InputStream stream) throws XMLStreamException {
updateCharactorEncodingInfo(exchange);
if (getXstreamDriver() != null) {
return getXstreamDriver().createReader(stream);
}
XMLStreamReader xmlReader = getStaxConverter().createXMLStreamReader(stream, exchange);
return new StaxReader(new QNameMap(), xmlReader);
}
use of javax.xml.stream.XMLStreamReader in project camel by apache.
the class JaxbDataFormat method unmarshal.
public Object unmarshal(Exchange exchange, InputStream stream) throws IOException, SAXException {
try {
Object answer;
XMLStreamReader xmlReader;
if (needFiltering(exchange)) {
xmlReader = typeConverter.convertTo(XMLStreamReader.class, createNonXmlFilterReader(exchange, stream));
} else {
xmlReader = typeConverter.convertTo(XMLStreamReader.class, stream);
}
if (partialClass != null) {
// partial unmarshalling
answer = createUnmarshaller().unmarshal(xmlReader, partialClass);
} else {
answer = createUnmarshaller().unmarshal(xmlReader);
}
if (answer instanceof JAXBElement && isIgnoreJAXBElement()) {
answer = ((JAXBElement<?>) answer).getValue();
}
return answer;
} catch (JAXBException e) {
throw new IOException(e);
}
}
use of javax.xml.stream.XMLStreamReader in project camel by apache.
the class FallbackTypeConverter method unmarshal.
protected Object unmarshal(Unmarshaller unmarshaller, Exchange exchange, Object value) throws JAXBException, UnsupportedEncodingException, XMLStreamException {
try {
XMLStreamReader xmlReader;
if (value instanceof XMLStreamReader) {
xmlReader = (XMLStreamReader) value;
} else if (value instanceof InputStream) {
if (needFiltering(exchange)) {
xmlReader = staxConverter.createXMLStreamReader(new NonXmlFilterReader(new InputStreamReader((InputStream) value, IOHelper.getCharsetName(exchange))));
} else {
xmlReader = staxConverter.createXMLStreamReader((InputStream) value, exchange);
}
} else if (value instanceof Reader) {
Reader reader = (Reader) value;
if (needFiltering(exchange)) {
if (!(value instanceof NonXmlFilterReader)) {
reader = new NonXmlFilterReader((Reader) value);
}
}
xmlReader = staxConverter.createXMLStreamReader(reader);
} else if (value instanceof Source) {
xmlReader = staxConverter.createXMLStreamReader((Source) value);
} else {
throw new IllegalArgumentException("Cannot convert from " + value.getClass());
}
return unmarshaller.unmarshal(xmlReader);
} finally {
if (value instanceof Closeable) {
IOHelper.close((Closeable) value, "Unmarshalling", LOG);
}
}
}
use of javax.xml.stream.XMLStreamReader in project camel by apache.
the class XmlConverter method toStAXSource.
/**
* Converts the source instance to a {@link StAXSource} or returns null if the conversion is not
* supported (making it easy to derive from this class to add new kinds of conversion).
* @throws FileNotFoundException
* @throws XMLStreamException
*/
@Converter
public StAXSource toStAXSource(File file, Exchange exchange) throws FileNotFoundException, XMLStreamException {
InputStream is = IOHelper.buffered(new FileInputStream(file));
XMLStreamReader r = new StaxConverter().createXMLStreamReader(is, exchange);
return new StAXSource(r);
}
use of javax.xml.stream.XMLStreamReader in project camel by apache.
the class StaxConverterTest method testEncodingXmlStreamReader.
public void testEncodingXmlStreamReader() throws Exception {
TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM.reset();
XMLStreamReader reader = null;
XMLStreamWriter writer = null;
ByteArrayOutputStream output = null;
try {
// enter text encoded with Latin1
reader = context.getTypeConverter().mandatoryConvertTo(XMLStreamReader.class, TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM);
output = new ByteArrayOutputStream();
// ensure UTF-8 encoding
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.name());
writer = context.getTypeConverter().mandatoryConvertTo(XMLStreamWriter.class, exchange, output);
// copy to writer
while (reader.hasNext()) {
reader.next();
switch(reader.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
writer.writeStartDocument();
break;
case XMLStreamConstants.END_DOCUMENT:
writer.writeEndDocument();
break;
case XMLStreamConstants.START_ELEMENT:
writer.writeStartElement(reader.getName().getLocalPart());
break;
case XMLStreamConstants.CHARACTERS:
writer.writeCharacters(reader.getText());
break;
case XMLStreamConstants.END_ELEMENT:
writer.writeEndElement();
break;
default:
break;
}
}
} finally {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}
assertNotNull(output);
String result = new String(output.toByteArray(), UTF_8.name());
assertEquals(TEST_XML, result);
}
Aggregations