Java验证XSD架构包含

时间:2014-08-05 14:45:20

标签: java xml resources xsd include

设置:

java文件,废品代码

   SchemaFactory customSchemaFactory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
   customSchemaFactory.setResourceResolver(new XSDResourceResolver());
   Source customSchemaSource = new StreamSource(this.getClass().getResourceAsStream("/path/to/customXSD.xsd"));
   Schema customSchema  = customSchemaFactory.newSchema(customSchemaSource);
   Validator customValidator = customSchema.newValidator();
   Source source = new StreamSource(new ByteArrayInputStream(data));
   customValidator.validate(source);

XSDResouceResolver看起来很像(经典的通用解析器) - http://pbin.oogly.co.uk/listings/viewlistingdetail/2a70d763929ce3053085bfaa1d78e2

customXSD.xsd是带有几个导入的经典XSD文件,特别困扰我的是MathML模式:

    <xs:import namespace="http://www.w3.org/1998/Math/MathML" schemaLocation="/path/to/mathml3.xsd"/>

默认情况下,此架构包含:

   <xs:include schemaLocation="mathml3-content.xsd"/>
   <xs:include schemaLocation="mathml3-presentation.xsd"/>
   <xs:include schemaLocation="mathml3-common.xsd"/>

如果我注释掉包含并且只留下带有注释主体的MathML模式的导入,那么一切进展顺利。但是如果包含在内,那么有很多例外情况(可能正好四个,因为所有模式中有四个包含)来自:

customSchema = customSchemaFactory.newSchema(customSchemaSource);

java.io.IOException: Stream closed
    at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
    at java.io.BufferedInputStream.available(Unknown Source)
    at ...validation.XSDResourceResolver$LSInputImpl.getStringData(XSDResourceResolver.java:63)
    at org.apache.xerces.util.DOMEntityResolverWrapper.resolveEntity(Unknown Source)
    at org.apache.xerces.impl.XMLEntityManager.resolveEntity(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaLoader.resolveDocument(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.resolveSchemaSource(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.constructTrees(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.constructTrees(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at org.apache.xerces.jaxp.validation.BaseSchemaFactory.newSchema(Unknown Source)
    at javax.xml.validation.SchemaFactory.newSchema(Unknown Source)

我到目前为止已尝试过:

  • 将所有包含位置重命名为/path/to/mathml3-content.xsd等(因为使用此/ path / to的导入正在运行,包括mathml3.xsd本身)
  • 改变包括进口,就像它们一样,我不期望这样做,它没有,但是,))
  • 将所有包含的内容粘贴到mathml3.xsd本身,它非常庞大,它给了我ArrayOutOfBoundsException

问题是 - 有没有办法解决包含其他模式的模式的流错误? (不是进口,他们工作)。我想我只是遗漏了一些重要的东西。也许它与资源路径/位置有关? (但是导入工作正常,所有XSD都在同一个文件夹/路径中,紧挨着另一个)

1 个答案:

答案 0 :(得分:0)

LSInput的实施似乎存在缺陷。

更换

@Override
public InputStream getByteStream() {            
    return null;
}

@Override
public InputStream getByteStream() {
    return inputStream;
}

@Override
public String getStringData() {
    synchronized (inputStream) {
        try {
            byte[] input = new byte[inputStream.available()];
            inputStream.read(input);
            String contents = new String(input);
            return contents;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Exception " + e);
            return null;
        }
    }
}

@Override
public String getStringData() {
    return null;
}

修复了我的测试设置中的错误。