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

- refactoring, renamed class

parent 28ad291d
Branches
Tags
No related merge requests found
...@@ -47,15 +47,16 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -47,15 +47,16 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
private boolean valid = false; private boolean valid = false;
private boolean wellformed = false; private boolean wellformed = false;
private static String schemaDir = "";
private final DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance(); private final DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance();
private final List<String> errors = new ArrayList<>(); private final List<String> errors = new ArrayList<>();
private final List<String> details = new ArrayList<>(); private final List<String> details = new ArrayList<>();
private static final HashSet<validationSchema> namespaceSchemaMap = new HashSet<>() { private static final HashSet<ValidationSchema> namespaceSchemaMap = new HashSet<>() {
{ {
add(new validationSchema("http://www.loc.gov/standards/alto/ns-v2#", ValidationSchemaType.schema, "http://www.loc.gov/standards/alto/alto-v2.0.xsd")); add(new ValidationSchema("http://www.loc.gov/standards/alto/ns-v2#", ValidationSchemaType.schema, "http://www.loc.gov/standards/alto/alto-v2.0.xsd"));
add(new validationSchema("http://www.loc.gov/mods/v3", ValidationSchemaType.schema, "http://www.loc.gov/standards/mods/v3/mods-3-8.xsd")); add(new ValidationSchema("http://www.loc.gov/mods/v3", ValidationSchemaType.schema, "http://www.loc.gov/standards/mods/v3/mods-3-8.xsd"));
add(new validationSchema("http://www.lido-schema.org", ValidationSchemaType.schema, "http://www.lido-schema.org/schema/v1.1/lido-v1.1.xsd")); add(new ValidationSchema("http://www.lido-schema.org", ValidationSchemaType.schema, "http://www.lido-schema.org/schema/v1.1/lido-v1.1.xsd"));
add(new validationSchema( "http://slubarchiv.slub-dresden.de/rights1", ValidationSchemaType.schema, "https://slubarchiv.slub-dresden.de/fileadmin/groups/slubsite/slubarchiv/standards/rights/rights1.xsd")); add(new ValidationSchema( "http://slubarchiv.slub-dresden.de/rights1", ValidationSchemaType.schema, "https://slubarchiv.slub-dresden.de/fileadmin/groups/slubsite/slubarchiv/standards/rights/rights1.xsd"));
// put("http://www.opengis.net/citygml/profiles/base/1.0", ""); // put("http://www.opengis.net/citygml/profiles/base/1.0", "");
// put("http://www.opengis.net/kml/2.2", ""); // put("http://www.opengis.net/kml/2.2", "");
// put("http://www.music-encoding.org/ns/mei", ""); // put("http://www.music-encoding.org/ns/mei", "");
...@@ -84,6 +85,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -84,6 +85,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
dbf.setValidating(false); dbf.setValidating(false);
dbf.setExpandEntityReferences(false); dbf.setExpandEntityReferences(false);
} }
/** init params to configure the plugin via xml forms /** init params to configure the plugin via xml forms
...@@ -94,6 +96,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -94,6 +96,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
initp.get("catalog").trim() initp.get("catalog").trim()
}; };
validationCatalogResolver = new ValidationCatalogResolver(catalogs, errors); validationCatalogResolver = new ValidationCatalogResolver(catalogs, errors);
schemaDir = initp.get("schemadir").trim();
} }
private static xmlInfoRecord getXMLinfo(Document doc) { private static xmlInfoRecord getXMLinfo(Document doc) {
...@@ -112,7 +115,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -112,7 +115,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
} }
return new xmlInfoRecord(namespaceURI, documentURI, xmlVersion, systemId, publicId); return new xmlInfoRecord(namespaceURI, documentURI, xmlVersion, systemId, publicId);
} }
private static void printXMLinfo(Document doc) { private void printXMLinfo(Document doc) {
xmlInfoRecord info = getXMLinfo(doc); xmlInfoRecord info = getXMLinfo(doc);
System.out.println( System.out.println(
"\n-------------------------------------------" "\n-------------------------------------------"
...@@ -124,15 +127,32 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -124,15 +127,32 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
+ "\n" + "\n"
); );
} }
private static Optional<validationSchema> assignSchema(Document doc) { private ValidationSchema localizeSchemaUrl(ValidationSchema validationSchema) {
switch (validationSchema.schemaType) {
case schema:
case schematron:
case relaxng: {
validationSchema.schemaLocalURL =
schemaDir +
validationSchema.schemaURL.substring(
validationSchema.schemaURL.lastIndexOf("/")
);
}
case dtd:
case nothing:
break;
}
return validationSchema;
}
private Optional<ValidationSchema> assignSchema(Document doc) {
xmlInfoRecord info = getXMLinfo(doc); xmlInfoRecord info = getXMLinfo(doc);
Optional<validationSchema> optEle = Optional.empty(); Optional<ValidationSchema> optEle = Optional.empty();
if (null == info.nameSpaceUri) { if (null == info.nameSpaceUri) {
/* try if a DTD is assignable */ /* try if a DTD is assignable */
var type = assignDtdIfApplicable(doc); var type = assignDtdIfApplicable(doc);
if (type.equals(ValidationSchemaType.dtd)) { if (type.equals(ValidationSchemaType.dtd)) {
System.out.println("found schema " + type); System.out.println("found schema " + type);
var ele = new validationSchema(info.nameSpaceUri, type, info.systemID); var ele = new ValidationSchema(null, type, info.systemID);
optEle = Optional.of(ele); optEle = Optional.of(ele);
} }
} else { } else {
...@@ -140,12 +160,14 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -140,12 +160,14 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
.filter( .filter(
entry -> (entry.schemaType.equals(ValidationSchemaType.schema)) && (entry.nameSpace.equals(info.nameSpaceUri)) entry -> (entry.schemaType.equals(ValidationSchemaType.schema)) && (entry.nameSpace.equals(info.nameSpaceUri))
) )
.map(this::localizeSchemaUrl)
.findAny(); .findAny();
} }
if ( optEle.isPresent() ) { if ( optEle.isPresent() ) {
System.out.println("found namespace " + optEle.get().nameSpace ); System.out.println("found namespace " + optEle.get().nameSpace );
System.out.println("found schematype " + optEle.get().schemaType ); System.out.println("found schematype " + optEle.get().schemaType );
System.out.println("found schemaURL " + optEle.get().schemaURL ); System.out.println("found schemaURL " + optEle.get().schemaURL );
System.out.println("found localized schemaURL " + optEle.get().schemaLocalURL );
} else { } else {
System.out.println("no element found"); System.out.println("no element found");
} }
...@@ -193,23 +215,25 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -193,23 +215,25 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
private boolean checkIfWellformed(String filePath) throws ParserConfigurationException, IOException, SAXException { private boolean checkIfWellformed(String filePath) throws ParserConfigurationException, IOException, SAXException {
/* detect XML type via NS */ /* detect XML type via NS */
boolean isWellformedXml = false; boolean isWellformedXml = false;
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
dbf.setValidating(false); dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder(); DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(filePath)); Document doc = db.parse(new File(filePath));
xmlInfoRecord info = getXMLinfo(doc); xmlInfoRecord info = getXMLinfo(doc);
reportDetail("detect XML type via NS:" + info.nameSpaceUri); reportDetail("detect XML type via NS:" + info.nameSpaceUri);
/* TODO: align corresponding Schema based on systemID */ /* TODO: align corresponding Schema based on systemID */
Optional<validationSchema> schema = assignSchema(doc); Optional<ValidationSchema> schema = assignSchema(doc);
if (schema.isEmpty()) { if (schema.isEmpty()) {
reportError("there is no related schema found in *our* catalog of allowed XML types.", filePath); reportError("there is no related schema found in *our* catalog of allowed XML types.", filePath);
} else { } else {
reportDetail("assigned schema of type: " + schema.get().schemaType); reportDetail("assigned schema of type: " + schema.get().schemaType);
reportDetail("assigned schema url: " + schema.get().schemaURL); reportDetail("assigned schema url: " + schema.get().schemaURL);
if (schema.get().schemaType == ValidationSchemaType.dtd) { if (schema.get().schemaType == ValidationSchemaType.dtd) {
assert(dbf.isValidating() == false); assert(!dbf.isValidating());
dbf.setValidating(true); /* only used if DTD */ dbf.setValidating(true); /* only used if DTD */
assert(dbf.isValidating() == true); assert(dbf.isValidating());
dbf.setFeature(XMLConstants.USE_CATALOG, true); dbf.setFeature(XMLConstants.USE_CATALOG, true);
dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
System.out.println("-> dtd detected, use catalog"); System.out.println("-> dtd detected, use catalog");
......
...@@ -3,7 +3,7 @@ package org.slub.rosetta.dps.repository.plugin; ...@@ -3,7 +3,7 @@ package org.slub.rosetta.dps.repository.plugin;
import javax.xml.validation.Schema; import javax.xml.validation.Schema;
import java.io.InputStream; import java.io.InputStream;
class validationSchema { class ValidationSchema {
public final String nameSpace; public final String nameSpace;
public final ValidationSchemaType schemaType; public final ValidationSchemaType schemaType;
public final String schemaURL; public final String schemaURL;
...@@ -11,7 +11,7 @@ class validationSchema { ...@@ -11,7 +11,7 @@ class validationSchema {
public InputStream schemaInputStream; public InputStream schemaInputStream;
public String schemaLocalURL; public String schemaLocalURL;
public validationSchema(String nameSpace, ValidationSchemaType schemaType, String schemaURL) { public ValidationSchema(String nameSpace, ValidationSchemaType schemaType, String schemaURL) {
this.nameSpace = nameSpace; this.nameSpace = nameSpace;
this.schemaURL = schemaURL; this.schemaURL = schemaURL;
this.schemaType = schemaType; this.schemaType = schemaType;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment