001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package javax.faces.component.html;
030:
031: import java.util.*;
032:
033: import javax.el.*;
034:
035: import javax.faces.*;
036: import javax.faces.application.*;
037: import javax.faces.context.*;
038: import javax.faces.convert.*;
039:
040: final class Util {
041: static Object eval(ValueExpression expr) {
042: try {
043: return expr.getValue(currentELContext());
044: } catch (ELException e) {
045: throw new FacesException(e);
046: }
047: }
048:
049: static boolean evalBoolean(ValueExpression expr) {
050: try {
051: return (Boolean) expr.getValue(currentELContext());
052: } catch (ELException e) {
053: throw new FacesException(e);
054: }
055: }
056:
057: static int evalInt(ValueExpression expr) {
058: try {
059: Object value = expr.getValue(currentELContext());
060:
061: if (value instanceof Number)
062: return ((Number) value).intValue();
063: else if (value == null)
064: return Integer.MIN_VALUE;
065: else
066: return (Integer) value;
067: } catch (ELException e) {
068: throw new FacesException(e);
069: }
070: }
071:
072: static String evalString(ValueExpression expr) {
073: try {
074: Object value = expr.getValue(currentELContext());
075:
076: if (value instanceof String || value == null)
077: return (String) value;
078: else
079: return value.toString();
080: } catch (ELException e) {
081: throw new FacesException(e);
082: }
083: }
084:
085: static String save(ValueExpression expr, FacesContext context) {
086: if (expr != null) {
087: return expr.getExpressionString();
088: } else
089: return null;
090: }
091:
092: static ValueExpression restoreBoolean(Object value,
093: FacesContext context) {
094: return restore(value, Boolean.class, context);
095: }
096:
097: static ValueExpression restoreString(Object value,
098: FacesContext context) {
099: return restore(value, String.class, context);
100: }
101:
102: static ValueExpression restoreInt(Object value, FacesContext context) {
103: return restore(value, Integer.class, context);
104: }
105:
106: static ValueExpression restore(Object value, Class type,
107: FacesContext context) {
108: if (value == null)
109: return null;
110:
111: String expr = (String) value;
112:
113: Application app = context.getApplication();
114: ExpressionFactory factory = app.getExpressionFactory();
115:
116: return factory.createValueExpression(context.getELContext(),
117: expr, type);
118: }
119:
120: static ELContext currentELContext() {
121: FacesContext facesContext = FacesContext.getCurrentInstance();
122:
123: if (facesContext != null)
124: return facesContext.getELContext();
125: else
126: return null;
127: }
128: }
|