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

- refactoring, simplified signature of validateFormatAgainstAnySchema()

- ns-aware validation of schema files added using check_ns_of_childs() and validateFormatAgainstSchemaRecursively()
parent 7fbf7acb
Branches
Tags
No related merge requests found
......@@ -277,7 +277,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
valid = false;
} else {
ValidationSchema validationSchema = optSchema.get();
validateFormatAgainstAnySchema(filePath, doc, validationSchema);
validateFormatAgainstAnySchema(filePath, validationSchema);
}
}
} catch (ParserConfigurationException e) {
......@@ -302,25 +302,22 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
return valid;
}
private void check_ns_of_childs(Node root) {
private void check_ns_of_childs(Node root) throws IOException, SAXException {
String root_ns = root.getNamespaceURI();
NodeList childNodes = root.getChildNodes();
for (int i=0; i<childNodes.getLength(); i++) {
var child_ns = childNodes.item(i).getNamespaceURI();
var child = childNodes.item(i);
var child_ns = child.getNamespaceURI();
if (child_ns != null && child_ns != root_ns) {
System.out.println( "root ns = " + root_ns + " child ns = " + child_ns);
var res = validationResourceResolver.find_by_ns( child_ns);
if (res.isEmpty()) {
System.out.println( "no entry for child ns: " + child_ns + " found!");
this.valid=false;
throw new SAXException("No entry for child namespace found: " + child_ns);
} else {
assert(res.get().schemaType.equals("schema"));
var schema = res.get().schemaURI;
SLUBValidateSchema validator = new SLUBValidateSchema( validationResourceResolver );
try {
valid = validator.validateAgainst(childNodes.item(i), schema);
} catch (IOException e) {
System.out.println( e.getMessage() );
}
assert(! res.get().schemaType.equals("dtd"));
var schema = res.get();
validateFormatAgainstSchemaRecursively(childNodes.item(i), schema);
}
}
}
......@@ -329,26 +326,37 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
}
}
private void validateFormatAgainstAnySchema(String filePath, Document doc, ValidationSchema validationSchema) throws IOException, ParserConfigurationException, SAXException {
reportDetail("assigned schema of type: " + validationSchema.schemaType);
reportDetail("assigned schema uri: " + validationSchema.schemaURI);
private void validateFormatAgainstSchemaRecursively(Node root, ValidationSchema validationSchema) throws IOException, SAXException {
var schemaType = validationSchema.schemaType;
if (schemaType == ValidationSchemaType.dtd) {
SLUBValidateDTD validator = new SLUBValidateDTD( validationCatalogResolver );
valid = validator.validateAgainst(filePath);
errors.addAll( validator.get_errors() );
} else if (schemaType == ValidationSchemaType.schema) {
if (schemaType == ValidationSchemaType.schema) {
SLUBValidateSchema validator = new SLUBValidateSchema( validationResourceResolver );
// Slect validation roots from DOM
Element root = doc.getDocumentElement();
check_ns_of_childs(root);
valid = validator.validateAgainst(doc, validationSchema.schemaURI);
valid = validator.validateAgainst(root, validationSchema.schemaURI);
/*
valid = validator.validateAgainst(doc, validationSchema.schemaURI);
*/
errors.addAll( validator.get_errors() );
} else if (schemaType == ValidationSchemaType.relaxng) {
}
else {
reportError("unsupported schema uri="+ validationSchema.schemaURI + " of type: " + validationSchema.schemaType);
valid = false;
}
}
private void validateFormatAgainstAnySchema(String filePath, ValidationSchema validationSchema) throws IOException, ParserConfigurationException, SAXException {
reportDetail("assigned schema of type: " + validationSchema.schemaType);
reportDetail("assigned schema uri: " + validationSchema.schemaURI);
var schemaType = validationSchema.schemaType;
Document doc = getDocument(filePath);
if (schemaType == ValidationSchemaType.dtd) {
SLUBValidateDTD validator = new SLUBValidateDTD( validationCatalogResolver );
valid = validator.validateAgainst(filePath);
errors.addAll( validator.get_errors() );
}
else if (schemaType == ValidationSchemaType.relaxng) {
SLUBValidateRelaxNG validator = new SLUBValidateRelaxNG( );
valid = validator.validateAgainst(doc, validationSchema.schemaURI);
errors.addAll( validator.get_errors() );
......@@ -359,8 +367,9 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
reportError("unsupported schematron schema uri="+ validationSchema.schemaURI + " of type: " + validationSchema.schemaType);
valid = false;
/* */
}
else {
} else if (schemaType == ValidationSchemaType.schema) {
validateFormatAgainstSchemaRecursively(doc, validationSchema);
} else {
reportError("unsupported schema uri="+ validationSchema.schemaURI + " of type: " + validationSchema.schemaType);
valid = false;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment