Print this Page

Sample XML Grammar Pool Setup

  1. package org.starstandard.xml.grammar.cache;
  2.  
  3. import org.apache.xerces.xni.grammars.XMLGrammarDescription;
  4. import org.apache.xerces.xni.grammars.XMLGrammarPool;
  5. import org.apache.xerces.xni.parser.XMLInputSource;
  6. import org.apache.xerces.util.XMLGrammarPoolImpl;
  7. import org.apache.xerces.impl.Constants;
  8. import org.apache.xerces.xni.parser.*;
  9. import org.apache.xerces.parsers.*;
  10.  
  11. public class GrammarCache {
  12.  
  13.    public GrammarCache() {
  14.  
  15.    }
  16.  
  17.    public static void main(String[] args) {
  18.         GrammarCache test = new GrammarCache();
  19.         test.cacheGrammars();
  20.    }
  21.  
  22.    public void cacheGrammars() {
  23.       // create grammar preparser
  24.       XMLGrammarPreparser preparser = new XMLGrammarPreparser();
  25.  
  26.       // register a specialized pre-parser
  27.       preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
  28.  
  29.       // create two grammar pools.  One for GetPartsShipment, one for ShowPartsShipment
  30.       XMLGrammarPool getPartsShipmentGrammarPool = new XMLGrammarPoolImpl();
  31.       XMLGrammarPool showPartsShipmentGrammarPool = new XMLGrammarPoolImpl();
  32.  
  33.       String GRAMMAR_POOL = Constants.XERCES_PROPERTY_PREFIX
  34.         + Constants.XMLGRAMMAR_POOL_PROPERTY;      
  35.  
  36.  
  37.       // set properties
  38.       preparser.setFeature("http://xml.org/sax/features/namespaces", true);
  39.       preparser.setFeature("http://xml.org/sax/features/validation", true);
  40.  
  41.  
  42.       // parse grammar(s)
  43.       try {
  44.         // set the grammar pool on the grammar preparser
  45.         // so that all the compiled grammars are automatically
  46.         // placed to the grammar pool
  47.         preparser.setProperty(GRAMMAR_POOL, getPartsShipmentGrammarPool);
  48.         preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
  49.             new XMLInputSource(null, "D:/Work/STAR Schemas/Development/STAR/Rev/BODs/Developer/GetPartsShipment.xsd", null));
  50.         getPartsShipmentGrammarPool.lockPool();  
  51.  
  52.         // Since there are more than one Grammar that needs to be loaded and it resides in the same namespace
  53.         // Each grammar should be have it's own Grammar Pool.  So create one for the ShowPartsShipment
  54.  
  55.         preparser.setProperty(GRAMMAR_POOL, showPartsShipmentGrammarPool);
  56.         preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
  57.             new XMLInputSource(null, "D:/Work/STAR Schemas/Development/STAR/Rev/BODs/Developer/ShowPartsShipment.xsd", null));
  58.         showPartsShipmentGrammarPool.lockPool();               
  59.  
  60.       } catch (Exception ex) {
  61.           // Put exception handling here
  62.       }
  63.  
  64.       XMLParserConfiguration config = new StandardParserConfiguration();
  65.  
  66.       // Tell the Parser which Grammar Pool to use:
  67.       config.setProperty("http://apache.org/xml/properties/internal/grammar-pool",
  68.       getPartsShipmentGrammarPool);
  69.  
  70.       // Create the appropriate parser
  71.       DOMParser parser = new DOMParser(config);
  72.  
  73.       try {
  74.           parser.setFeature("http://xml.org/sax/features/validation", true);
  75.           parser.setFeature("http://apache.org/xml/features/validation/schema", true);
  76.           parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
  77.           parser.parse(new XMLInputSource(null, "D:/Work/STAR Schemas/Development/STAR/Rev/BODExamples/GetPartsShipment.xml", null));
  78.  
  79.       } catch (Exception ex) {
  80.           ex.printStackTrace();
  81.       }
  82.  
  83.       // To setup a Parser so that it uses the ShowPartsShipmentGrammarPool instead create a new configuration so
  84.       // That it uses the appropriate grammar pool.
  85.   }
  86. }