1 /*
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * This file was originally developed by Keith Visco during the
44 * course of employment at Intalio Inc.
45 * All portions of this file developed by Keith Visco after Jan 19 2005 are
46 * Copyright (C) 2005 Keith Visco. All Rights Reserved.
47 *
48 * $Id$
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 * A class which can perform Validation on an Object model. This class uses the
66 * ClassDescriptors and FieldDescriptors to perform the validation.
67 *
68 * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
69 * @version $Revision$ $Date: 2005-02-28 17:43:25 -0700 (Mon, 28 Feb 2005) $
70 */
71 public class Validator implements ClassValidator {
72 private static final Log LOG = LogFactory.getLog(Validator.class);
73
74 /** resource bundle */
75 protected static ResourceBundle resourceBundle;
76
77 static {
78 resourceBundle = ResourceBundle.getBundle("ValidationMessages", Locale
79 .getDefault());
80 }
81
82 /**
83 * Creates a new Validator.
84 */
85 public Validator() {
86 super();
87 //usage
88 //MessageFormat.format(rbm.getString(validator.cannot.validate.null.object), new Object[] {"Yes!!!", "is"});
89 } //-- Validator
90
91 /**
92 * Validates the given Object.
93 *
94 * @param object the Object to validate
95 * @throws ValidationException if validation fails.
96 */
97 public void validate(final Object object) throws ValidationException {
98 validate(object, (ValidationContext) null);
99 }
100
101 /**
102 * Validates the given Object.
103 *
104 * @param object the Object to validate
105 * @param context the ValidationContext to use during validation.
106 * @throws ValidationException if validation fails.
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 //-- we cannot validate an object if ClassDescriptor is null
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 // Default validation -- just validate each field
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 //-- add location information
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 // checkUnresolvedIdrefs(context);
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 // TODO: add cleanup life-cycle method to be called from outside
197
198 }