Question

After reading this I've been trying to implement a custom datatype to be used by a RelaxNG XML validator (Jing). I've successfully ran the example implementation which is provided by Jing (they call it datatype-sample) through command line but I keep failing to do it from java code.

From command line (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

Validation was performed without any problems. But now I'm trying to use the same datatype library from the following java code:

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

With first argument being a path to a file with the following content:

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

And the second argument being a path to a file with the following content:

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

Which gives me the following output:

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
...

This clearly indicates that the datatype could not be resolved. The only requirement for this to work (have both jing.jar and datatype-sample.jar on classpath) has been satisfied as far as I can tell. So what am I doing wrong?

P.S: for the above code to work you have to put jing.jar and datatype-sample.jar on your classpath AND provide arguments to it where the first one is path to datatype-sample.rng and the second one is path to valid.xml or invalid.xml. All of these are distributed with Jing.

Edit1: the above program also doesn't work outside my IDE when ran as a JAR (java -jar) with a proper MANIFEST.MF file. Also doesn't work when classpath is set manually (java -classpath). So I suspect something is wrong with the actual code.

Was it helpful?

Solution

It appears that using custom datatype libraries via Jing through JAXP API is broken somehow. It doesn't work even though it should. Perhaps some additional properties need to be set somewhere and I'm just not aware of this.

So I guess I found a workaround by mimicking Jing's com.thaiopensource.relaxng.util.Driver and therefore using Jing's own API to perform the validation. Note that doing so restricts your code so it works only with 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);
    }
}

This enables you to validate your XML files from java code based on a RNG schema which uses custom datatype libraries. Note that the Diver class I mentioned earlier cannot be used directly.

The above program uses the same classpath and arguments as the example in my own question.

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

After fiddling around a bit more I found the property that needs to be set in order to get my JAXP example to play along with Jing when using custom datatype libraries. Simply add the following line after you obtain an instance of SchemaFactory:

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

This is a much more elegant solution that using Jing native API.

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

OTHER TIPS

Your JAR file must include additional metadata in the form of a file META-INF/services/org.relaxng.datatype.DatatypeLibraryFactory, which must contain the name of the class that implements the interface org.relaxng.datatype.DatatypeLibraryFactory

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top