Beginner's problem when using jing Relax NG validator through JARVinterface

J

Jari Kujansuu

I am trying to learn to use jing through JARV interface and I have read
JARV User's Guide (http://iso-relax.sourceforge.net/JARV/JARV.html).
I have downloaded jingjarv-examples.zip from JARV User's Guide page and
latest jing version 20030619 from jing home page.

I can run jing successfully from command line like this (which I guess
proves that schema should be correct)

C:\Temp\APU> java -jar jing.jar schema.xsd valid.xml

C:\Temp\APU> java -jar jing.jar schema.xsd invalid.xml
C:\Temp\APU\invalid.xml:4:12: error: required elements missing

But for some reason I don't get any java program to perform validation
using JARV interface. For example if I try to run precompiled
SingleThreadDriver that comes in jingjarv-examples.zip package this is
what happens.

C:\Temp\APU>java SingleThreadDriver schema.xsd valid.xml
driver is com.thaiopensource.relaxng.jarv.VerifierFactoryImpl

C:\Temp\APU>java SingleThreadDriver schema.xsd invalid.xml
driver is com.thaiopensource.relaxng.jarv.VerifierFactoryImpl

So it doesn't throw any exception but it doesn't print any messages
either after finding driver (Of course I don't know if this class is
compiled from same source code that is included in package).

I can successfully compile the SingleThreadDriver.java by myself but
after that the class throws java.lang.NullPointerException if I try to
run it. I have added few debug printing lines to the code to locate the
source of the exception (I have added the modified
SingleThreadDriver.java, schema.xsd, valid.xml and invalid.xml files to
the end of the message). So this is what happens:

C:\Temp\APU>set CLASSPATH

CLASSPATH=C:\APPS\j2sdk1.4.1_03\lib\tools.jar;.\jing.jar;.\isorelax.jar;.\saxon.jar;.\xercesImpl.jar;.\xml-apis.jar;.

C:\Temp\APU>javac SingleThreadDriver.java

C:\Temp\APU>java SingleThreadDriver schema.xsd invalid.xml
driver is com.thaiopensource.relaxng.jarv.VerifierFactoryImpl
parsing schema.xsd
schema != null
Exception in thread "main" java.lang.NullPointerException
at
com.thaiopensource.relaxng.jarv.VerifierHandlerImpl.reset(Unknown Source)
at
com.thaiopensource.relaxng.impl.PatternValidator.<init>(Unknown Source)
at
com.thaiopensource.relaxng.jarv.VerifierHandlerImpl.<init>(Unknown Source)
at
com.thaiopensource.relaxng.jarv.VerifierImpl.<init>(Unknown Source)
at
com.thaiopensource.relaxng.jarv.SchemaImpl.newVerifier(Unknown Source)
at SingleThreadDriver.main(SingleThreadDriver.java:39)

The NullPointerException occurs in this line

Verifier verifier = schema.newVerifier();

and based on the debug printing at least schema object is not null so
exception must occur inside newVerifier method.

So finally the actual question. Do you know based on my explanation what
goes wrong? Can I use jing alone directly from java program or do I need
some other libraries than what comes in jing-20030619.zip package
(isorelax.jar, jing.jar, saxon.jar, xercesImpl.jar, xml-apis.jar)?

One thing that I noticed is that there is different namespace URI used
in SingleThreadDriver.java and schema.xsd (I copied schema example from
internet). I tried to change these URIs also but I didn't notice any
effects with changing the URIs to match each other.

And here are the example files:
-------------------------------

---SingleThreadDriver.java starts after this line---

import org.iso_relax.verifier.*;
import java.io.File;

public class SingleThreadDriver
{
public static void main( String[] args ) throws Exception {
if(args.length<1) {
System.out.println("SingleThreadDriver <schema> <document>
....");
return;
}

// find a JARV implementation. This will find JingJarv if
// it is in the classpath.
VerifierFactory factory = VerifierFactory.newInstance(
"http://relaxng.org/ns/structure/0.9");
if(factory==null) {
System.out.println("No JARV implementation found");
return;
}

System.out.println("driver is "+factory.getClass().getName());

// use VerifierFactory to compile schemas.
// if errors are found in the schema, an exception will be thrown.
// thus in a real application, you should catch it.
System.out.println("parsing "+args[0]);
Schema schema = factory.compileSchema(new File(args[0]));

// TEST!!! Check if schema is null and thus causes null pointer
exception
if (schema == null) {
System.out.println("schema == null");
} else {
System.out.println("schema != null");
}

// the newVerifier method will create a Verifier object,
// which will perform actual validation.
Verifier verifier = schema.newVerifier();

// TEST!!! Test if we succeeded to create new Verifier
System.out.println("Did we reach this point?");

for( int i=1; i<args.length; i++ ) {
System.out.println("validating "+args);

// validate documents
if( verifier.verify(new
File(args).toURL().toExternalForm()) )
System.out.println("valid");
else
System.out.println("invalid");
}
}
}

---schema.xsd starts after this line---
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<element name="addressBook">
<zeroOrMore>
<element name="card">
<element name="name">
<text/>
</element>
<element name="email">
<text/>
</element>
</element>
</zeroOrMore>
</element>
</start>
</grammar>

---valid.xml starts after this line---
<?xml version="1.0" encoding="UTF-8"?>
<addressBook>
<card>
<name>John Smith</name>
<email>[email protected]</email>
</card>
<card>
<name>Fred Bloggs</name>
<email>[email protected]</email>
</card>
</addressBook>

---invalid.xml starts after this line---
<?xml version="1.0" encoding="UTF-8"?>
<addressBook>
<card>
<email>[email protected]</email>
</card>
<card>
<name>Fred Bloggs</name>
<email>[email protected]</email>
</card>
</addressBook>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top