1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
65
66
67
68
69
70
71 public class ComplexContentRestrictionUnmarshaller extends ComponentReader {
72
73
74
75
76
77
78
79
80
81 private ComponentReader unmarshaller;
82
83
84
85
86 private int depth = 0;
87
88
89
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
101
102
103
104
105
106
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
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 }
154 _complexType.setBase(base);
155 _complexType.setBaseType(baseType);
156
157 }
158
159
160 }
161
162
163
164
165
166
167
168
169
170
171
172 public String elementName() {
173 return SchemaNames.RESTRICTION;
174 }
175
176
177
178
179
180 public Object getObject() {
181 return null;
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public void startElement(String name, String namespace, AttributeSet atts,
198 Namespaces nsDecls)
199 throws XMLException
200 {
201
202 if (unmarshaller != null) {
203 unmarshaller.startElement(name, namespace, atts, nsDecls);
204 ++depth;
205 return;
206 }
207
208
209
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
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
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
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 }
264
265
266
267
268
269
270
271
272 public void endElement(String name, String namespace)
273 throws XMLException
274 {
275
276
277 if ((unmarshaller != null) && (depth > 0)) {
278 unmarshaller.endElement(name, namespace);
279 --depth;
280 return;
281 }
282
283
284 unmarshaller.finish();
285
286
287 if (SchemaNames.ANNOTATION.equals(name)) {
288 Annotation ann = ((AnnotationUnmarshaller)unmarshaller).getAnnotation();
289 _complexType.addAnnotation(ann);
290 }
291
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
302 else if (SchemaNames.ATTRIBUTE.equals(name)) {
303 AttributeDecl attrDecl =
304 ((AttributeUnmarshaller)unmarshaller).getAttribute();
305
306
307 _complexType.addAttributeDecl(attrDecl);
308 }
309
310 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
311 AttributeGroupReference attrGroupRef =
312 (AttributeGroupReference) unmarshaller.getObject();
313 _complexType.addAttributeGroupReference(attrGroupRef);
314 }
315
316 else if (SchemaNames.isGroupName(name)) {
317 Group group = ((GroupUnmarshaller)unmarshaller).getGroup();
318 _complexType.addGroup(group);
319 }
320 unmarshaller = null;
321 }
322
323 public void characters(char[] ch, int start, int length)
324 throws XMLException
325 {
326
327 if (unmarshaller != null) {
328 unmarshaller.characters(ch, start, length);
329 }
330 }
331 }