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: ModelGroupUnmarshaller.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.Group;
052: import org.exolab.castor.xml.schema.ModelGroup;
053: import org.exolab.castor.xml.schema.Resolver;
054: import org.exolab.castor.xml.schema.Schema;
055: import org.exolab.castor.xml.schema.SchemaException;
056: import org.exolab.castor.xml.schema.SchemaNames;
057:
058: /**
059: * A class for Unmarshalling ModelGroups Definition
060: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
061: * @version $Revisio:$ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
062: **/
063: public class ModelGroupUnmarshaller extends ComponentReader {
064:
065: //--------------------------/
066: //- Static Class Variables -/
067: //--------------------------/
068:
069: /**
070: * The value of the maximum occurance wild card
071: */
072: private static final String MAX_OCCURS_WILDCARD = "unbounded";
073:
074: //--------------------/
075: //- Member Variables -/
076: //--------------------/
077:
078: /**
079: * The current ComponentReader
080: **/
081: private ComponentReader unmarshaller;
082:
083: /**
084: * The current branch depth
085: **/
086: private int depth = 0;
087:
088: /**
089: * The ModelGroup reference for the ModelGroup we are constructing
090: **/
091: private ModelGroup _group = null;
092:
093: /**
094: * The Schema being "unmarshalled"
095: **/
096: private Schema _schema = null;
097:
098: /**
099: * Flag to indicate if we have already encounter an {@literal <annotation>}
100: */
101: private boolean foundAnnotation = false;
102:
103: //----------------/
104: //- Constructors -/
105: //----------------/
106:
107: /**
108: * Creates a new ModelGroupUnmarshaller
109: * @param schema the Schema to which the ModelGroup belongs
110: * @param atts the AttributeList
111: * @param resolver the resolver being used for reference resolving
112: **/
113: public ModelGroupUnmarshaller(Schema schema, AttributeSet atts,
114: Resolver resolver) {
115: super ();
116: setResolver(resolver);
117: this ._schema = schema;
118:
119: _group = new ModelGroup(_schema);
120:
121: //-- handle attributes
122: String attValue = null;
123:
124: //-- set name
125: _group.setName(atts.getValue("name"));
126:
127: /*
128: * @maxOccurs
129: * If maxOccurs is present, the value is either unbounded
130: * or the int value of the attribute, otherwise maxOccurs
131: * equals the minOccurs value.
132: */
133: attValue = atts.getValue(SchemaNames.MAX_OCCURS_ATTR);
134: if (attValue != null) {
135: if (_group.getName() != null)
136: throw new IllegalArgumentException(
137: "In <group>: "
138: + _group.getName()
139: + "'maxOccurs' cannot appear in a named <group>");
140: if (MAX_OCCURS_WILDCARD.equals(attValue))
141: attValue = "-1";
142: int maxOccurs = toInt(attValue);
143: _group.setMaxOccurs(maxOccurs);
144: }
145: //-- minOccurs
146: attValue = atts.getValue("minOccurs");
147: if (attValue != null) {
148: if (_group.getName() != null)
149: throw new IllegalArgumentException(
150: "In <group>: "
151: + _group.getName()
152: + ", 'minOccurs' cannot appear in a named <group>");
153: _group.setMinOccurs(toInt(attValue));
154: }
155:
156: //-- @ref
157: attValue = atts.getValue("ref");
158: if (attValue != null) {
159: if (_group.getName() != null)
160: throw new IllegalArgumentException("In <group>: "
161: + _group.getName()
162: + ", 'ref' cannot appear in a named <group>");
163: // String name = attValue;
164: // int idx = name.indexOf(':');
165: // String ns = null;
166: // if (idx >= 0) {
167: // String nsPrefix = name.substring(0,idx);
168: // name = name.substring(idx + 1);
169: // ns = (String) schema.getNamespace(nsPrefix);
170: // if (ns == null) {
171: // String err = "in the <group> referring: "+attValue;
172: // err += " The Namespace prefix is not recognized '"+nsPrefix+"'";
173: // throw new IllegalArgumentException(err);
174: // }
175: // }
176: //
177: // if (ns != null) {
178: // Schema tempSchema = schema.getImportedSchema(ns);
179: // if (tempSchema!=null) {
180: // _group.setSchema(tempSchema);
181: // }
182: // tempSchema = null;
183: // }
184:
185: _group.setReference(attValue);
186: }
187: //-- id
188: attValue = atts.getValue("id");
189: _group.setId(attValue);
190: //-- not yet supported
191:
192: } //-- ModelGroupUnmarshaller
193:
194: //-----------/
195: //- Methods -/
196: //-----------/
197:
198: /**
199: * Returns the Group that was unmarshalled by this Unmarshaller.
200: * This method should only be called after unmarshalling
201: * has been completed.
202: *
203: * @return the unmarshalled Group
204: **/
205: public ModelGroup getGroup() {
206: return _group;
207: } //-- getGroup
208:
209: /**
210: * Returns the Object created by this ComponentReader
211: * @return the Object created by this ComponentReader
212: **/
213: public Object getObject() {
214: return getGroup();
215: } //-- getObject
216:
217: /**
218: * Sets the name of the element that this UnknownUnmarshaller handles
219: **/
220: public String elementName() {
221: return SchemaNames.GROUP;
222: } //-- elementName
223:
224: /**
225: * Signals the start of an element with the given name.
226: *
227: * @param name the NCName of the element. It is an error
228: * if the name is a QName (ie. contains a prefix).
229: * @param namespace the namespace of the element. This may be null.
230: * Note: A null namespace is not the same as the default namespace unless
231: * the default namespace is also null.
232: * @param atts the AttributeSet containing the attributes associated
233: * with the element.
234: * @param nsDecls the namespace declarations being declared for this
235: * element. This may be null.
236: **/
237: public void startElement(String name, String namespace,
238: AttributeSet atts, Namespaces nsDecls) throws XMLException {
239: //-- Do delagation if necessary
240: if (unmarshaller != null) {
241: unmarshaller.startElement(name, namespace, atts, nsDecls);
242: ++depth;
243: return;
244: }
245:
246: if (SchemaNames.ANNOTATION.equals(name)) {
247: if (foundAnnotation)
248: error("Only one (1) 'annotation' is allowed as a child of "
249: + "element definitions.");
250:
251: foundAnnotation = true;
252: unmarshaller = new AnnotationUnmarshaller(atts);
253: }
254:
255: else if (SchemaNames.isGroupName(name)) {
256: unmarshaller = new GroupUnmarshaller(_schema, name, atts,
257: getResolver());
258: } else {
259: StringBuffer err = new StringBuffer("illegal element <");
260: err.append(name);
261: err.append("> found in <group>.");
262: throw new SchemaException(err.toString());
263: }
264:
265: } //-- startElement
266:
267: /**
268: * Signals to end of the element with the given name.
269: *
270: * @param name the NCName of the element. It is an error
271: * if the name is a QName (ie. contains a prefix).
272: * @param namespace the namespace of the element.
273: **/
274: public void endElement(String name, String namespace)
275: throws XMLException {
276:
277: //-- Do delagation if necessary
278: if ((unmarshaller != null) && (depth > 0)) {
279: unmarshaller.endElement(name, namespace);
280: --depth;
281: return;
282: }
283:
284: //-- check for name mismatches
285: if (unmarshaller != null) {
286: if (!name.equals(unmarshaller.elementName())) {
287: String err = "missing end element for ";
288: err += unmarshaller.elementName();
289: throw new SchemaException(err);
290: }
291: }
292:
293: if (SchemaNames.ANNOTATION.equals(name)) {
294: Annotation ann = (Annotation) unmarshaller.getObject();
295: _group.addAnnotation(ann);
296: }
297:
298: else if (SchemaNames.isGroupName(name)) {
299: Group group = ((GroupUnmarshaller) unmarshaller).getGroup();
300: _group.addGroup(group);
301: }
302:
303: //-- have unmarshaller perform any necessary clean up
304: unmarshaller.finish();
305: unmarshaller = null;
306: } //-- endElement
307:
308: public void characters(char[] ch, int start, int length)
309: throws XMLException {
310: //-- Do delagation if necessary
311: if (unmarshaller != null) {
312: unmarshaller.characters(ch, start, length);
313: }
314: } //-- characters
315:
316: } //-- ModelGroupUnmarshaller
|