1 17 18 package org.apache.el.lang; 19 20 import java.beans.PropertyEditor ; 21 import java.beans.PropertyEditorManager ; 22 import java.math.BigDecimal ; 23 import java.math.BigInteger ; 24 25 import javax.el.ELException; 26 import javax.el.PropertyNotFoundException; 27 28 import org.apache.el.util.MessageFactory; 29 30 31 37 public class ELSupport { 38 39 private final static ELSupport REF = new ELSupport(); 40 41 private final static Long ZERO = new Long (0L); 42 43 public final static void throwUnhandled(Object base, Object property) 44 throws ELException { 45 if (base == null) { 46 throw new PropertyNotFoundException(MessageFactory.get( 47 "error.resolver.unhandled.null", property)); 48 } else { 49 throw new PropertyNotFoundException(MessageFactory.get( 50 "error.resolver.unhandled", base.getClass(), property)); 51 } 52 } 53 54 60 public final static int compare(final Object obj0, final Object obj1) 61 throws ELException { 62 if (obj0 == obj1 || equals(obj0, obj1)) { 63 return 0; 64 } 65 if (isBigDecimalOp(obj0, obj1)) { 66 BigDecimal bd0 = (BigDecimal ) coerceToNumber(obj0, BigDecimal .class); 67 BigDecimal bd1 = (BigDecimal ) coerceToNumber(obj1, BigDecimal .class); 68 return bd0.compareTo(bd1); 69 } 70 if (isDoubleOp(obj0, obj1)) { 71 Double d0 = (Double ) coerceToNumber(obj0, Double .class); 72 Double d1 = (Double ) coerceToNumber(obj1, Double .class); 73 return d0.compareTo(d1); 74 } 75 if (isBigIntegerOp(obj0, obj1)) { 76 BigInteger bi0 = (BigInteger ) coerceToNumber(obj0, BigInteger .class); 77 BigInteger bi1 = (BigInteger ) coerceToNumber(obj1, BigInteger .class); 78 return bi0.compareTo(bi1); 79 } 80 if (isLongOp(obj0, obj1)) { 81 Long l0 = (Long ) coerceToNumber(obj0, Long .class); 82 Long l1 = (Long ) coerceToNumber(obj1, Long .class); 83 return l0.compareTo(l1); 84 } 85 if (obj0 instanceof String || obj1 instanceof String ) { 86 return coerceToString(obj0).compareTo(coerceToString(obj1)); 87 } 88 if (obj0 instanceof Comparable ) { 89 return (obj1 != null) ? ((Comparable ) obj0).compareTo(obj1) : 1; 90 } 91 if (obj1 instanceof Comparable ) { 92 return (obj0 != null) ? -((Comparable ) obj1).compareTo(obj0) : -1; 93 } 94 throw new ELException(MessageFactory.get("error.compare", obj0, obj1)); 95 } 96 97 103 public final static boolean equals(final Object obj0, final Object obj1) 104 throws ELException { 105 if (obj0 == obj1) { 106 return true; 107 } else if (obj0 == null || obj1 == null) { 108 return false; 109 } else if (obj0 instanceof Boolean || obj1 instanceof Boolean ) { 110 return coerceToBoolean(obj0).equals(coerceToBoolean(obj1)); 111 } else if (obj0.getClass().isEnum()) { 112 return obj0.equals(coerceToEnum(obj1, obj0.getClass())); 113 } else if (obj1.getClass().isEnum()) { 114 return obj1.equals(coerceToEnum(obj0, obj1.getClass())); 115 } else if (obj0 instanceof String || obj1 instanceof String ) { 116 int lexCompare = coerceToString(obj0).compareTo(coerceToString(obj1)); 117 return (lexCompare == 0) ? true : false; 118 } 119 if (isBigDecimalOp(obj0, obj1)) { 120 BigDecimal bd0 = (BigDecimal ) coerceToNumber(obj0, BigDecimal .class); 121 BigDecimal bd1 = (BigDecimal ) coerceToNumber(obj1, BigDecimal .class); 122 return bd0.equals(bd1); 123 } 124 if (isDoubleOp(obj0, obj1)) { 125 Double d0 = (Double ) coerceToNumber(obj0, Double .class); 126 Double d1 = (Double ) coerceToNumber(obj1, Double .class); 127 return d0.equals(d1); 128 } 129 if (isBigIntegerOp(obj0, obj1)) { 130 BigInteger bi0 = (BigInteger ) coerceToNumber(obj0, BigInteger .class); 131 BigInteger bi1 = (BigInteger ) coerceToNumber(obj1, BigInteger .class); 132 return bi0.equals(bi1); 133 } 134 if (isLongOp(obj0, obj1)) { 135 Long l0 = (Long ) coerceToNumber(obj0, Long .class); 136 Long l1 = (Long ) coerceToNumber(obj1, Long .class); 137 return l0.equals(l1); 138 } else { 139 return obj0.equals(obj1); 140 } 141 } 142 143 148 public final static Enum coerceToEnum(final Object obj, Class type) { 149 if (obj == null || "".equals(obj)) { 150 return null; 151 } 152 if (obj.getClass().isEnum()) { 153 return (Enum ) obj; 154 } 155 return Enum.valueOf(type, obj.toString()); 156 } 157 158 162 public final static Boolean coerceToBoolean(final Object obj) 163 throws IllegalArgumentException { 164 if (obj == null || "".equals(obj)) { 165 return Boolean.FALSE; 166 } 167 if (obj instanceof Boolean || obj.getClass() == Boolean.TYPE) { 168 return (Boolean ) obj; 169 } 170 if (obj instanceof String ) { 171 return Boolean.valueOf((String ) obj); 172 } 173 174 throw new IllegalArgumentException (MessageFactory.get("error.convert", 175 obj, obj.getClass(), Boolean .class)); 176 } 177 178 public final static Character coerceToCharacter(final Object obj) 179 throws IllegalArgumentException { 180 if (obj == null || "".equals(obj)) { 181 return new Character ((char) 0); 182 } 183 if (obj instanceof String ) { 184 return new Character (((String ) obj).charAt(0)); 185 } 186 if (ELArithmetic.isNumber(obj)) { 187 return new Character ((char) ((Number ) obj).shortValue()); 188 } 189 Class objType = obj.getClass(); 190 if (obj instanceof Character || objType == Character.TYPE) { 191 return (Character ) obj; 192 } 193 194 throw new IllegalArgumentException (MessageFactory.get("error.convert", 195 obj, objType, Character .class)); 196 } 197 198 public final static Number coerceToNumber(final Object obj) { 199 if (obj == null) { 200 return ZERO; 201 } else if (obj instanceof Number ) { 202 return (Number ) obj; 203 } else { 204 String str = coerceToString(obj); 205 if (isStringFloat(str)) { 206 return toFloat(str); 207 } else { 208 return toNumber(str); 209 } 210 } 211 } 212 213 protected final static Number coerceToNumber(final Number number, 214 final Class type) throws IllegalArgumentException { 215 if (Long.TYPE == type || Long .class.equals(type)) { 216 return new Long (number.longValue()); 217 } 218 if (Double.TYPE == type || Double .class.equals(type)) { 219 return new Double (number.doubleValue()); 220 } 221 if (Integer.TYPE == type || Integer .class.equals(type)) { 222 return new Integer (number.intValue()); 223 } 224 if (BigInteger .class.equals(type)) { 225 if (number instanceof BigDecimal ) { 226 return ((BigDecimal ) number).toBigInteger(); 227 } 228 return BigInteger.valueOf(number.longValue()); 229 } 230 if (BigDecimal .class.equals(type)) { 231 if (number instanceof BigInteger ) { 232 return new BigDecimal ((BigInteger ) number); 233 } 234 return new BigDecimal (number.doubleValue()); 235 } 236 if (Byte.TYPE == type || Byte .class.equals(type)) { 237 return new Byte (number.byteValue()); 238 } 239 if (Short.TYPE == type || Short .class.equals(type)) { 240 return new Short (number.shortValue()); 241 } 242 if (Float.TYPE == type || Float .class.equals(type)) { 243 return new Float (number.floatValue()); 244 } 245 246 throw new IllegalArgumentException (MessageFactory.get("error.convert", 247 number, number.getClass(), type)); 248 } 249 250 public final static Number coerceToNumber(final Object obj, final Class type) 251 throws IllegalArgumentException { 252 if (obj == null || "".equals(obj)) { 253 return coerceToNumber(ZERO, type); 254 } 255 if (obj instanceof String ) { 256 return coerceToNumber((String ) obj, type); 257 } 258 if (ELArithmetic.isNumber(obj)) { 259 return coerceToNumber((Number ) obj, type); 260 } 261 262 Class objType = obj.getClass(); 263 if (Character .class.equals(objType) || Character.TYPE == objType) { 264 return coerceToNumber(new Short ((short) ((Character ) obj) 265 .charValue()), type); 266 } 267 268 throw new IllegalArgumentException (MessageFactory.get("error.convert", 269 obj, objType, type)); 270 } 271 272 protected final static Number coerceToNumber(final String val, 273 final Class type) throws IllegalArgumentException { 274 if (Long.TYPE == type || Long .class.equals(type)) { 275 return Long.valueOf(val); 276 } 277 if (Integer.TYPE == type || Integer .class.equals(type)) { 278 return Integer.valueOf(val); 279 } 280 if (Double.TYPE == type || Double .class.equals(type)) { 281 return Double.valueOf(val); 282 } 283 if (BigInteger .class.equals(type)) { 284 return new BigInteger (val); 285 } 286 if (BigDecimal .class.equals(type)) { 287 return new BigDecimal (val); 288 } 289 if (Byte.TYPE == type || Byte .class.equals(type)) { 290 return Byte.valueOf(val); 291 } 292 if (Short.TYPE == type || Short .class.equals(type)) { 293 return Short.valueOf(val); 294 } 295 if (Float.TYPE == type || Float .class.equals(type)) { 296 return Float.valueOf(val); 297 } 298 299 throw new IllegalArgumentException (MessageFactory.get("error.convert", 300 val, String .class, type)); 301 } 302 303 307 public final static String coerceToString(final Object obj) { 308 if (obj == null) { 309 return ""; 310 } else if (obj instanceof String ) { 311 return (String ) obj; 312 } else if (obj instanceof Enum ) { 313 return ((Enum ) obj).name(); 314 } else { 315 return obj.toString(); 316 } 317 } 318 319 public final static Object coerceToType(final Object obj, final Class type) 320 throws IllegalArgumentException { 321 if (type == null || Object .class.equals(type)) { 322 return obj; 323 } 324 if (String .class.equals(type)) { 325 return coerceToString(obj); 326 } 327 if (ELArithmetic.isNumberType(type)) { 328 return coerceToNumber(obj, type); 329 } 330 if (Character .class.equals(type) || Character.TYPE == type) { 331 return coerceToCharacter(obj); 332 } 333 if (Boolean .class.equals(type) || Boolean.TYPE == type) { 334 return coerceToBoolean(obj); 335 } 336 if (obj != null && type.isAssignableFrom(obj.getClass())) { 337 return obj; 338 } 339 if (type.isEnum()) { 340 return coerceToEnum(obj, type); 341 } 342 343 if (obj == null) 345 return null; 346 if (obj instanceof String ) { 347 if ("".equals(obj)) 348 return null; 349 PropertyEditor editor = PropertyEditorManager.findEditor(type); 350 if (editor != null) { 351 editor.setAsText((String ) obj); 352 return editor.getValue(); 353 } 354 } 355 throw new IllegalArgumentException (MessageFactory.get("error.convert", 356 obj, obj.getClass(), type)); 357 } 358 359 363 public final static boolean containsNulls(final Object [] obj) { 364 for (int i = 0; i < obj.length; i++) { 365 if (obj[0] == null) { 366 return true; 367 } 368 } 369 return false; 370 } 371 372 public final static boolean isBigDecimalOp(final Object obj0, 373 final Object obj1) { 374 return (obj0 instanceof BigDecimal || obj1 instanceof BigDecimal ); 375 } 376 377 public final static boolean isBigIntegerOp(final Object obj0, 378 final Object obj1) { 379 return (obj0 instanceof BigInteger || obj1 instanceof BigInteger ); 380 } 381 382 public final static boolean isDoubleOp(final Object obj0, final Object obj1) { 383 return (obj0 instanceof Double 384 || obj1 instanceof Double 385 || obj0 instanceof Float 386 || obj1 instanceof Float 387 || (obj0 != null && (Double.TYPE == obj0.getClass() || Float.TYPE == obj0 388 .getClass())) || (obj1 != null && (Double.TYPE == obj1 389 .getClass() || Float.TYPE == obj1.getClass()))); 390 } 391 392 public final static boolean isDoubleStringOp(final Object obj0, 393 final Object obj1) { 394 return (isDoubleOp(obj0, obj1) 395 || (obj0 instanceof String && isStringFloat((String ) obj0)) || (obj1 instanceof String && isStringFloat((String ) obj1))); 396 } 397 398 public final static boolean isLongOp(final Object obj0, final Object obj1) { 399 return (obj0 instanceof Long 400 || obj1 instanceof Long 401 || obj0 instanceof Integer 402 || obj1 instanceof Integer 403 || obj0 instanceof Character 404 || obj1 instanceof Character 405 || obj0 instanceof Short 406 || obj1 instanceof Short 407 || obj0 instanceof Byte 408 || obj1 instanceof Byte 409 || (obj0 != null && (Long.TYPE == obj0.getClass() 410 || Integer.TYPE == obj0.getClass() 411 || Character.TYPE == obj0.getClass() 412 || Short.TYPE == obj0.getClass() || Byte.TYPE == obj0 413 .getClass())) || (obj0 != null && (Long.TYPE == obj0 414 .getClass() 415 || Integer.TYPE == obj0.getClass() 416 || Character.TYPE == obj0.getClass() 417 || Short.TYPE == obj0.getClass() || Byte.TYPE == obj0 418 .getClass()))); 419 } 420 421 public final static boolean isStringFloat(final String str) { 422 int len = str.length(); 423 if (len > 1) { 424 char c = 0; 425 for (int i = 0; i < len; i++) { 426 switch (c = str.charAt(i)) { 427 case 'E': 428 return true; 429 case 'e': 430 return true; 431 case '.': 432 return true; 433 } 434 } 435 } 436 return false; 437 } 438 439 public final static Number toFloat(final String value) { 440 try { 441 if (Double.parseDouble(value) > Double.MAX_VALUE) { 442 return new BigDecimal (value); 443 } else { 444 return new Double (value); 445 } 446 } catch (NumberFormatException e0) { 447 return new BigDecimal (value); 448 } 449 } 450 451 public final static Number toNumber(final String value) { 452 try { 453 return new Integer (Integer.parseInt(value)); 454 } catch (NumberFormatException e0) { 455 try { 456 return new Long (Long.parseLong(value)); 457 } catch (NumberFormatException e1) { 458 return new BigInteger (value); 459 } 460 } 461 } 462 463 466 public ELSupport() { 467 super(); 468 } 469 470 } 471 | Popular Tags |