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