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;
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 final Object eval(ValueExpression expr, FacesContext context) {
042: try {
043: return expr.getValue(context.getELContext());
044: } catch (ELException e) {
045: throw new FacesException(e);
046: }
047: }
048:
049: static final Boolean booleanValueOf(Object value) {
050: if (value == null)
051: return Boolean.FALSE;
052: else if (value instanceof Boolean)
053: return (Boolean) value;
054: else if (value instanceof String)
055: return ("true".equalsIgnoreCase((String) value) ? Boolean.TRUE
056: : Boolean.FALSE);
057: else
058: return Boolean.FALSE;
059: }
060:
061: static final boolean evalBoolean(ValueExpression expr,
062: FacesContext context) {
063: try {
064: Object value = expr.getValue(context.getELContext());
065:
066: if (value == null)
067: return false;
068: else if (value instanceof Boolean)
069: return ((Boolean) value).booleanValue();
070: else if (value instanceof String)
071: return "true".equalsIgnoreCase((String) value);
072: else
073: return false;
074: } catch (ELException e) {
075: throw new FacesException(e);
076: }
077: }
078:
079: static final int evalInt(ValueExpression expr, FacesContext context) {
080: try {
081: return (Integer) expr.getValue(context.getELContext());
082: } catch (ELException e) {
083: throw new FacesException(e);
084: }
085: }
086:
087: static final String evalString(ValueExpression expr,
088: FacesContext context) {
089: try {
090: return (String) expr.getValue(context.getELContext());
091: } catch (ELException e) {
092: throw new FacesException(e);
093: }
094: }
095:
096: static String save(ValueExpression expr, FacesContext context) {
097: if (expr != null)
098: return expr.getExpressionString();
099: else
100: return null;
101: }
102:
103: static Object saveWithType(ValueExpression expr,
104: FacesContext context) {
105: if (expr != null) {
106: return new Object[] { expr.getExpressionString(),
107: expr.getExpectedType() };
108: } else
109: return null;
110: }
111:
112: static ValueExpression restoreWithType(Object value,
113: FacesContext context) {
114: if (value == null)
115: return null;
116:
117: Object[] state = (Object[]) value;
118:
119: String expr = (String) state[0];
120: Class type = (Class) state[1];
121:
122: Application app = context.getApplication();
123: ExpressionFactory factory = app.getExpressionFactory();
124:
125: return factory.createValueExpression(context.getELContext(),
126: expr, type);
127: }
128:
129: static ValueExpression restoreBoolean(Object value,
130: FacesContext context) {
131: return restore(value, Boolean.class, context);
132: }
133:
134: static ValueExpression restoreString(Object value,
135: FacesContext context) {
136: return restore(value, String.class, context);
137: }
138:
139: static ValueExpression restore(Object value, Class type,
140: FacesContext context) {
141: if (value == null)
142: return null;
143:
144: String expr = (String) value;
145:
146: Application app = context.getApplication();
147: ExpressionFactory factory = app.getExpressionFactory();
148:
149: return factory.createValueExpression(context.getELContext(),
150: expr, type);
151: }
152:
153: public static String l10n(FacesContext context, String id,
154: String defaultMessage, Object... args) {
155: String message = getMessage(context, id, defaultMessage);
156:
157: StringBuilder sb = new StringBuilder();
158:
159: int len = message.length();
160: for (int i = 0; i < len; i++) {
161: char ch = message.charAt(i);
162:
163: if (ch == '{' && i + 2 < len
164: && '0' <= message.charAt(i + 1)
165: && message.charAt(i + 1) <= '9'
166: && message.charAt(i + 2) == '}') {
167: int index = message.charAt(i + 1) - '0';
168:
169: if (index < args.length)
170: sb.append(args[index]);
171:
172: i += 2;
173: } else
174: sb.append(ch);
175: }
176:
177: return sb.toString();
178: }
179:
180: public static String getLabel(FacesContext context,
181: UIComponent component) {
182: String label = (String) component.getAttributes().get("label");
183:
184: if (label != null && !"".equals(label))
185: return label;
186: else
187: return component.getClientId(context);
188: }
189:
190: private static String getMessage(FacesContext context,
191: String messageId, String defaultMessage) {
192: Application app = context.getApplication();
193:
194: String bundleName = app.getMessageBundle();
195:
196: if (bundleName == null)
197: return defaultMessage;
198:
199: ResourceBundle bundle = app.getResourceBundle(context,
200: bundleName);
201:
202: if (bundle == null)
203: return defaultMessage;
204:
205: String msg = bundle.getString(messageId);
206:
207: if (msg != null)
208: return msg;
209: else
210: return defaultMessage;
211: }
212: }
|