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: SimpleTypeUnmarshaller.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.Schema;
052: import org.exolab.castor.xml.schema.SchemaNames;
053: import org.exolab.castor.xml.schema.SimpleType;
054:
055: /**
056: * A class for Unmarshalling SimpleTypes
057: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
058: * @version $Revision: 6230 $ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
059: **/
060: public class SimpleTypeUnmarshaller extends ComponentReader {
061:
062: //--------------------/
063: //- Member Variables -/
064: //--------------------/
065:
066: /**
067: * The current ComponentReader
068: **/
069: private ComponentReader unmarshaller;
070:
071: /**
072: * The current branch depth
073: **/
074: private int depth = 0;
075:
076: /**
077: * The SimpleTypeDefinition we are unmarshalling
078: **/
079: private SimpleTypeDefinition _simpleTypeDef = null;
080:
081: /**
082: * A reference to the SimpleType we are unmarshalling
083: **/
084: private SimpleType _simpleType = null;
085:
086: private boolean foundAnnotation = false;
087: private boolean foundList = false;
088: private boolean foundRestriction = false;
089: private boolean foundUnion = false;
090:
091: //----------------/
092: //- Constructors -/
093: //----------------/
094:
095: /**
096: * Creates a new SimpleTypeUnmarshaller
097: * @param schema the Schema to which the SimpleType belongs
098: * @param atts the AttributeList
099: **/
100: public SimpleTypeUnmarshaller(Schema schema, AttributeSet atts)
101: throws XMLException {
102: super ();
103:
104: String name = atts.getValue(SchemaNames.NAME_ATTR);
105:
106: //-- strip off prefix if necessary
107: if (name != null) {
108: int idx = name.indexOf(':');
109: if (idx >= 0) {
110: String prefix = name.substring(0, idx);
111: String ns = schema.getNamespace(prefix);
112: if (ns == null) {
113: //-- should report an error here
114: } else {
115: if (ns.equals(schema.getTargetNamespace())) {
116: name = name.substring(idx + 1);
117: } else {
118: //-- should report an error here
119: }
120: }
121: }
122: }
123:
124: String id = atts.getValue(SchemaNames.ID_ATTR);
125: _simpleTypeDef = new SimpleTypeDefinition(schema, name, id);
126:
127: //-- @final
128: _simpleTypeDef.setFinal(atts.getValue(SchemaNames.FINAL_ATTR));
129:
130: } //-- SimpleTypeUnmarshaller
131:
132: //-----------/
133: //- Methods -/
134: //-----------/
135:
136: /**
137: * Returns the name of the element that this ComponentReader
138: * handles
139: * @return the name of the element that this ComponentReader
140: * handles
141: **/
142: public String elementName() {
143: return SchemaNames.SIMPLE_TYPE;
144: } //-- elementName
145:
146: /**
147: * Returns the SimpleType created
148: * @return the SimpleType created
149: **/
150: public SimpleType getSimpleType() {
151: if (_simpleType == null) {
152: _simpleType = _simpleTypeDef.createSimpleType();
153: }
154: return _simpleType;
155: } //-- getSimpletype
156:
157: /**
158: * Returns the Object created by this ComponentReader
159: * @return the Object created by this ComponentReader
160: **/
161: public Object getObject() {
162: return getSimpleType();
163: } //-- getObject
164:
165: public void finish() throws XMLException {
166: if (!(foundList || foundUnion || foundRestriction))
167: error("Invalid 'simpleType'; missing 'restriction' "
168: + "| 'union' | 'list'.");
169:
170: } //-- finish
171:
172: /**
173: * Signals the start of an element with the given name.
174: *
175: * @param name the NCName of the element. It is an error
176: * if the name is a QName (ie. contains a prefix).
177: * @param namespace the namespace of the element. This may be null.
178: * Note: A null namespace is not the same as the default namespace unless
179: * the default namespace is also null.
180: * @param atts the AttributeSet containing the attributes associated
181: * with the element.
182: * @param nsDecls the namespace declarations being declared for this
183: * element. This may be null.
184: **/
185: public void startElement(String name, String namespace,
186: AttributeSet atts, Namespaces nsDecls) throws XMLException {
187: //-- Do delagation if necessary
188: if (unmarshaller != null) {
189: unmarshaller.startElement(name, namespace, atts, nsDecls);
190: ++depth;
191: return;
192: }
193:
194: if (SchemaNames.ANNOTATION.equals(name)) {
195:
196: if (foundAnnotation)
197: error("Only one (1) annotation may appear as a child of "
198: + "'simpleType'.");
199:
200: if (foundList || foundUnion || foundRestriction)
201: error("An annotation may only appear as the first child "
202: + "of 'simpleType'.");
203:
204: foundAnnotation = true;
205: unmarshaller = new AnnotationUnmarshaller(atts);
206: } else if (SchemaNames.RESTRICTION.equals(name)) {
207:
208: if (foundList)
209: error("A 'simpleType' cannot have both a 'list' and a "
210: + "'restriction' in the same definition.");
211:
212: if (foundUnion)
213: error("A 'simpleType' cannot have both a 'union' and a "
214: + "'restriction' in the same definition.");
215:
216: foundRestriction = true;
217:
218: unmarshaller = new SimpleTypeRestrictionUnmarshaller(
219: _simpleTypeDef, atts);
220: } else if (SchemaNames.LIST.equals(name)) {
221: foundList = true;
222: Schema schema = _simpleTypeDef.getSchema();
223: unmarshaller = new SimpleTypeListUnmarshaller(schema, atts);
224: } else if (SchemaNames.UNION.equals(name)) {
225: foundUnion = true;
226: Schema schema = _simpleTypeDef.getSchema();
227: unmarshaller = new UnionUnmarshaller(schema, atts);
228: } else
229: illegalElement(name);
230:
231: } //-- startElement
232:
233: /**
234: * Signals to end of the element with the given name.
235: *
236: * @param name the NCName of the element. It is an error
237: * if the name is a QName (ie. contains a prefix).
238: * @param namespace the namespace of the element.
239: **/
240: public void endElement(String name, String namespace)
241: throws XMLException {
242:
243: //-- Do delagation if necessary
244: if ((unmarshaller != null) && (depth > 0)) {
245: unmarshaller.endElement(name, namespace);
246: --depth;
247: return;
248: }
249:
250: //-- have unmarshaller perform any necessary clean up
251: unmarshaller.finish();
252:
253: if (SchemaNames.ANNOTATION.equals(name)) {
254: Annotation annotation = (Annotation) unmarshaller
255: .getObject();
256: _simpleTypeDef.setAnnotation(annotation);
257: } else if (SchemaNames.LIST.equals(name)) {
258: _simpleType = (SimpleType) unmarshaller.getObject();
259: _simpleTypeDef.copyInto(_simpleType);
260:
261: } else if (SchemaNames.UNION.equals(name)) {
262: _simpleType = (SimpleType) unmarshaller.getObject();
263: _simpleTypeDef.copyInto(_simpleType);
264:
265: }
266:
267: unmarshaller = null;
268: } //-- endElement
269:
270: public void characters(char[] ch, int start, int length)
271: throws XMLException {
272: //-- Do delagation if necessary
273: if (unmarshaller != null) {
274: unmarshaller.characters(ch, start, length);
275: }
276: } //-- characters
277:
278: } //-- SimpleTypeUnmarshaller
|