Datasheet

Now you’re ready to associate a grammar pool with the preparser. This is done using the preparser’s
setProperty method and supplying the appropriate values (line 45). XMLGrammarPreparser provides a
feature/property API like the regular SAX and DOM parsers in Xerces. The difference is that when you
set a feature or property on an instance of XMLGrammarPreparser, you’re actually setting the feature or
property on all XMLGrammarLoader instances that have been registered with the preparser. So the next
two setFeature calls (in lines 46-51) tell all registered XMLGrammarLoaders to validate their inputs and
to do so using XML Schema if possible. Note that implementers of XMLGrammarLoader aren’t required
to implement any features or properties (just as with SAX features and properties).
Once all the configuration steps are complete, all that is left to do is to call the preparseGrammar method
for all the grammars you want loaded into the cache. Note that you need to use the XMLInputSource
class from org.apache.xni.parser to specify how to get the grammar file. This all happens in lines 54-63.
How do you make use of a loaded cache? It turns out to be fairly simple, but it means a more circuitous
route to creating a parser. The XMLParserConfiguration interface has a setProperty method that accepts
a property named http://apache.org/xml/properties/internal/grammar-pool, whose value is a gram-
mar pool the parser configuration should use. The constructors for the various Xerces parser classes can
take an XMLParserConfiguration as an argument. So, you need to get hold of a parser configuration, set
the grammar pool property of that configuration to the grammar pool that loadCache created, and then
create a SAX or DOM parser based on that configuration. Pretty straightforward, right?
The first thing you need is an XMLParserConfiguration. You can use the Xerces supplied
org.apache.xerces.parsers.StandardParserConfiguration because you aren’t doing anything else fancy:
67: public synchronized Book useCache(String uri) {
68: Book book = null;
69: XMLParserConfiguration parserConfiguration =
70: new StandardParserConfiguration();
Next you need to set the grammar pool property on the parserConfiguration to be the grammarPool cre-
ated by loadCache:
71:
72: String grammarPoolProperty =
73: "http://apache.org/xml/properties/internal/grammar-pool";
74: try {
75: parserConfiguration.setProperty(grammarPoolProperty,
76: grammarPool);
In this example you’re using a SAX parser to process documents. The constructor for the Xerces SAX
parser takes an XMLParserConfiguration as an argument, so you just pass the parserConfiguration as
the argument, and now you have a SAXParser that’s using the grammar cache!
77: parserConfiguration.setFeature(
78: "http://xml.org/sax/features/validation",
79: true);
80: parserConfiguration.setFeature(
81: "http://apache.org/xml/features/validation/schema",
82: true);
83: } catch (XMLConfigurationException xce) {
84: xce.printStackTrace();
27
Xerces
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 27