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