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 package org.exolab.castor.xml;
46
47 import java.lang.reflect.Array;
48 import java.text.MessageFormat;
49 import java.util.Enumeration;
50 import java.util.List;
51 import java.util.Vector;
52
53 import org.exolab.castor.mapping.FieldHandler;
54 import org.exolab.castor.xml.location.XPathLocation;
55
56
57
58
59
60
61
62 public class FieldValidator extends Validator {
63
64
65
66 private static final String ERROR_NAME = "-error-if-this-is-used-";
67
68 private static final int DEFAULT_MIN = 0;
69
70 private static final int DEFAULT_MAX = -1;
71
72
73 private int _minOccurs = DEFAULT_MIN;
74
75 private int _maxOccurs = DEFAULT_MAX;
76
77 private XMLFieldDescriptor _descriptor = null;
78
79
80 private TypeValidator _validator = null;
81
82
83
84
85 public FieldValidator() {
86 super();
87 }
88
89
90
91
92
93 public FieldValidator(final TypeValidator validator) {
94 super();
95 this._validator = validator;
96 }
97
98
99
100
101
102
103
104 public int getMinOccurs() {
105 return _minOccurs;
106 }
107
108
109
110
111
112
113
114 public int getMaxOccurs() {
115 return _maxOccurs;
116 }
117
118
119
120
121
122 public TypeValidator getTypeValidator() {
123 return _validator;
124 }
125
126
127
128
129
130 public boolean hasTypeValidator() {
131 return _validator != null;
132 }
133
134
135
136
137
138
139
140
141 public void setMinOccurs(final int minOccurs) {
142 this._minOccurs = (minOccurs < 0) ? 0 : minOccurs;
143 }
144
145
146
147
148
149
150
151 public void setMaxOccurs(final int maxOccurs) {
152 this._maxOccurs = maxOccurs;
153 }
154
155
156
157
158
159
160
161 public void setDescriptor(final XMLFieldDescriptor descriptor) {
162 this._descriptor = descriptor;
163 }
164
165 public void setValidator(final TypeValidator validator) {
166 this._validator = validator;
167 }
168
169
170
171
172
173
174
175
176 public void validate(final Object object, final ValidationContext context)
177 throws ValidationException {
178 if (_descriptor == null || object == null || context.isValidated(object)) {
179 return;
180 }
181
182
183 if (_descriptor.isTransient()) {
184 return;
185 }
186
187 FieldHandler handler = _descriptor.getHandler();
188 if (handler == null) {
189 return;
190 }
191
192
193 Object value = handler.getValue(object);
194 if (value == null) {
195 if (!_descriptor.isRequired() || _descriptor.isNillable()) {
196 return;
197 }
198
199
200 if (_descriptor.isRequired()
201 && _descriptor.getSchemaType() != null
202 && _descriptor.getSchemaType().equals("IDREF")
203 && context.getInternalContext().getLenientIdValidation()) {
204 return;
205 }
206 StringBuffer buff = new StringBuffer();
207 if (!ERROR_NAME.equals(_descriptor.getXMLName())) {
208 buff.append(MessageFormat.format(resourceBundle.getString("validatorField.error.required.field.whose"),
209 new Object[] {_descriptor.getFieldName(), object.getClass().getName(), _descriptor.getXMLName()}));
210 }
211 else {
212 buff.append(MessageFormat.format(resourceBundle.getString("validatorField.error.required.field"),
213 new Object[] {_descriptor.getFieldName(), object.getClass().getName()}));
214 }
215
216 throw new ValidationException(buff.toString());
217 }
218
219 if (_descriptor.isReference()) {
220 if (_validator != null) {
221 _validator.validate(value, context);
222 }
223 return;
224 }
225
226
227 if (context != null) {
228 if (context.isValidated(object)) {
229 return;
230 }
231
232 context.addValidated(object);
233 }
234
235
236
237 Class type = value.getClass();
238 int size = 1;
239 long occurence = -1;
240
241 try {
242 if (type.isArray()) {
243
244 if (type.getComponentType() != Byte.TYPE) {
245 size = Array.getLength(value);
246 if (_validator != null) {
247 for (int i = 0; i < size; i++) {
248 occurence = i + 1;
249 _validator.validate(Array.get(value, i), context);
250 }
251 } else {
252 for (int i = 0; i < size; i++) {
253 super.validate(Array.get(value, i), context);
254 }
255 }
256 }
257 } else if (value instanceof Enumeration) {
258
259
260
261 size = 0;
262 for (Enumeration enumeration = (Enumeration) value;
263 enumeration.hasMoreElements(); ) {
264 ++size;
265 validateInstance(context, enumeration.nextElement());
266 }
267 } else if (value instanceof Vector) {
268 Vector vector = (Vector) value;
269 size = vector.size();
270 for (int i = 0; i < size; i++) {
271 occurence = i + 1;
272 validateInstance(context, vector.elementAt(i));
273 }
274 } else if (value instanceof List) {
275 List list = (List) value;
276 size = list.size();
277 for (int i = 0; i < size; i++) {
278 occurence = i + 1;
279 validateInstance(context, list.get(i));
280 }
281 } else {
282 validateInstance(context, value);
283 }
284 } catch (ValidationException vx) {
285
286 String err = MessageFormat.format(resourceBundle.getString("validatorField.error.exception"),
287 new Object[] { _descriptor.getFieldName(), object.getClass().getName()});
288 ValidationException validationException = new ValidationException(err, vx);
289 addLocationInformation(_descriptor, validationException, occurence);
290 throw validationException;
291 }
292
293
294
295
296
297
298
299 if (size < _minOccurs && (size != 0 || _descriptor.isRequired())) {
300 StringBuffer buff = new StringBuffer();
301 if (!ERROR_NAME.equals(_descriptor.getXMLName())) {
302 buff.append(MessageFormat.format(resourceBundle.getString("validatorField.error.exception.min.occurs.whose"),
303 new Object[] { _minOccurs, _descriptor.getFieldName(), object.getClass().getName(),
304 _descriptor.getXMLName()}));
305 } else {
306 buff.append(MessageFormat.format(resourceBundle.getString("validatorField.error.exception.min.occurs"),
307 new Object[] { _minOccurs, _descriptor.getFieldName(), object.getClass().getName()}));
308 }
309 throw new ValidationException(buff.toString());
310 }
311
312
313 if (_maxOccurs >= 0 && size > _maxOccurs) {
314 StringBuffer buff = new StringBuffer();
315 if (!ERROR_NAME.equals(_descriptor.getXMLName())) {
316 buff.append(MessageFormat.format(resourceBundle.getString("validatorField.error.exception.max.occurs.whose"),
317 new Object[] { _maxOccurs, _descriptor.getFieldName(), object.getClass().getName(), _descriptor.getXMLName()}));
318 } else {
319 buff.append(MessageFormat.format(resourceBundle.getString("validatorField.error.exception.max.occurs"),
320 new Object[] { _maxOccurs, _descriptor.getFieldName(), object.getClass().getName()}));
321 }
322
323 throw new ValidationException(buff.toString());
324 }
325
326 if (context != null) {
327 context.removeValidated(object);
328 }
329 }
330
331
332
333
334
335
336
337 private void validateInstance(final ValidationContext context, final Object value)
338 throws ValidationException {
339 if (_validator != null) {
340 _validator.validate(value, context);
341 } else {
342 super.validate(value, context);
343 }
344 }
345
346
347
348
349
350
351
352
353 private void addLocationInformation(final XMLFieldDescriptor fieldDescriptor,
354 final ValidationException e, final long occurence) {
355 XPathLocation loc = (XPathLocation) e.getLocation();
356 if (loc == null) {
357 loc = new XPathLocation();
358 e.setLocation(loc);
359 String xmlName = fieldDescriptor.getXMLName();
360 if (occurence > 0) {
361 xmlName += "[" + occurence + "]";
362 }
363 if (fieldDescriptor.getNodeType() == NodeType.Attribute) {
364 loc.addAttribute(xmlName);
365 } else {
366 loc.addChild(xmlName);
367 }
368 }
369 }
370
371 }