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