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.ContentType;
56 import org.exolab.castor.xml.schema.Facet;
57 import org.exolab.castor.xml.schema.Schema;
58 import org.exolab.castor.xml.schema.SchemaContext;
59 import org.exolab.castor.xml.schema.SchemaException;
60 import org.exolab.castor.xml.schema.SchemaNames;
61 import org.exolab.castor.xml.schema.SimpleContent;
62 import org.exolab.castor.xml.schema.SimpleType;
63 import org.exolab.castor.xml.schema.Wildcard;
64 import org.exolab.castor.xml.schema.XMLType;
65
66
67
68
69
70
71
72 public class SimpleContentRestrictionUnmarshaller extends ComponentReader {
73
74
75
76
77
78
79
80
81
82 private ComponentReader unmarshaller;
83
84
85
86
87 private int depth = 0;
88
89
90
91
92 private ComplexType _complexType = null;
93
94 private Schema _schema = null;
95 private String _id = null;
96 private boolean foundAnnotation = false;
97 private boolean foundSimpleType = false;
98 private boolean foundFacets = false;
99 private boolean foundAttribute = false;
100 private boolean foundAttributeGroup = false;
101
102
103
104
105 private SimpleTypeDefinition _simpleTypeDef = null;
106
107
108
109
110
111
112
113
114
115
116 public SimpleContentRestrictionUnmarshaller(
117 final SchemaContext schemaContext,
118 final ComplexType complexType,
119 final AttributeSet atts) {
120
121 super(schemaContext);
122
123 _complexType = complexType;
124 _complexType.setDerivationMethod(SchemaNames.RESTRICTION);
125 _complexType.setRestriction(true);
126 _schema = complexType.getSchema();
127 _id = atts.getValue(SchemaNames.ID_ATTR);
128
129
130 String base = atts.getValue(SchemaNames.BASE_ATTR);
131
132 if ((base != null) && (base.length() > 0)) {
133
134 XMLType baseType= _schema.getType(base);
135 if (baseType == null) {
136 _complexType.setBase(base);
137 _complexType.setContentType(new SimpleContent(_schema, base));
138 }
139 else if (baseType.isSimpleType()) {
140 String err ="complexType: ";
141 String name = _complexType.getName();
142 if (name != null) {
143 err += name;
144 } else {
145 err += "#anonymous-complexType#";
146 }
147
148 err += "A complex type cannot be a restriction"+
149 " of a simpleType:";
150 err += baseType.getName();
151 throw new IllegalStateException(err);
152 }
153
154
155 else {
156 ComplexType temp = (ComplexType) baseType;
157
158 if ( ! temp.isSimpleContent() ) {
159
160
161
162
163 if ( (temp.getContentType().getType() == ContentType.MIXED) && temp.isEmptiable() ) {
164
165 }
166 else {
167 String err ="complexType: ";
168 String name = _complexType.getName();
169 if (name != null) {
170 err += name;
171 } else {
172 err += "#anonymous-complexType#";
173 }
174
175 err += ": In a simpleContent when using restriction the base type"+
176 " must be a complexType with a simple content model or it must" +
177 " be a complex content model which is mixed and emptiable.";
178 throw new IllegalStateException(err);
179 }
180 }
181 else {
182
183
184
185 SimpleContent contentType = (SimpleContent)temp.getContentType();
186 _complexType.setBaseType(temp);
187 _complexType.setBase(temp.getName());
188 _simpleTypeDef = new SimpleTypeDefinition(_schema, temp.getName(),_id);
189 SimpleType simpleType = contentType.getSimpleType();
190 if (simpleType != null) {
191 _simpleTypeDef.setBaseType(simpleType);
192 }
193 else {
194 _simpleTypeDef.setBaseTypeName(contentType.getTypeName());
195 }
196 }
197 }
198 }
199
200
201 }
202
203
204
205
206
207
208
209
210
211
212
213 public String elementName() {
214 return SchemaNames.RESTRICTION;
215 }
216
217
218
219
220
221 public Object getObject() {
222 return null;
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 public void startElement(String name, String namespace, AttributeSet atts,
239 Namespaces nsDecls)
240 throws XMLException
241 {
242
243 if (unmarshaller != null) {
244 unmarshaller.startElement(name, namespace, atts, nsDecls);
245 ++depth;
246 return;
247 }
248
249
250
251 if (name.equals(SchemaNames.ANNOTATION)) {
252
253 if (foundFacets || foundSimpleType ||
254 foundAttribute || foundAttributeGroup)
255 error("An annotation must appear as the first child " +
256 "of 'restriction' elements.");
257
258 if (foundAnnotation)
259 error("Only one (1) annotation may appear as a child of "+
260 "'restriction' elements.");
261
262 foundAnnotation = true;
263 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
264 }
265
266 else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
267 if (foundSimpleType)
268 error("Only one (1) 'simpleType' may appear as a child of "+
269 "'restriction' elements.");
270
271 if (foundFacets)
272 error("A 'simpleType', as a child of 'restriction' "+
273 "elements, must appear before any facets.");
274
275 if (foundAttribute || foundAttributeGroup)
276 error("A 'simpleType', as a child of 'restriction' "+
277 "elements, must appear before any attribute elements.");
278
279 foundSimpleType = true;
280 unmarshaller = new SimpleTypeUnmarshaller(getSchemaContext(), _schema, atts);
281
282 }
283 else if (FacetUnmarshaller.isFacet(name)) {
284 foundFacets = true;
285 if (foundAttribute || foundAttributeGroup)
286 error("A 'facet', as a child of 'restriction' "+
287 "elements, must appear before any attribute elements.");
288
289 unmarshaller = new FacetUnmarshaller(getSchemaContext(), name, atts);
290 if (_simpleTypeDef == null) {
291 SimpleContent content = (SimpleContent)_complexType.getContentType();
292 _simpleTypeDef = new SimpleTypeDefinition(_schema, content.getTypeName(),_id);
293 }
294 }
295 else if (SchemaNames.ATTRIBUTE.equals(name)) {
296 foundAttribute = true;
297 unmarshaller = new AttributeUnmarshaller(getSchemaContext(), _schema, atts);
298 }
299 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
300
301
302 if (atts.getValue(SchemaNames.REF_ATTR) == null) {
303 error("A 'complexType' may contain referring "+
304 "attributeGroups, but not defining ones.");
305 }
306 foundAttributeGroup = true;
307 unmarshaller = new AttributeGroupUnmarshaller(getSchemaContext(), _schema, atts);
308 }
309
310 else if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
311 unmarshaller
312 = new WildcardUnmarshaller(getSchemaContext(), _complexType, _schema, name, atts);
313 }
314
315 else illegalElement(name);
316 }
317
318
319
320
321
322
323
324
325 public void endElement(String name, String namespace)
326 throws XMLException
327 {
328
329
330 if ((unmarshaller != null) && (depth > 0)) {
331 unmarshaller.endElement(name, namespace);
332 --depth;
333 return;
334 }
335
336
337 unmarshaller.finish();
338
339
340 if (SchemaNames.ANNOTATION.equals(name)) {
341 Annotation ann = ((AnnotationUnmarshaller)unmarshaller).getAnnotation();
342 _complexType.addAnnotation(ann);
343 }
344
345 else if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
346 Wildcard wildcard =
347 ((WildcardUnmarshaller)unmarshaller).getWildcard();
348 try {
349 _complexType.setAnyAttribute(wildcard);
350 } catch (SchemaException e) {
351 throw new IllegalArgumentException(e.getMessage());
352 }
353 }
354
355
356
357 else if (SchemaNames.ATTRIBUTE.equals(name)) {
358 AttributeDecl attrDecl =
359 ((AttributeUnmarshaller)unmarshaller).getAttribute();
360
361
362
363
364
365
366
367
368
369
370 _complexType.addAttributeDecl(attrDecl);
371 }
372
373 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
374 AttributeGroupReference attrGroupRef =
375 (AttributeGroupReference) unmarshaller.getObject();
376 _complexType.addAttributeGroupReference(attrGroupRef);
377 }
378
379 else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
380 SimpleType type = (SimpleType) unmarshaller.getObject();
381 _complexType.setContentType(new SimpleContent(type));
382 }
383
384 else {
385 _simpleTypeDef.addFacet((Facet)unmarshaller.getObject());
386 foundFacets = true;
387
388
389 }
390
391
392 unmarshaller = null;
393 }
394
395 public void characters(char[] ch, int start, int length)
396 throws XMLException
397 {
398
399 if (unmarshaller != null) {
400 unmarshaller.characters(ch, start, length);
401 }
402 }
403
404
405
406
407
408
409
410
411
412 public void finish() {
413
414 if (_simpleTypeDef != null) {
415 SimpleType baseType = _simpleTypeDef.createSimpleType();
416 _complexType.setContentType(new SimpleContent(baseType));
417 }
418
419
420 _complexType.setRestriction(true);
421 }
422 }