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
39 import org.exolab.castor.xml.AttributeSet;
40 import org.exolab.castor.xml.Namespaces;
41 import org.exolab.castor.xml.XMLException;
42 import org.exolab.castor.xml.schema.Annotation;
43 import org.exolab.castor.xml.schema.AttributeDecl;
44 import org.exolab.castor.xml.schema.AttributeGroupReference;
45 import org.exolab.castor.xml.schema.ComplexType;
46 import org.exolab.castor.xml.schema.Group;
47 import org.exolab.castor.xml.schema.ModelGroup;
48 import org.exolab.castor.xml.schema.Schema;
49 import org.exolab.castor.xml.schema.SchemaContext;
50 import org.exolab.castor.xml.schema.SchemaException;
51 import org.exolab.castor.xml.schema.SchemaNames;
52 import org.exolab.castor.xml.schema.SimpleContent;
53 import org.exolab.castor.xml.schema.SimpleType;
54 import org.exolab.castor.xml.schema.Wildcard;
55 import org.exolab.castor.xml.schema.XMLType;
56
57
58
59
60
61
62
63 public class ExtensionUnmarshaller 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 private Schema _schema = null;
85
86 private boolean foundAnnotation = false;
87 private boolean foundAttributes = false;
88 private boolean foundModelGroup = false;
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public ExtensionUnmarshaller(final SchemaContext schemaContext, final ComplexType complexType,
103 final AttributeSet atts) throws XMLException {
104 super(schemaContext);
105
106 _complexType = complexType;
107 _schema = complexType.getSchema();
108
109 _complexType.setDerivationMethod(SchemaNames.EXTENSION);
110
111
112 String base = atts.getValue(SchemaNames.BASE_ATTR);
113 if ((base != null) && (base.length() > 0)) {
114
115 XMLType baseType = _schema.getType(base);
116 if (baseType == null) {
117 _complexType.setBase(base);
118 if (_complexType.isSimpleContent()) {
119 _complexType.setContentType(new SimpleContent(_schema, base));
120 }
121 } else {
122
123 if ((baseType.isSimpleType()) && (_complexType.isComplexContent())) {
124 String err = "In a 'complexContent', the base attribute " + "must be a complexType but "
125 + base + " is a simpleType.\n";
126 error(err);
127 }
128 _complexType.setBase(base);
129 _complexType.setBaseType(baseType);
130 if (_complexType.isSimpleContent()) {
131
132 if (baseType.isSimpleType()) {
133 SimpleType simpleType = (SimpleType) baseType;
134 _complexType.setContentType(new SimpleContent(simpleType));
135 } else {
136 ComplexType temp = (ComplexType) baseType;
137 SimpleContent simpleContent = (SimpleContent) temp.getContentType();
138 _complexType.setContentType(simpleContent.copy());
139 }
140 }
141
142 }
143
144 }
145
146 }
147
148
149
150
151
152
153
154
155
156
157 public String elementName() {
158 return SchemaNames.EXTENSION;
159 }
160
161
162
163
164
165
166 public Object getObject() {
167 return null;
168 }
169
170
171
172
173
174
175
176
177
178
179
180 public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
181 throws XMLException {
182
183 if (unmarshaller != null) {
184 unmarshaller.startElement(name, namespace, atts, nsDecls);
185 ++depth;
186 return;
187 }
188
189
190 if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
191 unmarshaller =
192 new WildcardUnmarshaller(getSchemaContext(), _complexType, _schema, name, atts);
193 }
194
195
196 else if (SchemaNames.ATTRIBUTE.equals(name)) {
197 foundAttributes = true;
198 unmarshaller = new AttributeUnmarshaller(getSchemaContext(), _schema, atts);
199 }
200
201 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
202
203
204
205
206 if (atts.getValue(SchemaNames.REF_ATTR) == null) {
207 String err =
208 "A complexType may contain referring " + "attribute groups, but not defining ones.";
209 error(err);
210 }
211
212 foundAttributes = true;
213 unmarshaller = new AttributeGroupUnmarshaller(getSchemaContext(), _schema, atts);
214 }
215
216 else if (name.equals(SchemaNames.GROUP)) {
217 if (foundAttributes)
218 error("'" + name + "' must appear before any attribute "
219 + "definitions when a child of 'complexType'.");
220 if (foundModelGroup)
221 error("'" + name + "' cannot appear as a child of 'complexType' "
222 + "if another 'all', 'sequence', 'choice' or " + "'group' also exists.");
223
224 foundModelGroup = true;
225 unmarshaller = new ModelGroupUnmarshaller(getSchemaContext(), _schema, atts);
226 } else if (SchemaNames.isGroupName(name) && (name != SchemaNames.GROUP)) {
227 if (foundAttributes)
228 error("'" + name + "' must appear before attribute "
229 + "definitions in an 'extension' element.");
230
231 if (foundModelGroup)
232 error("'" + name + "' cannot appear as a child of 'extension' "
233 + "if another 'all', 'sequence', 'choice' or " + "'group' already exists.");
234
235 if (_complexType.isSimpleContent())
236 error("'" + name + "' may not appear in a 'extension' of " + "'simpleContent'.");
237
238 foundModelGroup = true;
239 unmarshaller = new GroupUnmarshaller(getSchemaContext(), _schema, name, atts);
240 }
241
242 else if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
243
244 error("anyAttribute is not yet supported.");
245 } else if (name.equals(SchemaNames.ANNOTATION)) {
246 if (foundAttributes || foundModelGroup)
247 error("An annotation must appear as the first child of an " + "'extension' element.");
248
249 if (foundAnnotation)
250 error("Only one (1) annotation may appear as the child of " + "an 'extension' element.");
251
252 foundAnnotation = true;
253 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
254 } else
255 illegalElement(name);
256
257 }
258
259
260
261
262
263
264
265
266 public void endElement(String name, String namespace) throws XMLException {
267
268
269 if ((unmarshaller != null) && (depth > 0)) {
270 unmarshaller.endElement(name, namespace);
271 --depth;
272 return;
273 }
274
275
276 unmarshaller.finish();
277
278
279 if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
280 Wildcard wildcard = ((WildcardUnmarshaller) unmarshaller).getWildcard();
281 try {
282 _complexType.setAnyAttribute(wildcard);
283 } catch (SchemaException e) {
284 throw new IllegalArgumentException(e.getMessage());
285 }
286 }
287
288
289 else if (SchemaNames.ATTRIBUTE.equals(name)) {
290 AttributeDecl attrDecl = ((AttributeUnmarshaller) unmarshaller).getAttribute();
291
292 _complexType.addAttributeDecl(attrDecl);
293 }
294
295 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
296 AttributeGroupReference attrGroupRef = (AttributeGroupReference) unmarshaller.getObject();
297 _complexType.addAttributeGroupReference(attrGroupRef);
298 }
299
300 else if (name.equals(SchemaNames.GROUP)) {
301 ModelGroup group = ((ModelGroupUnmarshaller) unmarshaller).getGroup();
302 _complexType.addGroup(group);
303 }
304
305
306 else if ((SchemaNames.isGroupName(name)) && (name != SchemaNames.GROUP)) {
307 Group group = ((GroupUnmarshaller) unmarshaller).getGroup();
308 _complexType.addGroup(group);
309 }
310
311 else if (SchemaNames.ANNOTATION.equals(name)) {
312 Annotation ann = ((AnnotationUnmarshaller) unmarshaller).getAnnotation();
313 _complexType.addAnnotation(ann);
314 }
315
316 unmarshaller = null;
317 }
318
319 public void characters(char[] ch, int start, int length) throws XMLException {
320
321 if (unmarshaller != null) {
322 unmarshaller.characters(ch, start, length);
323 }
324 }
325
326 }