Search in sources :

Example 1 with XmlValidationError

use of org.apache.xmlbeans.XmlValidationError in project arctic-sea by 52North.

the class XmlHelper method validateDocument.

/**
 * checks whether the XMLDocument is valid
 *
 * @param doc
 *            the document which should be checked
 *
 * @throws T
 *             * if the Document is not valid
 */
/*
     * TODO Replace this version with a method that uses LaxValidationCases and
     * provides means to access the errors after validating the document
     */
public static <X extends XmlObject, T extends Throwable> X validateDocument(X doc, Function<Throwable, T> supplier) throws T {
    // Create an XmlOptions instance and set the error listener.
    LinkedList<XmlError> validationErrors = new LinkedList<>();
    XmlOptions validationOptions = new XmlOptions().setErrorListener(validationErrors).setLoadLineNumbers(XmlOptions.LOAD_LINE_NUMBERS_END_ELEMENT);
    // Create Exception with error message if the xml document is invalid
    if (!doc.validate(validationOptions)) {
        String message;
        // getValidation error and throw service exception for the first
        // error
        Iterator<XmlError> iter = validationErrors.iterator();
        List<XmlError> errors = new LinkedList<>();
        while (iter.hasNext()) {
            XmlError error = iter.next();
            boolean shouldPass = false;
            if (error instanceof XmlValidationError) {
                for (LaxValidationCase lvc : LaxValidationCase.values()) {
                    if (lvc.shouldPass((XmlValidationError) error)) {
                        shouldPass = true;
                        LOGGER.debug("Lax validation case found for XML validation error: {}", error);
                        break;
                    }
                }
            }
            if (!shouldPass) {
                errors.add(error);
            }
        }
        CompositeException exceptions = new CompositeException();
        for (XmlError error : errors) {
            // get name of the missing or invalid parameter
            message = error.getMessage();
            if (message != null) {
                exceptions.add(new DecodingException(message, "[XmlBeans validation error:] %s", message));
            }
        }
        if (!errors.isEmpty()) {
            throw supplier.apply(exceptions);
        }
    }
    return doc;
}
Also used : XmlOptions(org.apache.xmlbeans.XmlOptions) CompositeException(org.n52.janmayen.exception.CompositeException) XmlError(org.apache.xmlbeans.XmlError) DecodingException(org.n52.svalbard.decode.exception.DecodingException) LinkedList(java.util.LinkedList) XmlValidationError(org.apache.xmlbeans.XmlValidationError)

Aggregations

LinkedList (java.util.LinkedList)1 XmlError (org.apache.xmlbeans.XmlError)1 XmlOptions (org.apache.xmlbeans.XmlOptions)1 XmlValidationError (org.apache.xmlbeans.XmlValidationError)1 CompositeException (org.n52.janmayen.exception.CompositeException)1 DecodingException (org.n52.svalbard.decode.exception.DecodingException)1