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.AttributeGroup;
53 import org.exolab.castor.xml.schema.ComplexType;
54 import org.exolab.castor.xml.schema.SchemaContext;
55 import org.exolab.castor.xml.schema.Group;
56 import org.exolab.castor.xml.schema.Schema;
57 import org.exolab.castor.xml.schema.SchemaException;
58 import org.exolab.castor.xml.schema.SchemaNames;
59 import org.exolab.castor.xml.schema.Wildcard;
60
61 import java.util.StringTokenizer;
62
63
64
65
66
67
68 public class WildcardUnmarshaller extends ComponentReader {
69
70
71
72
73
74 private static final String MAX_OCCURS_WILDCARD = "unbounded";
75
76
77
78
79
80
81
82
83
84 private ComponentReader unmarshaller;
85
86
87
88
89 private int depth = 0;
90
91
92
93
94 private Wildcard _wildcard = null;
95
96
97
98 private Schema _schema = null;
99
100
101
102
103 private String _element = SchemaNames.ANY;
104
105
106
107
108
109 public WildcardUnmarshaller(
110 final SchemaContext schemaContext,
111 final ComplexType complexType,
112 final Schema schema,
113 final String element,
114 final AttributeSet atts) {
115 this(schemaContext, schema, element, atts, new Wildcard(complexType));
116 }
117
118 public WildcardUnmarshaller(
119 final SchemaContext schemaContext,
120 final Group group,
121 final Schema schema,
122 final String element,
123 final AttributeSet atts) {
124 this(schemaContext, schema, element, atts, new Wildcard(group));
125 }
126
127 public WildcardUnmarshaller(
128 final SchemaContext schemaContext,
129 final AttributeGroup attGroup,
130 final Schema schema,
131 final String element,
132 final AttributeSet atts) {
133 this(schemaContext, schema, element, atts, new Wildcard(attGroup));
134 }
135
136
137
138
139
140
141
142
143
144 private WildcardUnmarshaller(
145 final SchemaContext schemaContext,
146 final Schema schema,
147 final String element,
148 final AttributeSet atts,
149 final Wildcard wildcard) {
150 super(schemaContext);
151 _wildcard = wildcard;
152 this._schema = schema;
153 this._element = element;
154
155
156 String attValue = null;
157
158 if (SchemaNames.ANY_ATTRIBUTE.equals(element))
159 _wildcard.setAttributeWildcard();
160 _element = element;
161
162
163 attValue = atts.getValue(SchemaNames.NAMESPACE);
164
165 if (attValue != null) {
166
167 StringTokenizer tokenizer = new StringTokenizer(attValue);
168 while (tokenizer.hasMoreTokens()) {
169
170 String temp = tokenizer.nextToken();
171
172
173
174 if (tokenizer.countTokens() >1 )
175 if ( (SchemaNames.NAMESPACE_ANY.equals(temp)) ||
176 (SchemaNames.NAMESPACE_OTHER.equals(temp)) )
177 throw new IllegalArgumentException(temp+" is not valid when multiple namespaces are listed.");
178
179
180
181
182
183
184
185 if (SchemaNames.isNamespaceName(temp))
186 _wildcard.addNamespace(temp);
187 else {
188 String err = "Invalid 'namespace' value: "+temp;
189 throw new IllegalArgumentException(err);
190 }
191 }
192 }
193 else _wildcard.addNamespace(SchemaNames.NAMESPACE_ANY);
194
195
196
197
198
199
200
201 attValue = atts.getValue(SchemaNames.MAX_OCCURS_ATTR);
202 if (attValue != null) {
203 if (_wildcard.isAttributeWildcard())
204 throw new IllegalStateException("'maxOccurs' is prohibited on a <anyAttribute> element.");
205 if (MAX_OCCURS_WILDCARD.equals(attValue)) attValue = "-1";
206 int maxOccurs = toInt(attValue);
207 _wildcard.setMaxOccurs(maxOccurs);
208 }
209
210 attValue = atts.getValue("minOccurs");
211 if (attValue != null) {
212 if (_wildcard.isAttributeWildcard())
213 throw new IllegalStateException("'minOccurs' is prohibited on a <anyAttribute> element.");
214 _wildcard.setMinOccurs(toInt(attValue));
215 }
216
217 attValue = atts.getValue("processContents");
218
219 if (attValue != null) {
220 try {
221 _wildcard.setProcessContents(attValue);
222 } catch (SchemaException e) {
223 throw new IllegalArgumentException(e.getMessage());
224 }
225 }
226
227
228
229 _wildcard.setId(atts.getValue("id"));
230
231 }
232
233
234
235
236
237
238
239
240
241
242
243 public String elementName() {
244 return _element;
245 }
246
247
248
249
250
251
252 public Wildcard getWildcard() {
253 return _wildcard;
254 }
255
256
257
258
259 public Object getObject() {
260 return getWildcard();
261 }
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 public void startElement(String name, String namespace, AttributeSet atts,
277 Namespaces nsDecls)
278 throws XMLException
279 {
280
281 if (unmarshaller != null) {
282 unmarshaller.startElement(name, namespace, atts, nsDecls);
283 ++depth;
284 return;
285 }
286
287 if (SchemaNames.ANNOTATION.equals(name)) {
288 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
289 }
290
291 else {
292 StringBuffer err = new StringBuffer("illegal element <");
293 err.append(name);
294 err.append("> found in <");
295 err.append(_element);
296 err.append(">");
297 throw new SchemaException(err.toString());
298 }
299
300 }
301
302
303
304
305
306
307
308
309 public void endElement(String name, String namespace)
310 throws XMLException
311 {
312
313
314 if ((unmarshaller != null) && (depth > 0)) {
315 unmarshaller.endElement(name, namespace);
316 --depth;
317 return;
318 }
319
320
321 if (unmarshaller != null) {
322 if (!name.equals(unmarshaller.elementName())) {
323 String err = "missing end element for ";
324 err += unmarshaller.elementName();
325 throw new SchemaException(err);
326 }
327 }
328
329
330 unmarshaller.finish();
331
332
333 if (name == SchemaNames.ANNOTATION) {
334 _schema.addAnnotation((Annotation)unmarshaller.getObject());
335 }
336 unmarshaller = null;
337 }
338
339 public void characters(char[] ch, int start, int length)
340 throws XMLException
341 {
342
343 if (unmarshaller != null) {
344 unmarshaller.characters(ch, start, length);
345 }
346 }
347
348 }