Convert an object to Boolean. Null and empty string are false. @param ctx the context in which this conversion is taking place @param obj the object to convert @param primitive is the target a primitive in which case coercion to null is not permitted @return the Boolean value of the ob
(final ELContext ctx, final Object obj, boolean primitive)
| 241 | * @throws ELException if object is not Boolean or String |
| 242 | */ |
| 243 | public static Boolean coerceToBoolean(final ELContext ctx, final Object obj, boolean primitive) throws ELException { |
| 244 | |
| 245 | if (ctx != null) { |
| 246 | boolean originalIsPropertyResolved = ctx.isPropertyResolved(); |
| 247 | try { |
| 248 | Boolean result = ctx.getELResolver().convertToType(ctx, obj, Boolean.class); |
| 249 | if (ctx.isPropertyResolved()) { |
| 250 | return result; |
| 251 | } |
| 252 | } finally { |
| 253 | ctx.setPropertyResolved(originalIsPropertyResolved); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (!COERCE_TO_ZERO && !primitive) { |
| 258 | if (obj == null) { |
| 259 | return null; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (obj == null || "".equals(obj)) { |
| 264 | return Boolean.FALSE; |
| 265 | } |
| 266 | if (obj instanceof Boolean) { |
| 267 | return (Boolean) obj; |
| 268 | } |
| 269 | if (obj instanceof String) { |
| 270 | return Boolean.valueOf((String) obj); |
| 271 | } |
| 272 | |
| 273 | throw new ELException(MessageFactory.get("error.convert", obj, obj.getClass(), Boolean.class)); |
| 274 | } |
| 275 | |
| 276 | private static Character coerceToCharacter(final ELContext ctx, final Object obj) throws ELException { |
| 277 |