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.AttributeDecl;
53 import org.exolab.castor.xml.schema.Form;
54 import org.exolab.castor.xml.schema.SchemaContext;
55 import org.exolab.castor.xml.schema.Schema;
56 import org.exolab.castor.xml.schema.SchemaNames;
57 import org.exolab.castor.xml.schema.SimpleType;
58
59
60
61
62
63
64
65 public class AttributeUnmarshaller extends ComponentReader {
66
67
68
69
70
71
72
73
74
75 private ComponentReader unmarshaller;
76
77
78
79
80 private int depth = 0;
81
82
83
84
85 private AttributeDecl _attribute = null;
86
87 private Schema _schema = null;
88
89 private boolean foundAnnotation = false;
90 private boolean foundSimpleType = false;
91
92
93
94
95
96
97 public AttributeUnmarshaller(
98 final SchemaContext schemaContext,
99 final Schema schema,
100 final AttributeSet atts)
101 {
102 super(schemaContext);
103 this._schema = schema;
104
105 _attribute = new AttributeDecl(schema);
106
107
108 String attValue = atts.getValue(SchemaNames.REF_ATTR);
109 if (attValue != null) {
110 _attribute.setReference(attValue);
111 }
112
113
114 attValue = atts.getValue(SchemaNames.NAME_ATTR);
115 if (attValue != null) {
116 if (_attribute.isReference()) {
117 String err = "An attribute cannot have a 'name' attribute and a 'ref' attribute at the same time.";
118 throw new IllegalStateException(err);
119 }
120 _attribute.setName(attValue);
121 }
122
123
124 attValue = atts.getValue(SchemaNames.DEFAULT_ATTR);
125 if (attValue != null) {
126 _attribute.setDefaultValue(attValue);
127 }
128
129
130 _attribute.setId(atts.getValue(SchemaNames.ID_ATTR));
131
132
133 attValue = atts.getValue(SchemaNames.FIXED_ATTR);
134 if (attValue != null) {
135 _attribute.setFixedValue(attValue);
136 }
137
138
139 attValue = atts.getValue(SchemaNames.FORM);
140 if (attValue != null) {
141 if (_attribute.isReference()) {
142 String err = "An attribute reference cannot have a 'form' attribute.";
143 throw new IllegalArgumentException(err);
144 }
145 _attribute.setForm(Form.valueOf(attValue));
146 }
147
148
149 attValue = atts.getValue(SchemaNames.TYPE_ATTR);
150 if (attValue != null) {
151 if (_attribute.isReference()) {
152 String err = "An attribute reference cannot have a 'type' attribute.";
153 throw new IllegalArgumentException(err);
154 }
155 _attribute.setSimpleTypeReference(attValue);
156 }
157
158
159 attValue = atts.getValue(SchemaNames.USE_ATTR);
160 if (attValue != null) {
161 if (_attribute.isDefault() && (!attValue.equals(AttributeDecl.USE_OPTIONAL)) )
162 throw new IllegalArgumentException("When 'default' is present, the 'use' attribute must have the value 'optional'.");
163 _attribute.setUse(attValue);
164 }
165
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179 public String elementName() {
180 return SchemaNames.ATTRIBUTE;
181 }
182
183
184
185
186 public AttributeDecl getAttribute() {
187 return _attribute;
188 }
189
190
191
192
193
194 public Object getObject() {
195 return getAttribute();
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public void startElement(String name, String namespace, AttributeSet atts,
212 Namespaces nsDecls)
213 throws XMLException
214 {
215
216 if (unmarshaller != null) {
217 unmarshaller.startElement(name, namespace, atts, nsDecls);
218 ++depth;
219 return;
220 }
221
222 if (SchemaNames.ANNOTATION.equals(name)) {
223
224 if (foundAnnotation)
225 error("Only one (1) annotation is allowed as a child of " +
226 "an attribute declaration.");
227
228 if (foundSimpleType)
229 error("An annotation may only appear as the first child of "+
230 "an attribute declaration.");
231
232 foundAnnotation = true;
233 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
234 }
235 else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
236 if (foundSimpleType)
237 error("Only one (1) simpleType is allowed as a child of " +
238 "an attribute declaration.");
239
240 foundSimpleType = true;
241 unmarshaller = new SimpleTypeUnmarshaller(getSchemaContext(), _schema, atts);
242 }
243 else {
244 illegalElement(name);
245 }
246
247 }
248
249
250
251
252
253
254
255
256 public void endElement(String name, String namespace)
257 throws XMLException
258 {
259
260
261 if ((unmarshaller != null) && (depth > 0)) {
262 unmarshaller.endElement(name, namespace);
263 --depth;
264 return;
265 }
266
267
268 unmarshaller.finish();
269
270 if (SchemaNames.ANNOTATION.equals(name)) {
271 Annotation ann = (Annotation) unmarshaller.getObject();
272 _attribute.addAnnotation(ann);
273 }
274 else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
275 SimpleType simpleType =
276 ((SimpleTypeUnmarshaller)unmarshaller).getSimpleType();
277 _attribute.setSimpleType(simpleType);
278 }
279
280 unmarshaller = null;
281
282 }
283
284 public void characters(char[] ch, int start, int length)
285 throws XMLException
286 {
287
288 if (unmarshaller != null) {
289 unmarshaller.characters(ch, start, length);
290 }
291 }
292
293 }