From 152ec13338eb581a523bf9af79328783a7fa47ae Mon Sep 17 00:00:00 2001 From: Anne Ferger Date: Wed, 10 Aug 2022 10:53:08 +0200 Subject: [PATCH] enable validation for all MEI versions and customizations in 4.0.1 using gihtub rngs --- pom.xml | 8 +- .../de/edirom/meigarage/MEIValidator.java | 9 +- .../meigarage/StandardErrorHandler.java | 91 +++++++++++++++++++ .../de/edirom/meigarage/xml/RNGValidator.java | 40 +++++++- src/main/resources/mei-validators.xml | 21 ++++- 5 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 src/main/java/de/edirom/meigarage/StandardErrorHandler.java diff --git a/pom.xml b/pom.xml index b029e5e..8e25b38 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.edirom.meigarage mei-validator - 0.2 + 0.3.0 MEI Validator @@ -121,6 +121,12 @@ 0.5 compile + + + com.thaiopensource + jing + 20091111 + diff --git a/src/main/java/de/edirom/meigarage/MEIValidator.java b/src/main/java/de/edirom/meigarage/MEIValidator.java index c29658f..6ad9310 100644 --- a/src/main/java/de/edirom/meigarage/MEIValidator.java +++ b/src/main/java/de/edirom/meigarage/MEIValidator.java @@ -1,12 +1,16 @@ package de.edirom.meigarage; +import de.edirom.meigarage.xml.XmlValidator; import org.apache.log4j.Logger; +import org.jdom2.JDOMException; +import org.xml.sax.SAXParseException; import pl.psnc.dl.ege.component.Validator; import pl.psnc.dl.ege.exception.EGEException; import pl.psnc.dl.ege.exception.ValidatorException; import pl.psnc.dl.ege.types.DataType; import pl.psnc.dl.ege.types.ValidationResult; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -38,7 +42,6 @@ public ValidationResult validate(InputStream inputData, throws IOException, ValidatorException, EGEException { checkIfSupported(inputDataType); - /* XmlValidator validator = (XmlValidator)provider.getValidator(inputDataType); try { StandardErrorHandler seh = new StandardErrorHandler(); @@ -66,9 +69,7 @@ public ValidationResult validate(InputStream inputData, ValidatorException ve = new ValidatorException(ex.getMessage()); ve.setStackTrace(ex.getStackTrace()); throw ve; - }*/ - - return null; + } } diff --git a/src/main/java/de/edirom/meigarage/StandardErrorHandler.java b/src/main/java/de/edirom/meigarage/StandardErrorHandler.java new file mode 100644 index 0000000..1c58945 --- /dev/null +++ b/src/main/java/de/edirom/meigarage/StandardErrorHandler.java @@ -0,0 +1,91 @@ +package de.edirom.meigarage; + +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import pl.psnc.dl.ege.types.ValidationResult; + +/** + * Catches errors received during XML validation process + * of SAX parser.
+ * + * Each object contains one {@link ValidationResult} instance - where + * all received notifications are stored. Depending on received errors + * status of validation result is changed.
+ * + * After validation, validation result instance can be retrieved in order + * to read status and contained messages. + * + * @author mariuszs + * + */ +public class StandardErrorHandler + implements ErrorHandler +{ + + private final ValidationResult valResult; + + /** + * Constructs error handler with validation result of SUCCESS status. + */ + public StandardErrorHandler(){ + valResult = new ValidationResult(ValidationResult.Status.SUCCESS); + } + + /** + * Constructs error handler with specified validation result instance. + * If referenced valResult parameter is 'null' a default instance is created. + * + * @param valResult + */ + public StandardErrorHandler(ValidationResult valResult){ + if(valResult == null){ + valResult = new ValidationResult(ValidationResult.Status.SUCCESS); + } + this.valResult = valResult; + } + + public void error(SAXParseException exception) + throws SAXException + { + valResult.putMessage( + "Error in line (" + exception.getLineNumber() + "), column (" + + exception.getColumnNumber() + ") : " + + exception.getMessage()); + if(!valResult.getStatus().equals(ValidationResult.Status.FATAL)){ + valResult.setStatus(ValidationResult.Status.ERROR); + } + } + + + public void fatalError(SAXParseException exception) + throws SAXException + { + valResult.putMessage( + "Fatal error! in line (" + exception.getLineNumber() + + "), column (" + exception.getColumnNumber() + ") : " + + exception.getMessage()); + valResult.setStatus(ValidationResult.Status.FATAL); + } + + + public void warning(SAXParseException exception) + throws SAXException + { + valResult.putMessage( + "Warning in line (" + exception.getLineNumber() + "), column (" + + exception.getColumnNumber() + ") : " + + exception.getMessage()); + } + + /** + * Returns contained validation result instance. + * + * @return + */ + public ValidationResult getValidationResult() + { + return valResult; + } + +} diff --git a/src/main/java/de/edirom/meigarage/xml/RNGValidator.java b/src/main/java/de/edirom/meigarage/xml/RNGValidator.java index 34c4d58..3685eb8 100644 --- a/src/main/java/de/edirom/meigarage/xml/RNGValidator.java +++ b/src/main/java/de/edirom/meigarage/xml/RNGValidator.java @@ -7,6 +7,13 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.net.URL; + +import javax.xml.XMLConstants; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; public class RNGValidator implements XmlValidator { @@ -15,7 +22,7 @@ public class RNGValidator implements XmlValidator { private final String schemeUrl; public RNGValidator(String schemeUrl) { - if(schemeUrl == null){ + if (schemeUrl == null) { throw new IllegalArgumentException(); } this.schemeUrl = schemeUrl; @@ -23,5 +30,36 @@ public RNGValidator(String schemeUrl) { public void validateXml(InputStream inputData, ErrorHandler errorHandler) throws SAXException, FileNotFoundException, IOException, Exception { + try { + // Specify you want a factory for RELAX NG + System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.XMLSyntaxSchemaFactory"); + SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI); + LOGGER.debug("Uses xml constant : " + XMLConstants.RELAXNG_NS_URI); + URL schemaURL = new URL(schemeUrl); + //try to download schema by external URL + InputStream urlStream = null; + try{ + urlStream = schemaURL.openStream(); + }catch(IOException ex){ + throw ex; + } + LOGGER.debug("Uses schema url : " + schemeUrl); + StreamSource sss = new StreamSource(urlStream); + LOGGER.debug("Uses schema source : " + sss); + LOGGER.debug(schemaURL); + // Load the specific schema you want. + // Compile the schema. + Schema schema = factory.newSchema(sss); + // Get a validator from the schema. + Validator validator = schema.newValidator(); + validator.setErrorHandler(errorHandler); + LOGGER.debug("Uses validator : " + validator); + StreamSource ssi = new StreamSource(inputData); + validator.validate(ssi); + LOGGER.info("Inputfile is valid."); + } catch (SAXException ex) { + throw new SAXException(ex); + } + } } diff --git a/src/main/resources/mei-validators.xml b/src/main/resources/mei-validators.xml index 69d7fd0..4a983e0 100644 --- a/src/main/resources/mei-validators.xml +++ b/src/main/resources/mei-validators.xml @@ -1,4 +1,23 @@ - + + + + + + + + + +