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.ElementDecl;
44  import org.exolab.castor.xml.schema.Group;
45  import org.exolab.castor.xml.schema.ModelGroup;
46  import org.exolab.castor.xml.schema.Order;
47  import org.exolab.castor.xml.schema.Schema;
48  import org.exolab.castor.xml.schema.SchemaContext;
49  import org.exolab.castor.xml.schema.SchemaException;
50  import org.exolab.castor.xml.schema.SchemaNames;
51  import org.exolab.castor.xml.schema.Wildcard;
52  
53  /**
54   * A class for Unmarshalling ModelGroups
55   * 
56   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
57   * @version $Revision$ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
58   **/
59  public class GroupUnmarshaller extends ComponentReader {
60  
61    // --------------------------/
62    // - Static Class Variables -/
63    // --------------------------/
64  
65    /**
66     * The value of the maximum occurance wild card
67     */
68    private static final String MAX_OCCURS_WILDCARD = "unbounded";
69  
70    // --------------------/
71    // - Member Variables -/
72    // --------------------/
73  
74    /**
75     * The current ComponentReader
76     **/
77    private ComponentReader unmarshaller;
78  
79    /**
80     * The current branch depth
81     **/
82    private int depth = 0;
83  
84    /**
85     * The ModelGroup reference for the ModelGroup we are constructing
86     **/
87    private Group _group = null;
88  
89    /**
90     * The Schema being "unmarshalled"
91     **/
92    private Schema _schema = null;
93  
94    /**
95     * The element name of the Group to be unmarshalled.
96     **/
97    private String _element = SchemaNames.SEQUENCE;
98  
99    private boolean foundAll = false;
100   private boolean foundElement = false;
101   private boolean foundGroup = false;
102   private boolean foundModelGroup = false;
103   private boolean foundAnnotation = false;
104 
105   // ----------------/
106   // - Constructors -/
107   // ----------------/
108 
109   /**
110    * Creates a new GroupUnmarshaller.
111    * 
112    * @param schemaContext the {@link SchemaContext} to get some configuration settings from
113    * @param schema the Schema to which the Group belongs
114    * @param element the element name for this type of group
115    * @param atts the AttributeList
116    **/
117   public GroupUnmarshaller(final SchemaContext schemaContext, final Schema schema,
118       final String element, final AttributeSet atts) {
119     super(schemaContext);
120 
121     this._schema = schema;
122 
123     _group = new Group();
124     // -- handle attributes
125     String attValue = null;
126 
127 
128     if (SchemaNames.SEQUENCE.equals(element)) {
129       _group.setOrder(Order.sequence);
130     } else if (SchemaNames.CHOICE.equals(element)) {
131       _group.setOrder(Order.choice);
132     } else if (SchemaNames.ALL.equals(element)) {
133       foundAll = true;
134       _group.setOrder(Order.all);
135     } else {
136       String err = "Invalid group element name: '" + element + "'";
137       throw new IllegalArgumentException(err);
138     }
139 
140 
141     _element = element;
142 
143     // -- set name
144     attValue = atts.getValue(SchemaNames.NAME_ATTR);
145     if (attValue != null) {
146       _group.setName(attValue);
147     }
148 
149     /*
150      * @maxOccurs If maxOccurs is present, the value is either unbounded or the int value of the
151      * attribute, otherwise maxOccurs equals the minOccurs value.
152      */
153     attValue = atts.getValue(SchemaNames.MAX_OCCURS_ATTR);
154 
155     if (attValue != null) {
156       if (MAX_OCCURS_WILDCARD.equals(attValue))
157         _group.setMaxOccurs(-1);
158       else
159         _group.setMaxOccurs(toInt(attValue));
160     }
161     // -- minOccurs
162     attValue = atts.getValue("minOccurs");
163     if (attValue != null)
164       _group.setMinOccurs(toInt(attValue));
165 
166     if (_group.getOrder() == Order.all) {
167       if (_group.getMaxOccurs() != 1) {
168         String err = "Wrong maxOccurs value for a <all>:" + _group.getMaxOccurs();
169         err += "\n1 is the only possible value.";
170         throw new IllegalArgumentException(err);
171       }
172       if (_group.getMinOccurs() > 1) {
173         String err = "Wrong minOccurs value for a <all>:" + _group.getMinOccurs();
174         err += "\n0 or 1 are the only possible values.";
175         throw new IllegalArgumentException(err);
176       }
177     }
178     // -- id
179     _group.setId(atts.getValue("id"));
180 
181   } // -- GroupUnmarshaller
182 
183   // -----------/
184   // - Methods -/
185   // -----------/
186 
187   /**
188    * Returns the name of the element that this ComponentReader handles
189    * 
190    * @return the name of the element that this ComponentReader handles
191    **/
192   public String elementName() {
193     return _element;
194   } // -- elementName
195 
196 
197   /**
198    * Returns the Group that was unmarshalled by this Unmarshaller. This method should only be called
199    * after unmarshalling has been completed.
200    *
201    * @return the unmarshalled Group
202    **/
203   public Group getGroup() {
204     return _group;
205   } // -- getGroup
206 
207   /**
208    * Returns the Object created by this ComponentReader
209    * 
210    * @return the Object created by this ComponentReader
211    **/
212   public Object getObject() {
213     return getGroup();
214   } // -- getObject
215 
216   /**
217    * Signals the start of an element with the given name.
218    *
219    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
220    *        prefix).
221    * @param namespace the namespace of the element. This may be null. Note: A null namespace is not
222    *        the same as the default namespace unless the default namespace is also null.
223    * @param atts the AttributeSet containing the attributes associated with the element.
224    * @param nsDecls the namespace declarations being declared for this element. This may be null.
225    **/
226   public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
227       throws XMLException {
228     // -- Do delagation if necessary
229     if (unmarshaller != null) {
230       unmarshaller.startElement(name, namespace, atts, nsDecls);
231       ++depth;
232       return;
233     }
234 
235     if (SchemaNames.ANNOTATION.equals(name)) {
236       if (foundElement || foundGroup || foundModelGroup)
237         error("An annotation may only appear as the first child " + "of an element definition.");
238 
239 
240       if (foundAnnotation)
241         error("Only one (1) 'annotation' is allowed as a child of " + "element definitions.");
242 
243       foundAnnotation = true;
244       unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
245     } else if (SchemaNames.ELEMENT.equals(name)) {
246       foundElement = true;
247       unmarshaller = new ElementUnmarshaller(getSchemaContext(), _schema, atts);
248     }
249     // --group
250     else if (name.equals(SchemaNames.GROUP)) {
251       foundModelGroup = true;
252       unmarshaller = new ModelGroupUnmarshaller(getSchemaContext(), _schema, atts);
253     }
254 
255     // --all, sequence, choice
256     else if ((SchemaNames.isGroupName(name)) && (name != SchemaNames.GROUP)) {
257       foundGroup = true;
258       if (SchemaNames.ALL.equals(name))
259         foundAll = true;
260       unmarshaller = new GroupUnmarshaller(getSchemaContext(), _schema, name, atts);
261     }
262     // --any
263     else if (SchemaNames.ANY.equals(name)) {
264       if (foundAll)
265         error("<any> can not appear as a child of a <all> element");
266       unmarshaller = new WildcardUnmarshaller(getSchemaContext(), _group, _schema, name, atts);
267     }
268 
269     else {
270       StringBuffer err = new StringBuffer("illegal element <");
271       err.append(name);
272       err.append("> found in <group>.");
273       throw new SchemaException(err.toString());
274     }
275 
276   } // -- startElement
277 
278   /**
279    * Signals to end of the element with the given name.
280    *
281    * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a
282    *        prefix).
283    * @param namespace the namespace of the element.
284    **/
285   public void endElement(String name, String namespace) throws XMLException {
286 
287     // -- Do delagation if necessary
288     if ((unmarshaller != null) && (depth > 0)) {
289       unmarshaller.endElement(name, namespace);
290       --depth;
291       return;
292     }
293     // -- check for name mismatches
294     if (unmarshaller != null) {
295       if (!name.equals(unmarshaller.elementName())) {
296         String err = "missing end element for ";
297         err += unmarshaller.elementName();
298         throw new SchemaException(err);
299       }
300     }
301 
302     // -- have unmarshaller perform any necessary clean up
303     unmarshaller.finish();
304 
305     // -- <any>
306     if (SchemaNames.ANY.equals(name)) {
307       Wildcard wildcard = ((WildcardUnmarshaller) unmarshaller).getWildcard();
308       try {
309         _group.addWildcard(wildcard);
310       } catch (SchemaException e) {
311         throw new IllegalArgumentException(e.getMessage());
312       }
313     }
314     if (SchemaNames.ANNOTATION.equals(name)) {
315       Annotation ann = (Annotation) unmarshaller.getObject();
316       _group.addAnnotation(ann);
317     } else if (SchemaNames.ELEMENT.equals(name)) {
318       ElementDecl element = (ElementDecl) unmarshaller.getObject();
319       _group.addElementDecl(element);
320     } else if (name.equals(SchemaNames.GROUP)) {
321       ModelGroup group = (ModelGroup) unmarshaller.getObject();
322       _group.addGroup(group);
323     } else if ((SchemaNames.isGroupName(name)) && (name != SchemaNames.GROUP)) {
324       Group group = ((GroupUnmarshaller) unmarshaller).getGroup();
325       _group.addGroup(group);
326     }
327 
328     unmarshaller = null;
329   } // -- endElement
330 
331   public void characters(char[] ch, int start, int length) throws XMLException {
332     // -- Do delagation if necessary
333     if (unmarshaller != null) {
334       unmarshaller.characters(ch, start, length);
335     }
336   } // -- characters
337 
338 } // -- GroupUnmarshaller