[
Lists Home |
Date Index |
Thread Index
]
We have a situation wherin we should validate all request XMLs against a
w3c schema.
Obviously it will be very expensive to do fileOpen/schemaXML parse etc
with every request XML. After all it is a wasteful excersice as it is
exactly the same schema ( xsd ) file.
We want to be able to open/pre-process the schema file when the
application comes up and apply this pre-processed form of schema against
the incoming XML requests.
After following an example from xerces-J/XNI I was able to do just this.
But I found that XNI is not very well documented.
Also I would like to get the parse tree of the request XML, as a DOM(
jdom is fine too ) Document as a side effect of teh validation. I could
not figure out how to do this with XNI.
1. Is there any book out there that explains XNI.
2. Are there any packages other than XNI that will do this pre-processed
schema validation.
3. I saw a package named irisserv (
http://iris.verisignlabs.com/index.html ) that has a convenient wrapper
classes named XercesParserFactory and XercesParserImpl (
http://iris.verisignlabs.com/docs/irisserv/api/index.html ) that seems
to be doing this. But I could not make it to work with my first attempt.
I was wondering if there is any other package out there acting as a
wrapper for XNI, that is meant specifically for this.
4. As a side effect of the validation I would like to get the dom ( or
jdom ) tree for the XML. This is actually where I stumbled with XNI. How
do I do this with XNI.
I m attaching my working XNI based validation code below. One only needs
to look at init() and isValid() to see its dynamics. ( Agree its a pain
to read long code lines just to help a stranger. But I m just hoping
that somebody might help as before )
Thanks for reading
--sony
import java.util.Properties ;
import java.io.File ;
import java.io.Reader ;
import java.io.StringReader ;
import java.io.FileReader ;
import org.apache.xerces.xni.parser.XMLInputSource ;
import org.apache.xerces.xni.grammars.XMLGrammarDescription ;
import org.apache.xerces.parsers.XMLGrammarPreparser ;
import org.apache.xerces.util.SymbolTable ;
import org.apache.xerces.util.XMLGrammarPoolImpl ;
import org.apache.xerces.impl.Constants ;
import org.apache.xerces.xni.parser.XMLParserConfiguration ;
import org.apache.xerces.parsers.IntegratedParserConfiguration ;
import org.apache.xerces.xni.parser.XMLErrorHandler ;
import org.apache.xerces.xni.parser.XMLParseException ;
public class FastSchemaValidator implements XMLValidator {
static private class ErrorQuitter implements XMLErrorHandler {
public void error( String domain, String key, XMLParseException
e ) throws XMLParseException {
throw e ;
}
public void fatalError( String domain, String key,
XMLParseException e ) throws XMLParseException {
throw e ;
}
public void warning( String domain, String key,
XMLParseException e ) throws XMLParseException {
throw e ;
}
}
private static final String NAMESPACES_FEATURE_ID =
"http://xml.org/sax/features/namespaces";
private static final String VALIDATION_FEATURE_ID =
"http://xml.org/sax/features/validation";
private static final String SCHEMA_VALIDATION_FEATURE_ID =
"http://apache.org/xml/features/validation/schema";
private static final String SCHEMA_FULL_CHECKING_FEATURE_ID =
"http://apache.org/xml/features/validation/schema-full-checking";
private static final String GRAMMAR_POOL =
Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
private static final int SYM_TABL_SIZ = 2039;
private XMLParserConfiguration parserConfiguration_ = null ;
private String clNam_ = getClass().getName() ;
protected String xsd_ = null ;
protected File xsdFil_ = null ;
protected Reader rdr_ = null;
FastSchemaValidator () {
}
FastSchemaValidator ( Reader reader) throws XMLValidatorException {
if( reader == null ) {
throw new XMLValidatorException( "Empty reader cannot be
used for validation" ) ;
}
rdr_ = reader ;
initialize_() ;
}
FastSchemaValidator( File xsdFil ) throws XMLValidatorException {
if( xsdFil == null ) {
throw new XMLValidatorException( "Empty schema file cannot
be used for validation" ) ;
}
xsdFil_ = xsdFil ;
initialize_() ;
}
FastSchemaValidator( String xsd ) throws XMLValidatorException {
if( xsd == null ) {
throw new XMLValidatorException( "Empty schema string cannot
be used for validation" ) ;
}
xsd_ = xsd ;
initialize_() ;
}
FastSchemaValidator( Properties p ) throws XMLValidatorException {
this( new File( p.getProperty( "XSD_FILE_PATH" ) ) ) ;
}
protected void initialize_() throws XMLValidatorException {
try {
SymbolTable sym = new SymbolTable( SYM_TABL_SIZ ) ;
XMLGrammarPreparser preparser = new XMLGrammarPreparser( sym
) ;
XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl() ;
preparser.registerPreparser(
XMLGrammarDescription.XML_SCHEMA, null ) ;
preparser.setProperty( GRAMMAR_POOL, grammarPool ) ;
preparser.setFeature( NAMESPACES_FEATURE_ID, true ) ;
preparser.setFeature( VALIDATION_FEATURE_ID, true ) ;
preparser.setFeature( SCHEMA_VALIDATION_FEATURE_ID, true ) ;
preparser.setFeature( SCHEMA_FULL_CHECKING_FEATURE_ID, true
) ;
if (rdr_ == null)
rdr_ = ( xsdFil_ != null ) ? ( Reader )new FileReader(
xsdFil_ ) : ( Reader )new StringReader( xsd_ ) ;
preparser.preparseGrammar( XMLGrammarDescription.XML_SCHEMA,
new XMLInputSource( null, null, null, rdr_, null ) ) ;
parserConfiguration_ = new IntegratedParserConfiguration(
sym, grammarPool ) ;
parserConfiguration_.setFeature( NAMESPACES_FEATURE_ID, true
) ;
parserConfiguration_.setFeature( VALIDATION_FEATURE_ID, true
) ;
parserConfiguration_.setFeature(
SCHEMA_VALIDATION_FEATURE_ID, true ) ;
parserConfiguration_.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
parserConfiguration_.setErrorHandler( new ErrorQuitter() ) ;
}
catch( Exception e ) {
throw new XMLValidatorException( e ) ;
}
}
public boolean isValid( String xml ) throws XMLValidatorException {
try {
parserConfiguration_.parse( new XMLInputSource( null, null,
null, new StringReader( xml ), null ) ) ;
return true ;
}
catch( Exception e ) {
throw new XMLValidatorException( e ) ;
}
}
public boolean isValid( File xmlFil ) throws XMLValidatorException {
try {
parserConfiguration_.parse( new XMLInputSource( null, null,
null, new FileReader( xmlFil ), null ) ) ;
return true ;
}
catch( Exception e ) {
throw new XMLValidatorException( e ) ;
}
}
}
--
Sony Antony <santony@bellsouth.net>
|