首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RelaxNG验证器和自定义数据类型库的java代码

RelaxNG验证器和自定义数据类型库的java代码
EN

Stack Overflow用户
提问于 2012-05-31 14:04:14
回答 2查看 2.2K关注 0票数 3

在阅读之后,我一直在尝试实现一个自定义数据类型,以供RelaxNG XML验证器(Jing)使用。我已经成功地通过命令行运行了Jing (他们称之为datatype-sample)提供的示例实现,但我一直无法从java代码中执行它。

从命令行(windows):

代码语言:javascript
复制
> set CLASSPATH=path\to\jing-20091111\bin\jing.jar;path\to\jing-20091111\sample\datatype\datatype-sample.jar
> cd path\to\jing-20091111\sample\datatype
> java com.thaiopensource.relaxng.util.Driver datatype-sample.rng valid.xml

执行验证时没有任何问题。但是现在我尝试使用来自以下java代码的相同的数据类型库:

代码语言:javascript
复制
package rngdatatype;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

public class Main {

    public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, SAXException, IOException {
        // make sure our jars are on classpath
        System.out.println("Classpath: " + System.getProperty("java.class.path"));

        // args
        String rng = args[0];
        String xml = args[1];
        File rngFile = new File(rng);
        File xmlFile = new File(xml);

        // setup rng validator through JAXP
        System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.XMLSyntaxSchemaFactory");
        SchemaFactory rngSchemaFactory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);

        // obtain a schema object
        InputStreamReader rngReader = new InputStreamReader(new FileInputStream(rngFile), "UTF-8");
        Schema schema = rngSchemaFactory.newSchema(new StreamSource(rngReader));

        // validate using schema based validator
        Validator validator = schema.newValidator();
        InputStreamReader xmlReader = new InputStreamReader(new FileInputStream(xmlFile), "UTF-8");
        validator.validate(new StreamSource(xmlReader));
    }
}

第一个参数是具有以下内容的文件的路径:

代码语言:javascript
复制
<element name="balancedString"
   xmlns="http://relaxng.org/ns/structure/1.0"
   datatypeLibrary="http://www.thaiopensource.com/relaxng/datatypes/sample">
  <data type="balancedString"/>
</element>

第二个参数是具有以下内容的文件的路径:

代码语言:javascript
复制
<balancedString>foo(bar(baz))</balancedString>

这给了我以下输出:

代码语言:javascript
复制
Classpath: path\to\RNGDataType\lib\datatype-sample.jar;path\to\RNGDataType\lib\jing.jar;path\to\RNGDataType\build\classes;path\to\RNGDataType\src
Exception in thread "main" org.xml.sax.SAXParseException: datatype library "http://www.thaiopensource.com/relaxng/datatypes/sample" not recognized
...

这清楚地表明数据类型无法解析。据我所知,实现此功能的唯一要求(在类路径上同时具有jing.jardatatype-sample.jar )已经满足。那我做错什么了?

P.S:要使上面的代码正常工作,您必须将jing.jardatatype-sample.jar放在类路径上,并提供参数,其中第一个参数是指向datatype-sample.rng的路径,第二个参数是指向valid.xmlinvalid.xml的路径。所有这些都是用静静发布的。

Edit1:当以JAR (java -jar)的形式运行带有适当的MANIFEST.MF文件时,上面的程序在IDE之外也不工作。当手动设置类路径(java -classpath)时,也不起作用。所以我怀疑实际代码有问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-08 09:22:56

看来,通过JAXP通过Jing使用自定义的数据类型库似乎是有问题的。它不工作,即使它应该。也许需要在某个地方设置一些额外的属性,而我只是不知道。

因此,我想我找到了一个解决办法,通过模仿静的com.thaiopensource.relaxng.util.Driver,从而使用自己的API来执行验证。请注意,这样做限制了您的代码,因此它只适用于Jing。

代码语言:javascript
复制
package rngdatatype;

import com.thaiopensource.validate.SchemaReader;
import com.thaiopensource.validate.ValidationDriver;
import com.thaiopensource.validate.auto.AutoSchemaReader;
import java.io.File;
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class JingApi {

    public static void main(String[] args) throws SAXException, IOException {
        String rng = args[0];
        String xml = args[1];
        File rngFile = new File(rng);
        File xmlFile = new File(xml);

        SchemaReader sr = new AutoSchemaReader();
        ValidationDriver driver = new ValidationDriver(sr);
        InputSource inRng = ValidationDriver.fileInputSource(rngFile);
        inRng.setEncoding("UTF-8");
        driver.loadSchema(inRng);
        InputSource inXml = ValidationDriver.fileInputSource(xmlFile);
        inXml.setEncoding("UTF-8");
        driver.validate(inXml);
    }
}

这使您能够基于使用自定义数据类型库的RNG模式验证java代码中的XML文件。注意,前面提到的Diver类不能直接使用。

上面的程序使用与我自己问题中的示例相同的类路径和参数。

--------------------------------------------- Edit1

在进一步修改之后,我找到了需要设置的属性,以便在使用自定义数据类型库时让JAXP示例与Jing一起使用。只需在获得SchemaFactory实例后添加以下行

代码语言:javascript
复制
rngSchemaFactory.setProperty("http://relaxng.org/properties/datatype-library-factory", new org.relaxng.datatype.helpers.DatatypeLibraryLoader());

这是一个更优雅的解决方案,它使用了Jing本地API。

-------------------------------------------- /Edit1

票数 3
EN

Stack Overflow用户

发布于 2012-06-01 08:17:57

您的JAR文件必须以文件META-INF/services/org.relaxng.datatype.DatatypeLibraryFactory,的形式包含其他元数据,该文件必须包含实现接口org.relaxng.datatype.DatatypeLibraryFactory的类的名称。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10835208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档