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