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  // -- imported classes and packages
39  import org.exolab.castor.xml.AttributeSet;
40  import org.exolab.castor.xml.Namespaces;
41  import org.exolab.castor.xml.XMLException;
42  import org.exolab.castor.xml.schema.Annotation;
43  import org.exolab.castor.xml.schema.ComplexType;
44  import org.exolab.castor.xml.schema.SchemaContext;
45  import org.exolab.castor.xml.schema.SchemaNames;
46  
47  /**
48   * A class for Unmarshalling simpleContent
49   * 
50   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
51   * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
52   **/
53  public class SimpleContentUnmarshaller extends ComponentReader {
54  
55    // --------------------/
56    // - Member Variables -/
57    // --------------------/
58  
59    /**
60     * The current ComponentReader
61     **/
62    private ComponentReader unmarshaller;
63  
64    /**
65     * The current branch depth
66     **/
67    private int depth = 0;
68  
69    /**
70     * The Attribute reference for the Attribute we are constructing
71     **/
72    private ComplexType _complexType = null;
73  
74    private boolean foundAnnotation = false;
75    private boolean foundExtension = false;
76    private boolean foundRestriction = false;
77  
78    // ----------------/
79    // - Constructors -/
80    // ----------------/
81  
82    /**
83     * Creates a new SimpleContentUnmarshaller.
84     * 
85     * @param schemaContext the {@link SchemaContext} to get some configuration settings from
86     * @param complexType the complexType we are unmarshalling
87     * @param atts the AttributeList
88     **/
89    public SimpleContentUnmarshaller(final SchemaContext schemaContext, final ComplexType complexType,
90        final AttributeSet atts) throws XMLException {
91  
92      super(schemaContext);
93  
94      _complexType = complexType;
95    } // -- SimpleContentUnmarshaller
96  
97    // -----------/
98    // - Methods -/
99    // -----------/
100 
101   /**
102    * Returns the name of the element that this ComponentReader handles
103    * 
104    * @return the name of the element that this ComponentReader handles
105    **/
106   public String elementName() {
107     return SchemaNames.SIMPLE_CONTENT;
108   } // -- elementName
109 
110   /**
111    * Returns the Object created by this ComponentReader
112    * 
113    * @return the Object created by this ComponentReader
114    **/
115   public Object getObject() {
116     return null;
117   } // -- getObject
118 
119   /**
120    * Signals the start of an element with the given name.
121    *
122    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
123    *        prefix).
124    * @param namespace the namespace of the element. This may be null. Note: A null namespace is not
125    *        the same as the default namespace unless the default namespace is also null.
126    * @param atts the AttributeSet containing the attributes associated with the element.
127    * @param nsDecls the namespace declarations being declared for this element. This may be null.
128    **/
129   public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
130       throws XMLException {
131     // -- Do delagation if necessary
132     if (unmarshaller != null) {
133       unmarshaller.startElement(name, namespace, atts, nsDecls);
134       ++depth;
135       return;
136     }
137 
138     // -- extension
139     if (SchemaNames.EXTENSION.equals(name)) {
140 
141       if (foundExtension)
142         error(
143             "Only (1) 'extension' element may appear as a child " + "of 'simpleContent' elements.");
144 
145       if (foundRestriction)
146         error("Both 'extension' and 'restriction' elements may not "
147             + "appear as children of the same simpleContent " + "definition.");
148 
149       foundExtension = true;
150       unmarshaller = new ExtensionUnmarshaller(getSchemaContext(), _complexType, atts);
151     }
152     // -- restriction
153     else if (SchemaNames.RESTRICTION.equals(name)) {
154 
155       if (foundRestriction)
156         error("Only (1) 'restriction' element may appear as a child "
157             + "of 'simpleContent' elements.");
158 
159       if (foundExtension)
160         error("Both 'extension' and 'restriction' elements may not "
161             + "appear as children of the same simpleContent " + "definition.");
162 
163       foundRestriction = true;
164 
165       unmarshaller =
166           new SimpleContentRestrictionUnmarshaller(getSchemaContext(), _complexType, atts);
167     }
168     // -- annotation
169     else if (name.equals(SchemaNames.ANNOTATION)) {
170       if (foundAnnotation)
171         error("Only (1) 'annotation' element may appear as a child "
172             + "of 'simpleContent' elements.");
173 
174       if (foundRestriction || foundExtension)
175         error("An 'annotation' may only appear as the first child "
176             + "of a 'simpleContent' element.");
177 
178       foundAnnotation = true;
179       unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
180     } else
181       illegalElement(name);
182 
183     unmarshaller.setDocumentLocator(getDocumentLocator());
184   } // -- startElement
185 
186   /**
187    * Signals to end of the element with the given name.
188    *
189    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
190    *        prefix).
191    * @param namespace the namespace of the element.
192    **/
193   public void endElement(String name, String namespace) throws XMLException {
194 
195     // -- Do delagation if necessary
196     if ((unmarshaller != null) && (depth > 0)) {
197       unmarshaller.endElement(name, namespace);
198       --depth;
199       return;
200     }
201 
202     // -- have unmarshaller perform any necessary clean up
203     unmarshaller.finish();
204 
205     // -- annotation
206     if (SchemaNames.ANNOTATION.equals(name)) {
207       Annotation ann = ((AnnotationUnmarshaller) unmarshaller).getAnnotation();
208       _complexType.addAnnotation(ann);
209     }
210 
211     unmarshaller = null;
212   } // -- endElement
213 
214   public void characters(char[] ch, int start, int length) throws XMLException {
215     // -- Do delagation if necessary
216     if (unmarshaller != null) {
217       unmarshaller.characters(ch, start, length);
218     }
219   } // -- characters
220 
221 } // -- SimpleContentUnmarshaller