Search in sources :

Example 1 with DOMConverter

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");
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) SystemException(com.evolveum.midpoint.util.exception.SystemException) XMLStreamException(javax.xml.stream.XMLStreamException) HashMap(java.util.HashMap) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) DOMConverter(org.codehaus.staxmate.dom.DOMConverter) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with DOMConverter

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");
}
Also used : DOMReader(org.dom4j.io.DOMReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) FixedXMLEventStreamReader(org.springframework.util.xml.FixedXMLEventStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) DOMConverter(org.codehaus.staxmate.dom.DOMConverter)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 DOMConverter (org.codehaus.staxmate.dom.DOMConverter)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 HashMap (java.util.HashMap)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 StAXSource (javax.xml.transform.stax.StAXSource)1 DOMReader (org.dom4j.io.DOMReader)1 FixedXMLEventStreamReader (org.springframework.util.xml.FixedXMLEventStreamReader)1 Document (org.w3c.dom.Document)1