1 /**
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2000-2002 (C) Intalio Inc. All Rights Reserved.
42 *
43 * $Id$
44 */
45
46 package org.exolab.castor.xml.schema.reader;
47
48 import org.exolab.castor.xml.AttributeSet;
49 import org.exolab.castor.xml.Namespaces;
50 import org.exolab.castor.xml.XMLException;
51 import org.exolab.castor.xml.schema.Annotation;
52 import org.exolab.castor.xml.schema.AttributeDecl;
53 import org.exolab.castor.xml.schema.AttributeGroupReference;
54 import org.exolab.castor.xml.schema.ComplexType;
55 import org.exolab.castor.xml.schema.SchemaContext;
56 import org.exolab.castor.xml.schema.Group;
57 import org.exolab.castor.xml.schema.Schema;
58 import org.exolab.castor.xml.schema.SchemaException;
59 import org.exolab.castor.xml.schema.SchemaNames;
60 import org.exolab.castor.xml.schema.Wildcard;
61 import org.exolab.castor.xml.schema.XMLType;
62
63 /**
64 * A class for unmarshalling restriction elements of a complexContent
65 * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
66 * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
67 * TODO: validation stuff about the data
68 * TODO: set the flag of restriction for the complexType
69
70 **/
71 public class ComplexContentRestrictionUnmarshaller extends ComponentReader {
72
73
74 //--------------------/
75 //- Member Variables -/
76 //--------------------/
77
78 /**
79 * The current ComponentReader
80 **/
81 private ComponentReader unmarshaller;
82
83 /**
84 * The current branch depth
85 **/
86 private int depth = 0;
87
88 /**
89 * The complexType we are unmarshalling
90 **/
91 private ComplexType _complexType = null;
92
93 private Schema _schema = null;
94 private boolean foundAnnotation = false;
95 private boolean foundAttribute = false;
96 private boolean foundAttributeGroup = false;
97 private boolean foundModelGroup = false;
98
99 //----------------/
100 //- Constructors -/
101 //----------------/
102 /**
103 * Creates a new RestrictionUnmarshaller.
104 * @param schemaContext the schema context to get some configuration settings from
105 * @param complexType the complexType being unmarshalled
106 * @param atts the AttributeList
107 */
108 public ComplexContentRestrictionUnmarshaller(
109 final SchemaContext schemaContext,
110 final ComplexType complexType,
111 final AttributeSet atts)
112 throws XMLException {
113 super(schemaContext);
114 _complexType = complexType;
115 _schema = complexType.getSchema();
116
117 _complexType.setDerivationMethod(SchemaNames.RESTRICTION);
118 _complexType.setRestriction(true);
119
120 //-- base
121 String base = atts.getValue(SchemaNames.BASE_ATTR);
122 if ((base != null) && (base.length() > 0)) {
123
124 XMLType baseType= _schema.getType(base);
125 if (baseType == null)
126 _complexType.setBase(base);
127 // the base must not be a simpleType
128 else if (baseType.isSimpleType()) {
129 String err ="complexType: "+(_complexType.getName()) != null?
130 _complexType.getName():"\n";
131 err += "A complex type cannot be a restriction"+
132 " of a simpleType.";
133 throw new IllegalStateException(err);
134 }
135 else if (!baseType.isAnyType()) {
136 // We are now sure the base is a complexType
137 // but is it already a restriction? (see PR 5.11->restriction->1.1)
138
139 //-- KV 2004-03-15
140 //-- Need to valididate this constraint...I couldn't
141 //-- find it in the XML Schema 1.0 Recommendation,
142 //-- commenting out for now.
143 /*
144 if (((ComplexType)baseType).isRestricted()) {
145 String err="complexType: "+(_complexType.getName()) != null?
146 _complexType.getName():"\n";
147 err +="A complex type cannot be a restriction"+
148 " of a restriction.";
149 throw new IllegalStateException(err);
150 }
151
152 */
153 }
154 _complexType.setBase(base);
155 _complexType.setBaseType(baseType);
156
157 }
158
159
160 } //-- ComplexContentRestrictionUnmarshaller
161
162 //-----------/
163 //- Methods -/
164 //-----------/
165
166 /**
167 * Returns the name of the element that this ComponentReader
168 * handles
169 * @return the name of the element that this ComponentReader
170 * handles
171 **/
172 public String elementName() {
173 return SchemaNames.RESTRICTION;
174 } //-- elementName
175
176 /**
177 * Returns the Object created by this ComponentReader
178 * @return the Object created by this ComponentReader
179 **/
180 public Object getObject() {
181 return null;
182 } //-- getObject
183
184 /**
185 * Signals the start of an element with the given name.
186 *
187 * @param name the NCName of the element. It is an error
188 * if the name is a QName (ie. contains a prefix).
189 * @param namespace the namespace of the element. This may be null.
190 * Note: A null namespace is not the same as the default namespace unless
191 * the default namespace is also null.
192 * @param atts the AttributeSet containing the attributes associated
193 * with the element.
194 * @param nsDecls the namespace declarations being declared for this
195 * element. This may be null.
196 **/
197 public void startElement(String name, String namespace, AttributeSet atts,
198 Namespaces nsDecls)
199 throws XMLException
200 {
201 //-- Do delagation if necessary
202 if (unmarshaller != null) {
203 unmarshaller.startElement(name, namespace, atts, nsDecls);
204 ++depth;
205 return;
206 }
207
208
209 //-- annotation
210 if (name.equals(SchemaNames.ANNOTATION)) {
211
212 if (foundModelGroup || foundAttribute || foundAttributeGroup)
213 error("An annotation must appear as the first child " +
214 "of 'restriction' elements.");
215
216 if (foundAnnotation)
217 error("Only one (1) annotation may appear as a child of "+
218 "'restriction' elements.");
219
220 foundAnnotation = true;
221 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
222 }
223
224 //-- ModelGroup declarations (choice, all, sequence, group)
225 else if (SchemaNames.isGroupName(name)) {
226
227 if (foundAttribute || foundAttributeGroup)
228 error("'" + name + "' must appear before any attribute "+
229 "definitions when a child of 'restriction'.");
230 if (foundModelGroup)
231 error("'"+name+"' cannot appear as a child of 'restriction' "+
232 "if another 'all', 'sequence', 'choice' or "+
233 "'group' also exists.");
234
235 foundModelGroup = true;
236 unmarshaller
237 = new GroupUnmarshaller(getSchemaContext(), _schema, name, atts);
238 }
239
240 else if (SchemaNames.ATTRIBUTE.equals(name)) {
241 foundAttribute = true;
242 unmarshaller = new AttributeUnmarshaller(getSchemaContext(), _schema,atts);
243 }
244
245 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
246
247 //--In a complexType we only reference attribute group
248 if (atts.getValue(SchemaNames.REF_ATTR) == null) {
249 error("A 'complexType' may contain referring "+
250 "attributeGroups, but not defining ones.");
251 }
252 foundAttributeGroup = true;
253 unmarshaller = new AttributeGroupUnmarshaller(getSchemaContext(), _schema,atts);
254 }
255 //-- <anyAttribute>
256 else if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
257 unmarshaller
258 = new WildcardUnmarshaller(getSchemaContext(), _complexType, _schema, name, atts);
259 }
260
261 else illegalElement(name);
262
263 } //-- startElement
264
265 /**
266 * Signals to end of the element with the given name.
267 *
268 * @param name the NCName of the element. It is an error
269 * if the name is a QName (ie. contains a prefix).
270 * @param namespace the namespace of the element.
271 **/
272 public void endElement(String name, String namespace)
273 throws XMLException
274 {
275
276 //-- Do delagation if necessary
277 if ((unmarshaller != null) && (depth > 0)) {
278 unmarshaller.endElement(name, namespace);
279 --depth;
280 return;
281 }
282
283 //-- have unmarshaller perform any necessary clean up
284 unmarshaller.finish();
285
286 //-- annotation
287 if (SchemaNames.ANNOTATION.equals(name)) {
288 Annotation ann = ((AnnotationUnmarshaller)unmarshaller).getAnnotation();
289 _complexType.addAnnotation(ann);
290 }
291 //-- <anyAttribute>
292 else if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
293 Wildcard wildcard =
294 ((WildcardUnmarshaller)unmarshaller).getWildcard();
295 try {
296 _complexType.setAnyAttribute(wildcard);
297 } catch (SchemaException e) {
298 throw new IllegalArgumentException(e.getMessage());
299 }
300 }
301 //-- attribute
302 else if (SchemaNames.ATTRIBUTE.equals(name)) {
303 AttributeDecl attrDecl =
304 ((AttributeUnmarshaller)unmarshaller).getAttribute();
305
306 /* TODO: do the validation later*/
307 _complexType.addAttributeDecl(attrDecl);
308 }
309 //-- attribute groups
310 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
311 AttributeGroupReference attrGroupRef =
312 (AttributeGroupReference) unmarshaller.getObject();
313 _complexType.addAttributeGroupReference(attrGroupRef);
314 }
315 //-- group declarations (all, choice, group, sequence)
316 else if (SchemaNames.isGroupName(name)) {
317 Group group = ((GroupUnmarshaller)unmarshaller).getGroup();
318 _complexType.addGroup(group);
319 }
320 unmarshaller = null;
321 } //-- endElement
322
323 public void characters(char[] ch, int start, int length)
324 throws XMLException
325 {
326 //-- Do delagation if necessary
327 if (unmarshaller != null) {
328 unmarshaller.characters(ch, start, length);
329 }
330 } //-- characters
331 } //-- ComplexContentRestrictionUnmarshaller