Partial implementation of XML parse for FindBugs output
[cfb.git] / prod / net / jaekl / qd / xml / ParseHandler.java
1 // Copyright (C) 2004, 2015 Christian Jaekl
2
3 package net.jaekl.qd.xml;
4
5 import java.util.Stack;
6
7 import org.xml.sax.Attributes;
8 import org.xml.sax.ContentHandler;
9 import org.xml.sax.Locator;
10 import org.xml.sax.SAXException;
11
12 public class ParseHandler implements ContentHandler
13 {
14         Stack<ParseResult> m_stack;
15
16         public ParseHandler(ParseResult root) {
17                 m_stack = new Stack<ParseResult>();
18                 m_stack.push(root);
19         }
20
21         @Override 
22         public void characters(char[] ch, int start, int length) throws SAXException
23         {
24                 if (m_stack.isEmpty()) {
25                         return;
26                 }
27                 
28                 try {
29                         m_stack.peek().characters(ch, start, length);
30                 }
31                 catch (XmlParseException xpe) {
32                         throw new SAXException(xpe);
33                 }
34         }
35
36         @Override 
37         public void endElement(String uri, String localName, String qName) throws SAXException
38         {
39                 try {
40                         if (m_stack.isEmpty()) {
41                                 return;
42                         }
43                         
44                         boolean pop = m_stack.peek().endElement(uri, localName, qName);
45                         if (pop) {
46                                 m_stack.pop();
47
48                                 if (m_stack.isEmpty()) {
49                                         return;
50                                 }
51                                 
52                                 m_stack.peek().endExternal(uri, localName, qName);
53                         }
54                 }
55                 catch (XmlParseException xpe) {
56                         throw new SAXException(xpe);
57                 }
58         }
59
60         @Override
61         public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
62         {
63                 try {
64                         ParseResult current = m_stack.peek();
65                         ParseResult next = current.startElement(uri, localName, qName, attributes);
66                         if (next != current) {
67                                 m_stack.push(next);
68                         }
69                 }
70                 catch (XmlParseException xpe) {
71                         throw new SAXException(xpe);
72                 }
73         }
74
75         @Override
76         public void endDocument() throws SAXException {
77                 if (! m_stack.isEmpty()) {
78                         String missingTag = m_stack.peek().getTagName();
79                         throw new SAXException(new MissingInfoException(missingTag));
80                 }
81         }
82
83         @Override
84         public void endPrefixMapping(String prefix) throws SAXException {
85                 // no-op
86         }
87
88         @Override
89         public void ignorableWhitespace(char[] ch, int start, int length)
90         throws SAXException 
91         {
92                 // no-op
93         }
94
95         @Override
96         public void processingInstruction(String target, String data)
97         throws SAXException 
98         {
99                 // no-op
100         }
101
102         @Override
103         public void setDocumentLocator(Locator locator) {
104                 // no-op
105         }
106
107         @Override
108         public void skippedEntity(String name) throws SAXException {
109                 // no-op
110         }
111
112         @Override
113         public void startDocument() throws SAXException {
114                 // no-op
115         }
116
117         @Override
118         public void startPrefixMapping(String prefix, String uri)
119         throws SAXException 
120         {
121                 // no-op
122         }
123 }