1
2
3
4 package org.rcfaces.core.internal.tools;
5
6 import java.util.Map;
7
8 import javax.el.ELContext;
9 import javax.el.MethodExpression;
10 import javax.el.ValueExpression;
11 import javax.faces.FacesException;
12 import javax.faces.application.Application;
13 import javax.faces.component.UICommand;
14 import javax.faces.component.UIComponent;
15 import javax.faces.context.FacesContext;
16 import javax.faces.el.ValueBinding;
17 import javax.faces.event.ActionEvent;
18 import javax.faces.event.PhaseId;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.rcfaces.core.internal.capability.IVariableScopeCapability;
23 import org.rcfaces.core.internal.manager.ITransientAttributesManager;
24 import org.rcfaces.core.internal.tools.ComponentTools.IVarScope;
25
26
27
28
29
30
31 public class BindingTools {
32 private static final String REVISION = "$Revision: 1.6 $";
33
34 private static final Log LOG = LogFactory.getLog(BindingTools.class);
35
36 public static Object resolveBinding(FacesContext facesContext, Object object) {
37
38
39 if (object instanceof ValueBinding) {
40 if (facesContext == null) {
41 facesContext = FacesContext.getCurrentInstance();
42 }
43 object = ((ValueBinding) object).getValue(facesContext);
44
45 if (LOG.isDebugEnabled()) {
46 LOG.debug("Get value of binding => " + object);
47 }
48
49 return object;
50 }
51
52 if (object instanceof ValueExpression) {
53 if (facesContext == null) {
54 facesContext = FacesContext.getCurrentInstance();
55 }
56
57 object = ((ValueExpression) object).getValue(facesContext
58 .getELContext());
59
60 if (LOG.isDebugEnabled()) {
61 LOG.debug("Get value of binding => " + object);
62 }
63
64 return object;
65 }
66
67 if (LOG.isDebugEnabled()) {
68 LOG.debug("Unknown type of value => " + object);
69 }
70
71 return object;
72 }
73
74 public static boolean isBindingExpression(String value) {
75 if (value.length() < 4) {
76 return false;
77 }
78
79 int pos = value.indexOf("#{");
80
81 if (pos >= 0 && pos < value.indexOf('}', pos + 2)) {
82 return true;
83 }
84
85 return false;
86 }
87
88 public static Object evalBinding(FacesContext facesContext,
89 String expression, Class type) {
90
91 if (facesContext == null) {
92 facesContext = FacesContext.getCurrentInstance();
93 }
94
95 Application application = facesContext.getApplication();
96
97 ELContext elContext = facesContext.getELContext();
98
99 ValueExpression valueExpression = application.getExpressionFactory()
100 .createValueExpression(elContext, expression, type);
101
102 return valueExpression.getValue(elContext);
103 }
104
105 public static IVarScope processVariableScope(FacesContext facesContext,
106 IVariableScopeCapability variableScopeCapability, PhaseId phaseId) {
107 String var = variableScopeCapability.getScopeVar();
108 if (var == null || var.length() < 1) {
109 return null;
110 }
111
112 ITransientAttributesManager manager = (ITransientAttributesManager) variableScopeCapability;
113
114 if (false) {
115
116
117
118
119 Object ret = manager
120 .getTransientAttribute(ComponentTools.VARIABLE_SCOPE_VALUE);
121 if (ret != null) {
122 if (ret == ComponentTools.NONE_VARIABLE_SCOPE) {
123 ret = null;
124 }
125
126 Map requestMap = facesContext.getExternalContext()
127 .getRequestMap();
128
129 Object old = requestMap.put(var, ret);
130
131 return new ComponentTools.VarScope(var, old);
132 }
133 }
134
135 Object ret = variableScopeCapability.getScopeValue();
136
137 Map requestMap = facesContext.getExternalContext().getRequestMap();
138
139 if (false) {
140
141
142
143
144 if (ret == null) {
145 manager.setTransientAttribute(
146 ComponentTools.VARIABLE_SCOPE_VALUE,
147 ComponentTools.NONE_VARIABLE_SCOPE);
148
149 } else {
150 manager.setTransientAttribute(
151 ComponentTools.VARIABLE_SCOPE_VALUE, ret);
152 }
153 }
154
155 Object old = requestMap.put(var, ret);
156
157 if (LOG.isDebugEnabled()) {
158 LOG.debug("Process variable scope '" + var + "' for component '"
159 + ((UIComponent) variableScopeCapability).getId()
160 + "' phase='" + phaseId + "' oldValue='" + old
161 + "' newValue='" + ret + "'");
162 }
163
164 return new ComponentTools.VarScope(var, old);
165 }
166
167 public static void invokeActionListener(FacesContext facesContext,
168 UICommand component, ActionEvent actionEvent) {
169
170
171 MethodExpression me = component.getActionExpression();
172
173 if (LOG.isDebugEnabled()) {
174 LOG.debug("invokeAction on '" + component + "' actionEvent='"
175 + actionEvent + " => methodExpression='" + me + "'");
176 }
177
178 if (me != null) {
179 me
180 .invoke(facesContext.getELContext(),
181 new Object[] { actionEvent });
182 }
183 }
184
185 public static void setBoundValue(FacesContext context,
186 UIComponent component, String attribute, Object value) {
187
188 ValueExpression ve = component.getValueExpression(attribute);
189 if (ve == null) {
190 return;
191 }
192
193 try {
194 ve.setValue(context.getELContext(), value);
195
196 } catch (Exception ex) {
197 throw new FacesException("Can not set value", ex);
198 }
199 }
200 }