Skip to main content

Problem with StAXValidatorHelper

3 replies [Last post]
amorlupi
Offline
Joined: 2007-06-06
Points: 0

Hi,

we are using jaxp 1.4.2_20070530 to parse and validate an xml file against an xsd grammar.
Code, we are using, is as follows:

XMLInputFactory factory = XMLInputFactory.newInstance();
try{
// TODO code application logic here
StreamSource stsource = new StreamSource(new FileInputStream(filename));
XMLStreamReader streamReader = factory.createXMLStreamReader(stsource);
Handler handler = new Handler();
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
schemaFactory.setErrorHandler(handler);

Schema schemaGrammar = schemaFactory.newSchema(new File(schemafilename));

System.out.println("Created Grammar object for schema : " + schemafilename);

Resolver resolver = new Resolver();
//create a validator to validate against grammar sch.
Validator schemaValidator = schemaGrammar.newValidator();
schemaValidator.setResourceResolver(resolver);
schemaValidator.setErrorHandler(handler);
System.out.println("Validating " + filename + " against grammar " + schemafilename);
schemaValidator.validate(new StAXSource(streamReader));

while(streamReader.hasNext()){
System.out.println(streamReader.next());
}

} catch (Exception e) {
// TODO Blocco catch generato automaticamente
e.printStackTrace();
}

We get the following exception:
ERROR: ''
java.lang.NullPointerException
at com.sun.org.apache.xerces.internal.jaxp.validation.StAXValidatorHelper.validate(StAXValidatorHelper.java:95)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:114)
at javax.xml.validation.Validator.validate(Validator.java:127)
at teststax.TestStAX.main(TestStAX.java:110)

Any help???

Thank you in advance.

Antonio

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hille
Offline
Joined: 2007-06-24
Points: 0

Hi,

ran into the same problem (otherwise I'd now shamelessly hijacking your thread ;-)). The problem is imho getting your "Source" from the "XMLStreamReader" when saying "schemaValidator.validate(new StAXSource(streamReader));". Changing things around, i.e. first getting your "Source" via the filename and then getting your "XMLStreamReader" from the source (via "factory.createXMLStreamReader( source );") makes things work.

To illustrate things look at the following code; "methodB" does the validation more or less like you wrote, "methodA" follows above advice. When run against some valid xml-file and schema, the call to the first succeeds to the latter fails (with exactly your exception, when run on java 1.6.0_01 with jaxp 1.4.2). Both mathods differ only in two lines of code.


import java.io.File;
import java.io.FileInputStream;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class StaxExample {

public static void main( String[] args ) throws Exception {
if( args.length != 2 ) {
System.out.println( "Usage : StaxExample " );
} else {
methodA( args[0], args[1] );
methodB( args[0], args[1] );
}
}

private static void methodA( String xmlFile, String xsdFile ) throws Exception {
XMLInputFactory xmlif = XMLInputFactory.newInstance();
xmlif.setProperty( XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
Boolean.FALSE );

SchemaFactory schemaFactory = SchemaFactory
.newInstance( "http://www.w3.org/2001/XMLSchema" );
Schema schemaGrammar = schemaFactory
.newSchema( new File( xsdFile ) );

Validator schemaValidator = schemaGrammar.newValidator();

Source source = new StreamSource( xmlFile );
XMLStreamReader xmlr = xmlif.createXMLStreamReader( source );

schemaValidator.validate( source );

while( xmlr.hasNext() ) {
int eventType = xmlr.next();
System.out.println( "Event of type: " + eventType );
}
}

private static void methodB( String xmlFile, String xsdFile ) throws Exception {
XMLInputFactory xmlif = XMLInputFactory.newInstance();
xmlif.setProperty( XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
Boolean.FALSE );

SchemaFactory schemaFactory = SchemaFactory
.newInstance( "http://www.w3.org/2001/XMLSchema" );
Schema schemaGrammar = schemaFactory
.newSchema( new File( xsdFile ) );

Validator schemaValidator = schemaGrammar.newValidator();

XMLStreamReader xmlr = xmlif.createXMLStreamReader( new FileInputStream( xmlFile) );
Source source = new StAXSource( xmlr );

schemaValidator.validate( source );

while( xmlr.hasNext() ) {
int eventType = xmlr.next();
System.out.println( "Event of type: " + eventType );
}
}
}

Wished someone more knowledgable took a closer look at the code and commented on it, because by reading the docs I'd say both methods ought to work.

rongxing
Offline
Joined: 2008-09-19
Points: 0

This has been identified as a bug and will be fixed in next JAXP 1.4 release. The fix was put there on Jul, 2008 so it's available in current nightly or weekly build.

http://bugs.sun.com/view_bug.do?bug_id=6708840

The reason of the defect was because that the implementation requires a Result sink. As a workaround before the fix, you could create a Result instance and call method Validator.validate(source, result) instead. Please look to:

http://fisheye5.cenqua.com/browse/jaxp-sources/jaxp-ri/src/unit-test/jav...

joehw
Offline
Joined: 2004-12-15
Points: 0

It would be helpful to others who would try to help if you could provide workable testcase, that is, code and files that people can get and run to reproduce your issue.