Datasheet

9: import java.io.IOException;
10:
11: import org.apache.xerces.parsers.SAXParser;
12: import org.xml.sax.SAXException;
13: import org.xml.sax.SAXNotRecognizedException;
14: import org.xml.sax.SAXNotSupportedException;
15: import org.xml.sax.XMLReader;
16:
17: public class PassiveSchemaCache {
18:
19: public static void main(String[] args) {
20: System.setProperty(
21: "org.apache.xerces.xni.parser.Configuration",
22: "org.apache.xerces.parsers.XMLGrammarCachingConfiguration");
Lines 20-22 contain the code that turns on passive grammar caching. All you have to do is set the Java
property org.apache.xerces.xni.parser.Configuration to a configuration that understands grammar
caching. One such configuration is org.apache.xerces.parsers.XMLGrammarCachingConfiguration. After
that, the code is essentially the same as what you are used to seeing. This shows how easy it is to use
passive grammar caching. Add three lines and you’re done.
23:
24: XMLReader r = new SAXParser();
25: try {
26: r.setFeature("http://xml.org/sax/features/validation",
27: true);
28: r.setFeature(
29: "http://apache.org/xml/features/validation/schema",
30: true);
31: } catch (SAXNotRecognizedException snre) {
32: snre.printStackTrace();
33: } catch (SAXNotSupportedException snre) {
34: snre.printStackTrace();
35: }
36: BookHandler bookHandler = new BookHandler();
37: r.setContentHandler(bookHandler);
38: r.setErrorHandler(bookHandler);
39:
40: for (int i = 0; i < 5; i++)
41: try {
42: r.parse(args[0]);
43: System.out.println(bookHandler.getBook().toString());
44: } catch (SAXException se) {
45: System.out.println("SAX Error during parsing " +
46: se.getMessage());
47: se.printStackTrace();
48: } catch (IOException ioe) {
49: System.out.println("I/O Error during parsing " +
50: ioe.getMessage());
51: ioe.printStackTrace();
52: } catch (Exception e) {
53: System.out.println("Error during parsing " +
54: e.getMessage());
55: e.printStackTrace();
24
Chapter 1
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 24