use of javax.xml.transform.sax.SAXSource in project camel by apache.
the class CachedCxfPayloadTest method testCachedCxfPayloadSAXSource.
@Test
public void testCachedCxfPayloadSAXSource() throws TypeConversionException, NoTypeConversionAvailableException, IOException {
SAXSource source = context.getTypeConverter().mandatoryConvertTo(SAXSource.class, PAYLOAD);
// this conversion uses org.apache.camel.converter.jaxp.XmlConverter.toDOMNodeFromSAX which uses Transformer
// to convert SAXSource to DOM. This conversion preserves the content but loses its original representation.
doTest(source, PAYLOAD_AMPED);
}
use of javax.xml.transform.sax.SAXSource in project camel by apache.
the class XsltBuilderTest method testXsltTemplates.
public void testXsltTemplates() throws Exception {
File file = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl");
Source source = new SAXSource(new InputSource(new FileInputStream(file)));
XmlConverter converter = new XmlConverter();
Templates styleSheet = converter.getTransformerFactory().newTemplates(source);
XsltBuilder builder = XsltBuilder.xslt(styleSheet);
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 javax.xml.transform.sax.SAXSource in project camel by apache.
the class StreamCacheConverterTest method testConvertToStreamCache.
public void testConvertToStreamCache() throws Exception {
context.start();
ByteArrayInputStream inputStream = new ByteArrayInputStream(MESSAGE.getBytes());
StreamCache streamCache = StreamCacheConverter.convertToStreamCache(new SAXSource(new InputSource(inputStream)), exchange);
String message = exchange.getContext().getTypeConverter().convertTo(String.class, streamCache);
assertNotNull(message);
assertEquals("The converted message is wrong", MESSAGE, message);
}
use of javax.xml.transform.sax.SAXSource in project camel by apache.
the class ValidatingProcessor method doProcess.
protected void doProcess(Exchange exchange) throws Exception {
Schema schema;
if (isUseSharedSchema()) {
schema = getSchema();
} else {
schema = createSchema();
}
Validator validator = schema.newValidator();
// the underlying input stream, which we need to close to avoid locking files or other resources
Source source = null;
InputStream is = null;
try {
Result result = null;
// only convert to input stream if really needed
if (isInputStreamNeeded(exchange)) {
is = getContentToValidate(exchange, InputStream.class);
if (is != null) {
source = getSource(exchange, is);
}
} else {
Object content = getContentToValidate(exchange);
if (content != null) {
source = getSource(exchange, content);
}
}
if (shouldUseHeader()) {
if (source == null && isFailOnNullHeader()) {
throw new NoXmlHeaderValidationException(exchange, headerName);
}
} else {
if (source == null && isFailOnNullBody()) {
throw new NoXmlBodyValidationException(exchange);
}
}
//CAMEL-7036 We don't need to set the result if the source is an instance of StreamSource
if (source instanceof DOMSource) {
result = new DOMResult();
} else if (source instanceof SAXSource) {
result = new SAXResult();
} else if (source instanceof StAXSource || source instanceof StreamSource) {
result = null;
}
if (source != null) {
// create a new errorHandler and set it on the validator
// must be a local instance to avoid problems with concurrency (to be
// thread safe)
ValidatorErrorHandler handler = errorHandler.getClass().newInstance();
validator.setErrorHandler(handler);
try {
LOG.trace("Validating {}", source);
validator.validate(source, result);
handler.handleErrors(exchange, schema, result);
} catch (SAXParseException e) {
// can be thrown for non well formed XML
throw new SchemaValidationException(exchange, schema, Collections.singletonList(e), Collections.<SAXParseException>emptyList(), Collections.<SAXParseException>emptyList());
}
}
} finally {
IOHelper.close(is);
}
}
use of javax.xml.transform.sax.SAXSource in project camel by apache.
the class XmlConverter method toSAXSourceFromStAX.
@Converter
public SAXSource toSAXSourceFromStAX(StAXSource source, Exchange exchange) throws TransformerException {
String str = toString(source, exchange);
StringReader reader = new StringReader(str);
return new SAXSource(new InputSource(reader));
}
Aggregations