Source code: com/virtuosotechnologies/asaph/standardmodel/ParseHandlerBase.java
1 /*
2 ================================================================================
3
4 FILE: ParseHandlerBase.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Base class for parse handler tree
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.asaph.standardmodel;
43
44
45 import org.xml.sax.Attributes;
46 import org.xml.sax.SAXException;
47 import org.xml.sax.SAXParseException;
48 import org.xml.sax.ErrorHandler;
49 import org.xml.sax.Locator;
50
51
52 /**
53 * Base class for parse handler tree
54 */
55 /*package*/ abstract class ParseHandlerBase
56 {
57 private boolean defunct_;
58 private ParseHandlerBase delegate_;
59 private String localElement_;
60 private Locator locator_;
61 private ErrorHandler errorHandler_;
62
63
64 /*package*/ ParseHandlerBase(
65 ErrorHandler errorHandler,
66 Locator locator,
67 String localElement)
68 {
69 defunct_ = false;
70 delegate_ = null;
71 localElement_ = localElement;
72 locator_ = locator;
73 errorHandler_ = errorHandler;
74 }
75
76
77 /*package*/ void setDocumentLocator(
78 Locator locator)
79 {
80 locator_ = locator;
81 }
82
83
84 /*package*/ ErrorHandler getErrorHandler()
85 {
86 return errorHandler_;
87 }
88
89
90 /*package*/ Locator getDocumentLocator()
91 {
92 return locator_;
93 }
94
95
96 /*package*/ String getElementName()
97 {
98 return localElement_;
99 }
100
101
102 /**
103 * Handle elements at the top level. Override this.
104 */
105 /*package*/ ParseHandlerBase localStartElement(
106 String uri,
107 String localName,
108 String qName,
109 Attributes attributes)
110 throws
111 SAXException
112 {
113 reportFatalError(ResourceAccess.Strings.buildString(
114 "xml_UnexpectedElement", localName, localElement_));
115 throw new AssertionError("reportFatalError should have terminated parsing");
116 }
117
118
119 /**
120 * Handle characters at the top level. Override this.
121 */
122 /*package*/ void localCharacters(
123 char[] chs,
124 int start,
125 int length)
126 throws
127 SAXException
128 {
129 for (int i=start; i<start+length; ++i)
130 {
131 char ch = chs[i];
132 if (ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r')
133 {
134 reportFatalError(ResourceAccess.Strings.buildString(
135 "xml_UnexpectedCharacters", localElement_));
136 }
137 }
138 }
139
140
141 /**
142 * Finished a child element. Override this.
143 */
144 /*package*/ void childFinished(
145 String uri,
146 String localName,
147 String qName)
148 throws
149 SAXException
150 {
151 }
152
153
154 /**
155 * Delegate startElement calls to this method.
156 */
157 /*package*/ void startElement(
158 String uri,
159 String localName,
160 String qName,
161 Attributes attributes)
162 throws
163 SAXException
164 {
165 if (defunct_)
166 {
167 throw new IllegalStateException("Parse handler is defunct");
168 }
169 if (delegate_ != null)
170 {
171 delegate_.startElement(uri, localName, qName, attributes);
172 }
173 else
174 {
175 delegate_ = localStartElement(uri, localName, qName, attributes);
176 }
177 }
178
179
180 /**
181 * Delegate endElement calls to this method.
182 * Returns true when this ParseHandler is going defunct.
183 */
184 /*package*/ boolean endElement(
185 String uri,
186 String localName,
187 String qName)
188 throws
189 SAXException
190 {
191 if (defunct_)
192 {
193 throw new IllegalStateException("Parse handler is defunct");
194 }
195 if (delegate_ == null)
196 {
197 if (localName.equals(localElement_))
198 {
199 defunct_ = true;
200 return true;
201 }
202 else
203 {
204 reportFatalError(ResourceAccess.Strings.buildString(
205 "xml_WrongClosingElement", localName, localElement_));
206 }
207 }
208 else
209 {
210 if (delegate_.endElement(uri, localName, qName))
211 {
212 delegate_ = null;
213 childFinished(uri, localName, qName);
214 }
215 }
216 return false;
217 }
218
219
220 /**
221 * Delegate characters calls to this method.
222 */
223 /*package*/ void characters(
224 char[] chs,
225 int start,
226 int length)
227 throws
228 SAXException
229 {
230 if (defunct_)
231 {
232 throw new IllegalStateException("Parse handler is defunct");
233 }
234 if (delegate_ != null)
235 {
236 delegate_.characters(chs, start, length);
237 }
238 else
239 {
240 localCharacters(chs, start, length);
241 }
242 }
243
244
245 /**
246 * Report an error to the error handler.
247 */
248 /*package*/ void reportError(
249 String message)
250 throws
251 SAXException
252 {
253 errorHandler_.error(new SAXParseException(message, locator_));
254 }
255
256
257 /**
258 * Report a fatal error to the error handler.
259 */
260 /*package*/ void reportFatalError(
261 String message)
262 throws
263 SAXException
264 {
265 errorHandler_.fatalError(new SAXParseException(message, locator_));
266 }
267
268
269 /**
270 * Report a warning to the error handler.
271 */
272 /*package*/ void reportWarning(
273 String message)
274 throws
275 SAXException
276 {
277 errorHandler_.warning(new SAXParseException(message, locator_));
278 }
279 }