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