Datasheet
32: static final String GRAMMAR_POOL =
33: Constants.XERCES_PROPERTY_PREFIX +
34: Constants.XMLGRAMMAR_POOL_PROPERTY;
35:
36: SymbolTable sym = null;
37: XMLGrammarPoolImpl grammarPool = null;
38: XMLReader reader = null;
39:
40: public void loadCache() {
41: grammarPool = new XMLGrammarPoolImpl();
42: XMLGrammarPreparser preparser = new XMLGrammarPreparser();
43: preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA,
44: null);
45: preparser.setProperty(GRAMMAR_POOL, grammarPool);
46: preparser.setFeature(
47: "http://xml.org/sax/features/validation",
48: true);
49: preparser.setFeature(
50: "http://apache.org/xml/features/validation/schema",
51: true);
52: // parse the grammar...
53:
54: try {
55: Grammar g =
56: preparser.preparseGrammar(
57: XMLGrammarDescription.XML_SCHEMA,
58: new XMLInputSource(null, "book.xsd", null));
59: } catch (XNIException xe) {
60: xe.printStackTrace();
61: } catch (IOException ioe) {
62: ioe.printStackTrace();
63: }
64:
65: }
66:
The loadCache method takes care of creating the data structures needed to cache grammars. The cache
itself is an instance of org.apache.xerces.util.XMLGrammarPoolImpl, created in line 41. The object that
knows the workflow of how to preprocess a grammar file is an instance of XMLGrammarPreparser, so in
line 42 you create an instance of XMLGrammarPreparser.
XMLGrammarPreparsers need to know which kind of grammar they will be dealing with. They have a
method called registerPreparser that allows them to associate a string (representing URIs for particular
grammars) with an object that knows how to preprocess a specific type of grammar. This means a single
XMLGrammarPreparser can preprocess multiple types of grammars (for example, both DTDs and XML
schemas). In this example, you’re only interested in allowing XML schemas to be cached, so you register
XML schemas with the preparser (lines 43-44). If you’re registering either XML schemas or DTDs with a
preparser, then you can pass null as the second argument to registerPreparser. Otherwise, you have to
provide an instance of org,apache.xerces.xni.grammarsXMLGrammarLoader, which can process the
grammar you’re registering.
26
Chapter 1
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 26