use of javax.xml.transform.stax.StAXSource in project camel by apache.
the class XmlConverterTest method testToStreamSourceByStAXSource.
public void testToStreamSourceByStAXSource() throws Exception {
XmlConverter conv = new XmlConverter();
StAXSource source = conv.toStAXSource("<foo>bar</foo>", null);
StreamSource out = conv.toStreamSource(source, null);
assertNotSame(source, out);
assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
use of javax.xml.transform.stax.StAXSource in project camel by apache.
the class XmlConverterTest method testToStAXSourceByInputStream.
public void testToStAXSourceByInputStream() throws Exception {
XmlConverter conv = new XmlConverter();
InputStream is = context.getTypeConverter().convertTo(InputStream.class, "<foo>bar</foo>");
StAXSource out = conv.toStAXSource(is, null);
assertNotNull(out);
assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
use of javax.xml.transform.stax.StAXSource in project camel by apache.
the class CxfPayloadConverter method convertTo.
@SuppressWarnings("unchecked")
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
// CxfPayloads from other types
if (type.isAssignableFrom(CxfPayload.class)) {
try {
if (!value.getClass().isArray()) {
Source src = null;
// directly
if (value instanceof InputStream) {
src = new StreamSource((InputStream) value);
} else if (value instanceof Reader) {
src = new StreamSource((Reader) value);
} else if (value instanceof String) {
src = new StreamSource(new StringReader((String) value));
} else if (value instanceof Node) {
src = new DOMSource((Node) value);
} else if (value instanceof Source) {
src = (Source) value;
}
if (src == null) {
// assuming staxsource is preferred, otherwise use the
// one preferred
TypeConverter tc = registry.lookup(javax.xml.transform.stax.StAXSource.class, value.getClass());
if (tc == null) {
tc = registry.lookup(Source.class, value.getClass());
}
if (tc != null) {
src = tc.convertTo(Source.class, exchange, value);
}
}
if (src != null) {
return (T) sourceToCxfPayload(src, exchange);
}
}
TypeConverter tc = registry.lookup(NodeList.class, value.getClass());
if (tc != null) {
NodeList nodeList = tc.convertTo(NodeList.class, exchange, value);
return (T) nodeListToCxfPayload(nodeList, exchange);
}
tc = registry.lookup(Document.class, value.getClass());
if (tc != null) {
Document document = tc.convertTo(Document.class, exchange, value);
return (T) documentToCxfPayload(document, exchange);
}
// maybe we can convert via an InputStream
CxfPayload<?> p;
p = convertVia(InputStream.class, exchange, value, registry);
if (p != null) {
return (T) p;
}
// String is the converter of last resort
p = convertVia(String.class, exchange, value, registry);
if (p != null) {
return (T) p;
}
} catch (RuntimeCamelException e) {
// the internal conversion to XML can throw an exception if the content is not XML
// ignore this and return Void.TYPE to indicate that we cannot convert this
}
// no we could not do it currently
return (T) Void.TYPE;
}
// Convert a CxfPayload into something else
if (CxfPayload.class.isAssignableFrom(value.getClass())) {
CxfPayload<?> payload = (CxfPayload<?>) value;
int size = payload.getBodySources().size();
if (size == 1) {
if (type.isAssignableFrom(Document.class)) {
Source s = payload.getBodySources().get(0);
Document d;
try {
d = StaxUtils.read(s);
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
return type.cast(d);
}
// CAMEL-8410 Just make sure we get the Source object directly from the payload body source
Source s = payload.getBodySources().get(0);
if (type.isInstance(s)) {
return type.cast(s);
}
TypeConverter tc = registry.lookup(type, XMLStreamReader.class);
if (tc != null && (s instanceof StaxSource || s instanceof StAXSource)) {
XMLStreamReader r = (s instanceof StAXSource) ? ((StAXSource) s).getXMLStreamReader() : ((StaxSource) s).getXMLStreamReader();
if (payload.getNsMap() != null) {
r = new DelegatingXMLStreamReader(r, payload.getNsMap());
}
return tc.convertTo(type, exchange, r);
}
tc = registry.lookup(type, Source.class);
if (tc != null) {
XMLStreamReader r = null;
if (payload.getNsMap() != null) {
if (s instanceof StaxSource) {
r = ((StaxSource) s).getXMLStreamReader();
} else if (s instanceof StAXSource) {
r = ((StAXSource) s).getXMLStreamReader();
}
if (r != null) {
s = new StAXSource(new DelegatingXMLStreamReader(r, payload.getNsMap()));
}
}
return tc.convertTo(type, exchange, s);
}
}
TypeConverter tc = registry.lookup(type, NodeList.class);
if (tc != null) {
Object result = tc.convertTo(type, exchange, cxfPayloadToNodeList((CxfPayload<?>) value, exchange));
if (result == null) {
// no we could not do it currently, and we just abort the convert here
return (T) Void.TYPE;
} else {
return (T) result;
}
}
// we cannot convert a node list, so we try the first item from the
// node list
tc = registry.lookup(type, Node.class);
if (tc != null) {
NodeList nodeList = cxfPayloadToNodeList((CxfPayload<?>) value, exchange);
if (nodeList.getLength() > 0) {
return tc.convertTo(type, exchange, nodeList.item(0));
} else {
// no we could not do it currently
return (T) Void.TYPE;
}
} else {
if (size == 0) {
// empty size so we cannot convert
return (T) Void.TYPE;
}
}
}
return null;
}
use of javax.xml.transform.stax.StAXSource in project camel by apache.
the class CxfPayloadConverterTest method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
File file = new File("src/test/resources/org/apache/camel/component/cxf/converter/test.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
document = documentBuilder.parse(file);
document.getDocumentElement().normalize();
List<Source> body = new ArrayList<Source>();
body.add(new DOMSource(document.getDocumentElement()));
List<Source> staxbody = new ArrayList<Source>();
staxbody.add(new StAXSource(StaxUtils.createXMLStreamReader(new FileInputStream(file), "utf-8")));
payload = new CxfPayload<String[]>(new ArrayList<String[]>(), body, null);
emptyPayload = new CxfPayload<String[]>(new ArrayList<String[]>(), new ArrayList<Source>(), null);
staxpayload = new CxfPayload<String[]>(new ArrayList<String[]>(), staxbody, null);
inputStream = new FileInputStream(file);
}
use of javax.xml.transform.stax.StAXSource in project hale by halestudio.
the class JsonXML method toJson.
public static void toJson(Reader xmlReader, Writer jsonWriter) throws XMLStreamException, FactoryConfigurationError, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError {
/*
* If we want to insert JSON array boundaries for multiple elements, we
* need to set the <code>autoArray</code> property. If our XML source
* was decorated with <code><?xml-multiple?></code> processing
* instructions, we'd set the <code>multiplePI</code> property instead.
* With the <code>autoPrimitive</code> property set, element text gets
* automatically converted to JSON primitives (number, boolean, null).
*/
JsonXMLConfig config = new JsonXMLConfigBuilder().namespaceDeclarations(true).autoArray(true).autoPrimitive(true).prettyPrint(false).build();
/*
* Create source (XML).
*/
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(xmlReader);
Source source = new StAXSource(reader);
/*
* Create result (JSON).
*/
// create stream factory manually due to class loading issues
XMLStreamWriter writer = new JsonXMLOutputFactory(config, new JsonStreamFactoryImpl()).createXMLStreamWriter(jsonWriter);
Result result = new StAXResult(writer);
/*
* Copy source to result via "identity transform".
*/
TransformerFactory.newInstance().newTransformer().transform(source, result);
}
Aggregations