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
49 import org.exolab.castor.xml.AttributeSet;
50 import org.exolab.castor.xml.Namespaces;
51 import org.exolab.castor.xml.XMLException;
52 import org.exolab.castor.xml.schema.Annotation;
53 import org.exolab.castor.xml.schema.AttributeDecl;
54 import org.exolab.castor.xml.schema.AttributeGroupReference;
55 import org.exolab.castor.xml.schema.ComplexType;
56 import org.exolab.castor.xml.schema.Group;
57 import org.exolab.castor.xml.schema.ModelGroup;
58 import org.exolab.castor.xml.schema.Schema;
59 import org.exolab.castor.xml.schema.SchemaContext;
60 import org.exolab.castor.xml.schema.SchemaException;
61 import org.exolab.castor.xml.schema.SchemaNames;
62 import org.exolab.castor.xml.schema.SimpleContent;
63 import org.exolab.castor.xml.schema.SimpleType;
64 import org.exolab.castor.xml.schema.Wildcard;
65 import org.exolab.castor.xml.schema.XMLType;
66
67
68
69
70
71
72 public class ExtensionUnmarshaller 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 private Schema _schema = null;
94
95 private boolean foundAnnotation = false;
96 private boolean foundAttributes = false;
97 private boolean foundModelGroup = false;
98
99
100
101
102
103
104
105
106
107
108
109
110 public ExtensionUnmarshaller (
111 final SchemaContext schemaContext,
112 final ComplexType complexType,
113 final AttributeSet atts)
114 throws XMLException {
115 super(schemaContext);
116
117 _complexType = complexType;
118 _schema = complexType.getSchema();
119
120 _complexType.setDerivationMethod(SchemaNames.EXTENSION);
121
122
123 String base = atts.getValue(SchemaNames.BASE_ATTR);
124 if ((base != null) && (base.length() > 0)) {
125
126 XMLType baseType= _schema.getType(base);
127 if (baseType == null) {
128 _complexType.setBase(base);
129 if (_complexType.isSimpleContent()) {
130 _complexType.setContentType(new SimpleContent(_schema, base));
131 }
132 }
133 else {
134
135 if ( (baseType.isSimpleType()) &&
136 (_complexType.isComplexContent()) ) {
137 String err = "In a 'complexContent', the base attribute "+
138 "must be a complexType but "+ base+" is a simpleType.\n";
139 error(err);
140 }
141 _complexType.setBase(base);
142 _complexType.setBaseType(baseType);
143 if (_complexType.isSimpleContent()) {
144
145 if (baseType.isSimpleType()) {
146 SimpleType simpleType = (SimpleType)baseType;
147 _complexType.setContentType(new SimpleContent(simpleType));
148 }
149 else {
150 ComplexType temp = (ComplexType)baseType;
151 SimpleContent simpleContent = (SimpleContent) temp.getContentType();
152 _complexType.setContentType(simpleContent.copy());
153 }
154 }
155
156 }
157
158 }
159
160 }
161
162
163
164
165
166
167
168
169
170
171
172 public String elementName() {
173 return SchemaNames.EXTENSION;
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 if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
210 unmarshaller
211 = new WildcardUnmarshaller(getSchemaContext(), _complexType, _schema, name, atts);
212 }
213
214
215 else if (SchemaNames.ATTRIBUTE.equals(name)) {
216 foundAttributes = true;
217 unmarshaller
218 = new AttributeUnmarshaller(getSchemaContext(), _schema, atts);
219 }
220
221 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
222
223
224
225
226 if (atts.getValue(SchemaNames.REF_ATTR) == null) {
227 String err = "A complexType may contain referring "+
228 "attribute groups, but not defining ones.";
229 error(err);
230 }
231
232 foundAttributes = true;
233 unmarshaller
234 = new AttributeGroupUnmarshaller(getSchemaContext(), _schema, atts);
235 }
236
237 else if ( name.equals(SchemaNames.GROUP) )
238 {
239 if (foundAttributes)
240 error("'" + name + "' must appear before any attribute "+
241 "definitions when a child of 'complexType'.");
242 if (foundModelGroup)
243 error("'"+name+"' cannot appear as a child of 'complexType' "+
244 "if another 'all', 'sequence', 'choice' or "+
245 "'group' also exists.");
246
247 foundModelGroup = true;
248 unmarshaller
249 = new ModelGroupUnmarshaller(getSchemaContext(), _schema, atts);
250 }
251 else if (SchemaNames.isGroupName(name) && (name != SchemaNames.GROUP) ) {
252 if (foundAttributes)
253 error("'"+name+"' must appear before attribute "+
254 "definitions in an 'extension' element.");
255
256 if (foundModelGroup)
257 error("'"+name+"' cannot appear as a child of 'extension' "+
258 "if another 'all', 'sequence', 'choice' or "+
259 "'group' already exists.");
260
261 if (_complexType.isSimpleContent())
262 error("'"+name+"' may not appear in a 'extension' of "+
263 "'simpleContent'.");
264
265 foundModelGroup = true;
266 unmarshaller
267 = new GroupUnmarshaller(getSchemaContext(), _schema, name, atts);
268 }
269
270 else if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
271
272 error("anyAttribute is not yet supported.");
273 }
274 else if (name.equals(SchemaNames.ANNOTATION)) {
275 if (foundAttributes || foundModelGroup)
276 error("An annotation must appear as the first child of an " +
277 "'extension' element.");
278
279 if (foundAnnotation)
280 error("Only one (1) annotation may appear as the child of "+
281 "an 'extension' element.");
282
283 foundAnnotation = true;
284 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
285 }
286 else illegalElement(name);
287
288 }
289
290
291
292
293
294
295
296
297 public void endElement(String name, String namespace)
298 throws XMLException
299 {
300
301
302 if ((unmarshaller != null) && (depth > 0)) {
303 unmarshaller.endElement(name, namespace);
304 --depth;
305 return;
306 }
307
308
309 unmarshaller.finish();
310
311
312 if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
313 Wildcard wildcard =
314 ((WildcardUnmarshaller)unmarshaller).getWildcard();
315 try {
316 _complexType.setAnyAttribute(wildcard);
317 } catch (SchemaException e) {
318 throw new IllegalArgumentException(e.getMessage());
319 }
320 }
321
322
323 else if (SchemaNames.ATTRIBUTE.equals(name)) {
324 AttributeDecl attrDecl =
325 ((AttributeUnmarshaller)unmarshaller).getAttribute();
326
327 _complexType.addAttributeDecl(attrDecl);
328 }
329
330 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
331 AttributeGroupReference attrGroupRef =
332 (AttributeGroupReference) unmarshaller.getObject();
333 _complexType.addAttributeGroupReference(attrGroupRef);
334 }
335
336 else if (name.equals(SchemaNames.GROUP)) {
337 ModelGroup group = ((ModelGroupUnmarshaller)unmarshaller).getGroup();
338 _complexType.addGroup(group);
339 }
340
341
342 else if ( (SchemaNames.isGroupName(name)) && (name != SchemaNames.GROUP) )
343 {
344 Group group = ((GroupUnmarshaller)unmarshaller).getGroup();
345 _complexType.addGroup(group);
346 }
347
348 else if (SchemaNames.ANNOTATION.equals(name)) {
349 Annotation ann = ((AnnotationUnmarshaller)unmarshaller).getAnnotation();
350 _complexType.addAnnotation(ann);
351 }
352
353 unmarshaller = null;
354 }
355
356 public void characters(char[] ch, int start, int length)
357 throws XMLException
358 {
359
360 if (unmarshaller != null) {
361 unmarshaller.characters(ch, start, length);
362 }
363 }
364
365 }