KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > validation > BeanValidationFailure


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.validation;
21
22 import java.util.Collection JavaDoc;
23
24 import org.apache.cayenne.CayenneRuntimeException;
25 import org.apache.cayenne.reflect.PropertyUtils;
26
27 /**
28  * ValidationFailure implementation that described a failure of a single named property of
29  * a Java Bean object.
30  *
31  * @author Fabricio Voznika
32  * @author Andrus Adamchik
33  * @since 1.1
34  */

35 public class BeanValidationFailure extends SimpleValidationFailure {
36
37     protected String JavaDoc property;
38
39     private static String JavaDoc validationMessage(String JavaDoc attribute, String JavaDoc message) {
40         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(message.length() + attribute.length() + 5);
41         buffer.append('\"').append(attribute).append("\" ").append(message);
42         return buffer.toString();
43     }
44
45     /**
46      * Returns a ValidationFailure if a collection attribute of an object is null or
47      * empty.
48      */

49     public static ValidationFailure validateNotEmpty(
50             Object JavaDoc bean,
51             String JavaDoc attribute,
52             Collection JavaDoc value) {
53
54         if (value == null) {
55             return new BeanValidationFailure(bean, attribute, validationMessage(
56                     attribute,
57                     " is required."));
58         }
59
60         if (value.isEmpty()) {
61             return new BeanValidationFailure(bean, attribute, validationMessage(
62                     attribute,
63                     " can not be empty."));
64         }
65
66         return null;
67     }
68
69     public static ValidationFailure validateMandatory(
70             Object JavaDoc bean,
71             String JavaDoc attribute,
72             Object JavaDoc value) {
73
74         if (value instanceof String JavaDoc) {
75             return validateNotEmpty(bean, attribute, (String JavaDoc) value);
76         }
77         if (value instanceof Collection JavaDoc) {
78             return validateNotEmpty(bean, attribute, (Collection JavaDoc) value);
79         }
80         return validateNotNull(bean, attribute, value);
81     }
82
83     public static ValidationFailure validateMandatory(Object JavaDoc bean, String JavaDoc attribute) {
84         if (bean == null) {
85             throw new NullPointerException JavaDoc("Null bean.");
86         }
87
88         try {
89             Object JavaDoc result = PropertyUtils.getProperty(bean, attribute);
90             return validateMandatory(bean, attribute, result);
91         }
92         catch (Exception JavaDoc ex) {
93             throw new CayenneRuntimeException("Error validationg bean property: "
94                     + bean.getClass().getName()
95                     + "."
96                     + attribute, ex);
97         }
98     }
99
100     public static ValidationFailure validateNotNull(
101             Object JavaDoc bean,
102             String JavaDoc attribute,
103             Object JavaDoc value) {
104
105         if (value == null) {
106             return new BeanValidationFailure(bean, attribute, validationMessage(
107                     attribute,
108                     " is required."));
109         }
110
111         return null;
112     }
113
114     /**
115      * A utility method that returns a ValidationFailure if a string is either null or has
116      * a length of zero; otherwise returns null.
117      */

118     public static ValidationFailure validateNotEmpty(
119             Object JavaDoc bean,
120             String JavaDoc attribute,
121             String JavaDoc value) {
122
123         if (value == null || value.length() == 0) {
124             return new BeanValidationFailure(bean, attribute, validationMessage(
125                     attribute,
126                     " is a required field."));
127         }
128         return null;
129     }
130
131     /**
132      * A utility method that checks that a given string is a valid Java full class name,
133      * returning a non-null ValidationFailure if this is not so.
134      *
135      * Special case: primitive arrays like byte[] are also handled as a valid java
136      * class name.
137      *
138      * @since 1.2
139      */

140     public static ValidationFailure validateJavaClassName(
141             Object JavaDoc bean,
142             String JavaDoc attribute,
143             String JavaDoc identifier) {
144
145         ValidationFailure emptyFailure = validateNotEmpty(bean, attribute, identifier);
146         if (emptyFailure != null) {
147             return emptyFailure;
148         }
149
150         char c = identifier.charAt(0);
151         if (!Character.isJavaIdentifierStart(c)) {
152             return new BeanValidationFailure(bean, attribute, validationMessage(
153                     attribute,
154                     " starts with invalid character: " + c));
155         }
156
157         // handle arrays
158
if (identifier.endsWith("[]")) {
159             identifier = identifier.substring(0, identifier.length() - 2);
160         }
161
162         boolean wasDot = false;
163         for (int i = 1; i < identifier.length(); i++) {
164             c = identifier.charAt(i);
165
166             if (c == '.') {
167                 if (wasDot || i + 1 == identifier.length()) {
168                     return new BeanValidationFailure(bean, attribute, validationMessage(
169                             attribute,
170                             " is not a valid Java Class Name: " + identifier));
171                 }
172
173                 wasDot = true;
174                 continue;
175             }
176
177             if (!Character.isJavaIdentifierPart(c)) {
178                 return new BeanValidationFailure(bean, attribute, validationMessage(
179                         attribute,
180                         " contains invalid character: " + c));
181             }
182
183             wasDot = false;
184         }
185
186         return null;
187     }
188
189     /**
190      * Creates new BeanValidationFailure.
191      */

192     public BeanValidationFailure(Object JavaDoc source, String JavaDoc property, Object JavaDoc error) {
193         super(source, error);
194
195         if (source == null && property != null) {
196             throw new IllegalArgumentException JavaDoc(
197                     "ValidationFailure cannot have 'property' when 'source' is null.");
198         }
199
200         this.property = property;
201     }
202
203     /**
204      * Returns a failed property of the failure source object.
205      */

206     public String JavaDoc getProperty() {
207         return property;
208     }
209
210     /**
211      * Returns a String representation of the failure.
212      */

213     public String JavaDoc toString() {
214         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
215
216         buffer.append("Validation failure for ");
217         Object JavaDoc source = getSource();
218
219         if (source == null) {
220             buffer.append("[General]");
221         }
222         else {
223             String JavaDoc property = getProperty();
224             buffer.append(source.getClass().getName()).append('.').append(
225                     (property == null ? "[General]" : property));
226         }
227         buffer.append(": ");
228         buffer.append(getDescription());
229         return buffer.toString();
230     }
231 }
232
Popular Tags