Jing使用java代码放松NG验证器和自定义数据类型库

时间:2012-05-31 14:04:14

标签: java xml types schema relaxng

阅读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.xmlinvalid.xmljava -jar的路径。所有这些都与Jing一起分发。

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

2 个答案:

答案 0 :(得分:3)

似乎通过Jing通过JAXP API使用自定义数据类型库会以某种方式被破坏。它即使应该也不起作用。也许某些额外的属性需要在某处设置,我只是没有意识到这一点。

所以我猜我通过模仿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类不能直接使用。

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

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

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

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

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

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

答案 1 :(得分:0)

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