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