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.AttributeGroup;
54 import org.exolab.castor.xml.schema.AttributeGroupDecl;
55 import org.exolab.castor.xml.schema.AttributeGroupReference;
56 import org.exolab.castor.xml.schema.SchemaContext;
57 import org.exolab.castor.xml.schema.Schema;
58 import org.exolab.castor.xml.schema.SchemaException;
59 import org.exolab.castor.xml.schema.SchemaNames;
60 import org.exolab.castor.xml.schema.Wildcard;
61
62
63
64
65
66
67 public class AttributeGroupUnmarshaller extends ComponentReader {
68
69
70
71
72
73
74
75
76
77 private ComponentReader unmarshaller;
78
79
80
81
82 private int depth = 0;
83
84
85
86
87 private AttributeGroup _attributeGroup = null;
88
89 private boolean allowAnnotation = true;
90 private boolean foundAnyAttribute = false;
91 private boolean isRef = false;
92
93 private Schema _schema = null;
94
95
96
97
98
99
100
101
102
103
104
105 public AttributeGroupUnmarshaller(
106 final SchemaContext schemaContext,
107 final Schema schema,
108 final AttributeSet atts)
109 throws XMLException {
110 super(schemaContext);
111 this._schema = schema;
112
113
114 String ref = atts.getValue(SchemaNames.REF_ATTR);
115 if (ref != null) {
116 if (ref.length() > 0) {
117 isRef = true;
118 _attributeGroup = new AttributeGroupReference(schema, ref);
119 }
120 else {
121 String err = "The value of the '" + SchemaNames.REF_ATTR +
122 "' attribute for attribute group must contain a " +
123 "valid value.";
124 throw new SchemaException(err);
125 }
126 }
127 else {
128 AttributeGroupDecl attDecl = new AttributeGroupDecl(schema);
129 _attributeGroup = attDecl;
130
131
132 attDecl.setName(atts.getValue(SchemaNames.NAME_ATTR));
133 attDecl.setId(atts.getValue(SchemaNames.ID_ATTR));
134 }
135
136
137
138 }
139
140
141
142
143
144
145
146
147
148
149
150 public String elementName() {
151 return SchemaNames.ATTRIBUTE_GROUP;
152 }
153
154
155
156
157
158
159
160
161 public AttributeGroup getAttributeGroup() {
162 return _attributeGroup;
163 }
164
165
166
167
168
169 public Object getObject() {
170 return getAttributeGroup();
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 public void startElement(String name, String namespace, AttributeSet atts,
187 Namespaces nsDecls)
188 throws XMLException
189 {
190
191 if (unmarshaller != null) {
192 unmarshaller.startElement(name, namespace, atts, nsDecls);
193 ++depth;
194 return;
195 }
196
197
198 if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
199 if (foundAnyAttribute)
200 error("an anyAttribute element can appear only once as a child "+
201 "of an 'AttributeGroup'.");
202 foundAnyAttribute = true;
203 allowAnnotation = true;
204 unmarshaller
205 = new WildcardUnmarshaller(getSchemaContext(), _attributeGroup, _schema, name, atts);
206 }
207
208 else if (SchemaNames.ATTRIBUTE.equals(name)) {
209 allowAnnotation = false;
210
211 if (isRef)
212 error("AttributeGroup references may not have children.");
213
214 unmarshaller
215 = new AttributeUnmarshaller(getSchemaContext(), _schema, atts);
216 }
217
218 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
219 allowAnnotation = false;
220 if (isRef)
221 error("AttributeGroup references may not have children.");
222 unmarshaller
223 = new AttributeGroupUnmarshaller(getSchemaContext(), _schema, atts);
224 }
225 else if (name.equals(SchemaNames.ANNOTATION)) {
226 if (!allowAnnotation) outOfOrder(name);
227 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
228 }
229 else illegalElement(name);
230
231 }
232
233
234
235
236
237
238
239
240 public void endElement(String name, String namespace)
241 throws XMLException
242 {
243
244
245 if ((unmarshaller != null) && (depth > 0)) {
246 unmarshaller.endElement(name, namespace);
247 --depth;
248 return;
249 }
250
251
252 unmarshaller.finish();
253
254
255 if (SchemaNames.ANY_ATTRIBUTE.equals(name)) {
256 Wildcard wildcard =
257 ((WildcardUnmarshaller)unmarshaller).getWildcard();
258 try {
259 ((AttributeGroupDecl)_attributeGroup).setAnyAttribute(wildcard);
260 } catch (SchemaException e) {
261 throw new IllegalArgumentException(e.getMessage());
262 }
263 }
264
265
266
267 else if (SchemaNames.ATTRIBUTE.equals(name)) {
268 AttributeDecl attrDecl =
269 ((AttributeUnmarshaller)unmarshaller).getAttribute();
270
271 ((AttributeGroupDecl)_attributeGroup).addAttribute(attrDecl);
272 }
273
274 else if (SchemaNames.ATTRIBUTE_GROUP.equals(name)) {
275
276 Object obj = unmarshaller.getObject();
277
278 try {
279 ((AttributeGroupDecl)_attributeGroup).addReference(
280 (AttributeGroupReference) obj);
281 }
282 catch (ClassCastException ex) {
283 String err = "AttributeGroups cannot contain new " +
284 "AttributeGroup definitions, only references to " +
285 "top-level AttributeGroups are allowed.";
286 error(err);
287 }
288 }
289
290 else if (SchemaNames.ANNOTATION.equals(name)) {
291 Annotation ann = ((AnnotationUnmarshaller)unmarshaller).getAnnotation();
292 _attributeGroup.addAnnotation(ann);
293 }
294
295 unmarshaller = null;
296 }
297
298 public void characters(char[] ch, int start, int length)
299 throws XMLException
300 {
301
302 if (unmarshaller != null) {
303 unmarshaller.characters(ch, start, length);
304 }
305 }
306
307 }