1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.lang;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.el.ELException;
import javax.el.PropertyNotFoundException;
import org.apache.el.util.MessageFactory;
/**
* A helper class that implements the EL Specification
*
* @author Jacob Hookom [jacob@hookom.net]
* @version $Id: ELSupport.java 939519 2010-04-30 00:12:23Z kkolinko $
*/
public class ELSupport {
private final static Long ZERO = new Long(0L);
public final static void throwUnhandled(Object base, Object property)
throws ELException {
if (base == null) {
throw new PropertyNotFoundException(MessageFactory.get(
"error.resolver.unhandled.null", property));
} else {
throw new PropertyNotFoundException(MessageFactory.get(
"error.resolver.unhandled", base.getClass(), property));
}
}
/**
* @param obj0
* @param obj1
* @return
* @throws EvaluationException
*/
public final static int compare(final Object obj0, final Object obj1)
throws ELException {
if (obj0 == obj1 || equals(obj0, obj1)) {
return 0;
}
if (isBigDecimalOp(obj0, obj1)) {
BigDecimal bd0 = (BigDecimal) coerceToNumber(obj0, BigDecimal.class);
BigDecimal bd1 = (BigDecimal) coerceToNumber(obj1, BigDecimal.class);
return bd0.compareTo(bd1);
}
if (isDoubleOp(obj0, obj1)) {
Double d0 = (Double) coerceToNumber(obj0, Double.class);
Double d1 = (Double) coerceToNumber(obj1, Double.class);
return d0.compareTo(d1);
}
if (isBigIntegerOp(obj0, obj1)) {
BigInteger bi0 = (BigInteger) coerceToNumber(obj0, BigInteger.class);
BigInteger bi1 = (BigInteger) coerceToNumber(obj1, BigInteger.class);
return bi0.compareTo(bi1);
}
if (isLongOp(obj0, obj1)) {
Long l0 = (Long) coerceToNumber(obj0, Long.class);
Long l1 = (Long) coerceToNumber(obj1, Long.class);
return l0.compareTo(l1);
}
if (obj0 instanceof String || obj1 instanceof String) {
return coerceToString(obj0).compareTo(coerceToString(obj1));
}
if (obj0 instanceof Comparable) {
return (obj1 != null) ? ((Comparable) obj0).compareTo(obj1) : 1;
}
if (obj1 instanceof Comparable) {
return (obj0 != null) ? -((Comparable) obj1).compareTo(obj0) : -1;
}
throw new ELException(MessageFactory.get("error.compare", obj0, obj1));
}
/**
* @param obj0
* @param obj1
* @return
* @throws EvaluationException
*/
public final static boolean equals(final Object obj0, final Object obj1)
throws ELException {
if (obj0 == obj1) {
return true;
} else if (obj0 == null || obj1 == null) {
return false;
} else if (obj0 instanceof Boolean || obj1 instanceof Boolean) {
return coerceToBoolean(obj0).equals(coerceToBoolean(obj1));
} else if (obj0.getClass().isEnum()) {
return obj0.equals(coerceToEnum(obj1, obj0.getClass()));
} else if (obj1.getClass().isEnum()) {
return obj1.equals(coerceToEnum(obj0, obj1.getClass()));
} else if (obj0 instanceof String || obj1 instanceof String) {
int lexCompare = coerceToString(obj0).compareTo(coerceToString(obj1));
return (lexCompare == 0) ? true : false;
}
if (isBigDecimalOp(obj0, obj1)) {
BigDecimal bd0 = (BigDecimal) coerceToNumber(obj0, BigDecimal.class);
BigDecimal bd1 = (BigDecimal) coerceToNumber(obj1, BigDecimal.class);
return bd0.equals(bd1);
}
if (isDoubleOp(obj0, obj1)) {
Double d0 = (Double) coerceToNumber(obj0, Double.class);
Double d1 = (Double) coerceToNumber(obj1, Double.class);
return d0.equals(d1);
}
if (isBigIntegerOp(obj0, obj1)) {
BigInteger bi0 = (BigInteger) coerceToNumber(obj0, BigInteger.class);
BigInteger bi1 = (BigInteger) coerceToNumber(obj1, BigInteger.class);
return bi0.equals(bi1);
}
if (isLongOp(obj0, obj1)) {
Long l0 = (Long) coerceToNumber(obj0, Long.class);
Long l1 = (Long) coerceToNumber(obj1, Long.class);
return l0.equals(l1);
} else {
return obj0.equals(obj1);
}
}
/**
* @param obj
* @param type
* @return
*/
public final static Enum<?> coerceToEnum(final Object obj, Class type) {
if (obj == null || "".equals(obj)) {
return null;
}
if (type.isAssignableFrom(obj.getClass())) {
return (Enum) obj;
}
if (!(obj instanceof String)) {
throw new ELException(MessageFactory.get("error.convert",
obj, obj.getClass(), type));
}
Enum<?> result;
try {
result = Enum.valueOf(type, (String) obj);
} catch (IllegalArgumentException iae) {
throw new ELException(MessageFactory.get("error.convert",
obj, obj.getClass(), type));
}
return result;
}
/**
* Convert an object to Boolean.
* Null and empty string are false.
* @param obj the object to convert
* @return the Boolean value of the object
* @throws ELException if object is not Boolean or String
*/
public final static Boolean coerceToBoolean(final Object obj)
throws ELException {
if (obj == null || "".equals(obj)) {
return Boolean.FALSE;
}
if (obj instanceof Boolean) {
return (Boolean) obj;
}
if (obj instanceof String) {
return Boolean.valueOf((String) obj);
}
throw new ELException(MessageFactory.get("error.convert",
obj, obj.getClass(), Boolean.class));
}
public final static Character coerceToCharacter(final Object obj)
throws ELException {
if (obj == null || "".equals(obj)) {
return new Character((char) 0);
}
if (obj instanceof String) {
return new Character(((String) obj).charAt(0));
}
if (ELArithmetic.isNumber(obj)) {
return new Character((char) ((Number) obj).shortValue());
}
Class objType = obj.getClass();
if (obj instanceof Character) {
return (Character) obj;
}
throw new ELException(MessageFactory.get("error.convert",
obj, objType, Character.class));
}
public final static Number coerceToNumber(final Object obj) {
if (obj == null) {
return ZERO;
} else if (obj instanceof Number) {
return (Number) obj;
} else {
String str = coerceToString(obj);
if (isStringFloat(str)) {
return toFloat(str);
} else {
return toNumber(str);
}
}
}
protected final static Number coerceToNumber(final Number number,
final Class<?> type) throws ELException {
if (Long.TYPE == type || Long.class.equals(type)) {
return new Long(number.longValue());
}
if (Double.TYPE == type || Double.class.equals(type)) {
return new Double(number.doubleValue());
}
if (Integer.TYPE == type || Integer.class.equals(type)) {
return new Integer(number.intValue());
}
if (BigInteger.class.equals(type)) {
if (number instanceof BigDecimal) {
return ((BigDecimal) number).toBigInteger();
}
if (number instanceof BigInteger) {
return number;
}
return BigInteger.valueOf(number.longValue());
}
if (BigDecimal.class.equals(type)) {
if (number instanceof BigDecimal) {
return number;
}
if (number instanceof BigInteger) {
return new BigDecimal((BigInteger) number);
}
return new BigDecimal(number.doubleValue());
}
if (Byte.TYPE == type || Byte.class.equals(type)) {
return new Byte(number.byteValue());
}
if (Short.TYPE == type || Short.class.equals(type)) {
return new Short(number.shortValue());
}
if (Float.TYPE == type || Float.class.equals(type)) {
return new Float(number.floatValue());
}
if (Number.class.equals(type)) {
return number;
}
throw new ELException(MessageFactory.get("error.convert",
number, number.getClass(), type));
}
public final static Number coerceToNumber(final Object obj,
final Class<?> type) throws ELException {
if (obj == null || "".equals(obj)) {
return coerceToNumber(ZERO, type);
}
if (obj instanceof String) {
return coerceToNumber((String) obj, type);
}
if (ELArithmetic.isNumber(obj)) {
return coerceToNumber((Number) obj, type);
}
if (obj instanceof Character) {
return coerceToNumber(new Short((short) ((Character) obj)
.charValue()), type);
}
throw new ELException(MessageFactory.get("error.convert",
obj, obj.getClass(), type));
}
protected final static Number coerceToNumber(final String val,
final Class<?> type) throws ELException {
if (Long.TYPE == type || Long.class.equals(type)) {
try {
return Long.valueOf(val);
} catch (NumberFormatException nfe) {
throw new ELException(MessageFactory.get("error.convert",
val, String.class, type));
}
}
if (Integer.TYPE == type || Integer.class.equals(type)) {
try {
return Integer.valueOf(val);
} catch (NumberFormatException nfe) {
throw new ELException(MessageFactory.get("error.convert",
val, String.class, type));
}
}
if (Double.TYPE == type || Double.class.equals(type)) {
try {
return Double.valueOf(val);
} catch (NumberFormatException nfe) {
throw new ELException(MessageFactory.get("error.convert",
val, String.class, type));
}
}
if (BigInteger.class.equals(type)) {
try {
return new BigInteger(val);
} catch (NumberFormatException nfe) {
throw new ELException(MessageFactory.get("error.convert",
val, String.class, type));
}
}
if (BigDecimal.class.equals(type)) {
try {
return new BigDecimal(val);
} catch (NumberFormatException nfe) {
throw new ELException(MessageFactory.get("error.convert",
val, String.class, type));
}
}
if (Byte.TYPE == type || Byte.class.equals(type)) {
try {
return Byte.valueOf(val);
} catch (NumberFormatException nfe) {
throw new ELException(MessageFactory.get("error.convert",
val, String.class, type));
}
}
if (Short.TYPE == type || Short.class.equals(type)) {
try {
return Short.valueOf(val);
} catch (NumberFormatException nfe) {
throw new ELException(MessageFactory.get("error.convert",
val, String.class, type));
}
}
if (Float.TYPE == type || Float.class.equals(type)) {
try {
return Float.valueOf(val);
} catch (NumberFormatException nfe) {
throw new ELException(MessageFactory.get("error.convert",
val, String.class, type));
}
}
throw new ELException(MessageFactory.get("error.convert",
val, String.class, type));
}
/**
* @param obj
* @return
*/
public final static String coerceToString(final Object obj) {
if (obj == null) {
return "";
} else if (obj instanceof String) {
return (String) obj;
} else if (obj instanceof Enum) {
return ((Enum) obj).name();
} else {
return obj.toString();
}
}
public final static void checkType(final Object obj, final Class type)
throws ELException {
if (String.class.equals(type)) {
coerceToString(obj);
}
if (ELArithmetic.isNumberType(type)) {
coerceToNumber(obj, type);
}
if (Character.class.equals(type) || Character.TYPE == type) {
coerceToCharacter(obj);
}
if (Boolean.class.equals(type) || Boolean.TYPE == type) {
coerceToBoolean(obj);
}
if (type.isEnum()) {
coerceToEnum(obj, type);
}
}
public final static Object coerceToType(final Object obj,
final Class<?> type) throws ELException {
if (type == null || Object.class.equals(type) ||
(obj != null && type.isAssignableFrom(obj.getClass()))) {
return obj;
}
if (String.class.equals(type)) {
return coerceToString(obj);
}
if (ELArithmetic.isNumberType(type)) {
return coerceToNumber(obj, type);
}
if (Character.class.equals(type) || Character.TYPE == type) {
return coerceToCharacter(obj);
}
if (Boolean.class.equals(type) || Boolean.TYPE == type) {
return coerceToBoolean(obj);
}
if (type.isEnum()) {
return coerceToEnum(obj, type);
}
// new to spec
if (obj == null)
return null;
if (obj instanceof String) {
if ("".equals(obj))
return null;
PropertyEditor editor = PropertyEditorManager.findEditor(type);
if (editor != null) {
editor.setAsText((String) obj);
return editor.getValue();
}
}
throw new ELException(MessageFactory.get("error.convert",
obj, obj.getClass(), type));
}
/**
* @param obj
* @return
*/
public final static boolean containsNulls(final Object[] obj) {
for (int i = 0; i < obj.length; i++) {
if (obj[0] == null) {
return true;
}
}
return false;
}
public final static boolean isBigDecimalOp(final Object obj0,
final Object obj1) {
return (obj0 instanceof BigDecimal || obj1 instanceof BigDecimal);
}
public final static boolean isBigIntegerOp(final Object obj0,
final Object obj1) {
return (obj0 instanceof BigInteger || obj1 instanceof BigInteger);
}
public final static boolean isDoubleOp(final Object obj0, final Object obj1) {
return (obj0 instanceof Double
|| obj1 instanceof Double
|| obj0 instanceof Float
|| obj1 instanceof Float);
}
public final static boolean isDoubleStringOp(final Object obj0,
final Object obj1) {
return (isDoubleOp(obj0, obj1)
|| (obj0 instanceof String && isStringFloat((String) obj0)) || (obj1 instanceof String && isStringFloat((String) obj1)));
}
public final static boolean isLongOp(final Object obj0, final Object obj1) {
return (obj0 instanceof Long
|| obj1 instanceof Long
|| obj0 instanceof Integer
|| obj1 instanceof Integer
|| obj0 instanceof Character
|| obj1 instanceof Character
|| obj0 instanceof Short
|| obj1 instanceof Short
|| obj0 instanceof Byte
|| obj1 instanceof Byte);
}
public final static boolean isStringFloat(final String str) {
int len = str.length();
if (len > 1) {
for (int i = 0; i < len; i++) {
switch (str.charAt(i)) {
case 'E':
return true;
case 'e':
return true;
case '.':
return true;
}
}
}
return false;
}
public final static Number toFloat(final String value) {
try {
if (Double.parseDouble(value) > Double.MAX_VALUE) {
return new BigDecimal(value);
} else {
return new Double(value);
}
} catch (NumberFormatException e0) {
return new BigDecimal(value);
}
}
public final static Number toNumber(final String value) {
try {
return new Integer(Integer.parseInt(value));
} catch (NumberFormatException e0) {
try {
return new Long(Long.parseLong(value));
} catch (NumberFormatException e1) {
return new BigInteger(value);
}
}
}
/**
*
*/
public ELSupport() {
super();
}
}
|