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