1 17 18 package org.objectweb.jac.aspects.integrity; 19 20 import java.util.Iterator ; 21 import org.apache.log4j.Logger; 22 import org.objectweb.jac.core.ACConfiguration; 23 import org.objectweb.jac.core.ObjectRepository; 24 import org.objectweb.jac.core.rtti.ClassItem; 25 import org.objectweb.jac.core.rtti.ClassRepository; 26 import org.objectweb.jac.core.rtti.FieldItem; 27 28 49 50 public class GenericConditions { 51 static final Logger logger = Logger.getLogger("integrity.conditions"); 52 53 56 private static boolean areEqual(Object obj1, Object obj2) 57 { 58 logger.debug("areEqual("+obj1+","+obj2+")"); 59 if(obj1 instanceof String && obj2 instanceof String ) { 60 return ((String ) obj1).equals((String ) obj2); 61 } 62 63 return (obj1 == obj2); 64 } 65 66 75 public static boolean forbiddenValues(Object substance, 76 FieldItem field, 77 Object value, 78 Object [] values) 79 { 80 for (int i=0; i<values.length; i++) 81 if (areEqual(value, values[i])) 82 return false; 83 return true; 84 } 85 86 96 public static boolean authorizedValues(Object substance, 97 FieldItem field, 98 Object value, 99 Object [] values) 100 { 101 for (int i=0; i<values.length; i++) 102 if (areEqual(value, values[i])) 103 return true; 104 return false; 105 } 106 107 116 public static boolean isUniqueValue(Object substance, 117 FieldItem field, 118 Object value, 119 Object [] values) 120 { 121 ClassItem cli = ClassRepository.get().getClass(substance); 122 Iterator it = ObjectRepository.getObjects(cli).iterator(); 123 return true; 124 } 125 126 134 public static boolean isBeginingWithUpperCaseChar(Object substance, 135 FieldItem field, 136 Object value, 137 Object [] values) 138 { 139 if (value instanceof String ) { 140 String s = (String )value; 141 if (s==null || s.length()==0 || 142 Character.isUpperCase(s.charAt(0))) 143 { 144 return true; 145 } else { 146 return false; 147 } 148 } 149 throw new RuntimeException ("Invalid constraint check: "+value+ 150 " is not a string"); 151 } 152 153 public static boolean isNotNull(Object substance, 154 FieldItem field, 155 Object value, 156 Object [] values) 157 { 158 return value != null; 159 } 160 161 public static boolean isNull(Object substance, 162 FieldItem field, 163 Object value, 164 Object [] values) 165 { 166 return value == null; 167 } 168 169 170 178 public static boolean isBeginingWithLetter(Object substance, 179 FieldItem field, 180 Object value, 181 Object [] values) 182 { 183 if (value instanceof String ) { 184 String s = (String )value; 185 if (s==null || s.length()==0 || 186 Character.isLetter(s.charAt(0))) 187 { 188 return true; 189 } else { 190 return false; 191 } 192 } 193 throw new RuntimeException ("Invalid constraint check: "+value+ 194 " is not a string"); 195 } 196 197 205 public static boolean isJavaIdentifier(Object substance, 206 FieldItem field, 207 Object value, 208 Object [] values) 209 { 210 if (value instanceof String ) { 211 String s=(String )value; 212 if (s==null) 213 return true; 214 if (s.length()==0) 215 return false; 216 if (!Character.isJavaIdentifierStart(s.charAt(0))) 217 return false; 218 for(int i=1;i<s.length(); i++) { 219 if(!Character.isJavaIdentifierPart(s.charAt(i))) 220 return false; 221 } 222 return true; 223 } 224 throw new RuntimeException ("Invalid constraint check: "+value+ 225 " is not a string"); 226 } 227 228 236 public static boolean isGreaterThan(Object substance, 237 FieldItem field, 238 Object value, 239 Object [] values) 240 throws Exception 241 { 242 if (value instanceof Number ) { 243 Number th = (Number )ACConfiguration.convertValue(values[0], 244 value.getClass()); 245 if (((Number )value).doubleValue()>th.doubleValue()) { 246 return true; 247 } else { 248 return false; 249 } 250 } 251 throw new RuntimeException ("Invalid constraint check: "+value+ 252 " is not a number"); 253 } 254 255 263 public static boolean isLowerThan(Object substance, 264 FieldItem field, 265 Object value, 266 Object [] values) 267 throws Exception 268 { 269 if (value instanceof Number ) { 270 Number th = (Number )ACConfiguration.convertValue(values[0], 271 value.getClass()); 272 if (((Number )value).doubleValue()<th.doubleValue()) { 273 return true; 274 } else { 275 return false; 276 } 277 } 278 throw new RuntimeException ("Invalid constraint check: "+value+ 279 " is not a number"); 280 } 281 282 } 283 | Popular Tags |