I need to load XSD files from jar, so implemented LSResourceResolver as follows:
Source schemaFile = new StreamSource(getClass().getClassLoader().getResourceAsStream("resources/xsd/root/maindoc/MainSchema.xsd"));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setResourceResolver(new LSResourceResolver(){
@Override
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
LSInput input = new DOMInputImpl();
String filePath = getNormalizedPath("resources/xsd/root/maindoc/", systemId);
InputStream stream = getClass().getClassLoader().getResourceAsStream(filePath);
input.setPublicId(publicId);
input.setSystemId(systemId);
input.setBaseURI(baseURI);
input.setCharacterStream(new InputStreamReader(stream));
return input;
}
});
Schema schema = schemaFactory.newSchema(schemaFile);
Such implementation successfully resolves links from the main schema, but fails to resolve links from referenced documents.
With a call from referenced document I receive not null baseURI parameter, but it's value is like "file:///var/xxx/yyy.xsd" in my case, so it does not look possible to construct a valid path from this and systemId.
Am I missing something? Should it be possible to make resolver work recursively?
Surely there is a workaround with flattening the schema, but I don't like it much.