1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.exolab.castor.xml.validators;
15
16 import org.exolab.castor.mapping.ClassDescriptor;
17 import org.exolab.castor.mapping.FieldDescriptor;
18 import org.exolab.castor.mapping.FieldHandler;
19 import org.exolab.castor.xml.ClassDescriptorResolver;
20 import org.exolab.castor.xml.TypeValidator;
21 import org.exolab.castor.xml.ValidationContext;
22 import org.exolab.castor.xml.ValidationException;
23
24
25
26
27
28
29
30 public class IdRefValidator implements TypeValidator {
31
32
33
34
35 public IdRefValidator() {
36 super();
37 }
38
39
40
41
42
43
44
45
46 public void validate(final Object object, final ValidationContext context)
47 throws ValidationException {
48
49 if (object == null) {
50 String err = "The object associated with IDREF \"" + object + "\" is null!";
51 throw new ValidationException(err);
52 }
53
54
55 String id = null;
56 try {
57 ClassDescriptorResolver classDescriptorResolver = context.getClassDescriptorResolver();
58 ClassDescriptor classDescriptor = classDescriptorResolver.resolve(object.getClass());
59 FieldDescriptor fieldDescriptor = classDescriptor.getIdentity();
60 FieldHandler fieldHandler = fieldDescriptor.getHandler();
61 id = (String) fieldHandler.getValue(object);
62 } catch (Exception e) {
63 String err = "The object associated with IDREF \"" + object + "\" of type "
64 + object.getClass() + " has no ID!";
65 throw new ValidationException(err);
66 }
67
68 if (id == null) {
69 String err = "The object associated with IDREF \"" + object + "\" has no ID!";
70 throw new ValidationException(err);
71 }
72
73
74 context.checkIdRef(id);
75
76
77 }
78
79 }