Commit 62b9be7d authored by Dr. Martin Goik's avatar Dr. Martin Goik

Parser XSD 1.1 conformant

parent b3105ea6
......@@ -16,6 +16,13 @@
</properties>
<dependencies>
<dependency>
<groupId>org.opengis.cite.xerces</groupId>
<artifactId>xercesImpl-xsd11</artifactId>
<version>2.12-beta-r1667115</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
......@@ -58,7 +65,6 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
......
......@@ -42,7 +42,7 @@ public class InstanceSetEvaluation {
* @param args unused
*/
public static void main(String[] args) {
final InstanceSetEvaluation ise = new InstanceSetEvaluation("SchemaTest");
final InstanceSetEvaluation ise = new InstanceSetEvaluation("SchemaTest", "Schema/dictionary.xsd");
System.out.println(ise.getMessages());
System.out.println("All tests succeeded:" + ise.allTestsSucceeded);
}
......@@ -65,14 +65,14 @@ public class InstanceSetEvaluation {
*
* @param xmlTestFileDir Directory containing XML instances to be processed.
*/
public InstanceSetEvaluation(final String xmlTestFileDir) {
public InstanceSetEvaluation(final String xmlTestFileDir, final String xsdSchemaFilename) {
this.xmlTestFileDir = xmlTestFileDir;
final File rootDirectory = new File(xmlTestFileDir);
for (final File f: rootDirectory.listFiles(
path -> path.getPath().endsWith(".xml"))) {
readTestHeader(f);
readTestHeader(f, xsdSchemaFilename);
}
tests.forEach(t -> messages.append(t + "\n"));
......@@ -105,7 +105,7 @@ public class InstanceSetEvaluation {
void readTestHeader(final File instanceFilename) {
void readTestHeader(final File instanceFilename, final String xsdSchemaFilename) {
final SAXBuilder metainfoParser = new SAXBuilder(XMLReaders.NONVALIDATING);
try {
......@@ -123,7 +123,7 @@ public class InstanceSetEvaluation {
log.info("No 'xmlTest PI found, possible dependency file");
break;
case 1:
tests.add(new InstanceTest(xmlTestList.get(0), xmlTestFileDir, instanceFilename));
tests.add(new InstanceTest(xmlTestList.get(0), xmlTestFileDir, xsdSchemaFilename, instanceFilename));
break;
default:
log.error("Fatal: Found " + xmlTestList.size() +
......
......@@ -4,23 +4,30 @@ import java.io.File;
import java.io.IOException;
import java.util.Optional;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jdom2.JDOMException;
import org.jdom2.ProcessingInstruction;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaderSAX2Factory;
import org.jdom2.input.sax.XMLReaders;
import org.xml.sax.SAXException;
/**
* Test and evaluate an individual XML instance.
*
*/
public class InstanceTest {
final SAXBuilder parser = new SAXBuilder(XMLReaders.XSDVALIDATING);
final SAXBuilder b = new SAXBuilder(new XMLReaderSAX2Factory(false));
final File instanceFilename;
/**
* An examinee may reach this number of point. The value is being defined in
* the corresponding XML instance's PI like e.g.:
......@@ -30,7 +37,7 @@ public class InstanceTest {
*
*/
public final int reachablePoints;
/**
* Is this instance expected to be valid or not? The value is being defined in
* the corresponding XML instance's PI like e.g.:
......@@ -42,13 +49,13 @@ public class InstanceTest {
* Does the current instance test succeed?
*/
public final boolean testSucceeded;
/**
* Error message in case of unexpected behavior e.g.
* when an invalid instance is supposed to be valid.
*/
public final Optional<String> errMsg;
private static Logger log = LogManager.getLogger(InstanceTest.class);
@Override
......@@ -69,65 +76,87 @@ public class InstanceTest {
* @param xmlInstance The current instance to be evaluated.
*/
public InstanceTest(final ProcessingInstruction xmlTest,
final String xmlTestFileDir, final File xmlInstance) {
final String xmlTestFileDir, final String xsdSchemaFilename, final File xmlInstance) {
Validator validator = null;
SchemaFactory sf = SchemaFactory.newInstance(
"http://www.w3.org/XML/XMLSchema/v1.1");
Schema s;
try {
s = sf.newSchema (new File(xsdSchemaFilename));
validator = s.newValidator();
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.instanceFilename = xmlInstance;
if (null == xmlTest.getPseudoAttributeValue("points")) {
log.fatal("Mandatory <?xmlTest points='...' ... ?> is missing in file '"
+ xmlInstance + "'");
+ xmlInstance + "'");
System.exit(1);
}
reachablePoints = Integer.parseInt(xmlTest.getPseudoAttributeValue("points"));
if (null == xmlTest.getPseudoAttributeValue("expectedToBeValid")) {
log.fatal("Mandatory <?xmlTest expectedToBeValid='true|false' ... ?> is missing in file '" + xmlInstance + "'");
System.exit(1);
}
expectedToBeValid = Boolean.parseBoolean(xmlTest.getPseudoAttributeValue("expectedToBeValid"));
final Optional<String> preconditionValidFilename;
boolean preconditionSucceeded = false;
String tmpPreconditionErrMsg = null;
if (null == xmlTest.getPseudoAttributeValue("preconditionValid")) {
preconditionValidFilename = Optional.empty();
preconditionValidFilename = Optional.empty();
} else {
preconditionValidFilename = Optional.of(xmlTestFileDir + File.separator +
xmlTest.getPseudoAttributeValue("preconditionValid"));
try {
parser.build(preconditionValidFilename.get());
log.info("Precondition file '" + preconditionValidFilename.get() + "' is valid");
preconditionSucceeded = true;
} catch (final JDOMException | IOException e) {
log.info("Precondition file '" + preconditionValidFilename.get() + "' is invalid or missing:", e);
tmpPreconditionErrMsg = "File '" + preconditionValidFilename.get() + "' is either invalid or missing";
}
preconditionValidFilename = Optional.of(xmlTestFileDir + File.separator +
xmlTest.getPseudoAttributeValue("preconditionValid"));
try {
validator.validate(new StreamSource(new File(preconditionValidFilename.get())));
log.info("Precondition file '" + preconditionValidFilename.get() + "' is valid");
preconditionSucceeded = true;
} catch (final SAXException e) {
tmpPreconditionErrMsg = "Precondition file '" + preconditionValidFilename.get() + "' is invalid:" + e;
log.info(tmpPreconditionErrMsg + ": " + e.getCause() + ":" + e.getMessage(), e);
} catch (final IOException e) {
tmpPreconditionErrMsg = "Precondition file '" + preconditionValidFilename.get() + "' is missing:" + e;
log.info(tmpPreconditionErrMsg, e);
}
}
if (!preconditionValidFilename.isPresent() || preconditionSucceeded) {
boolean tmpInstanceIsValid = false;
try {
parser.build(xmlInstance);
tmpInstanceIsValid = true;
} catch (final JDOMException | IOException e) {
validator.validate(new StreamSource(xmlInstance));
tmpInstanceIsValid = true;
} catch (final SAXException e) {
log.info("Instance file '" + xmlInstance + "' is invalid:", e);
} catch (final IOException e) {
log.info("Instance file '" + xmlInstance + "' cannot be opened:", e);
}
testSucceeded = expectedToBeValid == tmpInstanceIsValid;
if (testSucceeded) {
errMsg = Optional.empty();
} else if (testSucceeded) {
} else if (expectedToBeValid) {
errMsg = Optional.of("Instance '" + xmlInstance +
"' is expected to be invalid!");
"' is expected to be valid!");
} else {
errMsg = Optional.of("Instance '" + xmlInstance +
"' is expected to be valid!");
"' is expected to be invalid!");
}
} else {
testSucceeded = false;
testSucceeded = false;
errMsg = Optional.of(tmpPreconditionErrMsg);
}
}
}
......@@ -15,7 +15,7 @@ public class SchemaTest {
@Test
public void testXmlInstanceSet() {
final InstanceSetEvaluation ise = new InstanceSetEvaluation("SchemaTest");
final InstanceSetEvaluation ise = new InstanceSetEvaluation("SchemaTest", "Schema/dictionary.xsd");
System.out.println(ise.getMessages());
......
......@@ -16,6 +16,13 @@
</properties>
<dependencies>
<dependency>
<groupId>org.opengis.cite.xerces</groupId>
<artifactId>xercesImpl-xsd11</artifactId>
<version>2.12-beta-r1667115</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
......
......@@ -42,7 +42,7 @@ public class InstanceSetEvaluation {
* @param args unused
*/
public static void main(String[] args) {
final InstanceSetEvaluation ise = new InstanceSetEvaluation("SchemaTest");
final InstanceSetEvaluation ise = new InstanceSetEvaluation("SchemaTest", "Schema/dictionary.xsd");
System.out.println(ise.getMessages());
System.out.println("All tests succeeded:" + ise.allTestsSucceeded);
}
......@@ -65,14 +65,14 @@ public class InstanceSetEvaluation {
*
* @param xmlTestFileDir Directory containing XML instances to be processed.
*/
public InstanceSetEvaluation(final String xmlTestFileDir) {
public InstanceSetEvaluation(final String xmlTestFileDir, final String xsdSchemaFilename) {
this.xmlTestFileDir = xmlTestFileDir;
final File rootDirectory = new File(xmlTestFileDir);
for (final File f: rootDirectory.listFiles(
path -> path.getPath().endsWith(".xml"))) {
readTestHeader(f);
readTestHeader(f, xsdSchemaFilename);
}
tests.forEach(t -> messages.append(t + "\n"));
......@@ -105,7 +105,7 @@ public class InstanceSetEvaluation {
void readTestHeader(final File instanceFilename) {
void readTestHeader(final File instanceFilename, final String xsdSchemaFilename) {
final SAXBuilder metainfoParser = new SAXBuilder(XMLReaders.NONVALIDATING);
try {
......@@ -123,7 +123,7 @@ public class InstanceSetEvaluation {
log.info("No 'xmlTest PI found, possible dependency file");
break;
case 1:
tests.add(new InstanceTest(xmlTestList.get(0), xmlTestFileDir, instanceFilename));
tests.add(new InstanceTest(xmlTestList.get(0), xmlTestFileDir, xsdSchemaFilename, instanceFilename));
break;
default:
log.error("Fatal: Found " + xmlTestList.size() +
......
......@@ -4,23 +4,30 @@ import java.io.File;
import java.io.IOException;
import java.util.Optional;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jdom2.JDOMException;
import org.jdom2.ProcessingInstruction;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaderSAX2Factory;
import org.jdom2.input.sax.XMLReaders;
import org.xml.sax.SAXException;
/**
* Test and evaluate an individual XML instance.
*
*/
public class InstanceTest {
final SAXBuilder parser = new SAXBuilder(XMLReaders.XSDVALIDATING);
final SAXBuilder b = new SAXBuilder(new XMLReaderSAX2Factory(false));
final File instanceFilename;
/**
* An examinee may reach this number of point. The value is being defined in
* the corresponding XML instance's PI like e.g.:
......@@ -30,7 +37,7 @@ public class InstanceTest {
*
*/
public final int reachablePoints;
/**
* Is this instance expected to be valid or not? The value is being defined in
* the corresponding XML instance's PI like e.g.:
......@@ -42,13 +49,13 @@ public class InstanceTest {
* Does the current instance test succeed?
*/
public final boolean testSucceeded;
/**
* Error message in case of unexpected behavior e.g.
* when an invalid instance is supposed to be valid.
*/
public final Optional<String> errMsg;
private static Logger log = LogManager.getLogger(InstanceTest.class);
@Override
......@@ -69,65 +76,87 @@ public class InstanceTest {
* @param xmlInstance The current instance to be evaluated.
*/
public InstanceTest(final ProcessingInstruction xmlTest,
final String xmlTestFileDir, final File xmlInstance) {
final String xmlTestFileDir, final String xsdSchemaFilename, final File xmlInstance) {
Validator validator = null;
SchemaFactory sf = SchemaFactory.newInstance(
"http://www.w3.org/XML/XMLSchema/v1.1");
Schema s;
try {
s = sf.newSchema (new File(xsdSchemaFilename));
validator = s.newValidator();
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.instanceFilename = xmlInstance;
if (null == xmlTest.getPseudoAttributeValue("points")) {
log.fatal("Mandatory <?xmlTest points='...' ... ?> is missing in file '"
+ xmlInstance + "'");
+ xmlInstance + "'");
System.exit(1);
}
reachablePoints = Integer.parseInt(xmlTest.getPseudoAttributeValue("points"));
if (null == xmlTest.getPseudoAttributeValue("expectedToBeValid")) {
log.fatal("Mandatory <?xmlTest expectedToBeValid='true|false' ... ?> is missing in file '" + xmlInstance + "'");
System.exit(1);
}
expectedToBeValid = Boolean.parseBoolean(xmlTest.getPseudoAttributeValue("expectedToBeValid"));
final Optional<String> preconditionValidFilename;
boolean preconditionSucceeded = false;
String tmpPreconditionErrMsg = null;
if (null == xmlTest.getPseudoAttributeValue("preconditionValid")) {
preconditionValidFilename = Optional.empty();
preconditionValidFilename = Optional.empty();
} else {
preconditionValidFilename = Optional.of(xmlTestFileDir + File.separator +
xmlTest.getPseudoAttributeValue("preconditionValid"));
try {
parser.build(preconditionValidFilename.get());
log.info("Precondition file '" + preconditionValidFilename.get() + "' is valid");
preconditionSucceeded = true;
} catch (final JDOMException | IOException e) {
log.info("Precondition file '" + preconditionValidFilename.get() + "' is invalid or missing:", e);
tmpPreconditionErrMsg = "File '" + preconditionValidFilename.get() + "' is either invalid or missing";
}
preconditionValidFilename = Optional.of(xmlTestFileDir + File.separator +
xmlTest.getPseudoAttributeValue("preconditionValid"));
try {
validator.validate(new StreamSource(new File(preconditionValidFilename.get())));
log.info("Precondition file '" + preconditionValidFilename.get() + "' is valid");
preconditionSucceeded = true;
} catch (final SAXException e) {
tmpPreconditionErrMsg = "Precondition file '" + preconditionValidFilename.get() + "' is invalid:" + e;
log.info(tmpPreconditionErrMsg + ": " + e.getCause() + ":" + e.getMessage(), e);
} catch (final IOException e) {
tmpPreconditionErrMsg = "Precondition file '" + preconditionValidFilename.get() + "' is missing:" + e;
log.info(tmpPreconditionErrMsg, e);
}
}
if (!preconditionValidFilename.isPresent() || preconditionSucceeded) {
boolean tmpInstanceIsValid = false;
try {
parser.build(xmlInstance);
tmpInstanceIsValid = true;
} catch (final JDOMException | IOException e) {
validator.validate(new StreamSource(xmlInstance));
tmpInstanceIsValid = true;
} catch (final SAXException e) {
log.info("Instance file '" + xmlInstance + "' is invalid:", e);
} catch (final IOException e) {
log.info("Instance file '" + xmlInstance + "' cannot be opened:", e);
}
testSucceeded = expectedToBeValid == tmpInstanceIsValid;
if (testSucceeded) {
errMsg = Optional.empty();
} else if (testSucceeded) {
} else if (expectedToBeValid) {
errMsg = Optional.of("Instance '" + xmlInstance +
"' is expected to be invalid!");
"' is expected to be valid!");
} else {
errMsg = Optional.of("Instance '" + xmlInstance +
"' is expected to be valid!");
"' is expected to be invalid!");
}
} else {
testSucceeded = false;
testSucceeded = false;
errMsg = Optional.of(tmpPreconditionErrMsg);
}
}
}
......@@ -15,7 +15,7 @@ public class SchemaTest {
@Test
public void testXmlInstanceSet() {
final InstanceSetEvaluation ise = new InstanceSetEvaluation("SchemaTest");
final InstanceSetEvaluation ise = new InstanceSetEvaluation("SchemaTest", "Schema/dictionary.xsd");
System.out.println(ise.getMessages());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment