use of org.apache.cxf.staxutils.validation.WoodstoxValidationImpl in project cxf by apache.
the class XMLStreamDataReader method validate.
private Element validate(XMLStreamReader input) throws XMLStreamException, IOException {
DOMSource ds = read(input);
Element rootElement = null;
if (ds.getNode() instanceof Document) {
rootElement = ((Document) ds.getNode()).getDocumentElement();
} else {
rootElement = (Element) ds.getNode();
}
WoodstoxValidationImpl impl = new WoodstoxValidationImpl();
XMLStreamWriter nullWriter = null;
if (impl.canValidate()) {
nullWriter = StaxUtils.createXMLStreamWriter(new NUllOutputStream());
impl.setupValidation(nullWriter, message.getExchange().getEndpoint(), message.getExchange().getService().getServiceInfos().get(0));
}
// check if the impl can still validate after the setup, possible issue loading schemas or similar
if (impl.canValidate()) {
// Can use the MSV libs and woodstox to handle the schema validation during
// parsing and processing. Much faster and single traversal
// filter xop node
XMLStreamReader reader = StaxUtils.createXMLStreamReader(ds);
XMLStreamReader filteredReader = StaxUtils.createFilteredReader(reader, new StaxStreamFilter(new QName[] { XOP }));
StaxUtils.copy(filteredReader, nullWriter);
} else {
// MSV not available, use a slower method of cloning the data, replace the xop's, validate
LOG.fine("NO_MSV_AVAILABLE");
Element newElement = rootElement;
if (DOMUtils.hasElementWithName(rootElement, "http://www.w3.org/2004/08/xop/include", "Include")) {
newElement = (Element) rootElement.cloneNode(true);
List<Element> elems = DOMUtils.findAllElementsByTagNameNS(newElement, "http://www.w3.org/2004/08/xop/include", "Include");
for (Element include : elems) {
Node parentNode = include.getParentNode();
parentNode.removeChild(include);
String cid = DOMUtils.getAttribute(include, "href");
// set the fake base64Binary to validate instead of reading the attachment from message
parentNode.setTextContent(javax.xml.bind.DatatypeConverter.printBase64Binary(cid.getBytes()));
}
}
try {
schema.newValidator().validate(new DOMSource(newElement));
} catch (SAXException e) {
throw new XMLStreamException(e.getMessage(), e);
}
}
return rootElement;
}
Aggregations