package org.starstandard.xml.grammar.cache;
import org.apache.xerces.xni.grammars.XMLGrammarDescription;
import org.apache.xerces.xni.grammars.XMLGrammarPool;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.util.XMLGrammarPoolImpl;
import org.apache.xerces.impl.Constants;
import org.apache.xerces.xni.parser.*;
import org.apache.xerces.parsers.*;
public class GrammarCache {
public GrammarCache() {
}
public static void main
(String[] args
) {
GrammarCache test = new GrammarCache();
test.cacheGrammars();
}
public void cacheGrammars() {
// create grammar preparser
XMLGrammarPreparser preparser = new XMLGrammarPreparser();
// register a specialized pre-parser
preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
// create two grammar pools. One for GetPartsShipment, one for ShowPartsShipment
XMLGrammarPool getPartsShipmentGrammarPool = new XMLGrammarPoolImpl();
XMLGrammarPool showPartsShipmentGrammarPool = new XMLGrammarPoolImpl();
String GRAMMAR_POOL = Constants.
XERCES_PROPERTY_PREFIX
+ Constants.XMLGRAMMAR_POOL_PROPERTY;
// set properties
preparser.setFeature("http://xml.org/sax/features/namespaces", true);
preparser.setFeature("http://xml.org/sax/features/validation", true);
// parse grammar(s)
try {
// set the grammar pool on the grammar preparser
// so that all the compiled grammars are automatically
// placed to the grammar pool
preparser.setProperty(GRAMMAR_POOL, getPartsShipmentGrammarPool);
preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
new XMLInputSource(null, "D:/Work/STAR Schemas/Development/STAR/Rev/BODs/Developer/GetPartsShipment.xsd", null));
getPartsShipmentGrammarPool.lockPool();
// Since there are more than one Grammar that needs to be loaded and it resides in the same namespace
// Each grammar should be have it's own Grammar Pool. So create one for the ShowPartsShipment
preparser.setProperty(GRAMMAR_POOL, showPartsShipmentGrammarPool);
preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
new XMLInputSource(null, "D:/Work/STAR Schemas/Development/STAR/Rev/BODs/Developer/ShowPartsShipment.xsd", null));
showPartsShipmentGrammarPool.lockPool();
// Put exception handling here
}
XMLParserConfiguration config = new StandardParserConfiguration();
// Tell the Parser which Grammar Pool to use:
config.setProperty("http://apache.org/xml/properties/internal/grammar-pool",
getPartsShipmentGrammarPool);
// Create the appropriate parser
DOMParser parser = new DOMParser(config);
try {
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
parser.parse(new XMLInputSource(null, "D:/Work/STAR Schemas/Development/STAR/Rev/BODExamples/GetPartsShipment.xml", null));
ex.printStackTrace();
}
// To setup a Parser so that it uses the ShowPartsShipmentGrammarPool instead create a new configuration so
// That it uses the appropriate grammar pool.
}
}