Jing RelaxNG validator and custom datatype library from java code

Shannonaoc

New Member
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 \[code\]datatype-sample\[/code\]) through command line but I keep failing to do it from java code.From command line (windows):\[code\]> 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\[/code\]Validation was performed without any problems. But now I'm trying to use the same datatype library from the following java code:\[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)); }}\[/code\]With first argument being a path to a file with the following content:\[code\]<element name="balancedString" xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.thaiopensource.com/relaxng/datatypes/sample"> <data type="balancedString"/></element>\[/code\]And the second argument being a path to a file with the following content:\[code\]<balancedString>foo(bar(baz))</balancedString>\[/code\]Which gives me the following output:\[code\]Classpath: path\to\RNGDataType\lib\datatype-sample.jar;path\to\RNGDataType\lib\jing.jar;path\to\RNGDataType\build\classes;path\to\RNGDataType\srcException in thread "main" org.xml.sax.SAXParseException: datatype library "http://www.thaiopensource.com/relaxng/datatypes/sample" not recognized...\[/code\]This clearly indicates that the datatype could not be resolved. The only requirement for this to work (have both \[code\]jing.jar\[/code\] and \[code\]datatype-sample.jar\[/code\] 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 \[code\]jing.jar\[/code\] and \[code\]datatype-sample.jar\[/code\] on your classpath AND provide arguments to it where the first one is path to \[code\]datatype-sample.rng\[/code\] and the second one is path to \[code\]valid.xml\[/code\] or \[code\]invalid.xml\[/code\]. All of these are distributed with Jing.Edit1: the above program also doesn't work outside my IDE when ran as a JAR (\[code\]java -jar\[/code\]) with a proper \[code\]MANIFEST.MF\[/code\] file. Also doesn't work when classpath is set manually (\[code\]java -classpath\[/code\]). So I suspect something is wrong with the actual code.
 
Back
Top