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-2002 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  package org.exolab.castor.xml.schema.reader;
37  
38  import java.net.URI;
39  
40  import org.exolab.castor.net.URIException;
41  import org.exolab.castor.net.URILocation;
42  import org.exolab.castor.net.URIResolver;
43  import org.exolab.castor.xml.AttributeSet;
44  import org.exolab.castor.xml.Namespaces;
45  import org.exolab.castor.xml.Unmarshaller;
46  import org.exolab.castor.xml.XMLException;
47  import org.exolab.castor.xml.schema.SchemaContext;
48  import org.exolab.castor.xml.schema.SchemaException;
49  import org.exolab.castor.xml.schema.Resolver;
50  import org.xml.sax.InputSource;
51  import org.xml.sax.Locator;
52  import org.xml.sax.Parser;
53  
54  /**
55   * The base class for separate component unmarshallers for reading an XML Schema component.
56   * 
57   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
58   * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
59   **/
60  public abstract class ComponentReader {
61  
62    /**
63     * The Castor XML context to use.
64     */
65    private SchemaContext _schemaContext;
66  
67    private Locator _documentLocator;
68  
69    /**
70     * The resolver to be used for resolving hrefs.
71     */
72    private URIResolver _uriResolver;
73  
74    private ComponentReader() {
75      super();
76    }
77  
78    /**
79     * To hand down a couple of configuration items to all {@link Unmarshaller} classes.
80     * 
81     * @param schemaContext the {@link SchemaContext} to use
82     */
83    protected ComponentReader(final SchemaContext schemaContext) {
84      this();
85      _schemaContext = schemaContext;
86    }
87  
88    /**
89     * Returns the name of the element that this ComponentReader handles
90     * 
91     * @return the name of the element that this ComponentReader handles
92     **/
93    public abstract String elementName();
94  
95    /**
96     * Returns the Object created by this {@link Unmarshaller}
97     * 
98     * @return the Object created by this {@link Unmarshaller}
99     **/
100   public abstract Object getObject();
101 
102   /**
103    * Called to signal an end of unmarshalling. This method should be overridden to perform any
104    * necessary clean up by an {@link Unmarshaller}
105    **/
106   public void finish() throws XMLException {}
107 
108   /**
109    * Returns the resolver used for resolving id references.
110    * 
111    * @return the resolver used for resolving id references.
112    **/
113   public Resolver getResolver() {
114     return _schemaContext.getSchemaResolver();
115   }
116 
117   /**
118    * Returns the URIresolver used for resolving hrefs.
119    * 
120    * @return the URIresolver used for resolving hrefs.
121    **/
122   public URIResolver getURIResolver() {
123     return _uriResolver;
124   }
125 
126   /**
127    * Sets the Resolver to be used for resolving id references
128    * 
129    * @param resolver the Resolver to be used for resolving id references
130    **/
131   public void setResolver(Resolver resolver) {
132     _schemaContext.setSchemaResolver(resolver);
133   }
134 
135   /**
136    * Sets the URIResolver to be used for resolving hrefs.
137    * 
138    * @param uriResolver the URIResolver to be used for resolving hrefs.
139    **/
140   public void setURIResolver(URIResolver uriResolver) {
141     _uriResolver = uriResolver;
142   }
143 
144   /**
145    * Determines if the given sequence of characters consists of whitespace characters
146    * 
147    * @param chars an array of characters to check for whitespace
148    * @param start the start index into the character array
149    * @param length the number of characters to check
150    * @return true if the characters specficied consist only of whitespace characters
151    **/
152   public static boolean isWhiteSpace(char[] chars, int start, int length) {
153     int max = start + length;
154     for (int i = start; i < max; i++) {
155       char ch = chars[i];
156       switch (ch) {
157         case ' ':
158         case '\n':
159         case '\t':
160         case '\r':
161           break;
162         default:
163           return false;
164       }
165     }
166     return true;
167   }
168 
169   /**
170    * This method is called for a general error.
171    * 
172    * @param err the error message to report
173    **/
174   public void error(String err) throws XMLException {
175     if (getDocumentLocator() != null) {
176       err += "\n   line: " + getDocumentLocator().getLineNumber();
177     }
178 
179     throw new XMLException(err);
180   }
181 
182   /**
183    * This method is called for a general error.
184    * 
185    * @param ex the Exception that caused the error.
186    */
187   public void error(Exception ex) throws XMLException {
188     if (getDocumentLocator() != null) {
189       String err = "An error occured at line: " + getDocumentLocator().getLineNumber();
190       throw new XMLException(err, ex);
191     }
192     throw new XMLException(ex);
193   }
194 
195   /**
196    * This method is called when an illegal Attribute is encountered.
197    * 
198    * @param attName the name of the illegal attribute.
199    **/
200   public void illegalAttribute(String attName) throws XMLException {
201     String err = "Illegal attribute '" + attName + "' found on element <" + elementName() + ">.";
202 
203     if (getDocumentLocator() != null) {
204       err += "\n   line: " + getDocumentLocator().getLineNumber();
205     }
206 
207     throw new XMLException(err);
208   }
209 
210   /**
211    * This method is called when an illegal Element is encountered.
212    * 
213    * @param name the name of the illegal element
214    **/
215   public void illegalElement(String name) throws XMLException {
216     String err = "Illegal element '" + name + "' found as child of <" + elementName() + ">.";
217 
218     if (getDocumentLocator() != null) {
219       err += "\n   line: " + getDocumentLocator().getLineNumber();
220     }
221 
222     throw new XMLException(err);
223   }
224 
225   /**
226    * This method is called when an element which may only be defined once, is redefined.
227    * 
228    * @param name the name of the element
229    **/
230   public void redefinedElement(String name) throws XMLException {
231     redefinedElement(name, null);
232   }
233 
234   /**
235    * This method is called when an element which may only be defined once, is redefined.
236    * 
237    * @param name the name of the element
238    **/
239   public void redefinedElement(String name, String xtraInfo) throws XMLException {
240     String err = "redefintion of element '" + name + "' within element <" + elementName() + ">.";
241 
242     if (getDocumentLocator() != null) {
243       err += "\n   line: " + getDocumentLocator().getLineNumber();
244     }
245 
246     if (xtraInfo != null) {
247       err += "\n   " + xtraInfo;
248     }
249 
250     throw new XMLException(err + "\n");
251   }
252 
253   /**
254    * This method is called when an out of order element is encountered
255    **/
256   public void outOfOrder(String name) throws XMLException {
257     String err = new StringBuilder("out of order element <").append(name).append("> found in <")
258         .append(elementName()).append(">.").toString();
259     throw new XMLException(err);
260   }
261 
262   /**
263    * Converts the given String to an int
264    * 
265    * @param str the String to convert to an int
266    * @return the int derived from the given String
267    * @exception IllegalArgumentException when the given String does not represent a valid int
268    **/
269   public static int toInt(String str) throws IllegalArgumentException {
270     try {
271       return Integer.parseInt(str);
272     } catch (NumberFormatException nfe) {
273       String err = str + " is not a valid integer. ";
274       throw new IllegalArgumentException(err);
275     }
276   }
277 
278   public Locator getDocumentLocator() {
279     return _documentLocator;
280   }
281 
282   public void setDocumentLocator(Locator documentLocator) {
283     _documentLocator = documentLocator;
284   }
285 
286   /**
287    * Signals to recieve charactes
288    * 
289    * @param chars the character array containing the characters
290    * @param start the starting index into the character array
291    * @param length the number of characters to recieve
292    **/
293   public void characters(char[] chars, int start, int length) throws XMLException {
294     // -- do nothing, this method is overwritten by subclasses
295   }
296 
297   /**
298    * Signals to end of the element with the given name.
299    * 
300    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
301    *        prefix).
302    * @param namespace the namespace of the element.
303    **/
304   public void endElement(String name, String namespace) throws XMLException {
305     // -- do nothing, this method is overwritten by subclasses
306   }
307 
308   /**
309    * Signals the start of an element with the given name.
310    * 
311    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
312    *        prefix).
313    * @param namespace the namespace of the element. This may be null. Note: A null namespace is not
314    *        the same as the default namespace unless the default namespace is also null.
315    * @param atts the AttributeSet containing the attributes associated with the element.
316    * @param nsDecls the namespace declarations being declared for this element. This may be null.
317    **/
318   public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
319       throws XMLException {
320     // -- do nothing, this method is overwritten by subclasses
321   }
322 
323   /**
324    * To set the Castor XML schema context to be used.
325    * 
326    * @param schemaContext the Castor XML schema context to be used
327    */
328   public void setSchemaContext(final SchemaContext schemaContext) {
329     _schemaContext = schemaContext;
330   }
331 
332   /**
333    * To get the Castor XML schema context used.
334    * 
335    * @return the Castor XML schema context used
336    */
337   public SchemaContext getSchemaContext() {
338     return _schemaContext;
339   }
340 
341   protected void parseSchema(Parser parser, SchemaUnmarshaller schemaUnmarshaller, URILocation uri,
342       String schemaLocation, String reason) throws SchemaException {
343     Sax2ComponentReader handler = new Sax2ComponentReader(schemaUnmarshaller);
344     parser.setDocumentHandler(handler);
345     parser.setErrorHandler(handler);
346 
347     try {
348       InputSource source = new InputSource(uri.getReader());
349       source.setSystemId(uri.getAbsoluteURI());
350       parser.parse(source);
351     } catch (java.io.IOException ioe) {
352       throw new SchemaException("Error reading " + reason + " file '" + schemaLocation + "'");
353     } catch (org.xml.sax.SAXException sx) {
354       throw new SchemaException(sx);
355     }
356   }
357   
358   protected Parser createParser(String reason) throws SchemaException {
359     Parser parser = null;
360     try {
361       parser = getSchemaContext().getParser();
362     } catch (RuntimeException rte) {
363     }
364     if (parser == null) {
365       throw new SchemaException("Error failed to create parser for " + reason);
366     }
367     return parser;
368   }
369   
370   protected URILocation derive(Locator locator, String schemaLocation) throws XMLException {
371     try {
372       String documentBase = locator.getSystemId();
373       if (documentBase != null) {
374         if (!documentBase.endsWith("/"))
375           documentBase = documentBase.substring(0, documentBase.lastIndexOf("/") + 1);
376       }
377       return getURIResolver().resolve(schemaLocation, documentBase);
378     } catch (URIException ure) {
379       throw new XMLException(ure);
380     }
381     
382   }
383 
384 }