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: AttributeGroupUnmarshaller.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.AttributeDecl;
052: import org.exolab.castor.xml.schema.AttributeGroup;
053: import org.exolab.castor.xml.schema.AttributeGroupDecl;
054: import org.exolab.castor.xml.schema.AttributeGroupReference;
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:
060: /**
061: * A class for Unmarshalling AttributeGroups
062: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
063: * @version $Revision: 6230 $ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
064: **/
065: public class AttributeGroupUnmarshaller extends ComponentReader {
066:
067: //--------------------/
068: //- Member Variables -/
069: //--------------------/
070:
071: /**
072: * The current ComponentReader
073: **/
074: private ComponentReader unmarshaller;
075:
076: /**
077: * The current branch depth
078: **/
079: private int depth = 0;
080:
081: /**
082: * The AttributeGroup we are constructing
083: **/
084: private AttributeGroup _attributeGroup = null;
085:
086: private boolean allowAnnotation = true;
087: private boolean foundAnyAttribute = false;
088: private boolean isRef = false;
089:
090: private Schema _schema = null;
091:
092: //----------------/
093: //- Constructors -/
094: //----------------/
095:
096: /**
097: * Creates a new AttributeGroupUnmarshaller
098: * @param schema the Schema to which the AttributeGroup belongs
099: * @param atts the AttributeList
100: **/
101: public AttributeGroupUnmarshaller(Schema schema, AttributeSet atts)
102: throws XMLException {
103: super ();
104: this ._schema = schema;
105:
106: String ref = atts.getValue(SchemaNames.REF_ATTR);
107: if (ref != null) {
108: if (ref.length() > 0) {
109: isRef = true;
110: _attributeGroup = new AttributeGroupReference(schema,
111: ref);
112: } else {
113: String err = "The value of the '"
114: + SchemaNames.REF_ATTR
115: + "' attribute for attribute group must contain a "
116: + "valid value.";
117: throw new SchemaException(err);
118: }
119: } else {
120: AttributeGroupDecl attDecl = new AttributeGroupDecl(schema);
121: _attributeGroup = attDecl;
122:
123: //-- handle attributes
124: attDecl.setName(atts.getValue(SchemaNames.NAME_ATTR));
125: attDecl.setId(atts.getValue(SchemaNames.ID_ATTR));
126: }
127:
128: } //-- AttributeGroupUnmarshaller
129:
130: //-----------/
131: //- Methods -/
132: //-----------/
133:
134: /**
135: * Returns the name of the element that this ComponentReader
136: * handles
137: * @return the name of the element that this ComponentReader
138: * handles
139: **/
140: public String elementName() {
141: return SchemaNames.ATTRIBUTE_GROUP;
142: } //-- elementName
143:
144: /**
145: * Returns the AttributeGroup created by this
146: * AttributeGroupUnmarshaller
147: *
148: * @return the AttributeGroup created by this
149: * AttributeGroupUnmarshaller
150: **/
151: public AttributeGroup getAttributeGroup() {
152: return _attributeGroup;
153: } //-- getAttributeGroup
154:
155: /**
156: * Returns the Object created by this ComponentReader
157: * @return the Object created by this ComponentReader
158: **/
159: public Object getObject() {
160: return getAttributeGroup();
161: } //-- getObject
162:
163: /**
164: * Signals the start of an element with the given name.
165: *
166: * @param name the NCName of the element. It is an error
167: * if the name is a QName (ie. contains a prefix).
168: * @param namespace the namespace of the element. This may be null.
169: * Note: A null namespace is not the same as the default namespace unless
170: * the default namespace is also null.
171: * @param atts the AttributeSet containing the attributes associated
172: * with the element.
173: * @param nsDecls the namespace declarations being declared for this
174: * element. This may be null.
175: **/
176: public void startElement(String name, String namespace,
177: AttributeSet atts, Namespaces nsDecls) throws XMLException {
178: //-- Do delagation if necessary
179: if (unmarshaller != null) {
180: unmarshaller.startElement(name, namespace, atts, nsDecls);
181: ++depth;
182: return;
183: }
184:
185: //-- <anyAttribute>
186: if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
187: if (foundAnyAttribute)
188: error("an anyAttribute element can appear only once as a child "
189: + "of an 'AttributeGroup'.");
190: foundAnyAttribute = true;
191: allowAnnotation = true;
192: unmarshaller = new WildcardUnmarshaller(_attributeGroup,
193: _schema, name, atts, getResolver());
194: }
195: //-- attribute declarations
196: else if (SchemaNames.ATTRIBUTE.equals(name)) {
197: allowAnnotation = false;
198:
199: if (isRef)
200: error("AttributeGroup references may not have children.");
201:
202: unmarshaller = new AttributeUnmarshaller(_schema, atts,
203: getResolver());
204: }
205: //-- element declarations
206: else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
207: allowAnnotation = false;
208: if (isRef)
209: error("AttributeGroup references may not have children.");
210: unmarshaller = new AttributeGroupUnmarshaller(_schema, atts);
211: } else if (name.equals(SchemaNames.ANNOTATION)) {
212: if (!allowAnnotation)
213: outOfOrder(name);
214: unmarshaller = new AnnotationUnmarshaller(atts);
215: } else
216: illegalElement(name);
217:
218: unmarshaller.setDocumentLocator(getDocumentLocator());
219: } //-- startElement
220:
221: /**
222: * Signals to end of the element with the given name.
223: *
224: * @param name the NCName of the element. It is an error
225: * if the name is a QName (ie. contains a prefix).
226: * @param namespace the namespace of the element.
227: **/
228: public void endElement(String name, String namespace)
229: throws XMLException {
230:
231: //-- Do delagation if necessary
232: if ((unmarshaller != null) && (depth > 0)) {
233: unmarshaller.endElement(name, namespace);
234: --depth;
235: return;
236: }
237:
238: //-- have unmarshaller perform any necessary clean up
239: unmarshaller.finish();
240:
241: //-- <anyAttribute>
242: if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
243: Wildcard wildcard = ((WildcardUnmarshaller) unmarshaller)
244: .getWildcard();
245: try {
246: ((AttributeGroupDecl) _attributeGroup)
247: .setAnyAttribute(wildcard);
248: } catch (SchemaException e) {
249: throw new IllegalArgumentException(e.getMessage());
250: }
251: }
252:
253: //-- attribute declarations
254: else if (SchemaNames.ATTRIBUTE.equals(name)) {
255: AttributeDecl attrDecl = ((AttributeUnmarshaller) unmarshaller)
256: .getAttribute();
257:
258: ((AttributeGroupDecl) _attributeGroup)
259: .addAttribute(attrDecl);
260: }
261: //-- attribute group references
262: else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
263:
264: Object obj = unmarshaller.getObject();
265:
266: try {
267: ((AttributeGroupDecl) _attributeGroup)
268: .addReference((AttributeGroupReference) obj);
269: } catch (ClassCastException ex) {
270: String err = "AttributeGroups cannot contain new "
271: + "AttributeGroup definitions, only references to "
272: + "top-level AttributeGroups are allowed.";
273: error(err);
274: }
275: }
276: //-- annotation
277: else if (SchemaNames.ANNOTATION.equals(name)) {
278: Annotation ann = ((AnnotationUnmarshaller) unmarshaller)
279: .getAnnotation();
280: _attributeGroup.addAnnotation(ann);
281: }
282:
283: unmarshaller = null;
284: } //-- endElement
285:
286: public void characters(char[] ch, int start, int length)
287: throws XMLException {
288: //-- Do delagation if necessary
289: if (unmarshaller != null) {
290: unmarshaller.characters(ch, start, length);
291: }
292: } //-- characters
293:
294: } //-- AttributeGroupUnmarshaller
|