Datasheet

85: }
86:
87: try {
88: if (reader == null)
89: reader = new SAXParser(parserConfiguration);
Something else is going on here: each instance of ActiveCache has a single SAXParser instance associ-
ated with it. You create an instance of SAXParser only if one doesn’t already exist. This cuts down on the
overhead of setting up and tearing down parser instances all the time.
One other detail. When you reuse a Xerces parser instance, you need to call the reset method in between
usages. Doing so ensures that the parser is ready to parse another document:
90: BookHandler bookHandler = new BookHandler();
91: reader.setContentHandler(bookHandler);
92: reader.setErrorHandler(bookHandler);
93: reader.parse(uri);
94: book = bookHandler.getBook();
95: ((org.apache.xerces.parsers.SAXParser) reader).reset();
96: } catch (IOException ioe) {
97: ioe.printStackTrace();
98: } catch (SAXException se) {
99: se.printStackTrace();
100: }
101: return book;
102: }
103:
104: public static void main(String[] args) {
105: ActiveSchemaCache c = new ActiveSchemaCache();
106: c.loadCache();
107: for (int i = 0; i < 5; i++) {
108: Book b = c.useCache("book.xml");
109: System.out.println(b.toString());
110: }
111:
112: }
113: }
The Xerces grammar-caching implementation uses hashing to determine whether two grammars are the
same. If the two grammars are XML schemas, then they are hashed according to their targetNamespace.
If the targetNamespaces are the same, the grammars are considered to be the same. For DTDs, it’s more
complicated. There are three conditions:
If their publicId or expanded SystemIds exist, they must be identical.
If one DTD defines a root element, it must either be the same as the root element of the second
DTD, or it must be a global element in the second DTD.
If neither DTD defines a root element, they must share a global element between the two of
them.
28
Chapter 1
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 28