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