001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: GroupUnmarshaller.java 6230 2006-09-19 07:56:07Z wguttmn $
044: */package org.exolab.castor.xml.schema.reader;
045:
046: //-- imported classes and packages
047: import org.exolab.castor.xml.AttributeSet;
048: import org.exolab.castor.xml.Namespaces;
049: import org.exolab.castor.xml.XMLException;
050: import org.exolab.castor.xml.schema.Annotation;
051: import org.exolab.castor.xml.schema.Group;
052: import org.exolab.castor.xml.schema.ElementDecl;
053: import org.exolab.castor.xml.schema.ModelGroup;
054: import org.exolab.castor.xml.schema.Order;
055: import org.exolab.castor.xml.schema.Resolver;
056: import org.exolab.castor.xml.schema.Schema;
057: import org.exolab.castor.xml.schema.SchemaException;
058: import org.exolab.castor.xml.schema.SchemaNames;
059: import org.exolab.castor.xml.schema.Wildcard;
060:
061: /**
062: * A class for Unmarshalling ModelGroups
063: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
064: * @version $Revision: 6230 $ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
065: **/
066: public class GroupUnmarshaller extends ComponentReader {
067:
068: //--------------------------/
069: //- Static Class Variables -/
070: //--------------------------/
071:
072: /**
073: * The value of the maximum occurance wild card
074: */
075: private static final String MAX_OCCURS_WILDCARD = "unbounded";
076:
077: //--------------------/
078: //- Member Variables -/
079: //--------------------/
080:
081: /**
082: * The current ComponentReader
083: **/
084: private ComponentReader unmarshaller;
085:
086: /**
087: * The current branch depth
088: **/
089: private int depth = 0;
090:
091: /**
092: * The ModelGroup reference for the ModelGroup we are constructing
093: **/
094: private Group _group = null;
095:
096: /**
097: * The Schema being "unmarshalled"
098: **/
099: private Schema _schema = null;
100:
101: /**
102: * The element name of the Group to be unmarshalled.
103: **/
104: private String _element = SchemaNames.SEQUENCE;
105:
106: private boolean foundAll = false;
107: private boolean foundElement = false;
108: private boolean foundGroup = false;
109: private boolean foundModelGroup = false;
110: private boolean foundAnnotation = false;
111:
112: //----------------/
113: //- Constructors -/
114: //----------------/
115:
116: /**
117: * Creates a new GroupUnmarshaller
118: * @param schema the Schema to which the Group belongs
119: * @param element the element name for this type of group
120: * @param atts the AttributeList
121: * @param resolver the resolver being used for reference resolving
122: **/
123: public GroupUnmarshaller(Schema schema, String element,
124: AttributeSet atts, Resolver resolver) {
125: super ();
126: setResolver(resolver);
127: this ._schema = schema;
128:
129: _group = new Group();
130: //-- handle attributes
131: String attValue = null;
132:
133: if (SchemaNames.SEQUENCE.equals(element)) {
134: _group.setOrder(Order.seq);
135: } else if (SchemaNames.CHOICE.equals(element)) {
136: _group.setOrder(Order.choice);
137: } else if (SchemaNames.ALL.equals(element)) {
138: foundAll = true;
139: _group.setOrder(Order.all);
140: } else {
141: String err = "Invalid group element name: '" + element
142: + "'";
143: throw new IllegalArgumentException(err);
144: }
145:
146: _element = element;
147:
148: //-- set name
149: attValue = atts.getValue(SchemaNames.NAME_ATTR);
150: if (attValue != null) {
151: _group.setName(attValue);
152: }
153:
154: /*
155: * @maxOccurs
156: * If maxOccurs is present, the value is either unbounded
157: * or the int value of the attribute, otherwise maxOccurs
158: * equals the minOccurs value.
159: */
160: attValue = atts.getValue(SchemaNames.MAX_OCCURS_ATTR);
161:
162: if (attValue != null) {
163: if (MAX_OCCURS_WILDCARD.equals(attValue))
164: _group.setMaxOccurs(-1);
165: else
166: _group.setMaxOccurs(toInt(attValue));
167: }
168: //-- minOccurs
169: attValue = atts.getValue("minOccurs");
170: if (attValue != null)
171: _group.setMinOccurs(toInt(attValue));
172:
173: if (_group.getOrder() == Order.all) {
174: if (_group.getMaxOccurs() != 1) {
175: String err = "Wrong maxOccurs value for a <all>:"
176: + _group.getMaxOccurs();
177: err += "\n1 is the only possible value.";
178: throw new IllegalArgumentException(err);
179: }
180: if (_group.getMinOccurs() > 1) {
181: String err = "Wrong minOccurs value for a <all>:"
182: + _group.getMinOccurs();
183: err += "\n0 or 1 are the only possible values.";
184: throw new IllegalArgumentException(err);
185: }
186: }
187: //-- id
188: _group.setId(atts.getValue("id"));
189:
190: } //-- GroupUnmarshaller
191:
192: //-----------/
193: //- Methods -/
194: //-----------/
195:
196: /**
197: * Returns the name of the element that this ComponentReader
198: * handles
199: * @return the name of the element that this ComponentReader
200: * handles
201: **/
202: public String elementName() {
203: return _element;
204: } //-- elementName
205:
206: /**
207: * Returns the Group that was unmarshalled by this Unmarshaller.
208: * This method should only be called after unmarshalling
209: * has been completed.
210: *
211: * @return the unmarshalled Group
212: **/
213: public Group getGroup() {
214: return _group;
215: } //-- getGroup
216:
217: /**
218: * Returns the Object created by this ComponentReader
219: * @return the Object created by this ComponentReader
220: **/
221: public Object getObject() {
222: return getGroup();
223: } //-- getObject
224:
225: /**
226: * Signals the start of an element with the given name.
227: *
228: * @param name the NCName of the element. It is an error
229: * if the name is a QName (ie. contains a prefix).
230: * @param namespace the namespace of the element. This may be null.
231: * Note: A null namespace is not the same as the default namespace unless
232: * the default namespace is also null.
233: * @param atts the AttributeSet containing the attributes associated
234: * with the element.
235: * @param nsDecls the namespace declarations being declared for this
236: * element. This may be null.
237: **/
238: public void startElement(String name, String namespace,
239: AttributeSet atts, Namespaces nsDecls) throws XMLException {
240: //-- Do delagation if necessary
241: if (unmarshaller != null) {
242: unmarshaller.startElement(name, namespace, atts, nsDecls);
243: ++depth;
244: return;
245: }
246:
247: if (SchemaNames.ANNOTATION.equals(name)) {
248: if (foundElement || foundGroup || foundModelGroup)
249: error("An annotation may only appear as the first child "
250: + "of an element definition.");
251:
252: if (foundAnnotation)
253: error("Only one (1) 'annotation' is allowed as a child of "
254: + "element definitions.");
255:
256: foundAnnotation = true;
257: unmarshaller = new AnnotationUnmarshaller(atts);
258: } else if (SchemaNames.ELEMENT.equals(name)) {
259: foundElement = true;
260: unmarshaller = new ElementUnmarshaller(_schema, atts,
261: getResolver());
262: }
263: //--group
264: else if (name.equals(SchemaNames.GROUP)) {
265: foundModelGroup = true;
266: unmarshaller = new ModelGroupUnmarshaller(_schema, atts,
267: getResolver());
268: }
269:
270: //--all, sequence, choice
271: else if ((SchemaNames.isGroupName(name))
272: && (name != SchemaNames.GROUP)) {
273: foundGroup = true;
274: if (SchemaNames.ALL.equals(name))
275: foundAll = true;
276: unmarshaller = new GroupUnmarshaller(_schema, name, atts,
277: getResolver());
278: }
279: //--any
280: else if (SchemaNames.ANY.equals(name)) {
281: if (foundAll)
282: error("<any> can not appear as a child of a <all> element");
283: unmarshaller = new WildcardUnmarshaller(_group, _schema,
284: name, atts, getResolver());
285: }
286:
287: else {
288: StringBuffer err = new StringBuffer("illegal element <");
289: err.append(name);
290: err.append("> found in <group>.");
291: throw new SchemaException(err.toString());
292: }
293: unmarshaller.setDocumentLocator(getDocumentLocator());
294:
295: } //-- startElement
296:
297: /**
298: * Signals to end of the element with the given name.
299: *
300: * @param name the NCName of the element. It is an error
301: * if the name is a QName (ie. contains a prefix).
302: * @param namespace the namespace of the element.
303: **/
304: public void endElement(String name, String namespace)
305: throws XMLException {
306:
307: //-- Do delagation if necessary
308: if ((unmarshaller != null) && (depth > 0)) {
309: unmarshaller.endElement(name, namespace);
310: --depth;
311: return;
312: }
313: //-- check for name mismatches
314: if (unmarshaller != null) {
315: if (!name.equals(unmarshaller.elementName())) {
316: String err = "missing end element for ";
317: err += unmarshaller.elementName();
318: throw new SchemaException(err);
319: }
320: }
321:
322: //-- have unmarshaller perform any necessary clean up
323: unmarshaller.finish();
324:
325: //-- <any>
326: if (SchemaNames.ANY.equals(name)) {
327: Wildcard wildcard = ((WildcardUnmarshaller) unmarshaller)
328: .getWildcard();
329: try {
330: _group.addWildcard(wildcard);
331: } catch (SchemaException e) {
332: throw new IllegalArgumentException(e.getMessage());
333: }
334: }
335: if (SchemaNames.ANNOTATION.equals(name)) {
336: Annotation ann = (Annotation) unmarshaller.getObject();
337: _group.addAnnotation(ann);
338: } else if (SchemaNames.ELEMENT.equals(name)) {
339: ElementDecl element = (ElementDecl) unmarshaller
340: .getObject();
341: _group.addElementDecl(element);
342: } else if (name.equals(SchemaNames.GROUP)) {
343: ModelGroup group = (ModelGroup) unmarshaller.getObject();
344: _group.addGroup(group);
345: } else if ((SchemaNames.isGroupName(name))
346: && (name != SchemaNames.GROUP)) {
347: Group group = ((GroupUnmarshaller) unmarshaller).getGroup();
348: _group.addGroup(group);
349: }
350:
351: unmarshaller = null;
352: } //-- endElement
353:
354: public void characters(char[] ch, int start, int length)
355: throws XMLException {
356: //-- Do delagation if necessary
357: if (unmarshaller != null) {
358: unmarshaller.characters(ch, start, length);
359: }
360: } //-- characters
361:
362: } //-- GroupUnmarshaller
|