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.Schema;
44 import org.exolab.castor.xml.schema.SchemaContext;
45 import org.exolab.castor.xml.schema.SchemaNames;
46 import org.exolab.castor.xml.schema.SimpleType;
47
48
49
50
51
52
53
54 public class SimpleTypeUnmarshaller extends ComponentReader {
55
56
57
58
59
60
61
62
63
64 private ComponentReader unmarshaller;
65
66
67
68
69 private int depth = 0;
70
71
72
73
74 private SimpleTypeDefinition _simpleTypeDef = null;
75
76
77
78
79 private SimpleType _simpleType = null;
80
81
82 private boolean foundAnnotation = false;
83 private boolean foundList = false;
84 private boolean foundRestriction = false;
85 private boolean foundUnion = false;
86
87
88
89
90
91
92
93
94
95
96
97
98 public SimpleTypeUnmarshaller(final SchemaContext schemaContext, final Schema schema,
99 final AttributeSet atts) throws XMLException {
100 super(schemaContext);
101
102 String name = atts.getValue(SchemaNames.NAME_ATTR);
103
104
105 if (name != null) {
106 int idx = name.indexOf(':');
107 if (idx >= 0) {
108 String prefix = name.substring(0, idx);
109 String ns = schema.getNamespace(prefix);
110 if (ns == null) {
111
112 } else {
113 if (ns.equals(schema.getTargetNamespace())) {
114 name = name.substring(idx + 1);
115 } else {
116
117 }
118 }
119 }
120 }
121
122 String id = atts.getValue(SchemaNames.ID_ATTR);
123 _simpleTypeDef = new SimpleTypeDefinition(schema, name, id);
124
125
126 _simpleTypeDef.setFinal(atts.getValue(SchemaNames.FINAL_ATTR));
127
128
129 }
130
131
132
133
134
135
136
137
138
139
140 public String elementName() {
141 return SchemaNames.SIMPLE_TYPE;
142 }
143
144
145
146
147
148
149 public SimpleType getSimpleType() {
150 if (_simpleType == null) {
151 _simpleType = _simpleTypeDef.createSimpleType();
152 }
153 return _simpleType;
154 }
155
156
157
158
159
160
161 public Object getObject() {
162 return getSimpleType();
163 }
164
165 public void finish() throws XMLException {
166 if (!(foundList || foundUnion || foundRestriction))
167 error("Invalid 'simpleType'; missing 'restriction' " + "| 'union' | 'list'.");
168
169 }
170
171
172
173
174
175
176
177
178
179
180
181 public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
182 throws XMLException {
183
184 if (unmarshaller != null) {
185 unmarshaller.startElement(name, namespace, atts, nsDecls);
186 ++depth;
187 return;
188 }
189
190 if (SchemaNames.ANNOTATION.equals(name)) {
191
192 if (foundAnnotation)
193 error("Only one (1) annotation may appear as a child of " + "'simpleType'.");
194
195 if (foundList || foundUnion || foundRestriction)
196 error("An annotation may only appear as the first child " + "of 'simpleType'.");
197
198 foundAnnotation = true;
199 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
200 } else if (SchemaNames.RESTRICTION.equals(name)) {
201
202 if (foundList)
203 error("A 'simpleType' cannot have both a 'list' and a "
204 + "'restriction' in the same definition.");
205
206 if (foundUnion)
207 error("A 'simpleType' cannot have both a 'union' and a "
208 + "'restriction' in the same definition.");
209
210
211 foundRestriction = true;
212
213 unmarshaller =
214 new SimpleTypeRestrictionUnmarshaller(getSchemaContext(), _simpleTypeDef, atts);
215 } else if (SchemaNames.LIST.equals(name)) {
216 foundList = true;
217 Schema schema = _simpleTypeDef.getSchema();
218 unmarshaller = new SimpleTypeListUnmarshaller(getSchemaContext(), schema, atts);
219 } else if (SchemaNames.UNION.equals(name)) {
220 foundUnion = true;
221 Schema schema = _simpleTypeDef.getSchema();
222 unmarshaller = new UnionUnmarshaller(getSchemaContext(), schema, atts);
223 } else
224 illegalElement(name);
225
226 }
227
228
229
230
231
232
233
234
235 public void endElement(String name, String namespace) throws XMLException {
236
237
238 if ((unmarshaller != null) && (depth > 0)) {
239 unmarshaller.endElement(name, namespace);
240 --depth;
241 return;
242 }
243
244
245 unmarshaller.finish();
246
247 if (SchemaNames.ANNOTATION.equals(name)) {
248 Annotation annotation = (Annotation) unmarshaller.getObject();
249 _simpleTypeDef.setAnnotation(annotation);
250 } else if (SchemaNames.LIST.equals(name)) {
251 _simpleType = (SimpleType) unmarshaller.getObject();
252 _simpleTypeDef.copyInto(_simpleType);
253
254 } else if (SchemaNames.UNION.equals(name)) {
255 _simpleType = (SimpleType) unmarshaller.getObject();
256 _simpleTypeDef.copyInto(_simpleType);
257
258 }
259
260 unmarshaller = null;
261 }
262
263 public void characters(char[] ch, int start, int length) throws XMLException {
264
265 if (unmarshaller != null) {
266 unmarshaller.characters(ch, start, length);
267 }
268 }
269
270 }