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 import org.exolab.castor.xml.schema.Structure;
48 import org.exolab.castor.xml.schema.XMLType;
49 import org.exolab.castor.xml.schema.simpletypes.ListType;
50
51
52
53
54
55
56
57
58
59
60 public class SimpleTypeListUnmarshaller extends ComponentReader {
61
62
63
64
65
66
67
68
69
70 private ComponentReader unmarshaller;
71
72
73
74
75 private int depth = 0;
76
77
78
79
80 private ListType _list = null;
81 private Schema _schema = null;
82 private boolean foundAnnotation = false;
83 private boolean foundSimpleType = false;
84 private boolean foundItemType = false;
85
86
87
88
89
90
91
92
93
94
95
96
97 SimpleTypeListUnmarshaller(final SchemaContext schemaContext, final Schema schema,
98 final AttributeSet atts) throws XMLException {
99 super(schemaContext);
100
101 _schema = schema;
102 _list = new ListType(schema);
103
104
105 String itemTypeName = atts.getValue(SchemaNames.ITEM_TYPE_ATTR);
106 if ((itemTypeName != null) && (itemTypeName.length() > 0)) {
107
108 foundItemType = true;
109 XMLType itemType = _schema.getType(itemTypeName);
110 if (itemType == null) {
111 _list.setItemType(itemTypeName);
112 } else if (itemType.getStructureType() == Structure.COMPLEX_TYPE) {
113 String err = "The item type of a list cannot " + "be a complexType.";
114 throw new IllegalStateException(err);
115 } else
116 _list.setItemType((SimpleType) itemType);
117 }
118
119
120 _list.setId(atts.getValue(SchemaNames.ID_ATTR));
121
122 }
123
124
125
126
127
128
129
130
131
132
133 public String elementName() {
134 return SchemaNames.LIST;
135 }
136
137
138
139
140
141 public void finish() throws XMLException {
142 if ((!foundItemType) && (!foundSimpleType)) {
143 String err = "Missing sub-component of <list>, either use "
144 + " the 'itemType' attribute, or an anonymous simpleType.";
145 error(err);
146 }
147 }
148
149
150
151
152
153
154 public Object getObject() {
155 if ((!foundItemType) && (!foundSimpleType)) {
156 String err = "Missing sub-component of <list>, either use "
157 + " the 'itemType' attribute, or an anonymous simpleType.";
158 throw new IllegalStateException(err);
159 }
160 return _list;
161 }
162
163
164
165
166
167
168
169
170
171
172
173 public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
174 throws XMLException {
175
176 if (unmarshaller != null) {
177 unmarshaller.startElement(name, namespace, atts, nsDecls);
178 ++depth;
179 return;
180 }
181
182
183
184 if (name.equals(SchemaNames.ANNOTATION)) {
185
186 if (foundSimpleType)
187 error("An annotation must appear as the first child " + "of 'list' elements.");
188
189 if (foundAnnotation)
190 error("Only one (1) annotation may appear as a child of " + "'list' elements.");
191
192 foundAnnotation = true;
193 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
194 } else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
195 if (foundItemType)
196 error("A 'list' element can have either an 'itemType' or " + " 'simpleType'.");
197
198 if (foundSimpleType)
199 error("Only one (1) 'simpleType' may appear as a child of " + "'list' elements.");
200
201 foundSimpleType = true;
202 unmarshaller = new SimpleTypeUnmarshaller(getSchemaContext(), _schema, atts);
203
204 } else
205 illegalElement(name);
206
207 unmarshaller.setDocumentLocator(getDocumentLocator());
208 }
209
210
211
212
213
214
215
216
217 public void endElement(String name, String namespace) throws XMLException {
218
219
220 if ((unmarshaller != null) && (depth > 0)) {
221 unmarshaller.endElement(name, namespace);
222 --depth;
223 return;
224 }
225
226
227 unmarshaller.finish();
228
229
230 if (SchemaNames.ANNOTATION.equals(name)) {
231 Annotation ann = ((AnnotationUnmarshaller) unmarshaller).getAnnotation();
232 _list.setLocalAnnotation(ann);
233 } else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
234 SimpleType type = (SimpleType) unmarshaller.getObject();
235 _list.setItemType(type);
236 }
237
238
239 unmarshaller = null;
240 }
241
242 public void characters(char[] ch, int start, int length) throws XMLException {
243
244 if (unmarshaller != null) {
245 unmarshaller.characters(ch, start, length);
246 }
247 }
248
249 }