001 package org.apache.myfaces.tobago.compat;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 import org.apache.myfaces.tobago.event.TabChangeSource;
021 import org.apache.myfaces.tobago.event.ValueBindingPopupActionListener;
022 import org.apache.myfaces.tobago.event.ValueBindingTabChangeListener;
023 import org.apache.myfaces.tobago.util.FacesVersion;
024 import org.apache.myfaces.tobago.util.ValueBindingComparator;
025 import org.slf4j.Logger;
026 import org.slf4j.LoggerFactory;
027
028 import javax.faces.component.ActionSource;
029 import javax.faces.component.ContextCallback;
030 import javax.faces.component.EditableValueHolder;
031 import javax.faces.component.NamingContainer;
032 import javax.faces.component.UIComponent;
033 import javax.faces.component.ValueHolder;
034 import javax.faces.context.FacesContext;
035 import javax.faces.convert.Converter;
036 import javax.faces.el.EvaluationException;
037 import javax.faces.el.MethodBinding;
038 import javax.faces.el.ValueBinding;
039 import javax.faces.event.AbortProcessingException;
040 import javax.faces.event.FacesEvent;
041 import javax.faces.webapp.UIComponentTag;
042 import java.util.Comparator;
043
044 @SuppressWarnings("deprecation")
045 public class FacesUtils {
046
047 private static final Logger LOG = LoggerFactory.getLogger(FacesUtilsEL.class);
048
049 public static final Class[] VALIDATOR_ARGS = {FacesContext.class, UIComponent.class, Object.class};
050
051 private static final boolean USE_BINDING = !FacesVersion.supports12();
052
053 public static boolean invokeOnComponent(
054 FacesContext context, UIComponent component, String clientId, ContextCallback callback) {
055 String thisClientId = component.getClientId(context);
056 if (USE_BINDING) {
057 if (clientId.equals(thisClientId)) {
058 callback.invokeContextCallback(context, component);
059 return true;
060 } else if (component instanceof NamingContainer) {
061 // This component is a naming container. If the client id shows it's inside this naming container,
062 // then process further.
063 // Otherwise we know the client id we're looking for is not in this naming container,
064 // so for improved performance short circuit and return false.
065 if (clientId.startsWith(thisClientId)
066 && (clientId.charAt(thisClientId.length()) == NamingContainer.SEPARATOR_CHAR)) {
067 if (invokeOnComponentFacetsAndChildren(context, component, clientId, callback)) {
068 return true;
069 }
070 }
071 } else {
072 if (invokeOnComponentFacetsAndChildren(context, component, clientId, callback)) {
073 return true;
074 }
075 }
076 return false;
077 } else {
078 return FacesInvokeOnComponent12.invokeOnComponent(context, component, clientId, callback);
079 }
080 }
081
082 private static boolean invokeOnComponentFacetsAndChildren(
083 FacesContext context, UIComponent component, String clientId, ContextCallback callback) {
084 for (java.util.Iterator<UIComponent> it = component.getFacetsAndChildren(); it.hasNext();) {
085 UIComponent child = it.next();
086
087 if (child instanceof InvokeOnComponent) {
088 if (LOG.isDebugEnabled()) {
089 LOG.debug("Found InvokeOnComponent with clientId " + child.getClientId(context));
090 }
091 if (((InvokeOnComponent) child).invokeOnComponent(context, clientId, callback)) {
092 return true;
093 }
094 } else {
095 if (LOG.isDebugEnabled()) {
096 LOG.debug("Did not found InvokeOnComponent " + child.getClass().getName() + " "
097 + child.getClientId(context) + " " + child.getRendererType()
098 + (child.getParent() != null ? child.getParent().getClass().getName() : "null"));
099 }
100 }
101 }
102 return false;
103 }
104
105 public static void invokeMethodBinding(FacesContext facesContext, MethodBinding methodBinding, FacesEvent event) {
106 if (methodBinding != null && event != null) {
107 try {
108 methodBinding.invoke(facesContext, new Object[]{event});
109 } catch (EvaluationException e) {
110 Throwable cause = e.getCause();
111 if (cause instanceof AbortProcessingException) {
112 throw (AbortProcessingException) cause;
113 } else {
114 throw e;
115 }
116 }
117 }
118 }
119
120 public static Object getValueFromValueBindingOrValueExpression(
121 FacesContext context, UIComponent component, String name) {
122 if (USE_BINDING) {
123 return component.getValueBinding(name).getValue(context);
124 } else {
125 return FacesUtilsEL.getValueFromValueBindingOrValueExpression(context, component, name);
126 }
127 }
128
129 public static boolean hasValueBindingOrValueExpression(UIComponent component, String name) {
130 if (USE_BINDING) {
131 return component.getValueBinding(name) != null;
132 } else {
133 return FacesUtilsEL.hasValueBindingOrValueExpression(component, name);
134 }
135 }
136
137 public static boolean isReadonlyValueBindingOrValueExpression(
138 FacesContext context, UIComponent component, String name) {
139 if (USE_BINDING) {
140 return component.getValueBinding(name).isReadOnly(context);
141 } else {
142 return FacesUtilsEL.isReadonlyValueBindingOrValueExpression(context, component, name);
143 }
144 }
145
146 public static String getExpressionString(UIComponent component, String name) {
147 if (USE_BINDING) {
148 return component.getValueBinding(name).getExpressionString();
149 } else {
150 return FacesUtilsEL.getExpressionString(component, name);
151 }
152 }
153
154 public static void setValueOfBindingOrExpression(
155 FacesContext context, Object value, UIComponent component, String bindingName) {
156 if (USE_BINDING) {
157 ValueBinding vb = component.getValueBinding(bindingName);
158 if (vb != null) {
159 vb.setValue(context, value);
160 }
161 } else {
162 FacesUtilsEL.setValueOfBindingOrExpression(context, value, component, bindingName);
163 }
164 }
165
166 public static void setValueOfBindingOrExpression(
167 FacesContext context, Object value, Object bindingOrExpression) {
168 if (USE_BINDING) {
169 if (bindingOrExpression instanceof ValueBinding) {
170 ValueBinding vb = (ValueBinding) bindingOrExpression;
171 vb.setValue(context, value);
172 }
173 } else {
174 FacesUtilsEL.setValueOfBindingOrExpression(context, value, bindingOrExpression);
175 }
176 }
177
178 public static void copyValueBindingOrValueExpression(
179 UIComponent fromComponent, String fromName, UIComponent toComponent, String toName) {
180 if (USE_BINDING) {
181 ValueBinding vb = fromComponent.getValueBinding(fromName);
182 if (vb != null) {
183 toComponent.setValueBinding(toName, vb);
184 }
185 } else {
186 FacesUtilsEL.copyValueBindingOrValueExpression(fromComponent, fromName, toComponent, toName);
187 }
188 }
189
190 public static Object getValueFromBindingOrExpression(Object obj) {
191 if (USE_BINDING) {
192 if (obj instanceof ValueBinding) {
193 return ((ValueBinding) obj).getValue(FacesContext.getCurrentInstance());
194 }
195 } else {
196 return FacesUtilsEL.getValueFromBindingOrExpression(obj);
197 }
198 return null;
199 }
200
201 public static Object createExpressionOrBinding(String string) {
202 if (USE_BINDING) {
203 FacesContext facesContext = FacesContext.getCurrentInstance();
204 ValueBinding valueBinding = facesContext.getApplication().createValueBinding(string);
205 return valueBinding;
206 } else {
207 return FacesUtilsEL.createExpressionOrBinding(string);
208 }
209 }
210
211 public static void setValidator(EditableValueHolder editableValueHolder, Object validator) {
212 if (USE_BINDING) {
213 MethodBinding methodBinding =
214 FacesContext.getCurrentInstance().getApplication().createMethodBinding(validator.toString(), VALIDATOR_ARGS);
215 editableValueHolder.setValidator(methodBinding);
216 } else {
217 FacesUtilsEL.setValidator(editableValueHolder, validator);
218 }
219 }
220
221 public static void setConverter(ValueHolder valueHolder, Object converterExpression) {
222 if (USE_BINDING) {
223 if (converterExpression != null && converterExpression instanceof String) {
224 String converterExpressionStr = (String) converterExpression;
225 FacesContext context = FacesContext.getCurrentInstance();
226 if (UIComponentTag.isValueReference(converterExpressionStr)) {
227 ValueBinding valueBinding = context.getApplication().createValueBinding(converterExpressionStr);
228 if (valueHolder instanceof UIComponent) {
229 ((UIComponent) valueHolder).setValueBinding("converter", valueBinding);
230 }
231 } else {
232 Converter converter = context.getApplication().createConverter(converterExpressionStr);
233 valueHolder.setConverter(converter);
234 }
235 }
236 } else {
237 FacesUtilsEL.setConverter(valueHolder, converterExpression);
238 }
239 }
240
241 public static void setBindingOrExpression(UIComponent component, String name, Object valueBindingOrExpression) {
242 if (USE_BINDING) {
243 component.setValueBinding(name, (ValueBinding) valueBindingOrExpression);
244 } else {
245 FacesUtilsEL.setBindingOrExpression(component, name, valueBindingOrExpression);
246 }
247 }
248
249 public static void addBindingOrExpressionTabChangeListener(TabChangeSource source, String type,
250 Object bindingOrExpression) {
251 if (USE_BINDING) {
252 source.addTabChangeListener(new ValueBindingTabChangeListener(type, (ValueBinding) bindingOrExpression));
253 } else {
254 FacesUtilsEL.addBindingOrExpressionTabChangeListener(source, type, bindingOrExpression);
255 }
256 }
257
258 public static Comparator getBindingOrExpressionComparator(
259 FacesContext facesContext, UIComponent child, String var, boolean descending, Comparator comparator) {
260 if (USE_BINDING) {
261 ValueBinding valueBinding = child.getValueBinding("value");
262 return new ValueBindingComparator(facesContext, var, valueBinding, descending, comparator);
263 } else {
264 return FacesUtilsEL.getBindingOrExpressionComparator(facesContext, child, var, descending, comparator);
265 }
266 }
267
268 public static void addBindingOrExpressionPopupActionListener(ActionSource actionSource, Object bindingOrExpression) {
269 if (USE_BINDING) {
270 actionSource.addActionListener(new ValueBindingPopupActionListener(bindingOrExpression));
271 } else {
272 FacesUtilsEL.addBindingOrExpressionPopupActionListener(actionSource, bindingOrExpression);
273 }
274 }
275
276 public static boolean supportsEL() {
277 return !USE_BINDING;
278 }
279 }