Datasheet
56: }
57: }
58:
59: }
Although passive caching is easy to use, it has one major drawback: You can’t specify which grammars
Xerces can cache. When you’re using passive caching, Xerces happily caches any grammar it finds in any
document. If you’re processing a high volume of documents, let’s say purchase orders, then you proba-
bly are using only one grammar, and you probably don’t want the author of those purchase order docu-
ments to be the one who determines which grammar file is used (and possibly cached).
The solution to this problem is to use active grammar caching. Active grammar caching requires you to
do more work in your application, but in general it’s worth it because you get complete control over
which grammars can be cached, as well as control over exactly which grammar files are used to populate
the grammar caches.
When you’re using active caching, you need to follow two steps. First, you create a grammar cache (an
instance of org.apache.xerces.util.XMLGrammarPoolImpl) and load it by pre-parsing all the grammar
files you want to cache. Then you call Xerces and make sure it’s using the cache you just created.
Here’s a program that makes use of active caching:
1: /*
2: *
3: * ActiveSchemaCache.java
4: *
5: * Example from "Professional XML Development with Apache Tools"
6: *
7: */
8: package com.sauria.apachexml.ch1;
9: import java.io.IOException;
10:
11: import org.apache.xerces.impl.Constants;
12: import org.apache.xerces.parsers.SAXParser;
13: import org.apache.xerces.parsers.StandardParserConfiguration;
14: import org.apache.xerces.parsers.XMLGrammarPreparser;
15: import org.apache.xerces.util.SymbolTable;
16: import org.apache.xerces.util.XMLGrammarPoolImpl;
17: import org.apache.xerces.xni.XNIException;
18: import org.apache.xerces.xni.grammars.Grammar;
19: import org.apache.xerces.xni.grammars.XMLGrammarDescription;
20: import org.apache.xerces.xni.parser.XMLConfigurationException;
21: import org.apache.xerces.xni.parser.XMLInputSource;
22: import org.apache.xerces.xni.parser.XMLParserConfiguration;
23: import org.xml.sax.SAXException;
24: import org.xml.sax.XMLReader;
25:
26:
27: public class ActiveSchemaCache {
28: static final String SYMBOL_TABLE =
29: Constants.XERCES_PROPERTY_PREFIX +
30: Constants.SYMBOL_TABLE_PROPERTY;
31:
25
Xerces
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 25