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 2000-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.AttributeDecl;
54  import org.exolab.castor.xml.schema.AttributeGroupReference;
55  import org.exolab.castor.xml.schema.ComplexType;
56  import org.exolab.castor.xml.schema.Group;
57  import org.exolab.castor.xml.schema.ModelGroup;
58  import org.exolab.castor.xml.schema.Schema;
59  import org.exolab.castor.xml.schema.SchemaContext;
60  import org.exolab.castor.xml.schema.SchemaException;
61  import org.exolab.castor.xml.schema.SchemaNames;
62  import org.exolab.castor.xml.schema.SimpleContent;
63  import org.exolab.castor.xml.schema.SimpleType;
64  import org.exolab.castor.xml.schema.Wildcard;
65  import org.exolab.castor.xml.schema.XMLType;
66  
67  /**
68   * A class for Unmarshalling extension elements
69   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
70   * @version $Revision$ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
71  **/
72  public class ExtensionUnmarshaller extends ComponentReader {
73  
74  
75        //--------------------/
76       //- Member Variables -/
77      //--------------------/
78  
79      /**
80       * The current ComponentReader
81      **/
82      private ComponentReader unmarshaller;
83  
84      /**
85       * The current branch depth
86      **/
87      private int depth = 0;
88  
89      /**
90       * The Attribute reference for the Attribute we are constructing
91      **/
92      private ComplexType _complexType = null;
93      private Schema      _schema      = null;
94  
95      private boolean foundAnnotation  = false;
96      private boolean foundAttributes  = false;
97      private boolean foundModelGroup  = false;
98  
99  
100       //----------------/
101      //- Constructors -/
102     //----------------/
103 
104     /**
105      * Creates a new ExtensionUnmarshaller.
106      * @param schemaContext the {@link SchemaContext} to get some configuration settings from
107      * @param complexType the ComplexType being unmarshalled
108      * @param atts the AttributeList
109     **/
110     public ExtensionUnmarshaller (
111             final SchemaContext schemaContext,
112             final ComplexType complexType,
113             final AttributeSet atts)
114         throws XMLException {
115         super(schemaContext);
116 
117         _complexType = complexType;
118         _schema      = complexType.getSchema();
119 
120         _complexType.setDerivationMethod(SchemaNames.EXTENSION);
121 
122         //-- base
123         String base = atts.getValue(SchemaNames.BASE_ATTR);
124         if ((base != null) && (base.length() > 0)) {
125 
126             XMLType baseType= _schema.getType(base);
127             if (baseType == null) {
128                 _complexType.setBase(base); //the base type has not been read
129                 if (_complexType.isSimpleContent()) {
130                     _complexType.setContentType(new SimpleContent(_schema, base));
131                 }
132             }
133 		    else {
134 				 //--we cannot extend a simpleType in <complexContent>
135 				 if ( (baseType.isSimpleType()) &&
136 					  (_complexType.isComplexContent()) ) {
137 					String err = "In a 'complexContent', the base attribute "+
138                     "must be a complexType but "+ base+" is a simpleType.\n";
139                     error(err);
140 				 }
141 				 _complexType.setBase(base);
142                  _complexType.setBaseType(baseType);
143                 if (_complexType.isSimpleContent()) {
144                     //--set the content type
145                     if (baseType.isSimpleType()) {
146                         SimpleType simpleType = (SimpleType)baseType;
147                 	    _complexType.setContentType(new SimpleContent(simpleType));
148                     } 
149                     else {
150                         ComplexType temp = (ComplexType)baseType;
151                         SimpleContent simpleContent = (SimpleContent) temp.getContentType();
152                         _complexType.setContentType(simpleContent.copy());
153                     }
154                 }
155                    
156 		    }
157 
158         }
159 
160     } //-- ExtensionUnmarshaller
161 
162       //-----------/
163      //- Methods -/
164     //-----------/
165 
166     /**
167      * Returns the name of the element that this ComponentReader
168      * handles
169      * @return the name of the element that this ComponentReader
170      * handles
171     **/
172     public String elementName() {
173         return SchemaNames.EXTENSION;
174     } //-- elementName
175 
176     /**
177      * Returns the Object created by this ComponentReader
178      * @return the Object created by this ComponentReader
179     **/
180     public Object getObject() {
181         return null;
182     } //-- getObject
183 
184     /**
185      * Signals the start of an element with the given name.
186      *
187      * @param name the NCName of the element. It is an error
188      * if the name is a QName (ie. contains a prefix).
189      * @param namespace the namespace of the element. This may be null.
190      * Note: A null namespace is not the same as the default namespace unless
191      * the default namespace is also null.
192      * @param atts the AttributeSet containing the attributes associated
193      * with the element.
194      * @param nsDecls the namespace declarations being declared for this 
195      * element. This may be null.
196     **/
197     public void startElement(String name, String namespace, AttributeSet atts,
198         Namespaces nsDecls)
199         throws XMLException
200     {
201         //-- Do delagation if necessary
202         if (unmarshaller != null) {
203             unmarshaller.startElement(name, namespace, atts, nsDecls);
204             ++depth;
205             return;
206         }
207 
208           //-- <anyAttribute>
209         if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
210             unmarshaller
211                  = new WildcardUnmarshaller(getSchemaContext(), _complexType, _schema, name, atts);
212         }
213 
214         //-- attribute declarations
215         else if (SchemaNames.ATTRIBUTE.equals(name)) {
216             foundAttributes = true;
217             unmarshaller
218                 = new AttributeUnmarshaller(getSchemaContext(), _schema, atts);
219         }
220         //-- attribute group declarations
221         else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
222 
223             //-- make sure we have an attribute group
224             //-- reference and not a definition
225 
226             if (atts.getValue(SchemaNames.REF_ATTR) == null) {
227                 String err = "A complexType may contain referring "+
228                     "attribute groups, but not defining ones.";
229                 error(err);
230             }
231 
232             foundAttributes = true;
233             unmarshaller
234                 = new AttributeGroupUnmarshaller(getSchemaContext(), _schema, atts);
235         }
236         //--<group>
237         else if ( name.equals(SchemaNames.GROUP) )
238         {
239             if (foundAttributes)
240                 error("'" + name + "' must appear before any attribute "+
241                     "definitions when a child of 'complexType'.");
242             if (foundModelGroup)
243                 error("'"+name+"' cannot appear as a child of 'complexType' "+
244                     "if another 'all', 'sequence', 'choice' or "+
245                     "'group' also exists.");
246 
247             foundModelGroup = true;
248             unmarshaller
249                 = new ModelGroupUnmarshaller(getSchemaContext(), _schema, atts);
250         }
251         else if (SchemaNames.isGroupName(name) && (name != SchemaNames.GROUP) ) {
252             if (foundAttributes)
253                 error("'"+name+"' must appear before attribute "+
254                     "definitions in an 'extension' element.");
255 
256             if (foundModelGroup)
257                 error("'"+name+"' cannot appear as a child of 'extension' "+
258                     "if another 'all', 'sequence', 'choice' or "+
259                     "'group' already exists.");
260 
261             if (_complexType.isSimpleContent())
262                 error("'"+name+"' may not appear in a 'extension' of "+
263                     "'simpleContent'.");
264 
265             foundModelGroup = true;
266             unmarshaller
267                 = new GroupUnmarshaller(getSchemaContext(), _schema, name, atts);
268         }
269         //-- element declarations
270         else if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
271             //-- not yet supported....
272             error("anyAttribute is not yet supported.");
273         }
274         else if (name.equals(SchemaNames.ANNOTATION)) {
275             if (foundAttributes || foundModelGroup)
276                 error("An annotation must appear as the first child of an " +
277                     "'extension' element.");
278 
279             if (foundAnnotation)
280                 error("Only one (1) annotation may appear as the child of "+
281                     "an 'extension' element.");
282 
283             foundAnnotation = true;
284             unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
285         }
286         else illegalElement(name);
287 
288     } //-- startElement
289 
290     /**
291      * Signals to end of the element with the given name.
292      *
293      * @param name the NCName of the element. It is an error
294      * if the name is a QName (ie. contains a prefix).
295      * @param namespace the namespace of the element.
296     **/
297     public void endElement(String name, String namespace)
298         throws XMLException
299     {
300 
301         //-- Do delagation if necessary
302         if ((unmarshaller != null) && (depth > 0)) {
303             unmarshaller.endElement(name, namespace);
304             --depth;
305             return;
306         }
307 
308         //-- have unmarshaller perform any necessary clean up
309         unmarshaller.finish();
310 
311         //-- <anyAttribute>
312         if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
313             Wildcard wildcard =
314                  ((WildcardUnmarshaller)unmarshaller).getWildcard();
315             try {
316                 _complexType.setAnyAttribute(wildcard);
317             } catch (SchemaException e) {
318                 throw new IllegalArgumentException(e.getMessage());
319             }
320         }
321 
322         //-- attribute declarations
323         else if (SchemaNames.ATTRIBUTE.equals(name)) {
324             AttributeDecl attrDecl =
325                 ((AttributeUnmarshaller)unmarshaller).getAttribute();
326 
327             _complexType.addAttributeDecl(attrDecl);
328         }
329         //-- attribute groups
330         else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
331             AttributeGroupReference attrGroupRef =
332                 (AttributeGroupReference) unmarshaller.getObject();
333             _complexType.addAttributeGroupReference(attrGroupRef);
334         }
335         //--group
336         else if (name.equals(SchemaNames.GROUP)) {
337             ModelGroup group = ((ModelGroupUnmarshaller)unmarshaller).getGroup();
338             _complexType.addGroup(group);
339         }
340 
341         //-- group declarations (all, choice, sequence)
342         else if ( (SchemaNames.isGroupName(name)) && (name != SchemaNames.GROUP) )
343         {
344             Group group = ((GroupUnmarshaller)unmarshaller).getGroup();
345             _complexType.addGroup(group);
346         }
347         //-- annotation
348         else if (SchemaNames.ANNOTATION.equals(name)) {
349             Annotation ann = ((AnnotationUnmarshaller)unmarshaller).getAnnotation();
350             _complexType.addAnnotation(ann);
351         }
352 
353         unmarshaller = null;
354     } //-- endElement
355 
356     public void characters(char[] ch, int start, int length)
357         throws XMLException
358     {
359         //-- Do delagation if necessary
360         if (unmarshaller != null) {
361             unmarshaller.characters(ch, start, length);
362         }
363     } //-- characters
364 
365 } //-- ExtensionUnmarshaller