use of org.codehaus.staxmate.dom.DOMConverter in project midpoint by Evolveum.
the class LegacyValidator method validate.
public void validate(InputStream inputStream, OperationResult validatorResult, String objectResultOperationName) {
DOMConverter domConverter = new DOMConverter();
XMLStreamReader stream;
try {
Map<String, String> rootNamespaceDeclarations = new HashMap<>();
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
stream = xmlInputFactory.createXMLStreamReader(inputStream);
int eventType = stream.nextTag();
if (eventType == XMLStreamConstants.DTD || eventType == XMLStreamConstants.ENTITY_DECLARATION || eventType == XMLStreamConstants.ENTITY_REFERENCE || eventType == XMLStreamConstants.NOTATION_DECLARATION) {
// We do not want those, e.g. we want to void XXE vulnerabilities. Make this check explicit.
throw new SystemException("Use of " + eventType + " in XML is prohibited");
}
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (!QNameUtil.match(stream.getName(), SchemaConstants.C_OBJECTS)) {
// This has to be an import file with a single objects. Try
// to process it.
OperationResult objectResult = validatorResult.createSubresult(objectResultOperationName);
progress++;
objectResult.addContext(OperationResult.CONTEXT_PROGRESS, progress);
EventResult cont;
try {
cont = readFromStreamAndValidate(stream, objectResult, rootNamespaceDeclarations, validatorResult, domConverter);
} catch (RuntimeException e) {
// Make sure that unexpected error is recorded.
objectResult.recordFatalError(e);
throw e;
}
if (!cont.isCont()) {
String message;
if (cont.getReason() != null) {
message = cont.getReason();
} else {
message = "Object validation failed (no reason given)";
}
if (objectResult.isUnknown()) {
objectResult.recordFatalError(message);
}
validatorResult.recordFatalError(message);
return;
}
// return to avoid processing objects in loop
validatorResult.computeStatus("Validation failed", "Validation warnings");
return;
}
// Extract root namespace declarations
for (int i = 0; i < stream.getNamespaceCount(); i++) {
rootNamespaceDeclarations.put(stream.getNamespacePrefix(i), stream.getNamespaceURI(i));
}
} else {
throw new SystemException("StAX Malfunction?");
}
while (stream.hasNext()) {
eventType = stream.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
OperationResult objectResult = validatorResult.createSubresult(objectResultOperationName);
progress++;
objectResult.addContext(OperationResult.CONTEXT_PROGRESS, progress);
EventResult cont;
try {
// Read and validate individual object from the stream
cont = readFromStreamAndValidate(stream, objectResult, rootNamespaceDeclarations, validatorResult, domConverter);
} catch (RuntimeException e) {
if (objectResult.isUnknown()) {
// Make sure that unexpected error is recorded.
objectResult.recordFatalError(e);
}
throw e;
} finally {
objectResult.close();
}
if (objectResult.isError()) {
errors++;
}
objectResult.cleanupResult();
validatorResult.summarize();
if (cont.isStop()) {
if (cont.getReason() != null) {
validatorResult.recordFatalError("Processing has been stopped: " + cont.getReason());
} else {
validatorResult.recordFatalError("Processing has been stopped");
}
// processed
return;
}
if (!cont.isCont()) {
if (stopAfterErrors > 0 && errors >= stopAfterErrors) {
if (errors == 1) {
validatorResult.recordFatalError("Stopping on error; " + (progress - errors) + " passed");
} else {
validatorResult.recordFatalError("Too many errors (" + errors + "); " + (progress - errors) + " passed");
}
return;
}
}
}
}
} catch (XMLStreamException ex) {
// validatorResult.recordFatalError("XML parsing error: " +
// ex.getMessage()+" on line "+stream.getLocation().getLineNumber(),ex);
validatorResult.recordFatalError("XML parsing error: " + ex.getMessage(), ex);
if (handler != null) {
handler.handleGlobalError(validatorResult, ex);
}
return;
}
// Error count is sufficient. Detailed messages are in subresults
validatorResult.computeStatus(errors + " errors, " + (progress - errors) + " passed");
}
use of org.codehaus.staxmate.dom.DOMConverter in project uPortal by Jasig.
the class AbstractDom4jImporter method convertToElement.
protected Element convertToElement(Source source) {
if (source instanceof StAXSource) {
final StAXSource staxSource = (StAXSource) source;
final DOMConverter domConverter = new DOMConverter();
final Document document;
try {
XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader();
if (xmlStreamReader == null) {
final XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
xmlStreamReader = new FixedXMLEventStreamReader(xmlEventReader);
}
document = domConverter.buildDocument(xmlStreamReader);
} catch (XMLStreamException e) {
throw new RuntimeException("Failed to parse StAX Reader into Dom4J Element", e);
}
final DOMReader domReader = new DOMReader();
final org.dom4j.Document dom4JDocument = domReader.read(document);
dom4JDocument.setName(source.getSystemId());
return dom4JDocument.getRootElement();
}
throw new IllegalArgumentException("Source of type " + source.getClass() + " is not supported");
}
Aggregations