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: ComplexTypeUnmarshaller.java 5951 2006-05-30 22:18:48Z bsnyder $
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.AttributeDecl;
052: import org.exolab.castor.xml.schema.AttributeGroupReference;
053: import org.exolab.castor.xml.schema.ComplexType;
054: import org.exolab.castor.xml.schema.ContentType;
055: import org.exolab.castor.xml.schema.ElementDecl;
056: import org.exolab.castor.xml.schema.Group;
057: import org.exolab.castor.xml.schema.ModelGroup;
058: import org.exolab.castor.xml.schema.Resolver;
059: import org.exolab.castor.xml.schema.Schema;
060: import org.exolab.castor.xml.schema.SchemaException;
061: import org.exolab.castor.xml.schema.SchemaNames;
062: import org.exolab.castor.xml.schema.Wildcard;
063: import org.exolab.castor.xml.schema.XMLType;
064:
065: /**
066: * A class for Unmarshalling ComplexTypes
067: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
068: * @version $Revision: 5951 $ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
069: **/
070: public class ComplexTypeUnmarshaller extends ComponentReader {
071:
072: //--------------------/
073: //- Member Variables -/
074: //--------------------/
075:
076: /**
077: * The current ComponentReader
078: **/
079: private ComponentReader unmarshaller;
080:
081: /**
082: * The current branch depth
083: **/
084: private int depth = 0;
085:
086: /**
087: * The Attribute reference for the Attribute we are constructing
088: **/
089: private ComplexType _complexType = null;
090: private boolean allowAnnotation = true;
091: private boolean foundAnnotation = false;
092: private boolean foundAnyAttribute = false;
093: private boolean foundAttributes = false;
094: private boolean foundSimpleContent = false;
095: private boolean foundComplexContent = false;
096: private boolean foundModelGroup = false;
097:
098: private Schema _schema = null;
099:
100: //----------------/
101: //- Constructors -/
102: //----------------/
103:
104: /**
105: * Creates a new ComplexTypeUnmarshaller
106: * @param schema the Schema to which the ComplexType belongs
107: * @param atts the AttributeList
108: * @param resolver the resolver being used for reference resolving
109: **/
110: public ComplexTypeUnmarshaller(Schema schema, AttributeSet atts,
111: Resolver resolver) throws XMLException {
112: super ();
113: setResolver(resolver);
114: this ._schema = schema;
115:
116: _complexType = schema.createComplexType();
117:
118: _complexType.useResolver(resolver);
119:
120: //-- handle attributes
121: String attValue = null;
122:
123: _complexType.setName(atts.getValue(SchemaNames.NAME_ATTR));
124:
125: //-- read contentType
126: String content = atts.getValue(SchemaNames.MIXED);
127:
128: if (content != null) {
129: if (content.equals("true"))
130: _complexType.setContentType(ContentType
131: .valueOf("mixed"));
132: if (content.equals("false"))
133: _complexType.setContentType(ContentType
134: .valueOf("elementOnly"));
135: }
136:
137: //-- base and derivedBy
138: String base = atts.getValue(SchemaNames.BASE_ATTR);
139: if ((base != null) && (base.length() > 0)) {
140:
141: String derivedBy = atts.getValue("derivedBy");
142: _complexType.setDerivationMethod(derivedBy);
143: if ((derivedBy == null) || (derivedBy.length() == 0)
144: || (derivedBy.equals("extension"))) {
145: XMLType baseType = schema.getType(base);
146: if (baseType == null)
147: _complexType.setBase(base); //the base type has not been read
148: else
149: _complexType.setBaseType(baseType);
150: } else if (derivedBy.equals("restrictions")) {
151: String err = "restrictions not yet supported for <type>.";
152: throw new SchemaException(err);
153: } else {
154: String err = "invalid value for derivedBy attribute of ";
155: err += "<type>: " + derivedBy;
156: throw new SchemaException(err);
157: }
158:
159: }
160:
161: //-- @abstract
162: attValue = atts.getValue(SchemaNames.ABSTRACT);
163: if (attValue != null) {
164: Boolean bool = Boolean.valueOf(attValue);
165: _complexType.setAbstract(bool.booleanValue());
166: }
167:
168: //-- @block
169: _complexType.setBlock(atts.getValue(SchemaNames.BLOCK_ATTR));
170:
171: //-- @final
172: _complexType.setFinal(atts.getValue(SchemaNames.FINAL_ATTR));
173:
174: //-- @id
175: _complexType.setId(atts.getValue(SchemaNames.ID_ATTR));
176:
177: } //-- ComplexTypeUnmarshaller
178:
179: //-----------/
180: //- Methods -/
181: //-----------/
182:
183: /**
184: * Returns the name of the element that this ComponentReader
185: * handles
186: * @return the name of the element that this ComponentReader
187: * handles
188: **/
189: public String elementName() {
190: return SchemaNames.COMPLEX_TYPE;
191: } //-- elementName
192:
193: /**
194: *
195: **/
196: public ComplexType getComplexType() {
197: return _complexType;
198: } //-- getComplexType
199:
200: /**
201: * Returns the Object created by this ComponentReader
202: * @return the Object created by this ComponentReader
203: **/
204: public Object getObject() {
205: return getComplexType();
206: } //-- getObject
207:
208: /**
209: * Signals the start of an element with the given name.
210: *
211: * @param name the NCName of the element. It is an error
212: * if the name is a QName (ie. contains a prefix).
213: * @param namespace the namespace of the element. This may be null.
214: * Note: A null namespace is not the same as the default namespace unless
215: * the default namespace is also null.
216: * @param atts the AttributeSet containing the attributes associated
217: * with the element.
218: * @param nsDecls the namespace declarations being declared for this
219: * element. This may be null.
220: **/
221: public void startElement(String name, String namespace,
222: AttributeSet atts, Namespaces nsDecls) throws XMLException {
223: //-- Do delagation if necessary
224: if (unmarshaller != null) {
225: unmarshaller.startElement(name, namespace, atts, nsDecls);
226: ++depth;
227: return;
228: }
229:
230: //-- <anyAttribute>
231: if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
232: if (foundComplexContent)
233: error("an anyAttribute element cannot appear as a child "
234: + "of 'complexType' if 'complexContent' also exists");
235: if (foundSimpleContent)
236: error("an anyAttribute element cannot appear as a child "
237: + "of 'complexType' if 'simpleContent' also exists");
238: foundAnyAttribute = true;
239: allowAnnotation = true;
240: unmarshaller = new WildcardUnmarshaller(_complexType,
241: _schema, name, atts, getResolver());
242: }
243:
244: //-- attribute declarations
245: else if (SchemaNames.ATTRIBUTE.equals(name)) {
246: if (foundComplexContent)
247: error("an attribute definition cannot appear as a child "
248: + "of 'complexType' if 'complexContent' also exists");
249: if (foundSimpleContent)
250: error("an 'attribute' definition cannot appear as a child "
251: + "of 'complexType' if 'simpleContent' also exists");
252: if (foundAnyAttribute)
253: error("an attribute definition cannot appear after "
254: + "the definition of an 'anyAttribute' element");
255: foundAttributes = true;
256: allowAnnotation = false;
257: unmarshaller = new AttributeUnmarshaller(_schema, atts,
258: getResolver());
259: }
260: //-- attribute group declarations
261: else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
262: //-- make sure we have an attribute group
263: //-- reference and not a definition
264:
265: if (atts.getValue(SchemaNames.REF_ATTR) == null) {
266: error("A 'complexType' may contain referring "
267: + "attributeGroups, but not defining ones.");
268: }
269: if (foundComplexContent)
270: error("an attributeGroup reference cannot appear as a child "
271: + "of 'complexType' if 'complexContent' also exists");
272: if (foundSimpleContent)
273: error("an attributeGroup reference cannot appear as a child "
274: + "of 'complexType' if 'simpleContent' also exists");
275: if (foundAnyAttribute)
276: error("an 'attributeGroup' reference cannot appear after "
277: + "the definition of an 'anyAttribute' element");
278:
279: foundAttributes = true;
280: allowAnnotation = false;
281: unmarshaller = new AttributeGroupUnmarshaller(_schema, atts);
282: }
283: //-- simpleContent
284: else if (SchemaNames.SIMPLE_CONTENT.equals(name)) {
285:
286: if (foundAttributes)
287: error("'simpleContent' and attribute definitions cannot both "
288: + "appear as children of 'complexType' at the same time.");
289: if (foundComplexContent)
290: error("'simpleContent' and 'complexContent' cannot both "
291: + "appear as children of 'complexType'.");
292: if (foundSimpleContent)
293: error("Only one (1) 'simpleContent' may appear as a child of "
294: + "'complexType'.");
295: if (foundModelGroup)
296: error("'simpleContent' cannot appear as a child of "
297: + "'complexType' if 'all', 'sequence', 'choice' or "
298: + "'group' also exist");
299:
300: foundSimpleContent = true;
301: allowAnnotation = false;
302: _complexType.setSimpleContent(true);
303: unmarshaller = new SimpleContentUnmarshaller(_complexType,
304: atts, getResolver());
305: }
306: //-- complexContent
307: else if (SchemaNames.COMPLEX_CONTENT.equals(name)) {
308:
309: if (foundAttributes)
310: error("'complexContent' and attribute definitions cannot both "
311: + "appear as children of 'complexType' at the same time.");
312: if (foundSimpleContent)
313: error("'simpleContent' and 'complexContent' cannot both "
314: + "appear as children of 'complexType'.");
315: if (foundComplexContent)
316: error("Only one (1) 'complexContent' may appear as a child of "
317: + "'complexType'.");
318: if (foundModelGroup)
319: error("'complexContent' cannot appear as a child of "
320: + "'complexType' if 'all', 'sequence', 'choice' or "
321: + "'group' also exist");
322:
323: foundComplexContent = true;
324: allowAnnotation = false;
325:
326: _complexType.setComplexContent(true);
327: unmarshaller = new ComplexContentUnmarshaller(_complexType,
328: atts, getResolver());
329: }
330: //--<group>
331: else if (name.equals(SchemaNames.GROUP)) {
332: if (foundAttributes)
333: error("'" + name
334: + "' must appear before any attribute "
335: + "definitions when a child of 'complexType'.");
336: if (foundComplexContent)
337: error("'" + name
338: + "' and 'complexContent' cannot both "
339: + "appear as children of 'complexType'.");
340: if (foundSimpleContent)
341: error("'" + name + "' and 'simpleContent' cannot both "
342: + "appear as children of 'complexType'.");
343: if (foundModelGroup)
344: error("'"
345: + name
346: + "' cannot appear as a child of 'complexType' "
347: + "if another 'all', 'sequence', 'choice' or "
348: + "'group' also exists.");
349:
350: foundModelGroup = true;
351: allowAnnotation = false;
352: unmarshaller = new ModelGroupUnmarshaller(_schema, atts,
353: getResolver());
354: }
355: //-- ModelGroup declarations (choice, all, sequence)
356: else if ((SchemaNames.isGroupName(name))
357: && (name != SchemaNames.GROUP)) {
358:
359: if (foundAttributes)
360: error("'" + name
361: + "' must appear before any attribute "
362: + "definitions when a child of 'complexType'.");
363: if (foundComplexContent)
364: error("'" + name
365: + "' and 'complexContent' cannot both "
366: + "appear as children of 'complexType'.");
367: if (foundSimpleContent)
368: error("'" + name + "' and 'simpleContent' cannot both "
369: + "appear as children of 'complexType'.");
370: if (foundModelGroup)
371: error("'"
372: + name
373: + "' cannot appear as a child of 'complexType' "
374: + "if another 'all', 'sequence', 'choice' or "
375: + "'group' also exists.");
376:
377: foundModelGroup = true;
378: allowAnnotation = false;
379: unmarshaller = new GroupUnmarshaller(_schema, name, atts,
380: getResolver());
381: } else if (name.equals(SchemaNames.ANNOTATION)) {
382: if (allowAnnotation) {
383: unmarshaller = new AnnotationUnmarshaller(atts);
384: allowAnnotation = false;
385: foundAnnotation = true;
386: } else {
387: if (foundAnnotation) {
388: error("Only one (1) annotation may appear as a child of "
389: + "'complexType' elements");
390: }
391: error("An annotation must appear as the first child of "
392: + "'complexType' elements.");
393: }
394: } else
395: illegalElement(name);
396:
397: unmarshaller.setDocumentLocator(getDocumentLocator());
398: } //-- startElement
399:
400: /**
401: * Signals to end of the element with the given name.
402: *
403: * @param name the NCName of the element. It is an error
404: * if the name is a QName (ie. contains a prefix).
405: * @param namespace the namespace of the element.
406: **/
407: public void endElement(String name, String namespace)
408: throws XMLException {
409:
410: //-- Do delagation if necessary
411: if ((unmarshaller != null) && (depth > 0)) {
412: unmarshaller.endElement(name, namespace);
413: --depth;
414: return;
415: }
416: //-- have unmarshaller perform any necessary clean up
417: unmarshaller.finish();
418:
419: //-- <anyAttribute>
420: if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
421: Wildcard wildcard = ((WildcardUnmarshaller) unmarshaller)
422: .getWildcard();
423: try {
424: _complexType.setAnyAttribute(wildcard);
425: } catch (SchemaException e) {
426: throw new IllegalArgumentException(e.getMessage());
427: }
428: }
429: //-- attribute declarations
430: if (SchemaNames.ATTRIBUTE.equals(name)) {
431: AttributeDecl attrDecl = ((AttributeUnmarshaller) unmarshaller)
432: .getAttribute();
433:
434: _complexType.addAttributeDecl(attrDecl);
435: }
436: //-- attribute groups
437: else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
438: AttributeGroupReference attrGroupRef = (AttributeGroupReference) unmarshaller
439: .getObject();
440: _complexType.addAttributeGroupReference(attrGroupRef);
441: }
442: //-- element declarations
443: else if (SchemaNames.ELEMENT.equals(name)) {
444:
445: ElementDecl element = ((ElementUnmarshaller) unmarshaller)
446: .getElement();
447: _complexType.addElementDecl(element);
448: }
449: //--group
450: else if (name.equals(SchemaNames.GROUP)) {
451: ModelGroup group = ((ModelGroupUnmarshaller) unmarshaller)
452: .getGroup();
453: _complexType.addGroup(group);
454: }
455:
456: //-- group declarations (all, choice, sequence)
457: else if ((SchemaNames.isGroupName(name))
458: && (name != SchemaNames.GROUP)) {
459: Group group = ((GroupUnmarshaller) unmarshaller).getGroup();
460: _complexType.addGroup(group);
461: }
462: //-- annotation
463: else if (SchemaNames.ANNOTATION.equals(name)) {
464: Annotation ann = ((AnnotationUnmarshaller) unmarshaller)
465: .getAnnotation();
466: _complexType.addAnnotation(ann);
467: }
468:
469: unmarshaller = null;
470: } //-- endElement
471:
472: public void characters(char[] ch, int start, int length)
473: throws XMLException {
474: //-- Do delagation if necessary
475: if (unmarshaller != null) {
476: unmarshaller.characters(ch, start, length);
477: }
478: } //-- characters
479:
480: } //-- ComplexTypeUnmarshaller
|