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

- added support for resultHandle

- added combined mapping of schemafiles if called from common baseURI,
  fixes issues #16
  and #15
parent bfac6a40
No related branches found
No related tags found
No related merge requests found
......@@ -19,16 +19,22 @@ import org.apache.xerces.dom.DOMInputImpl;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.Set;
public class ValidationResourceResolver implements LSResourceResolver {
private final Set<ValidationSchema> namespaceSchemaMap ;
private final boolean debug;
private final ValidationResultHandle resultHandle;
public ValidationResourceResolver(Set<ValidationSchema> namespaceSchemaMap, boolean debug) {
public ValidationResourceResolver(Set<ValidationSchema> namespaceSchemaMap, ValidationResultHandle resultHandle) {
this.namespaceSchemaMap = namespaceSchemaMap;
this.debug = debug;
this.resultHandle = resultHandle;
}
public Optional<ValidationSchema> find_by_ns(String nameSpaceUri) {
......@@ -46,36 +52,89 @@ public class ValidationResourceResolver implements LSResourceResolver {
.findAny();
}
private boolean checkUriStringIsLocal ( String s) {
try {
var uri = new URI(s);
return checkUriIsLocal(uri);
}
catch (Exception e ) {
return false;
}
}
private boolean checkUriIsLocal (URI uri) {
if (!uri.isAbsolute()) { return true; }
try {
return !uri.toURL().getProtocol().startsWith("http");
} catch (Exception e) {
return false;
}
}
private Path createCombinedSchemaFile (String systemId, String baseURIString ) {
Path basenameSystemID = Paths.get(systemId).getFileName();
Path dirnameSchema = null;
try {
dirnameSchema = Paths.get(new URI(baseURIString).getPath()).getParent().toAbsolutePath();
} catch (URISyntaxException e) {
throw new RuntimeException(e); // should not occure
}
//combine together
Path combinedSchema = dirnameSchema.resolve(basenameSystemID);
//System.out.println( "COMBINED: " + combinedSchema.toString());
return combinedSchema;
}
@Override
public LSInput resolveResource(String type, String nameSpaceUri, String publicId, String systemId, String baseURI) {
if (debug) {
System.out.println(
public LSInput resolveResource(String type, String nameSpace, String publicId, String systemId, String baseURIString) {
resultHandle.debug(
"RESOURCE_RESOLVER:"
// + type=" + type
+ " namespaceuri=" + nameSpaceUri
+ " publicId=" + publicId
+ " SystemId=" + systemId
+ " baseURI=" + baseURI
);
}
// + type=" + type
+ " namespace=" + nameSpace
+ " publicId=" + publicId
+ " SystemId=" + systemId
+ " baseURI=" + baseURIString
);
if (namespaceSchemaMap.isEmpty()) {
System.out.println("RESOURCE_RESOLVER: No schemacatalog given!");
resultHandle.warning("RESOURCE_RESOLVER: No schemacatalog given!");
return new DOMInputImpl(null, null, null); // disabled
}
/* TODO: check if uri in namespaceSchemaMap */
Optional<ValidationSchema> validationSchemaOptional = find_by_ns(nameSpaceUri);
Optional<ValidationSchema> validationSchemaOptional = find_by_ns(nameSpace);
if (validationSchemaOptional.isEmpty()) { // not found
if (debug) {
System.out.println("RESOURCE_RESOLVER: --> No entry in schemacatalog found for "+ nameSpaceUri + "!");
}
resultHandle.info("RESOURCE_RESOLVER: --> No entry in schemacatalog found for " + nameSpace + "!");
return new DOMInputImpl(null, null, null); // disabled
}
DOMInputImpl domInput = new DOMInputImpl();
if (debug) {
System.out.println("RESOURCE_RESOLVER: --> Found entry in schemacatalog for " + nameSpaceUri + ", mapping to " + validationSchemaOptional.get().schemaURI + " !");
resultHandle.debug("RESOURCE_RESOLVER: --> Found entry in schemacatalog for " + nameSpace);
/* if schemauri is local AND basename(SystemID) in dirname(schemaURI) exists
than map to this. This is needed because
a) METS by Library of congress uses own xlink implementatuin
b) MATHML3 uses includes of XSD to same namespaces
*/
URI schemaURI = validationSchemaOptional.get().schemaURI;
System.out.println("### BASEURI: " + baseURIString + " -> " + checkUriStringIsLocal( baseURIString ));
System.out.println("### SchemaURI: " + schemaURI + " -> " + checkUriIsLocal( schemaURI) );
if (checkUriStringIsLocal(baseURIString)) {
if (checkUriIsLocal(schemaURI)) {
Path basenameSystemID = Paths.get(systemId).getFileName();
Path dirnameSchema = null;
try {
dirnameSchema = Paths.get(new URI(baseURIString).getPath()).getParent().toAbsolutePath();
} catch (URISyntaxException e) {
throw new RuntimeException(e); // should not occure
}
//combine together
Path combinedSchema = createCombinedSchemaFile(systemId, baseURIString);
String combinedSchemaStr = combinedSchema.toString();
File combinedSchemaFile = new File( combinedSchemaStr );
if (combinedSchemaFile.isFile()) {
resultHandle.info("RESOURCE_RESOLVER: ignoring catalog, using a combined mapping to " + schemaURI + " !");
schemaURI = combinedSchema.toUri();
}
}
}
domInput.setSystemId( validationSchemaOptional.get().schemaURI.toString());
resultHandle.info("RESOURCE_RESOLVER: mapping to " + schemaURI + " !");
domInput.setSystemId(schemaURI.toString());
return domInput;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment