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 2000-2002 (C) Intalio Inc. All Rights Reserved.
042: *
043: * $Id: ComplexContentRestrictionUnmarshaller.java 6231 2006-09-20 10:55:10Z wguttmn $
044: */package org.exolab.castor.xml.schema.reader;
045:
046: import org.exolab.castor.xml.AttributeSet;
047: import org.exolab.castor.xml.Namespaces;
048: import org.exolab.castor.xml.XMLException;
049: import org.exolab.castor.xml.schema.Annotation;
050: import org.exolab.castor.xml.schema.AttributeDecl;
051: import org.exolab.castor.xml.schema.AttributeGroupReference;
052: import org.exolab.castor.xml.schema.ComplexType;
053: import org.exolab.castor.xml.schema.Group;
054: import org.exolab.castor.xml.schema.Resolver;
055: import org.exolab.castor.xml.schema.Schema;
056: import org.exolab.castor.xml.schema.SchemaException;
057: import org.exolab.castor.xml.schema.SchemaNames;
058: import org.exolab.castor.xml.schema.Wildcard;
059: import org.exolab.castor.xml.schema.XMLType;
060:
061: /**
062: * A class for unmarshalling restriction elements of a complexContent
063: * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
064: * @version $Revision: 6231 $ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
065: * TODO: validation stuff about the data
066: * TODO: set the flag of restriction for the complexType
067:
068: **/
069: public class ComplexContentRestrictionUnmarshaller extends
070: 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 complexType we are unmarshalling
088: **/
089: private ComplexType _complexType = null;
090:
091: private Schema _schema = null;
092: private boolean foundAnnotation = false;
093: private boolean foundAttribute = false;
094: private boolean foundAttributeGroup = false;
095: private boolean foundModelGroup = false;
096:
097: //----------------/
098: //- Constructors -/
099: //----------------/
100: /**
101: * Creates a new RestrictionUnmarshaller
102: * @param complexType the complexType being unmarshalled
103: * @param atts the AttributeList
104: */
105: public ComplexContentRestrictionUnmarshaller(
106: ComplexType complexType, AttributeSet atts,
107: Resolver resolver) throws XMLException {
108: super ();
109: setResolver(resolver);
110: _complexType = complexType;
111: _schema = complexType.getSchema();
112:
113: _complexType.setDerivationMethod(SchemaNames.RESTRICTION);
114: _complexType.setRestriction(true);
115:
116: //-- base
117: String base = atts.getValue(SchemaNames.BASE_ATTR);
118: if ((base != null) && (base.length() > 0)) {
119:
120: XMLType baseType = _schema.getType(base);
121: if (baseType == null)
122: _complexType.setBase(base);
123: // the base must not be a simpleType
124: else if (baseType.isSimpleType()) {
125: String err = "complexType: " + (_complexType.getName()) != null ? _complexType
126: .getName()
127: : "\n";
128: err += "A complex type cannot be a restriction"
129: + " of a simpleType.";
130: throw new IllegalStateException(err);
131: } else if (!baseType.isAnyType()) {
132: // We are now sure the base is a complexType
133: // but is it already a restriction? (see PR 5.11->restriction->1.1)
134:
135: //-- KV 2004-03-15
136: //-- Need to valididate this constraint...I couldn't
137: //-- find it in the XML Schema 1.0 Recommendation,
138: //-- commenting out for now.
139: /*
140: if (((ComplexType)baseType).isRestricted()) {
141: String err="complexType: "+(_complexType.getName()) != null?
142: _complexType.getName():"\n";
143: err +="A complex type cannot be a restriction"+
144: " of a restriction.";
145: throw new IllegalStateException(err);
146: }
147:
148: */
149: }
150: _complexType.setBase(base);
151: _complexType.setBaseType(baseType);
152:
153: }
154:
155: } //-- ComplexContentRestrictionUnmarshaller
156:
157: //-----------/
158: //- Methods -/
159: //-----------/
160:
161: /**
162: * Returns the name of the element that this ComponentReader
163: * handles
164: * @return the name of the element that this ComponentReader
165: * handles
166: **/
167: public String elementName() {
168: return SchemaNames.RESTRICTION;
169: } //-- elementName
170:
171: /**
172: * Returns the Object created by this ComponentReader
173: * @return the Object created by this ComponentReader
174: **/
175: public Object getObject() {
176: return null;
177: } //-- getObject
178:
179: /**
180: * Signals the start of an element with the given name.
181: *
182: * @param name the NCName of the element. It is an error
183: * if the name is a QName (ie. contains a prefix).
184: * @param namespace the namespace of the element. This may be null.
185: * Note: A null namespace is not the same as the default namespace unless
186: * the default namespace is also null.
187: * @param atts the AttributeSet containing the attributes associated
188: * with the element.
189: * @param nsDecls the namespace declarations being declared for this
190: * element. This may be null.
191: **/
192: public void startElement(String name, String namespace,
193: AttributeSet atts, Namespaces nsDecls) throws XMLException {
194: //-- Do delagation if necessary
195: if (unmarshaller != null) {
196: unmarshaller.startElement(name, namespace, atts, nsDecls);
197: ++depth;
198: return;
199: }
200:
201: //-- annotation
202: if (name.equals(SchemaNames.ANNOTATION)) {
203:
204: if (foundModelGroup || foundAttribute
205: || foundAttributeGroup)
206: error("An annotation must appear as the first child "
207: + "of 'restriction' elements.");
208:
209: if (foundAnnotation)
210: error("Only one (1) annotation may appear as a child of "
211: + "'restriction' elements.");
212:
213: foundAnnotation = true;
214: unmarshaller = new AnnotationUnmarshaller(atts);
215: }
216:
217: //-- ModelGroup declarations (choice, all, sequence, group)
218: else if (SchemaNames.isGroupName(name)) {
219:
220: if (foundAttribute || foundAttributeGroup)
221: error("'" + name
222: + "' must appear before any attribute "
223: + "definitions when a child of 'restriction'.");
224: if (foundModelGroup)
225: error("'"
226: + name
227: + "' cannot appear as a child of 'restriction' "
228: + "if another 'all', 'sequence', 'choice' or "
229: + "'group' also exists.");
230:
231: foundModelGroup = true;
232: unmarshaller = new GroupUnmarshaller(_schema, name, atts,
233: getResolver());
234: }
235:
236: else if (SchemaNames.ATTRIBUTE.equals(name)) {
237: foundAttribute = true;
238: unmarshaller = new AttributeUnmarshaller(_schema, atts,
239: getResolver());
240: }
241:
242: else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
243:
244: //--In a complexType we only reference attribute group
245: if (atts.getValue(SchemaNames.REF_ATTR) == null) {
246: error("A 'complexType' may contain referring "
247: + "attributeGroups, but not defining ones.");
248: }
249: foundAttributeGroup = true;
250: unmarshaller = new AttributeGroupUnmarshaller(_schema, atts);
251: }
252: //-- <anyAttribute>
253: else if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
254: unmarshaller = new WildcardUnmarshaller(_complexType,
255: _schema, name, atts, getResolver());
256: }
257:
258: else
259: illegalElement(name);
260:
261: unmarshaller.setDocumentLocator(getDocumentLocator());
262: } //-- startElement
263:
264: /**
265: * Signals to end of the element with the given name.
266: *
267: * @param name the NCName of the element. It is an error
268: * if the name is a QName (ie. contains a prefix).
269: * @param namespace the namespace of the element.
270: **/
271: public void endElement(String name, String namespace)
272: throws XMLException {
273:
274: //-- Do delagation if necessary
275: if ((unmarshaller != null) && (depth > 0)) {
276: unmarshaller.endElement(name, namespace);
277: --depth;
278: return;
279: }
280:
281: //-- have unmarshaller perform any necessary clean up
282: unmarshaller.finish();
283:
284: //-- annotation
285: if (SchemaNames.ANNOTATION.equals(name)) {
286: Annotation ann = ((AnnotationUnmarshaller) unmarshaller)
287: .getAnnotation();
288: _complexType.addAnnotation(ann);
289: }
290: //-- <anyAttribute>
291: else if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
292: Wildcard wildcard = ((WildcardUnmarshaller) unmarshaller)
293: .getWildcard();
294: try {
295: _complexType.setAnyAttribute(wildcard);
296: } catch (SchemaException e) {
297: throw new IllegalArgumentException(e.getMessage());
298: }
299: }
300: //-- attribute
301: else if (SchemaNames.ATTRIBUTE.equals(name)) {
302: AttributeDecl attrDecl = ((AttributeUnmarshaller) unmarshaller)
303: .getAttribute();
304:
305: /* TODO: do the validation later*/
306: _complexType.addAttributeDecl(attrDecl);
307: }
308: //-- attribute groups
309: else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
310: AttributeGroupReference attrGroupRef = (AttributeGroupReference) unmarshaller
311: .getObject();
312: _complexType.addAttributeGroupReference(attrGroupRef);
313: }
314: //-- group declarations (all, choice, group, sequence)
315: else if (SchemaNames.isGroupName(name)) {
316: Group group = ((GroupUnmarshaller) unmarshaller).getGroup();
317: _complexType.addGroup(group);
318: }
319: unmarshaller = null;
320: } //-- endElement
321:
322: public void characters(char[] ch, int start, int length)
323: throws XMLException {
324: //-- Do delagation if necessary
325: if (unmarshaller != null) {
326: unmarshaller.characters(ch, start, length);
327: }
328: } //-- characters
329: } //-- ComplexContentRestrictionUnmarshaller
|