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