Skip to content
Snippets Groups Projects
Commit 9186e035 authored by Andreas Romeyke's avatar Andreas Romeyke
Browse files

- added errorhandler

parent 70e3e6af
No related branches found
No related tags found
No related merge requests found
......@@ -2,8 +2,10 @@ package org.slub.rosetta.dps.repository.plugin.Validation;
import org.apache.xerces.jaxp.validation.XMLSchemaFactory;
import org.slub.rosetta.dps.repository.plugin.ValidationResourceResolver;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.XMLConstants;
import javax.xml.transform.dom.DOMSource;
......@@ -16,6 +18,7 @@ import java.util.ArrayList;
import java.util.List;
public class SLUBValidateSchema {
private boolean is_valid = true;
private final List<String> errors = new ArrayList<>();
public List<String> get_errors() {
return errors;
......@@ -24,14 +27,33 @@ public class SLUBValidateSchema {
public SLUBValidateSchema(ValidationResourceResolver validationResourceResolver) {
this.validationResourceResolver = validationResourceResolver;
}
public boolean validateAgainst(Document docvalidate, URI xsdUri) throws IOException {
private final ErrorHandler validationErrorHandler = new ErrorHandler() {
@Override
public void warning(SAXParseException e) {
errors.add("[WW] "+ e.getMessage());
}
@Override
public void error(SAXParseException e) {
is_valid = false;
errors.add("[EE] "+e.getMessage() + " line=" + e.getLineNumber() + " col=" + e.getColumnNumber());
}
@Override
public void fatalError(SAXParseException e) {
is_valid = false;
errors.add("[EE] Fatal, "+e.getMessage() + " line=" + e.getLineNumber() + " col=" + e.getColumnNumber());
}
};
public boolean validateAgainst(Node docvalidate, URI xsdUri) throws IOException {
boolean isValidXml = false;
try {
SchemaFactory sf = XMLSchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
sf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
sf.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
sf.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true);
sf.setResourceResolver( validationResourceResolver );
Schema s = sf.newSchema( xsdUri.toURL());
Validator v = s.newValidator();
DOMSource src = new DOMSource(docvalidate);
v.validate(src);
......@@ -48,4 +70,5 @@ public class SLUBValidateSchema {
}
return isValidXml;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment