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 java.util.StringTokenizer;
50
51 import org.exolab.castor.xml.AttributeSet;
52 import org.exolab.castor.xml.Namespaces;
53 import org.exolab.castor.xml.XMLException;
54 import org.exolab.castor.xml.schema.Annotation;
55 import org.exolab.castor.xml.schema.Schema;
56 import org.exolab.castor.xml.schema.SchemaContext;
57 import org.exolab.castor.xml.schema.SchemaNames;
58 import org.exolab.castor.xml.schema.SimpleType;
59 import org.exolab.castor.xml.schema.Union;
60
61
62
63
64
65
66
67 public class UnionUnmarshaller extends ComponentReader {
68
69
70
71
72
73
74
75
76 private ComponentReader _unmarshaller;
77
78
79
80
81 private int _depth = 0;
82
83
84
85
86 private Union _union = null;
87
88
89
90
91 private Schema _schema = null;
92
93 private boolean _foundAnnotation = false;
94 private boolean _foundSimpleType = false;
95
96
97
98
99
100
101
102
103
104
105
106 public UnionUnmarshaller(
107 final SchemaContext schemaContext,
108 final Schema schema,
109 final AttributeSet atts)
110 throws XMLException {
111 super(schemaContext);
112
113 if (schema == null) {
114 String err = "'schema' must not be null.";
115 throw new IllegalStateException(err);
116 }
117 _schema = schema;
118
119 _union = new Union(_schema);
120 _union.setId(atts.getValue(SchemaNames.ID_ATTR));
121
122 String memberTypes = atts.getValue(SchemaNames.MEMBER_TYPES_ATTR);
123 processMemberTypes(memberTypes);
124
125 }
126
127
128
129
130
131
132
133
134
135
136
137 public String elementName() {
138 return SchemaNames.UNION;
139 }
140
141
142
143
144
145
146 public Object getObject() {
147 return _union;
148 }
149
150 public void finish()
151 throws XMLException
152 {
153
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public void startElement(String name, String namespace, AttributeSet atts,
170 Namespaces nsDecls)
171 throws XMLException
172 {
173
174 if (_unmarshaller != null) {
175 _unmarshaller.startElement(name, namespace, atts, nsDecls);
176 ++_depth;
177 return;
178 }
179
180 if (SchemaNames.ANNOTATION.equals(name)) {
181
182 if (_foundAnnotation)
183 error("Only one (1) annotation may appear as a child of '" +
184 elementName() + "'.");
185
186 if (_foundSimpleType)
187 error("An annotation may only appear as the first child of '"+
188 elementName() + "'.");
189
190 _foundAnnotation = true;
191 _unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
192 }
193 else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
194 _foundSimpleType = true;
195 _unmarshaller = new SimpleTypeUnmarshaller(getSchemaContext(), _schema, atts);
196 }
197 else illegalElement(name);
198
199 }
200
201
202
203
204
205
206
207
208 public void endElement(String name, String namespace)
209 throws XMLException
210 {
211
212
213 if ((_unmarshaller != null) && (_depth > 0)) {
214 _unmarshaller.endElement(name, namespace);
215 --_depth;
216 return;
217 }
218
219
220 _unmarshaller.finish();
221
222 if (SchemaNames.ANNOTATION.equals(name)) {
223 _union.setLocalAnnotation((Annotation)_unmarshaller.getObject());
224 }
225 else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
226
227 SimpleType simpleType =
228 (SimpleType)_unmarshaller.getObject();
229
230
231 if (simpleType instanceof Union) {
232 String err = "A 'union' may only contain SimpleTypes of "+
233 "the atomic or list variety.";
234 error(err);
235 }
236
237
238
239 _union.addMemberType(simpleType);
240 }
241
242
243 _unmarshaller = null;
244 }
245
246 public void characters(char[] ch, int start, int length)
247 throws XMLException
248 {
249
250 if (_unmarshaller != null) {
251 _unmarshaller.characters(ch, start, length);
252 }
253 }
254
255
256
257
258
259
260
261
262 private void processMemberTypes(String memberTypes) {
263 if ((memberTypes == null) || (memberTypes.length() == 0))
264 return;
265
266 StringTokenizer st = new StringTokenizer(memberTypes);
267 while (st.hasMoreTokens()) {
268 String typeName = st.nextToken();
269 SimpleType simpleType = _schema.getSimpleType(typeName);
270 if (simpleType != null) {
271 _union.addMemberType(simpleType);
272 }
273 else {
274 _union.addMemberType(typeName);
275 }
276
277 }
278 }
279
280 }