use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class ModelHelper method modelToXml.
private static <T extends NamedNode> T modelToXml(CamelContext context, InputStream is, String xml, Class<T> type) throws JAXBException {
JAXBContext jaxbContext = getJAXBContext(context);
XmlConverter xmlConverter = newXmlConverter(context);
Document dom = null;
try {
if (is != null) {
dom = xmlConverter.toDOMDocument(is, null);
} else if (xml != null) {
dom = xmlConverter.toDOMDocument(xml, null);
}
} catch (Exception e) {
throw new TypeConversionException(xml, Document.class, e);
}
if (dom == null) {
throw new IllegalArgumentException("InputStream and XML is both null");
}
Map<String, String> namespaces = new LinkedHashMap<>();
extractNamespaces(dom, namespaces);
Binder<Node> binder = jaxbContext.createBinder();
Object result = binder.unmarshal(dom);
if (result == null) {
throw new JAXBException("Cannot unmarshal to " + type + " using JAXB");
}
// Restore namespaces to anything that's NamespaceAware
if (result instanceof RoutesDefinition) {
List<RouteDefinition> routes = ((RoutesDefinition) result).getRoutes();
for (RouteDefinition route : routes) {
applyNamespaces(route, namespaces);
}
} else if (result instanceof RouteDefinition) {
RouteDefinition route = (RouteDefinition) result;
applyNamespaces(route, namespaces);
}
return type.cast(result);
}
use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class CxfConsumerProviderTest method createRouteBuilder.
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
errorHandler(noErrorHandler());
from(getFromEndpointUri()).process(new Processor() {
public void process(final Exchange exchange) {
Message in = exchange.getIn();
Node node = in.getBody(Node.class);
assertNotNull(node);
XmlConverter xmlConverter = new XmlConverter();
// Put the result back
exchange.getOut().setBody(xmlConverter.toSource(RESPONSE));
}
});
}
};
}
use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class XsltBuilderTest method testXsltSetConverter.
public void testXsltSetConverter() throws Exception {
URL styleSheet = getClass().getResource("example.xsl");
XsltBuilder builder = XsltBuilder.xslt(styleSheet);
XmlConverter converter = new XmlConverter();
builder.setConverter(converter);
assertSame(converter, builder.getConverter());
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setBody("<hello>world!</hello>");
builder.process(exchange);
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>", exchange.getOut().getBody());
}
use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class MyCxfCustomerConverter method cxfPayloadToString.
@Converter
public static String cxfPayloadToString(final CxfPayload<?> payload) {
XmlConverter converter = new XmlConverter();
StringBuilder buf = new StringBuilder();
for (Object element : payload.getBody()) {
String elementString = "";
try {
elementString = converter.toString((Element) element, null);
} catch (TransformerException e) {
elementString = element.toString();
}
buf.append(elementString);
}
return buf.toString();
}
Aggregations