【问题标题】:Jing RelaxNG validator and custom datatype library from java code来自 java 代码的 Jing RelaxNG 验证器和自定义数据类型库
【发布时间】:2012-06-05 19:19:29
【问题描述】:

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

从命令行(Windows):

> 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 代码中的相同数据类型库:

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));
    }
}

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

<element name="balancedString"
   xmlns="http://relaxng.org/ns/structure/1.0"
   datatypeLibrary="http://www.thaiopensource.com/relaxng/datatypes/sample">
  <data type="balancedString"/>
</element>

第二个参数是文件的路径,内容如下:

<balancedString>foo(bar(baz))</balancedString>

这给了我以下输出:

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)已经得到满足。那我做错了什么?

PS:要使上述代码正常工作,您必须将jing.jardatatype-sample.jar 放在您的类路径中,并为其提供参数,其中第一个是datatype-sample.rng 的路径,第二个是valid.xml 的路径或invalid.xml。所有这些都与Jing一起分发。

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

【问题讨论】:

    标签: java xml types schema relaxng


    【解决方案1】:

    似乎通过 JAXP API 通过 Jing 使用自定义数据类型库在某种程度上被破坏了。即使它应该起作用,它也不起作用。也许需要在某处设置一些额外的属性,而我只是不知道这一点。

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

    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类不能直接使用。

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

    编辑1 ------------------------------- --

    在反复摆弄之后,我发现了需要设置的属性,以便在使用自定义数据类型库时让我的 JAXP 示例与 Jing 一起使用。获取SchemaFactory的实例后,只需添加以下行:

    rngSchemaFactory.setProperty("http://relaxng.org/properties/datatype-library-factory", new org.relaxng.datatype.helpers.DatatypeLibraryLoader());
    

    这是一个比使用 Jing 原生 API 更优雅的解决方案。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 它已经做到了。 datatype-sample.jar 如所解释的是来自 Jing 的实现者的示例数据类型库实现,具有您的建议。否则从命令行运行示例也不起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2016-05-07
      • 1970-01-01
      相关资源
      最近更新 更多