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 package org.exolab.castor.xml;
40
41 import java.text.MessageFormat;
42 import java.util.Locale;
43 import java.util.ResourceBundle;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47 import org.castor.xml.BackwardCompatibilityContext;
48 import org.castor.xml.InternalContext;
49 import org.exolab.castor.mapping.FieldDescriptor;
50 import org.exolab.castor.xml.location.XPathLocation;
51 import org.exolab.castor.xml.validators.ClassValidator;
52
53
54
55
56
57
58
59
60 public class Validator implements ClassValidator {
61 private static final Log LOG = LogFactory.getLog(Validator.class);
62
63
64 protected static ResourceBundle resourceBundle;
65
66 static {
67 resourceBundle = ResourceBundle.getBundle("ValidationMessages", Locale.getDefault());
68 }
69
70
71
72
73 public Validator() {
74 super();
75
76
77
78 }
79
80
81
82
83
84
85
86 public void validate(final Object object) throws ValidationException {
87 validate(object, (ValidationContext) null);
88 }
89
90
91
92
93
94
95
96
97 public void validate(final Object object, final ValidationContext context)
98 throws ValidationException {
99 if (object == null) {
100 throw new ValidationException(
101 resourceBundle.getString("validator.error.cannot.validate.null.object"));
102 }
103
104 if (context == null) {
105 ValidationContext v2 = new ValidationContext();
106 InternalContext ic = new BackwardCompatibilityContext();
107 ic.setClassLoader(object.getClass().getClassLoader());
108 v2.setInternalContext(ic);
109 validate(object, v2);
110 return;
111 }
112
113 if (context.getClassDescriptorResolver() == null) {
114 String message = resourceBundle.getString("validator.error.class.descriptor.resolver.null");
115 throw new IllegalStateException(message);
116 }
117
118 XMLClassDescriptor classDesc = null;
119
120 if (!MarshalFramework.isPrimitive(object.getClass())) {
121 try {
122 classDesc =
123 (XMLClassDescriptor) context.getClassDescriptorResolver().resolve(object.getClass());
124 } catch (ResolverException rx) {
125 throw new ValidationException(rx);
126 }
127 }
128
129
130 if (classDesc == null) {
131 return;
132 }
133
134 XMLFieldDescriptor fieldDesc = null;
135
136 try {
137 TypeValidator validator = classDesc.getValidator();
138 if (validator != null) {
139 validator.validate(object, context);
140 } else {
141
142 FieldDescriptor[] fields = classDesc.getFields();
143 if (fields != null) {
144 for (int i = 0; i < fields.length; i++) {
145 fieldDesc = (XMLFieldDescriptor) fields[i];
146 if (fieldDesc == null) {
147 continue;
148 }
149 FieldValidator fieldValidator = fieldDesc.getValidator();
150 if (fieldValidator != null) {
151 fieldValidator.validate(object, context);
152 }
153 }
154 }
155 }
156 } catch (ValidationException vx) {
157
158 XPathLocation loc = (XPathLocation) vx.getLocation();
159 if (loc == null) {
160 loc = new XPathLocation();
161 vx.setLocation(loc);
162 if (fieldDesc != null) {
163 if (fieldDesc.getNodeType() == NodeType.Attribute) {
164 loc.addAttribute(fieldDesc.getXMLName());
165 } else {
166 loc.addChild(fieldDesc.getXMLName());
167 }
168 }
169 }
170 if (classDesc.getXMLName() != null) {
171 loc.addParent(classDesc.getXMLName());
172 }
173 throw vx;
174 }
175
176
177
178 }
179
180 public void checkUnresolvedIdrefs(ValidationContext context) throws ValidationException {
181 if (!context.getUnresolvedIdRefs().isEmpty()) {
182 String err = MessageFormat.format(
183 resourceBundle.getString("validator.error.class.descriptor.resolver.null"),
184 new Object[] {context.getUnresolvedIdRefs().toString()});
185 throw new ValidationException(err);
186 }
187 }
188
189
190
191 }