/** * @param message * the message to be validated * @param schemaResource * the XML schema to validate against * @return an array of validation exceptions */ protected List<ValidationException> doValidate(Source message, String schemaResource) { try { LOG.debug("Validating XML message"); Schema schema = resource(schemaResource); javax.xml.validation.Validator validator = schema.newValidator(); CollectingErrorHandler errorHandler = new CollectingErrorHandler(); validator.setErrorHandler(errorHandler); validator.validate(message); List<ValidationException> exceptions = errorHandler.getExceptions(); if (! exceptions.isEmpty()) { LOG.debug("Message validation found {} problems", exceptions.size()); } else { LOG.debug("Message validation successful"); } return exceptions; } catch (Exception e) { return Collections.singletonList(new ValidationException( "Unexpected validation failure because " + e.getMessage(), e)); } }
@Override public void error(SAXParseException exception) throws SAXException { add(exception); }
@Override public void fatalError(SAXParseException exception) throws SAXException { add(exception); }