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: SimpleContentUnmarshaller.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.ComplexType;
052: import org.exolab.castor.xml.schema.Resolver;
053: import org.exolab.castor.xml.schema.SchemaNames;
054:
055: /**
056: * A class for Unmarshalling simpleContent
057: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
058: * @version $Revision: 5951 $ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
059: **/
060: public class SimpleContentUnmarshaller 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 Attribute reference for the Attribute we are constructing
078: **/
079: private ComplexType _complexType = null;
080:
081: private boolean foundAnnotation = false;
082: private boolean foundExtension = false;
083: private boolean foundRestriction = false;
084:
085: //----------------/
086: //- Constructors -/
087: //----------------/
088:
089: /**
090: * Creates a new SimpleContentUnmarshaller
091: * @param complexType the complexType we are unmarshalling
092: * @param atts the AttributeList
093: * @param resolver the resolver being used for reference resolving
094: **/
095: public SimpleContentUnmarshaller(ComplexType complexType,
096: AttributeSet atts, Resolver resolver) throws XMLException {
097:
098: _complexType = complexType;
099: setResolver(resolver);
100:
101: } //-- SimpleContentUnmarshaller
102:
103: //-----------/
104: //- Methods -/
105: //-----------/
106:
107: /**
108: * Returns the name of the element that this ComponentReader
109: * handles
110: * @return the name of the element that this ComponentReader
111: * handles
112: **/
113: public String elementName() {
114: return SchemaNames.SIMPLE_CONTENT;
115: } //-- elementName
116:
117: /**
118: * Returns the Object created by this ComponentReader
119: * @return the Object created by this ComponentReader
120: **/
121: public Object getObject() {
122: return null;
123: } //-- getObject
124:
125: /**
126: * Signals the start of an element with the given name.
127: *
128: * @param name the NCName of the element. It is an error
129: * if the name is a QName (ie. contains a prefix).
130: * @param namespace the namespace of the element. This may be null.
131: * Note: A null namespace is not the same as the default namespace unless
132: * the default namespace is also null.
133: * @param atts the AttributeSet containing the attributes associated
134: * with the element.
135: * @param nsDecls the namespace declarations being declared for this
136: * element. This may be null.
137: **/
138: public void startElement(String name, String namespace,
139: AttributeSet atts, Namespaces nsDecls) throws XMLException {
140: //-- Do delagation if necessary
141: if (unmarshaller != null) {
142: unmarshaller.startElement(name, namespace, atts, nsDecls);
143: ++depth;
144: return;
145: }
146:
147: //-- extension
148: if (SchemaNames.EXTENSION.equals(name)) {
149:
150: if (foundExtension)
151: error("Only (1) 'extension' element may appear as a child "
152: + "of 'simpleContent' elements.");
153:
154: if (foundRestriction)
155: error("Both 'extension' and 'restriction' elements may not "
156: + "appear as children of the same simpleContent "
157: + "definition.");
158:
159: foundExtension = true;
160: unmarshaller = new ExtensionUnmarshaller(_complexType,
161: atts, getResolver());
162: }
163: //-- restriction
164: else if (SchemaNames.RESTRICTION.equals(name)) {
165:
166: if (foundRestriction)
167: error("Only (1) 'restriction' element may appear as a child "
168: + "of 'simpleContent' elements.");
169:
170: if (foundExtension)
171: error("Both 'extension' and 'restriction' elements may not "
172: + "appear as children of the same simpleContent "
173: + "definition.");
174:
175: foundRestriction = true;
176:
177: unmarshaller = new SimpleContentRestrictionUnmarshaller(
178: _complexType, atts, getResolver());
179: }
180: //-- annotation
181: else if (name.equals(SchemaNames.ANNOTATION)) {
182: if (foundAnnotation)
183: error("Only (1) 'annotation' element may appear as a child "
184: + "of 'simpleContent' elements.");
185:
186: if (foundRestriction || foundExtension)
187: error("An 'annotation' may only appear as the first child "
188: + "of a 'simpleContent' element.");
189:
190: foundAnnotation = true;
191: unmarshaller = new AnnotationUnmarshaller(atts);
192: } else
193: illegalElement(name);
194:
195: unmarshaller.setDocumentLocator(getDocumentLocator());
196: } //-- startElement
197:
198: /**
199: * Signals to end of the element with the given name.
200: *
201: * @param name the NCName of the element. It is an error
202: * if the name is a QName (ie. contains a prefix).
203: * @param namespace the namespace of the element.
204: **/
205: public void endElement(String name, String namespace)
206: throws XMLException {
207:
208: //-- Do delagation if necessary
209: if ((unmarshaller != null) && (depth > 0)) {
210: unmarshaller.endElement(name, namespace);
211: --depth;
212: return;
213: }
214:
215: //-- have unmarshaller perform any necessary clean up
216: unmarshaller.finish();
217:
218: //-- annotation
219: if (SchemaNames.ANNOTATION.equals(name)) {
220: Annotation ann = ((AnnotationUnmarshaller) unmarshaller)
221: .getAnnotation();
222: _complexType.addAnnotation(ann);
223: }
224:
225: unmarshaller = null;
226: } //-- endElement
227:
228: public void characters(char[] ch, int start, int length)
229: throws XMLException {
230: //-- Do delagation if necessary
231: if (unmarshaller != null) {
232: unmarshaller.characters(ch, start, length);
233: }
234: } //-- characters
235:
236: } //-- SimpleContentUnmarshaller
|