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;
}
Aggregations