1 19 20 package org.apache.cayenne.validation; 21 22 import java.util.Collection ; 23 24 import org.apache.cayenne.CayenneRuntimeException; 25 import org.apache.cayenne.reflect.PropertyUtils; 26 27 35 public class BeanValidationFailure extends SimpleValidationFailure { 36 37 protected String property; 38 39 private static String validationMessage(String attribute, String message) { 40 StringBuffer buffer = new StringBuffer (message.length() + attribute.length() + 5); 41 buffer.append('\"').append(attribute).append("\" ").append(message); 42 return buffer.toString(); 43 } 44 45 49 public static ValidationFailure validateNotEmpty( 50 Object bean, 51 String attribute, 52 Collection 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 bean, 71 String attribute, 72 Object value) { 73 74 if (value instanceof String ) { 75 return validateNotEmpty(bean, attribute, (String ) value); 76 } 77 if (value instanceof Collection ) { 78 return validateNotEmpty(bean, attribute, (Collection ) value); 79 } 80 return validateNotNull(bean, attribute, value); 81 } 82 83 public static ValidationFailure validateMandatory(Object bean, String attribute) { 84 if (bean == null) { 85 throw new NullPointerException ("Null bean."); 86 } 87 88 try { 89 Object result = PropertyUtils.getProperty(bean, attribute); 90 return validateMandatory(bean, attribute, result); 91 } 92 catch (Exception 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 bean, 102 String attribute, 103 Object 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 118 public static ValidationFailure validateNotEmpty( 119 Object bean, 120 String attribute, 121 String 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 140 public static ValidationFailure validateJavaClassName( 141 Object bean, 142 String attribute, 143 String 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 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 192 public BeanValidationFailure(Object source, String property, Object error) { 193 super(source, error); 194 195 if (source == null && property != null) { 196 throw new IllegalArgumentException ( 197 "ValidationFailure cannot have 'property' when 'source' is null."); 198 } 199 200 this.property = property; 201 } 202 203 206 public String getProperty() { 207 return property; 208 } 209 210 213 public String toString() { 214 StringBuffer buffer = new StringBuffer (); 215 216 buffer.append("Validation failure for "); 217 Object source = getSource(); 218 219 if (source == null) { 220 buffer.append("[General]"); 221 } 222 else { 223 String 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 |