Loading ...
Sorry, an error occurred while loading the content.

Jing and a custom datatype library from java code

Expand Messages
  • preditcon
    After reading this I ve been trying to implement a custom datatype to be used by a RelaxNG XML
    Message 1 of 3 , May 31, 2012
    • 0 Attachment
      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?

      This happens with java version "1.6.0_27".

      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.


      Note: I am posting this here because Jing's website suggests so.
      Note: this question has also been posted at stackoverflow.
    • preditcon
      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
      Message 2 of 3 , Jun 8, 2012
      • 0 Attachment

        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.

        Cheers o/

        --- In rng-users@yahoogroups.com, "preditcon" <jernej.tuljak@...> wrote:
        >
        > After reading this
        > <http://www.ibm.com/developerworks/xml/library/x-custyp/> 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\sampl\
        > e\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?
        >
        > This happens with java version "1.6.0_27".
        >
        >
        > 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
        > <http://code.google.com/p/jing-trang/downloads/detail?name=jing-20091111\
        > .zip> .
        >
        >
        > Note: I am posting this here because Jing's website suggests so.
        > Note: this question has also been posted at stackoverflow
        > <http://stackoverflow.com/questions/10835208/jing-relaxng-validator-and-\
        > custom-datatype-library-from-java-code> .
        >
      • preditcon
        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
        Message 3 of 3 , Jun 11, 2012
        • 0 Attachment

          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 than using Jing native API.

          Jernej

          --- In rng-users@yahoogroups.com, "preditcon" <jernej.tuljak@...> wrote:
          >
          > After reading this
          > <http://www.ibm.com/developerworks/xml/library/x-custyp/> 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\sampl\
          > e\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?
          >
          > This happens with java version "1.6.0_27".
          >
          >
          > 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
          > <http://code.google.com/p/jing-trang/downloads/detail?name=jing-20091111\
          > .zip> .
          >
          >
          > Note: I am posting this here because Jing's website suggests so.
          > Note: this question has also been posted at stackoverflow
          > <http://stackoverflow.com/questions/10835208/jing-relaxng-validator-and-\
          > custom-datatype-library-from-java-code> .
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.