View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   package org.galagosearch.tupleflow.execution;
3   
4   import org.xml.sax.Attributes;
5   import org.xml.sax.SAXException;
6   import org.xml.sax.helpers.DefaultHandler;
7   
8   /***
9    *
10   * @author trevor
11   */
12  public class StackHandler extends DefaultHandler {
13      int levels = 0;
14      StackHandler handler;
15  
16      public void startHandler(String uri, String localName, String qName, Attributes attributes) throws SAXException {
17      }
18  
19      public void endHandler(String uri, String localName, String qName) throws SAXException {
20      }
21  
22      public void endChild(StackHandler handler, String uri, String localName, String qName) throws SAXException {
23      }
24  
25      public void unhandledStartElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
26      }
27  
28      public void unhandledEndElement(String uri, String localName, String qName) throws SAXException {
29      }
30  
31      public void unhandledCharacters(char[] buffer, int offset, int length) throws SAXException {
32      }
33  
34      public void addHandler(StackHandler hand, String uri, String localName, String qName, Attributes attributes) throws SAXException {
35          levels = 1;
36          this.handler = hand;
37          handler.startHandler(uri, localName, qName, attributes);
38      }
39  
40      public void addHandler(StackHandler hand) throws SAXException {
41          levels = 1;
42          this.handler = hand;
43          handler.startHandler(null, null, null, null);
44      }
45  
46      public void characters(char[] buffer, int offset, int length) throws SAXException {
47          if (handler != null) {
48              handler.characters(buffer, offset, length);
49          } else {
50              unhandledCharacters(buffer, offset, length);
51          }
52      }
53  
54      public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
55          if (handler != null) {
56              levels++;
57              handler.startElement(uri, localName, qName, attributes);
58          } else {
59              unhandledStartElement(uri, localName, qName, attributes);
60          }
61      }
62  
63      public void endElement(String uri, String localName, String qName) throws SAXException {
64          if (handler != null) {
65              levels--;
66              handler.endElement(uri, localName, qName);
67  
68              if (levels == 0) {
69                  handler.endHandler(uri, localName, qName);
70                  endChild(handler, uri, localName, qName);
71                  handler = null;
72              }
73          } else {
74              unhandledEndElement(uri, localName, qName);
75          }
76      }
77  }