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