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

- more clean output

parent 1dd26456
Branches
No related tags found
No related merge requests found
...@@ -37,6 +37,7 @@ import java.io.IOException; ...@@ -37,6 +37,7 @@ import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
...@@ -62,6 +63,8 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -62,6 +63,8 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
private static final Set<ValidationSchema> namespaceSchemaMap = new HashSet<>(); private static final Set<ValidationSchema> namespaceSchemaMap = new HashSet<>();
private ValidationCatalogResolver validationCatalogResolver; /* for DTDs */ private ValidationCatalogResolver validationCatalogResolver; /* for DTDs */
private ValidationResourceResolver validationResourceResolver; /* for schema/relaxNG/... */ private ValidationResourceResolver validationResourceResolver; /* for schema/relaxNG/... */
private SLUBXmlErrorHandler validationErrorHandler;
public Set<ValidationSchema> getNamespaceSchemaMap() { public Set<ValidationSchema> getNamespaceSchemaMap() {
return namespaceSchemaMap; return namespaceSchemaMap;
} }
...@@ -91,7 +94,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -91,7 +94,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
} }
} }
} catch (ParserConfigurationException | SAXException | IOException | URISyntaxException e) { } catch (ParserConfigurationException | SAXException | IOException | URISyntaxException e) {
System.out.println(" ERROR in loadnamespaceSchemaMap " + e.getStackTrace() ); System.out.println(" ERROR in loadnamespaceSchemaMap " + Arrays.toString(e.getStackTrace()));
log.error("parsing expection parsing namespace schema map file " + namespaceSchemaMapFile + " ," + e.getMessage()); log.error("parsing expection parsing namespace schema map file " + namespaceSchemaMapFile + " ," + e.getMessage());
} }
} }
...@@ -114,7 +117,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -114,7 +117,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
uri = new URI(sUri); uri = new URI(sUri);
} else { } else {
uri = remapUri(namespaceSchemaMapFile, sUri); uri = remapUri(namespaceSchemaMapFile, sUri);
if (debug) System.out.println( "================ URI: " + uri); if (debug) System.out.println( "SCHEMACATALOG, remap to URI: " + uri);
} }
ValidationSchema v = new ValidationSchema(namespace, schematype, uri); ValidationSchema v = new ValidationSchema(namespace, schematype, uri);
namespaceSchemaMap.add(v); namespaceSchemaMap.add(v);
...@@ -124,9 +127,6 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -124,9 +127,6 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
private URI remapUri(String namespaceSchemaMapFile, String sUri) throws URISyntaxException { private URI remapUri(String namespaceSchemaMapFile, String sUri) throws URISyntaxException {
URI partUri = new URI(sUri); URI partUri = new URI(sUri);
String baseStr = "file://" + new File(namespaceSchemaMapFile).getAbsoluteFile().getParent() + "/"; String baseStr = "file://" + new File(namespaceSchemaMapFile).getAbsoluteFile().getParent() + "/";
if (debug) {
System.out.println("BASE =" + baseStr);
}
URI baseUri = new URI( baseStr); URI baseUri = new URI( baseStr);
return baseUri.resolve(partUri); return baseUri.resolve(partUri);
} }
...@@ -137,8 +137,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -137,8 +137,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
assert ("namespace".equals(attr_strings.get(1))); assert ("namespace".equals(attr_strings.get(1)));
assert ("schemauri".equals(attr_strings.get(2))); assert ("schemauri".equals(attr_strings.get(2)));
NamedNodeMap attributes = node.getAttributes(); NamedNodeMap attributes = node.getAttributes();
List<String> attr_list = attr_strings.stream().map(s -> attributes.getNamedItem(s).getTextContent()).toList(); return attr_strings.stream().map(s -> attributes.getNamedItem(s).getTextContent()).toList();
return attr_list;
} }
private static boolean checkAttributesOfNamespaceSchemaMapFile(String attr_type) { private static boolean checkAttributesOfNamespaceSchemaMapFile(String attr_type) {
...@@ -149,6 +148,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -149,6 +148,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
dbf.setValidating(false); dbf.setValidating(false);
dbf.setExpandEntityReferences(false); dbf.setExpandEntityReferences(false);
validationErrorHandler = new SLUBXmlErrorHandler();
} }
/** init params to configure the plugin via xml forms /** init params to configure the plugin via xml forms
...@@ -165,7 +165,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -165,7 +165,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
}; };
validationCatalogResolver = new ValidationCatalogResolver(catalogs, errors, debug); validationCatalogResolver = new ValidationCatalogResolver(catalogs, errors, debug);
loadNamespaceSchemaMap( initp.get("schemacatalog").trim() ); loadNamespaceSchemaMap( initp.get("schemacatalog").trim() );
validationResourceResolver = new ValidationResourceResolver(namespaceSchemaMap, errors, debug); validationResourceResolver = new ValidationResourceResolver(namespaceSchemaMap, debug);
} }
private XmlInfoRecord getXMLinfo(Document doc) { private XmlInfoRecord getXMLinfo(Document doc) {
...@@ -269,6 +269,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -269,6 +269,7 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
//System.out.println("## is wellformed"); //System.out.println("## is wellformed");
wellformed = true; wellformed = true;
errors.clear(); errors.clear();
validationErrorHandler = new SLUBXmlErrorHandler(); // reset errorhandler
Optional<ValidationSchema> optSchema = assignSchema(doc); Optional<ValidationSchema> optSchema = assignSchema(doc);
//System.out.println("## schema assigned..."); //System.out.println("## schema assigned...");
if (optSchema.isEmpty()) { if (optSchema.isEmpty()) {
...@@ -308,14 +309,16 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -308,14 +309,16 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
for (int i=0; i<childNodes.getLength(); i++) { for (int i=0; i<childNodes.getLength(); i++) {
var child = childNodes.item(i); var child = childNodes.item(i);
var child_ns = child.getNamespaceURI(); var child_ns = child.getNamespaceURI();
if (child_ns != null && child_ns != root_ns) { if (child_ns != null && !child_ns.equals(root_ns)) {
System.out.println( "root ns = " + root_ns + " child ns = " + child_ns); if (debug) {
System.out.println("RECURSIVE SCHEMA VALIDATOR: root ns = " + root_ns + " child ns = " + child_ns);
}
var res = validationResourceResolver.find_by_ns( child_ns); var res = validationResourceResolver.find_by_ns( child_ns);
if (res.isEmpty()) { if (res.isEmpty()) {
this.valid=false; this.valid=false;
throw new SAXException("No entry for child namespace found: " + child_ns); throw new SAXException("RECURSIVE SCHEMA VALIDATOR: No entry for child namespace found: " + child_ns);
} else { } else {
assert(! res.get().schemaType.equals("dtd")); assert(res.get().schemaType == ValidationSchemaType.schema);
var schema = res.get(); var schema = res.get();
validateFormatAgainstSchemaRecursively(childNodes.item(i), schema); validateFormatAgainstSchemaRecursively(childNodes.item(i), schema);
} }
...@@ -330,18 +333,12 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -330,18 +333,12 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
var schemaType = validationSchema.schemaType; var schemaType = validationSchema.schemaType;
if (schemaType == ValidationSchemaType.schema) { if (schemaType == ValidationSchemaType.schema) {
SLUBValidateSchema validator = new SLUBValidateSchema( validationResourceResolver ); SLUBValidateSchema validator = new SLUBValidateSchema( validationResourceResolver );
// Slect validation roots from DOM // Select validation roots from DOM
check_ns_of_childs(root); check_ns_of_childs(root);
valid = validator.validateAgainst(root, validationSchema.schemaURI); valid = validator.validateAgainst(root, validationSchema.schemaURI);
/*
valid = validator.validateAgainst(doc, validationSchema.schemaURI);
*/
errors.addAll( validator.get_errors() );
} }
else { else {
reportError("unsupported schema uri="+ validationSchema.schemaURI + " of type: " + validationSchema.schemaType); reportError("RECURSIVE SCHEMA VALIDATOR: unsupported schema uri="+ validationSchema.schemaURI + " of type: " + validationSchema.schemaType);
valid = false; valid = false;
} }
} }
...@@ -352,18 +349,16 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -352,18 +349,16 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
var schemaType = validationSchema.schemaType; var schemaType = validationSchema.schemaType;
Document doc = getDocument(filePath); Document doc = getDocument(filePath);
if (schemaType == ValidationSchemaType.dtd) { if (schemaType == ValidationSchemaType.dtd) {
SLUBValidateDTD validator = new SLUBValidateDTD( validationCatalogResolver ); SLUBValidateDTD validator = new SLUBValidateDTD( validationCatalogResolver, validationErrorHandler );
valid = validator.validateAgainst(filePath); valid = validator.validateAgainst(filePath);
errors.addAll( validator.get_errors() );
} }
else if (schemaType == ValidationSchemaType.relaxng) { else if (schemaType == ValidationSchemaType.relaxng) {
SLUBValidateRelaxNG validator = new SLUBValidateRelaxNG( ); SLUBValidateRelaxNG validator = new SLUBValidateRelaxNG( );
valid = validator.validateAgainst(doc, validationSchema.schemaURI); valid = validator.validateAgainst(doc, validationSchema.schemaURI);
errors.addAll( validator.get_errors() );
} else if (schemaType == ValidationSchemaType.schematron) { } else if (schemaType == ValidationSchemaType.schematron) {
SLUBValidateSchematron validator = new SLUBValidateSchematron( ); SLUBValidateSchematron validator = new SLUBValidateSchematron( );
valid = validator.validateAgainst(doc, validationSchema.schemaURI); valid = validator.validateAgainst(doc, validationSchema.schemaURI);
errors.addAll( validator.get_errors() );
reportError("unsupported schematron schema uri="+ validationSchema.schemaURI + " of type: " + validationSchema.schemaType); reportError("unsupported schematron schema uri="+ validationSchema.schemaURI + " of type: " + validationSchema.schemaType);
valid = false; valid = false;
/* */ /* */
...@@ -413,12 +408,13 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin { ...@@ -413,12 +408,13 @@ public class SLUBXmlFormatValidationPlugin implements FormatValidationPlugin {
dbf.setSchema(null); dbf.setSchema(null);
//dbf.setXIncludeAware(true); //dbf.setXIncludeAware(true);
dbf.setNamespaceAware(true); dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder(); DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler( validationErrorHandler );
//db.setEntityResolver(validationCatalogResolver); //db.setEntityResolver(validationCatalogResolver);
//db.setErrorHandler(validationErrorHandler);
if (debug) System.out.println( if (debug) System.out.println(
"# ns aware=" + db.isNamespaceAware() "DOCUMENT LOADER: ns aware=" + db.isNamespaceAware()
+ " include aware=" + db.isXIncludeAware() + " include aware=" + db.isXIncludeAware()
+ " is validating=" + db.isValidating() + " is validating=" + db.isValidating()
); );
......
...@@ -26,12 +26,12 @@ public class ValidationCatalogResolver implements CatalogResolver { ...@@ -26,12 +26,12 @@ public class ValidationCatalogResolver implements CatalogResolver {
@Override @Override
public InputSource resolveEntity(String publicId, String systemId) { public InputSource resolveEntity(String publicId, String systemId) {
if (debug) { if (debug) {
System.out.println("======"); System.out.println("CATALOG RESOLVER: ======");
System.out.println("resolveEntity publicId=" + publicId + " systemId=" + systemId); System.out.println("CATALOG RESOLVER: resolveEntity publicId=" + publicId + " systemId=" + systemId);
System.out.println("catalogs:" + Arrays.stream(catalogs).toList()); System.out.println("CATALOG RESOLVER: catalogs:" + Arrays.stream(catalogs).toList());
} }
if (catalogs.length == 0) { if (catalogs.length == 0) {
System.out.println("No catalog given!"); System.out.println("CATALOG RESOLVER: No catalog given!");
errors.add("No catalog given!"); errors.add("No catalog given!");
return new InputSource(Reader.nullReader() ); // disabled further catalog checking to enable whitelist behaviour, see https://docs.oracle.com/en/java/javase/17/docs/api/java.xml/javax/xml/catalog/CatalogResolver.html return new InputSource(Reader.nullReader() ); // disabled further catalog checking to enable whitelist behaviour, see https://docs.oracle.com/en/java/javase/17/docs/api/java.xml/javax/xml/catalog/CatalogResolver.html
} }
...@@ -42,13 +42,13 @@ public class ValidationCatalogResolver implements CatalogResolver { ...@@ -42,13 +42,13 @@ public class ValidationCatalogResolver implements CatalogResolver {
cr.setCatalogList(catalogs); cr.setCatalogList(catalogs);
cr.setUseLiteralSystemId(true); // TODO: Prüfen, ob wenn auf false, sytemid analog zu dem in Datei aussieht (file:/// statt file:/) cr.setUseLiteralSystemId(true); // TODO: Prüfen, ob wenn auf false, sytemid analog zu dem in Datei aussieht (file:/// statt file:/)
if (debug) { if (debug) {
System.out.println("try to resolve for publicId=" + publicId + ", systemId=" + systemId + ", "); System.out.println("CATALOG RESOLVER: try to resolve for publicId=" + publicId + ", systemId=" + systemId + ", ");
} }
result = cr.resolveEntity(publicId, systemId); result = cr.resolveEntity(publicId, systemId);
} catch (IOException | CatalogException | SAXException e) { } catch (IOException | CatalogException | SAXException e) {
if (debug) { if (debug) {
System.out.println("No mapping found for publicId=" + publicId + ", systemId=" + systemId + ", " + e.getMessage()); System.out.println("CATALOG RESOLVER: No mapping found for publicId=" + publicId + ", systemId=" + systemId + ", " + e.getMessage());
} }
errors.add("No mapping found for publicId=" + publicId + ", systemId=" + systemId + ", " + e.getMessage()); errors.add("No mapping found for publicId=" + publicId + ", systemId=" + systemId + ", " + e.getMessage());
return new InputSource(Reader.nullReader() ); // disabled further catalog checking to enable whitelist behaviour, see https://docs.oracle.com/en/java/javase/17/docs/api/java.xml/javax/xml/catalog/CatalogResolver.html return new InputSource(Reader.nullReader() ); // disabled further catalog checking to enable whitelist behaviour, see https://docs.oracle.com/en/java/javase/17/docs/api/java.xml/javax/xml/catalog/CatalogResolver.html
...@@ -61,7 +61,7 @@ public class ValidationCatalogResolver implements CatalogResolver { ...@@ -61,7 +61,7 @@ public class ValidationCatalogResolver implements CatalogResolver {
return new InputSource(Reader.nullReader() ); // disabled further catalog checking to enable whitelist behaviour, see https://docs.oracle.com/en/java/javase/17/docs/api/java.xml/javax/xml/catalog/CatalogResolver.html return new InputSource(Reader.nullReader() ); // disabled further catalog checking to enable whitelist behaviour, see https://docs.oracle.com/en/java/javase/17/docs/api/java.xml/javax/xml/catalog/CatalogResolver.html
} else { } else {
if (debug) { if (debug) {
System.out.println(" ---> result: systemid=" + result.getSystemId() + " publicid=" + result.getPublicId() ); System.out.println("CATALOG RESOLVER: ---> result: systemid=" + result.getSystemId() + " publicid=" + result.getPublicId() );
} }
} }
return result; return result;
...@@ -70,8 +70,8 @@ public class ValidationCatalogResolver implements CatalogResolver { ...@@ -70,8 +70,8 @@ public class ValidationCatalogResolver implements CatalogResolver {
@Override @Override
public Source resolve(String href, String base) { public Source resolve(String href, String base) {
if (debug) { if (debug) {
System.out.println("======"); System.out.println("CATALOG RESOLVER: ======");
System.out.println("resolve href=" + href + " base=" + base); System.out.println("CATALOG RESOLVER: resolve href=" + href + " base=" + base);
} }
return null; return null;
} }
...@@ -79,8 +79,8 @@ public class ValidationCatalogResolver implements CatalogResolver { ...@@ -79,8 +79,8 @@ public class ValidationCatalogResolver implements CatalogResolver {
@Override @Override
public InputStream resolveEntity(String publicId, String systemId, String baseUri, String nameSpace) { public InputStream resolveEntity(String publicId, String systemId, String baseUri, String nameSpace) {
if (debug) { if (debug) {
System.out.println("======"); System.out.println("CATALOG RESOLVER: ======");
System.out.println("resolveEntity2 publicId=" + publicId + " systemId=" + systemId); System.out.println("CATALOG RESOLVER: resolveEntity2 publicId=" + publicId + " systemId=" + systemId);
} }
return null; return null;
} }
...@@ -88,8 +88,8 @@ public class ValidationCatalogResolver implements CatalogResolver { ...@@ -88,8 +88,8 @@ public class ValidationCatalogResolver implements CatalogResolver {
@Override @Override
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseUri) { public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseUri) {
if (debug) { if (debug) {
System.out.println("======"); System.out.println("CATALOG RESOLVER: ======");
System.out.println("resolveResource publicId=" + publicId + " systemId=" + systemId); System.out.println("CATALOG RESOLVER: resolveResource publicId=" + publicId + " systemId=" + systemId);
} }
return null; return null;
} }
......
...@@ -4,18 +4,15 @@ import org.apache.xerces.dom.DOMInputImpl; ...@@ -4,18 +4,15 @@ import org.apache.xerces.dom.DOMInputImpl;
import org.w3c.dom.ls.LSInput; import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver; import org.w3c.dom.ls.LSResourceResolver;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
public class ValidationResourceResolver implements LSResourceResolver { public class ValidationResourceResolver implements LSResourceResolver {
private Set<ValidationSchema> namespaceSchemaMap ; private final Set<ValidationSchema> namespaceSchemaMap ;
private List<String> errors; private final boolean debug;
private boolean debug;
public ValidationResourceResolver(Set<ValidationSchema> namespaceSchemaMap, List<String> errors, boolean debug) { public ValidationResourceResolver(Set<ValidationSchema> namespaceSchemaMap, boolean debug) {
this.namespaceSchemaMap = namespaceSchemaMap; this.namespaceSchemaMap = namespaceSchemaMap;
this.errors = errors;
this.debug = debug; this.debug = debug;
} }
...@@ -29,7 +26,7 @@ public class ValidationResourceResolver implements LSResourceResolver { ...@@ -29,7 +26,7 @@ public class ValidationResourceResolver implements LSResourceResolver {
public Optional<ValidationSchema> find_by_systemid(String SystemId) { public Optional<ValidationSchema> find_by_systemid(String SystemId) {
return namespaceSchemaMap.stream() return namespaceSchemaMap.stream()
.filter( .filter(
entry -> (entry.schemaURI.equals(SystemId)) entry -> (entry.schemaURI.toString().equals(SystemId))
) )
.findAny(); .findAny();
} }
...@@ -38,7 +35,8 @@ public class ValidationResourceResolver implements LSResourceResolver { ...@@ -38,7 +35,8 @@ public class ValidationResourceResolver implements LSResourceResolver {
public LSInput resolveResource(String type, String nameSpaceUri, String publicId, String systemId, String baseURI) { public LSInput resolveResource(String type, String nameSpaceUri, String publicId, String systemId, String baseURI) {
if (debug) { if (debug) {
System.out.println( System.out.println(
"RESOURCE_RESOLVER: type=" + type "RESOURCE_RESOLVER:"
// + type=" + type
+ " namespaceuri=" + nameSpaceUri + " namespaceuri=" + nameSpaceUri
+ " publicId=" + publicId + " publicId=" + publicId
+ " SystemId=" + systemId + " SystemId=" + systemId
...@@ -46,22 +44,21 @@ public class ValidationResourceResolver implements LSResourceResolver { ...@@ -46,22 +44,21 @@ public class ValidationResourceResolver implements LSResourceResolver {
); );
} }
if (namespaceSchemaMap.isEmpty()) { if (namespaceSchemaMap.isEmpty()) {
System.out.println("No schemacatalog given!"); System.out.println("RESOURCE_RESOLVER: No schemacatalog given!");
errors.add("No schemacatalog given!");
return new DOMInputImpl(null, null, null); // disabled return new DOMInputImpl(null, null, null); // disabled
} }
LSInput result;
/* TODO: check if uri in namespaceSchemaMap */ /* TODO: check if uri in namespaceSchemaMap */
Optional<ValidationSchema> validationSchemaOptional = find_by_ns(nameSpaceUri); Optional<ValidationSchema> validationSchemaOptional = find_by_ns(nameSpaceUri);
if (validationSchemaOptional.isEmpty()) { // not found if (validationSchemaOptional.isEmpty()) { // not found
if (debug) { if (debug) {
System.out.println("--> No entry in schemacatalog found for "+ nameSpaceUri + "!"); System.out.println("RESOURCE_RESOLVER: --> No entry in schemacatalog found for "+ nameSpaceUri + "!");
} }
return new DOMInputImpl(null, null, null); // disabled return new DOMInputImpl(null, null, null); // disabled
} }
DOMInputImpl domInput = new DOMInputImpl(); DOMInputImpl domInput = new DOMInputImpl();
if (debug) { if (debug) {
System.out.println("--> Found entry in schemacatalog for " + nameSpaceUri + ", mapping to " + validationSchemaOptional.get().schemaURI + " !"); System.out.println("RESOURCE_RESOLVER: --> Found entry in schemacatalog for " + nameSpaceUri + ", mapping to " + validationSchemaOptional.get().schemaURI + " !");
} }
domInput.setSystemId( validationSchemaOptional.get().schemaURI.toString()); domInput.setSystemId( validationSchemaOptional.get().schemaURI.toString());
return domInput; return domInput;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment