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-2003 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: AttributeUnmarshaller.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.Form;
053: import org.exolab.castor.xml.schema.Resolver;
054: import org.exolab.castor.xml.schema.Schema;
055: import org.exolab.castor.xml.schema.SchemaNames;
056: import org.exolab.castor.xml.schema.SimpleType;
057:
058: /**
059: * The Unmarshaller for Attribute declarations
060: *
061: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
062: * @version $Revision: 5951 $ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
063: **/
064: public class AttributeUnmarshaller extends ComponentReader {
065:
066: //--------------------/
067: //- Member Variables -/
068: //--------------------/
069:
070: /**
071: * The current ComponentReader
072: **/
073: private ComponentReader unmarshaller;
074:
075: /**
076: * The current branch depth
077: **/
078: private int depth = 0;
079:
080: /**
081: * The Attribute reference for the Attribute we are constructing
082: **/
083: private AttributeDecl _attribute = null;
084:
085: private Schema _schema = null;
086:
087: private boolean foundAnnotation = false;
088: private boolean foundSimpleType = false;
089:
090: //----------------/
091: //- Constructors -/
092: //----------------/
093:
094: public AttributeUnmarshaller(Schema schema, AttributeSet atts,
095: Resolver resolver) {
096: super ();
097: this ._schema = schema;
098:
099: setResolver(resolver);
100:
101: _attribute = new AttributeDecl(schema);
102:
103: //--@ref
104: String attValue = atts.getValue(SchemaNames.REF_ATTR);
105: if (attValue != null) {
106: _attribute.setReference(attValue);
107: }
108:
109: //-- @name
110: attValue = atts.getValue(SchemaNames.NAME_ATTR);
111: if (attValue != null) {
112: if (_attribute.isReference()) {
113: String err = "An attribute cannot have a 'name' attribute and a 'ref' attribute at the same time.";
114: throw new IllegalStateException(err);
115: }
116: _attribute.setName(attValue);
117: }
118:
119: //-- @default
120: attValue = atts.getValue(SchemaNames.DEFAULT_ATTR);
121: if (attValue != null) {
122: _attribute.setDefaultValue(attValue);
123: }
124:
125: //-- @id
126: _attribute.setId(atts.getValue(SchemaNames.ID_ATTR));
127:
128: //-- @fixed
129: attValue = atts.getValue(SchemaNames.FIXED_ATTR);
130: if (attValue != null) {
131: _attribute.setFixedValue(attValue);
132: }
133:
134: //-- @form
135: attValue = atts.getValue(SchemaNames.FORM);
136: if (attValue != null) {
137: if (_attribute.isReference()) {
138: String err = "An attribute reference cannot have a 'form' attribute.";
139: throw new IllegalArgumentException(err);
140: }
141: _attribute.setForm(Form.valueOf(attValue));
142: }
143:
144: //-- @type
145: attValue = atts.getValue(SchemaNames.TYPE_ATTR);
146: if (attValue != null) {
147: if (_attribute.isReference()) {
148: String err = "An attribute reference cannot have a 'type' attribute.";
149: throw new IllegalArgumentException(err);
150: }
151: _attribute.setSimpleTypeReference(attValue);
152: }
153:
154: //-- @use
155: attValue = atts.getValue(SchemaNames.USE_ATTR);
156: if (attValue != null) {
157: if (_attribute.isDefault()
158: && (!attValue.equals(AttributeDecl.USE_OPTIONAL)))
159: throw new IllegalArgumentException(
160: "When 'default' is present, the 'use' attribute must have the value 'optional'.");
161: _attribute.setUse(attValue);
162: }
163:
164: } //-- AttributeUnmarshaller
165:
166: //-----------/
167: //- Methods -/
168: //-----------/
169:
170: /**
171: * Returns the name of the element that this ComponentReader
172: * handles
173: * @return the name of the element that this ComponentReader
174: * handles
175: **/
176: public String elementName() {
177: return SchemaNames.ATTRIBUTE;
178: } //-- elementName
179:
180: /**
181: *
182: **/
183: public AttributeDecl getAttribute() {
184: return _attribute;
185: } //-- getAttribute
186:
187: /**
188: * Returns the Object created by this ComponentReader
189: * @return the Object created by this ComponentReader
190: **/
191: public Object getObject() {
192: return getAttribute();
193: } //-- getObject
194:
195: /**
196: * Signals the start of an element with the given name.
197: *
198: * @param name the NCName of the element. It is an error
199: * if the name is a QName (ie. contains a prefix).
200: * @param namespace the namespace of the element. This may be null.
201: * Note: A null namespace is not the same as the default namespace unless
202: * the default namespace is also null.
203: * @param atts the AttributeSet containing the attributes associated
204: * with the element.
205: * @param nsDecls the namespace declarations being declared for this
206: * element. This may be null.
207: **/
208: public void startElement(String name, String namespace,
209: AttributeSet atts, Namespaces nsDecls) throws XMLException {
210: //-- Do delagation if necessary
211: if (unmarshaller != null) {
212: unmarshaller.startElement(name, namespace, atts, nsDecls);
213: ++depth;
214: return;
215: }
216:
217: if (SchemaNames.ANNOTATION.equals(name)) {
218:
219: if (foundAnnotation)
220: error("Only one (1) annotation is allowed as a child of "
221: + "an attribute declaration.");
222:
223: if (foundSimpleType)
224: error("An annotation may only appear as the first child of "
225: + "an attribute declaration.");
226:
227: foundAnnotation = true;
228: unmarshaller = new AnnotationUnmarshaller(atts);
229: } else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
230: if (foundSimpleType)
231: error("Only one (1) simpleType is allowed as a child of "
232: + "an attribute declaration.");
233:
234: foundSimpleType = true;
235: unmarshaller = new SimpleTypeUnmarshaller(_schema, atts);
236: } else {
237: illegalElement(name);
238: }
239:
240: } //-- startElement
241:
242: /**
243: * Signals to end of the element with the given name.
244: *
245: * @param name the NCName of the element. It is an error
246: * if the name is a QName (ie. contains a prefix).
247: * @param namespace the namespace of the element.
248: **/
249: public void endElement(String name, String namespace)
250: throws XMLException {
251:
252: //-- Do delagation if necessary
253: if ((unmarshaller != null) && (depth > 0)) {
254: unmarshaller.endElement(name, namespace);
255: --depth;
256: return;
257: }
258:
259: //-- call unmarshaller finish to perform any necessary cleanup
260: unmarshaller.finish();
261:
262: if (SchemaNames.ANNOTATION.equals(name)) {
263: Annotation ann = (Annotation) unmarshaller.getObject();
264: _attribute.addAnnotation(ann);
265: } else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
266: SimpleType simpleType = ((SimpleTypeUnmarshaller) unmarshaller)
267: .getSimpleType();
268: _attribute.setSimpleType(simpleType);
269: }
270:
271: unmarshaller = null;
272:
273: } //-- endElement
274:
275: public void characters(char[] ch, int start, int length)
276: throws XMLException {
277: //-- Do delagation if necessary
278: if (unmarshaller != null) {
279: unmarshaller.characters(ch, start, length);
280: }
281: } //-- characters
282:
283: } //-- AttributeUnmarshaller
|