1 /**
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * $Id$
44 */
45
46 package org.exolab.castor.xml.schema.reader;
47
48 //-- imported classes and packages
49 import org.exolab.castor.xml.schema.Resolver;
50 import org.xml.sax.AttributeList;
51 import org.xml.sax.DocumentHandler;
52 import org.xml.sax.Locator;
53 import org.xml.sax.SAXException;
54 import org.xml.sax.SAXParseException;
55
56 /**
57 * The base class for unmarshallers
58 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
59 * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
60 **/
61 public abstract class SaxUnmarshaller
62 implements DocumentHandler, org.xml.sax.ErrorHandler
63 {
64
65
66 //--------------------/
67 //- Member Variables -/
68 //--------------------/
69
70 /**
71 * The document locator
72 **/
73 protected Locator _locator = null;
74
75 /**
76 * The resolver to be used for resolving id references
77 **/
78 private Resolver _resolver;
79
80 //----------------/
81 //- Constructors -/
82 //----------------/
83
84 public SaxUnmarshaller() {
85 super();
86 } //-- SaxUnmarshaller
87
88 //-----------/
89 //- Methods -/
90 //-----------/
91
92 /**
93 * Returns the name of the element that this SaxUnmarshaller
94 * handles
95 * @return the name of the element that this SaxUnmarshaller
96 * handles
97 **/
98 public abstract String elementName();
99
100 /**
101 * Returns the Object created by this Unmarshaller
102 * @return the Object created by this Unmarshaller
103 **/
104 public abstract Object getObject();
105
106 /**
107 * Called to signal an end of unmarshalling. This method should
108 * be overridden to perform any necessary clean up by an unmarshaller
109 **/
110 public void finish() throws SAXException {}
111
112 public Locator getDocumentLocator() {
113 return _locator;
114 } //-- getLocator
115
116 /**
117 * Returns the resolver used for resolving id references.
118 * @return the resolver used for resolving id references.
119 **/
120 public Resolver getResolver() {
121 return _resolver;
122 } //-- getResolver
123
124 /**
125 * Sets the Resolver to be used for resolving id references
126 * @param resolver the Resolver to be used for resolving
127 * id references
128 **/
129 public void setResolver(Resolver resolver) {
130 _resolver = resolver;
131 } //-- setResolver
132
133 /**
134 * Determines if the given sequence of characters consists
135 * of whitespace characters
136 * @param chars an array of characters to check for whitespace
137 * @param start the start index into the character array
138 * @param length the number of characters to check
139 * @return true if the characters specficied consist only
140 * of whitespace characters
141 **/
142 public static boolean isWhiteSpace(char[] chars, int start, int length) {
143 int max = start+length;
144 for (int i = start; i < max; i++) {
145 char ch = chars[i];
146 switch(ch) {
147 case ' ':
148 case '\n':
149 case '\t':
150 case '\r':
151 break;
152 default:
153 return false;
154 }
155 }
156 return true;
157 } //-- isWhiteSpace
158
159 /**
160 * This method is called for a general error.
161 * @param err the error message to report
162 * @exception org.xml.sax.SAXException always thrown.
163 **/
164 public void error(String err)
165 throws org.xml.sax.SAXException
166 {
167
168 if (_locator != null) {
169 err += "\n line: " + _locator.getLineNumber();
170 }
171
172 throw new SAXException(err);
173 } //-- error
174
175 /**
176 * This method is called when an illegal Attribute is encountered.
177 * @param attName the name of the illegal attribute.
178 * @exception org.xml.sax.SAXException always thrown.
179 **/
180 public void illegalAttribute(String attName)
181 throws org.xml.sax.SAXException
182 {
183 String err = "Illegal attribute '" + attName +
184 "' found on element <" + elementName() + ">.";
185
186 if (_locator != null) {
187 err += "\n line: " + _locator.getLineNumber();
188 }
189
190 throw new SAXException(err);
191 } //-- illegalAttribute
192
193 /**
194 * This method is called when an illegal Element is encountered.
195 * @param name the name of the illegal element
196 * @exception org.xml.sax.SAXException always thrown.
197 **/
198 public void illegalElement(String name)
199 throws org.xml.sax.SAXException
200 {
201 String err = "Illegal element '" + name +
202 "' found as child of <" + elementName() + ">.";
203
204 if (_locator != null) {
205 err += "\n line: " + _locator.getLineNumber();
206 }
207
208 throw new SAXException(err);
209 } //-- illegalElement
210
211
212 /**
213 * This method is called when an element which may only
214 * be defined once, is redefined.
215 * @param name the name of the element
216 * @exception org.xml.sax.SAXException always thrown.
217 **/
218 public void redefinedElement(String name)
219 throws org.xml.sax.SAXException
220 {
221 redefinedElement(name, null);
222 } //-- redefinedElement
223
224 /**
225 * This method is called when an element which may only
226 * be defined once, is redefined.
227 * @param name the name of the element
228 * @exception org.xml.sax.SAXException always thrown.
229 **/
230 public void redefinedElement(String name, String xtraInfo)
231 throws org.xml.sax.SAXException
232 {
233 String err = "redefintion of element '" + name +
234 "' within element <" + elementName() + ">.";
235
236 if (_locator != null) {
237 err += "\n line: " + _locator.getLineNumber();
238 }
239
240 if (xtraInfo != null) {
241 err += "\n " + xtraInfo;
242 }
243
244 throw new SAXException(err+"\n");
245 } //-- redefinedElement
246
247 /**
248 * This method is called when an out of order element is encountered
249 * @exception org.xml.sax.SAXException always thrown.
250 **/
251 public void outOfOrder(String name)
252 throws org.xml.sax.SAXException
253 {
254 StringBuffer err = new StringBuffer("out of order element <");
255 err.append(name);
256 err.append("> found in <");
257 err.append(elementName());
258 err.append(">.");
259 throw new SAXException(err.toString());
260 }
261
262 /**
263 * Converts the given String to an int
264 * @param str the String to convert to an int
265 * @return the int derived from the given String
266 * @exception IllegalArgumentException when the given
267 * String does not represent a valid int
268 **/
269 public static int toInt(String str)
270 throws IllegalArgumentException
271 {
272 try {
273 return Integer.parseInt(str);
274 }
275 catch(NumberFormatException nfe) {
276 String err = str+" is not a valid integer. ";
277 throw new IllegalArgumentException(err);
278 }
279 } //-- toInt
280
281
282 //---------------------------------------/
283 //- org.xml.sax.DocumentHandler methods -/
284 //---------------------------------------/
285
286 public void characters(char[] ch, int start, int length)
287 throws org.xml.sax.SAXException
288 {
289 //-- do nothing
290
291 } //-- characters
292
293 public void endDocument()
294 throws org.xml.sax.SAXException
295 {
296 //-- do nothing
297
298 } //-- endDocument
299
300 public void endElement(String name)
301 throws org.xml.sax.SAXException
302 {
303 //-- do nothing
304
305 } //-- endElement
306
307
308 public void ignorableWhitespace(char[] ch, int start, int length)
309 throws org.xml.sax.SAXException
310 {
311 //-- do nothing
312
313 } //-- ignorableWhitespace
314
315 public void processingInstruction(String target, String data)
316 throws org.xml.sax.SAXException
317 {
318 //-- do nothing
319
320 } //-- processingInstruction
321
322 public void setDocumentLocator(Locator locator) {
323 this._locator = locator;
324 } //-- setDocumentLocator
325
326 public void startDocument()
327 throws org.xml.sax.SAXException
328 {
329 //-- do nothing
330
331 } //-- startDocument
332
333
334 public void startElement(String name, AttributeList atts)
335 throws org.xml.sax.SAXException
336 {
337 //-- do nothing
338
339 } //-- startElement
340
341
342 //------------------------------------/
343 //- org.xml.sax.ErrorHandler methods -/
344 //------------------------------------/
345
346 public void error(SAXParseException exception)
347 throws org.xml.sax.SAXException
348 {
349 throw exception;
350
351 } //-- error
352
353 public void fatalError(SAXParseException exception)
354 throws org.xml.sax.SAXException
355 {
356 throw exception;
357
358 } //-- fatalError
359
360
361 public void warning(SAXParseException exception)
362 throws org.xml.sax.SAXException
363 {
364 throw exception;
365
366 } //-- warning
367
368 } //-- SaxUnmarshaller
369