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