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
49 import org.exolab.castor.xml.AttributeSet;
50 import org.exolab.castor.xml.Namespaces;
51 import org.exolab.castor.xml.XMLException;
52 import org.exolab.castor.xml.schema.Annotation;
53 import org.exolab.castor.xml.schema.IdentityConstraint;
54 import org.exolab.castor.xml.schema.IdentityField;
55 import org.exolab.castor.xml.schema.IdentitySelector;
56 import org.exolab.castor.xml.schema.Key;
57 import org.exolab.castor.xml.schema.KeyRef;
58 import org.exolab.castor.xml.schema.SchemaContext;
59 import org.exolab.castor.xml.schema.SchemaNames;
60 import org.exolab.castor.xml.schema.Unique;
61
62
63
64
65
66
67
68 public class IdentityConstraintUnmarshaller extends ComponentReader {
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 IdentityConstraint _identityConstraint = null;
88
89 private boolean _foundAnnotation = false;
90 private boolean _foundSelector = false;
91 private boolean _foundField = false;
92
93 private String _elementName = null;
94
95
96
97
98
99
100
101
102
103
104
105
106 public IdentityConstraintUnmarshaller(
107 final SchemaContext schemaContext,
108 final String elementName,
109 final AttributeSet atts)
110 throws XMLException {
111 super(schemaContext);
112
113 _elementName = elementName;
114
115 String name = atts.getValue(SchemaNames.NAME_ATTR);
116 if (name == null) {
117 error("The 'name' attribute for an identity-constraint must exist.");
118 }
119
120 atts.getValue(SchemaNames.ID_ATTR);
121
122
123 if (SchemaNames.KEYREF.equals(elementName)) {
124 String refer = atts.getValue("refer");
125 if (refer == null) {
126 error("The 'refer' attribute for keyref must exist.");
127 }
128 _identityConstraint = new KeyRef(name, refer);
129 }
130
131 else if (SchemaNames.UNIQUE.equals(elementName)) {
132 _identityConstraint = new Unique(name);
133 }
134
135 else {
136 _identityConstraint = new Key(name);
137 }
138
139 }
140
141
142
143
144
145
146
147
148
149
150
151 public String elementName() {
152 return _elementName;
153 }
154
155
156
157
158
159
160 public IdentityConstraint getIdentityConstraint() {
161 return _identityConstraint;
162 }
163
164
165
166
167
168
169 public Object getObject() {
170 return getIdentityConstraint();
171 }
172
173 public void finish() throws XMLException {
174 if (!_foundSelector) {
175 error ("Invalid " + _elementName + "; missing 'selector'.");
176 }
177 else if (!_foundField) {
178 error ("Invalid " + _elementName + "; missing 'field'.");
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public void startElement(String name, String namespace, AttributeSet atts,
196 Namespaces nsDecls)
197 throws XMLException
198 {
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 may appear as a child of '" +
210 _elementName + "'.");
211
212 if (_foundSelector || _foundField)
213 error("An annotation may only appear as the first child of '"+
214 _elementName + "'.");
215
216 _foundAnnotation = true;
217 _unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
218 }
219 else if (SchemaNames.SELECTOR.equals(name)) {
220
221 if (_foundField) {
222 String err = "The 'selector' element of '" + _elementName +
223 "' must appear before any 'field' elements.";
224 error(err);
225 }
226 if (_foundSelector)
227 error("Only one (1) 'selector' may appear as a child of '" +
228 _elementName + "'.");
229
230 _foundSelector = true;
231
232 _unmarshaller = new FieldOrSelectorUnmarshaller(getSchemaContext(), name, atts);
233 }
234 else if (SchemaNames.FIELD.equals(name)) {
235 _foundField = true;
236 _unmarshaller = new FieldOrSelectorUnmarshaller(getSchemaContext(), name, atts);
237 }
238 else illegalElement(name);
239
240 }
241
242
243
244
245
246
247
248
249 public void endElement(String name, String namespace)
250 throws XMLException
251 {
252
253
254 if ((_unmarshaller != null) && (_depth > 0)) {
255 _unmarshaller.endElement(name, namespace);
256 --_depth;
257 return;
258 }
259
260
261 _unmarshaller.finish();
262
263 if (SchemaNames.ANNOTATION.equals(name)) {
264 Annotation annotation = (Annotation)_unmarshaller.getObject();
265 _identityConstraint.addAnnotation(annotation);
266 }
267 else if (SchemaNames.SELECTOR.equals(name)) {
268 IdentitySelector selector
269 = (IdentitySelector)_unmarshaller.getObject();
270 _identityConstraint.setSelector(selector);
271 }
272 else if (SchemaNames.FIELD.equals(name)) {
273 IdentityField field
274 = (IdentityField)_unmarshaller.getObject();
275 _identityConstraint.addField(field);
276 }
277
278
279 _unmarshaller = null;
280 }
281
282 public void characters(char[] ch, int start, int length)
283 throws XMLException
284 {
285
286 if (_unmarshaller != null) {
287 _unmarshaller.characters(ch, start, length);
288 }
289 }
290
291 }