Skip to content

Commit

Permalink
Merge pull request #4 from Edirom/feature/validation
Browse files Browse the repository at this point in the history
enable validation for all MEI versions and customizations in 4.0.1 us…
  • Loading branch information
anneferger authored Aug 10, 2022
2 parents 1736e9a + 152ec13 commit ae3386e
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 7 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.edirom.meigarage</groupId>
<artifactId>mei-validator</artifactId>
<version>0.2</version>
<version>0.3.0</version>

<name>MEI Validator</name>
<properties>
Expand Down Expand Up @@ -121,6 +121,12 @@
<version>0.5</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.thaiopensource/jing -->
<dependency>
<groupId>com.thaiopensource</groupId>
<artifactId>jing</artifactId>
<version>20091111</version>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/de/edirom/meigarage/MEIValidator.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -66,9 +69,7 @@ public ValidationResult validate(InputStream inputData,
ValidatorException ve = new ValidatorException(ex.getMessage());
ve.setStackTrace(ex.getStackTrace());
throw ve;
}*/

return null;
}

}

Expand Down
91 changes: 91 additions & 0 deletions src/main/java/de/edirom/meigarage/StandardErrorHandler.java
Original file line number Diff line number Diff line change
@@ -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. <br/>
*
* Each object contains one {@link ValidationResult} instance - where
* all received notifications are stored. Depending on received errors
* status of validation result is changed.<br/>
*
* 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;
}

}
40 changes: 39 additions & 1 deletion src/main/java/de/edirom/meigarage/xml/RNGValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -15,13 +22,44 @@ 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;
}

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

}
}
21 changes: 20 additions & 1 deletion src/main/resources/mei-validators.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<validators>
<validator format="MEI 4.0" mimeType="text/xml" rng="https://raw.githubusercontent.com/music-encoding/music-encoding/develop/schemata/mei-all.rng"/>
<validator format="MEI 2.1.1" mimeType="text/xml"
rng="https://raw.githubusercontent.com/music-encoding/schema/main/2.1.1/mei-all.rng"/>
<validator format="MEI 3.0.0" mimeType="text/xml"
rng="https://raw.githubusercontent.com/music-encoding/schema/main/3.0.0/mei-all.rng"/>
<validator format="MEI 4.0.0" mimeType="text/xml"
rng="https://raw.githubusercontent.com/music-encoding/schema/main/4.0.0/mei-all.rng"/>
<validator format="MEI 4.0.1" mimeType="text/xml"
rng="https://raw.githubusercontent.com/music-encoding/schema/main/4.0.1/mei-all.rng"/>
<validator format="MEI dev" mimeType="text/xml"
rng="https://raw.githubusercontent.com/music-encoding/schema/main/dev/mei-all.rng"/>
<validator format="MEI 4.0.0" mimeType="text/xml"
rng="https://raw.githubusercontent.com/music-encoding/schema/main/4.0.0/mei-all.rng"/>
<validator format="MEI 4.0.1 all any" mimeType="text/xml"
rng="https://raw.githubusercontent.com/music-encoding/schema/4.0.1/mei-all_anyStart.rng"/>
<validator format="MEI 4.0.1 cmn" mimeType="text/xml"
rng="https://raw.githubusercontent.com/music-encoding/schema/4.0.1/mei-CMN.rng"/>
<validator format="MEI 4.0.1 mensural" mimeType="text/xml"
rng="https://raw.githubusercontent.com/music-encoding/schema/4.0.1/mei-Mensural.rng "/>
<validator format="MEI 4.0.1 neumes" mimeType="text/xml"
rng="https://raw.githubusercontent.com/music-encoding/schema/4.0.1/mei-Neumes.rng"/>
</validators>

0 comments on commit ae3386e

Please sign in to comment.