001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: ComponentReader.java 6285 2006-10-08 04:48:11Z ekuns $
044: */package org.exolab.castor.xml.schema.reader;
045:
046: //-- imported classes and packages
047: import org.exolab.castor.net.URIResolver;
048: import org.exolab.castor.xml.AttributeSet;
049: import org.exolab.castor.xml.Namespaces;
050: import org.exolab.castor.xml.XMLException;
051: import org.exolab.castor.xml.schema.Resolver;
052: import org.xml.sax.Locator;
053:
054: /**
055: * The base class for separate component unmarshallers for
056: * reading an XML Schema component.
057: *
058: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
059: * @version $Revision: 6285 $ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
060: **/
061: public abstract class ComponentReader {
062:
063: //--------------------/
064: //- Member Variables -/
065: //--------------------/
066:
067: /**
068: * The document locator
069: **/
070: protected Locator _locator = null;
071:
072: /**
073: * The resolver to be used for resolving id references
074: **/
075: private Resolver _resolver;
076:
077: /**
078: * The resolver to be used for resolving href
079: */
080: private URIResolver _uriResolver;
081:
082: //----------------/
083: //- Constructors -/
084: //----------------/
085:
086: public ComponentReader() {
087: super ();
088: } //-- ComponentReader
089:
090: //-----------/
091: //- Methods -/
092: //-----------/
093:
094: /**
095: * Returns the name of the element that this ComponentReader
096: * handles
097: * @return the name of the element that this ComponentReader
098: * handles
099: **/
100: public abstract String elementName();
101:
102: /**
103: * Returns the Object created by this Unmarshaller
104: * @return the Object created by this Unmarshaller
105: **/
106: public abstract Object getObject();
107:
108: /**
109: * Called to signal an end of unmarshalling. This method should
110: * be overridden to perform any necessary clean up by an unmarshaller
111: **/
112: public void finish() throws XMLException {
113: }
114:
115: public Locator getDocumentLocator() {
116: return _locator;
117: } //-- getLocator
118:
119: /**
120: * Returns the resolver used for resolving id references.
121: * @return the resolver used for resolving id references.
122: **/
123: public Resolver getResolver() {
124: return _resolver;
125: } //-- getResolver
126:
127: /**
128: * Returns the URIresolver used for resolving hrefs.
129: *
130: * @return the URIresolver used for resolving hrefs.
131: **/
132: public URIResolver getURIResolver() {
133: return _uriResolver;
134: } //-- getResolver
135:
136: /**
137: * Sets the Resolver to be used for resolving id references
138: * @param resolver the Resolver to be used for resolving
139: * id references
140: **/
141: public void setResolver(Resolver resolver) {
142: _resolver = resolver;
143: } //-- setResolver
144:
145: /**
146: * Sets the URIResolver to be used for resolving hrefs.
147: *
148: * @param uriResolver the URIResolver to be used for resolving hrefs.
149: **/
150: public void setURIResolver(URIResolver uriResolver) {
151: _uriResolver = uriResolver;
152: } //-- setResolver
153:
154: /**
155: * Determines if the given sequence of characters consists
156: * of whitespace characters
157: * @param chars an array of characters to check for whitespace
158: * @param start the start index into the character array
159: * @param length the number of characters to check
160: * @return true if the characters specficied consist only
161: * of whitespace characters
162: **/
163: public static boolean isWhiteSpace(char[] chars, int start,
164: int length) {
165: int max = start + length;
166: for (int i = start; i < max; i++) {
167: char ch = chars[i];
168: switch (ch) {
169: case ' ':
170: case '\n':
171: case '\t':
172: case '\r':
173: break;
174: default:
175: return false;
176: }
177: }
178: return true;
179: } //-- isWhiteSpace
180:
181: /**
182: * This method is called for a general error.
183: * @param err the error message to report
184: **/
185: public void error(String err) throws XMLException {
186:
187: if (_locator != null) {
188: err += "\n line: " + _locator.getLineNumber();
189: }
190:
191: throw new XMLException(err);
192: } //-- error
193:
194: /**
195: * This method is called for a general error.
196: *
197: * @param ex the Exception that caused the error.
198: */
199: public void error(Exception ex) throws XMLException {
200:
201: if (_locator != null) {
202: String err = "An error occured at line: "
203: + _locator.getLineNumber();
204: throw new XMLException(err, ex);
205: }
206: throw new XMLException(ex);
207: } //-- error
208:
209: /**
210: * This method is called when an illegal Attribute is encountered.
211: * @param attName the name of the illegal attribute.
212: **/
213: public void illegalAttribute(String attName) throws XMLException {
214: String err = "Illegal attribute '" + attName
215: + "' found on element <" + elementName() + ">.";
216:
217: if (_locator != null) {
218: err += "\n line: " + _locator.getLineNumber();
219: }
220:
221: throw new XMLException(err);
222: } //-- illegalAttribute
223:
224: /**
225: * This method is called when an illegal Element is encountered.
226: * @param name the name of the illegal element
227: **/
228: public void illegalElement(String name) throws XMLException {
229: String err = "Illegal element '" + name
230: + "' found as child of <" + elementName() + ">.";
231:
232: if (_locator != null) {
233: err += "\n line: " + _locator.getLineNumber();
234: }
235:
236: throw new XMLException(err);
237: } //-- illegalElement
238:
239: /**
240: * This method is called when an element which may only
241: * be defined once, is redefined.
242: * @param name the name of the element
243: **/
244: public void redefinedElement(String name) throws XMLException {
245: redefinedElement(name, null);
246: } //-- redefinedElement
247:
248: /**
249: * This method is called when an element which may only
250: * be defined once, is redefined.
251: * @param name the name of the element
252: **/
253: public void redefinedElement(String name, String xtraInfo)
254: throws XMLException {
255: String err = "redefintion of element '" + name
256: + "' within element <" + elementName() + ">.";
257:
258: if (_locator != null) {
259: err += "\n line: " + _locator.getLineNumber();
260: }
261:
262: if (xtraInfo != null) {
263: err += "\n " + xtraInfo;
264: }
265:
266: throw new XMLException(err + "\n");
267: } //-- redefinedElement
268:
269: /**
270: * This method is called when an out of order element is encountered
271: **/
272: public void outOfOrder(String name) throws XMLException {
273: StringBuffer err = new StringBuffer("out of order element <");
274: err.append(name);
275: err.append("> found in <");
276: err.append(elementName());
277: err.append(">.");
278: throw new XMLException(err.toString());
279: }
280:
281: /**
282: * Converts the given String to an int
283: * @param str the String to convert to an int
284: * @return the int derived from the given String
285: * @exception IllegalArgumentException when the given
286: * String does not represent a valid int
287: **/
288: public static int toInt(String str) throws IllegalArgumentException {
289: try {
290: return Integer.parseInt(str);
291: } catch (NumberFormatException nfe) {
292: String err = str + " is not a valid integer. ";
293: throw new IllegalArgumentException(err);
294: }
295: } //-- toInt
296:
297: public void setDocumentLocator(Locator locator) {
298: this ._locator = locator;
299: } //-- setDocumentLocator
300:
301: /**
302: * Signals to recieve charactes
303: *
304: * @param chars the character array containing the characters
305: * @param start the starting index into the character array
306: * @param length the number of characters to recieve
307: **/
308: public void characters(char[] chars, int start, int length)
309: throws XMLException {
310: //-- do nothing, this method is overwritten by subclasses
311:
312: } //-- characters
313:
314: /**
315: * Signals to end of the element with the given name.
316: *
317: * @param name the NCName of the element. It is an error
318: * if the name is a QName (ie. contains a prefix).
319: * @param namespace the namespace of the element.
320: **/
321: public void endElement(String name, String namespace)
322: throws XMLException {
323: //-- do nothing, this method is overwritten by subclasses
324: } //-- endElement
325:
326: /**
327: * Signals the start of an element with the given name.
328: *
329: * @param name the NCName of the element. It is an error
330: * if the name is a QName (ie. contains a prefix).
331: * @param namespace the namespace of the element. This may be null.
332: * Note: A null namespace is not the same as the default namespace unless
333: * the default namespace is also null.
334: * @param atts the AttributeSet containing the attributes associated
335: * with the element.
336: * @param nsDecls the namespace declarations being declared for this
337: * element. This may be null.
338: **/
339: public void startElement(String name, String namespace,
340: AttributeSet atts, Namespaces nsDecls) throws XMLException {
341: //-- do nothing, this method is overwritten by subclasses
342:
343: } //-- startElement
344:
345: } //-- ComponentReader
|