Hi ,
This may not solve your problem or answer your questions , but attached is a small program which i was working on , see if it is of any help.
Regards venu
[EMAIL PROTECTED] wrote:
Hi,
Perhaps this issue has been discussed on the list but the archives aren't available, so I have to post. Here goes...
I am trying to use what is called 'active' schema caching, as demonstrated by the XMLGrammarBuilder example, except, I want to use DOMParser and use the DOM of the XML instances that are successfully validated. This the code I'm using
SymbolTable symtab = new SymbolTable(); XMLGrammarPreparser preparser = new XMLGrammarPreparser(symtab); XMLGrammarPoolImpl pool = new XMLGrammarPoolImpl();
preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); preparser.setProperty(GRAMMAR_POOL, pool); 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); preparser.setErrorHandler(this);
System.out.println("Pre-compiling Schema from file " + xsd); Grammar g = preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, new XMLInputSource(null, xsd, null)); pool.lockPool();
XMLParserConfiguration parsercfg = new StandardParserConfiguration(symtab, pool); parsercfg.setFeature(NAMESPACES_FEATURE_ID, true); parsercfg.setFeature(VALIDATION_FEATURE_ID, true); parsercfg.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true); parsercfg.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true); parsercfg.setEntityResolver(new XMLEntityResolver() { public XMLInputSource resolveEntity(XMLResourceIdentifier id) throws XNIException { System.out.println("Resolving resource: " + id.getLiteralSystemId()); throw new XNIException("Not resolving entity..."); //return null; } });
First question is about the XMLEntityResolver. The FAQ instructs to register a "do-nothing" EntityResolver. What does it mean? If I simply return null (do nothing?) the XML instance document that should fail validation quietly validates. Does it mean I should throw exception, as I do? That seems to work but is it "do-nothing"?
More importantly, since not only need to validate XML but also get DOMs of the valid ones, I followed the FAQ again and registered the DOMParser:
parser = new DOMParser(parsercfg); parsercfg.setDocumentHandler(parser);
.... // f is File XMLInputSource xis = new XMLInputSource(null, f.getName(), null); System.out.println("Parsing instance XML in file " + f.getName() + " " + xis.getSystemId());
parsercfg.parse(xis);
But doe not work at all! I get a NullPointerException and the XMLEntityResolver isn't called at all:
Pre-compiling Schema from file 2.xsd Parsing instance XML in file 2.xml 2.xml java.lang.NullPointerException at org.apache.xerces.parsers.AbstractDOMParser.startDocument(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaValidator.startDocument(Unknown Source) at org.apache.xerces.impl.XMLNamespaceBinder.startDocument(Unknown Source) at org.apache.xerces.impl.dtd.XMLDTDValidator.startDocument(Unknown Source) at org.apache.xerces.impl.XMLDocumentScannerImpl.startEntity(Unknown Source) at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source) at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(Unknown Source) at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(Unknown Source) at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source) [java] at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source) at gov.treas.fms.sps.utils.SchemaVerifier.parse(SchemaVerifier.java:189) at gov.treas.fms.sps.utils.SchemaVerifier.parse(SchemaVerifier.java:199) ] at gov.treas.fms.sps.utils.SchemaVerifier.parse(SchemaVerifier.java:207) at gov.treas.fms.sps.utils.SchemaVerifier.main(SchemaVerifier.java:337)
I tried to test the entity resolver of the DOMParser but it isn't called either: parser.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String pubid, String sysid) throws SAXException, IOException { System.out.println("DOM parser entity resolver: pub id " + pubid + ", sysid: " + sysid); return null; } });
So, can somebody explain me what am I doing something wrong? And what should I be doing to get what I want. Almost forgot, I'm using Xerces 2.2.1 and changing parser with my client is very-very-very difficult - government agency. Please. lend a helping hand. TIA,
Regards,
-a
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
xni.jar
Description: Binary data--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
