Java XML Schema验证 - ResourceResolver麻烦
我正在尝试将一个组件构建到一个Web应用程序中,该应用程序将根据一组模式验证不同的XML文档。
我在java包com.example.xml中有验证器类,然后我有一个"包"对于schemas com.example.xml.Schemas,它们根据命名空间和任何包含的模式进行组织,如下所示:
com.example.xml/SchemaValidator.java
com.example.xml.Schemas/2008/07/05/Message.xsd
com.example.xml.Schemas/2008/07/05/Person.xsd
com.example.xml.Schemas/2008/07/05/Address.xsd
com.example.xml.Schemas/2010/09/21/Message.xsd
com.example.xml.Schemas/2010/09/21/Organization.xsd
com.example.xml.Schemas/2010/09/21/Person.xsd
com.example.xml.Schemas/2010/09/21/Address.xsd
因此,大多数文档元素都称为Message,但在不同的名称空间和不同的内部。 通过映射,我可以确定正确的模式所在的位置,然后我加载与根元素名称匹配的XSD - 但为了使解析器能够读取包含的模式,我尝试了一个org.w3c.dom.ls .LSResourceResolver实现基于systemId获取正确的文件。
public class ResourceResolver implements LSResourceResolver {
private String basePath;
public ResourceResolver( String baseDirectory ){
basePath = baseDirectory;
if( !basePath.endsWith("/")){
basePath += "/";
}
}
@Override
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
System.out.println("Resolving: " + type + ", " + namespaceURI + ", " + publicId + ", " + systemId + ", " + baseURI + " (basepath:" + basePath + ")");
String mypath = basePath + systemId;
InputStream resourceAsStream = getClass().getResourceAsStream(mypath);
if( resourceAsStream == null){ System.out.println("Ups! Stream is null"); }
String contents = null;
byte[] bytes = null;
try {
bytes = new byte[resourceAsStream.available()];
resourceAsStream.read(bytes);
contents = new String(bytes, Charset.forName("UTF-8"));
} catch (IOException e) { }
finally{
try { resourceAsStream.close(); }
catch (IOException e) { }
}
Input xmlInput = new Input(resourceAsStream, type, namespaceURI, publicId, systemId, basePath);
xmlInput.setStringData(contents); // avoid problems with inputstream position
return xmlInput;
}
}
我的ResourceResolver以这种方式使用:
public boolean validate(Document doc, String xsdPath) throws SAXException, IOException, ParserConfigurationException {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
String rootElmtName = doc.getDocumentElement().getLocalName();
String resourcePath = "/com/example/xml/" + xsdPath ;
InputStream topSchema = getClass().getResourceAsStream(xsdPath + "/" + rootElmtName + ".xsd");
Source schemaSource = new StreamSource(topSchema);
ResourceResolver resolver = new ResourceResolver(resourcePath);
schemaFactory.setResourceResolver(resolver);
Schema schema = schemaFactory.newSchema(schemaSource);
Validator validator = schema.newValidator();
validator.setResourceResolver(resolver);
Source source = new DOMSource(doc);
ValidationErrorHandler handler = new ValidationErrorHandler();
validator.setErrorHandler(handler);
validator.validate(source);
if(handler.hasErrors()){
for( ValidationError e : handler.getErrors()){
log(e);
}
return false;
}
return true;
}
然而,它不起作用! 它解析了一些架构资源然后停止 我尝试在我的资源解析器中在Eclipse中设置断点,并且输入流不为空 - 它指向包含XSD的正确文件。因此,Java XML解析器类中的某些内容必须从我的解析器中获得一些其他行为。
Resolving: http://www.w3.org/2001/XMLSchema, http://com/example/xml/schemas/2005/08/07/, null, Letters.xsd, file:///com/example/xml/Schemas/2005/08/07/Message.xsd (basepath:/com/example/xml/Schemas/2005/08/07/)
Resolving: http://www.w3.org/2001/XMLSchema, http://com/example/xml/schemas/2005/08/07/, null, GeneralElements.xsd, file:///com/example/xml/Schemas/2005/08/07/Message.xsd (basepath:/com/example/xml/Schemas/2005/08/07/)
Resolving: http://www.w3.org/2001/XMLSchema, http://com/example/xml/schemas/2005/08/07/, null, GeneralTypes.xsd, file:///com/example/xml/Schemas/2005/08/07/Message.xsd (basepath:/com/example/xml/Schemas/2005/08/07/)
Exception in thread "main" org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'Letters' to a(n) 'group' component.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:2537)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:2528)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(XSDHandler.java:1472)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDGroupTraverser.traverseLocal(XSDGroupTraverser.java:72)
Message.xsd的内容包含如下内容:
<xs:include schemaLocation="Letters.xsd"/>
<xs:include schemaLocation="GeneralElements.xsd"/>
<xs:include schemaLocation="GeneralTypes.xsd"/>
<xs:element name="Message">
- 而Letters.xsd也包含
<xs:include schemaLocation="GeneralElements.xsd"/>
<xs:include schemaLocation="GeneralTypes.xsd"/>
<xs:group name="Letters">
大纲属于ResourceResolver,输入类来自另一个SO帖子 - 所以我可能误解了一些东西。
这是我创建资源解析器的正确方法,还是我完全错过了什么?