use of javax.xml.transform.dom.DOMSource 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.dom.DOMSource in project camel by apache.
the class XmlConverter method toDOMSource.
@Converter
public DOMSource toDOMSource(InputStream is, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
InputSource source = new InputSource(is);
String systemId = source.getSystemId();
DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
Document document = builder.parse(source);
return new DOMSource(document, systemId);
}
use of javax.xml.transform.dom.DOMSource in project camel by apache.
the class XmlConverter method toDOMSourceFromStream.
@Converter
public DOMSource toDOMSourceFromStream(StreamSource source, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
Document document;
String systemId = source.getSystemId();
DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
Reader reader = source.getReader();
if (reader != null) {
document = builder.parse(new InputSource(reader));
} else {
InputStream inputStream = source.getInputStream();
if (inputStream != null) {
InputSource inputsource = new InputSource(inputStream);
inputsource.setSystemId(systemId);
document = builder.parse(inputsource);
} else {
throw new IOException("No input stream or reader available on StreamSource: " + source);
}
}
return new DOMSource(document, systemId);
}
use of javax.xml.transform.dom.DOMSource in project camel by apache.
the class XmlConverterTest method testToDomSourceBySaxSource.
public void testToDomSourceBySaxSource() throws Exception {
XmlConverter conv = new XmlConverter();
SAXSource source = conv.toSAXSource("<foo>bar</foo>", null);
DOMSource out = conv.toDOMSource(source);
assertNotSame(source, out);
assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
use of javax.xml.transform.dom.DOMSource in project camel by apache.
the class XmlConverterTest method testToDomSourceByDomSource.
public void testToDomSourceByDomSource() throws Exception {
XmlConverter conv = new XmlConverter();
DOMSource source = conv.toDOMSource("<foo>bar</foo>");
DOMSource out = conv.toDOMSource(source);
assertSame(source, out);
}
Aggregations