KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > component > html > Util


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2
11  * as published by the Free Software Foundation.
12  *
13  * Resin Open Source is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
16  * of NON-INFRINGEMENT. See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Resin Open Source; if not, write to the
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package javax.faces.component.html;
30
31 import java.util.*;
32
33 import javax.el.*;
34
35 import javax.faces.*;
36 import javax.faces.application.*;
37 import javax.faces.context.*;
38 import javax.faces.convert.*;
39
40 final class Util
41 {
42   static Object JavaDoc eval(ValueExpression expr)
43   {
44     try {
45       return expr.getValue(currentELContext());
46     } catch (ELException e) {
47       throw new FacesException(e);
48     }
49   }
50   
51   static boolean evalBoolean(ValueExpression expr)
52   {
53     try {
54       return (Boolean JavaDoc) expr.getValue(currentELContext());
55     } catch (ELException e) {
56       throw new FacesException(e);
57     }
58   }
59   
60   static int evalInt(ValueExpression expr)
61   {
62     try {
63       return (Integer JavaDoc) expr.getValue(currentELContext());
64     } catch (ELException e) {
65       throw new FacesException(e);
66     }
67   }
68   
69   static String JavaDoc evalString(ValueExpression expr)
70   {
71     try {
72       return (String JavaDoc) expr.getValue(currentELContext());
73     } catch (ELException e) {
74       throw new FacesException(e);
75     }
76   }
77
78   static String JavaDoc save(ValueExpression expr,
79              FacesContext context)
80   {
81     if (expr != null)
82       return expr.getExpressionString();
83     else
84       return null;
85   }
86
87   static ValueExpression restore(Object JavaDoc value,
88                  Class JavaDoc type,
89                  FacesContext context)
90   {
91     if (value == null)
92       return null;
93
94     String JavaDoc expr = (String JavaDoc) value;
95
96     Application app = context.getApplication();
97     ExpressionFactory factory = app.getExpressionFactory();
98     
99     return factory.createValueExpression(context.getELContext(),
100                      expr,
101                      type);
102   }
103
104   static ELContext currentELContext()
105   {
106     FacesContext facesContext = FacesContext.getCurrentInstance();
107
108     if (facesContext != null)
109       return facesContext.getELContext();
110     else
111       return null;
112   }
113 }
114
Popular Tags