1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.dynaForm.lib;
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23
24 import javax.faces.FacesException;
25 import javax.faces.application.Application;
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.FacesContext;
28 import javax.faces.el.ValueBinding;
29 import javax.el.ELContext;
30 import javax.el.ExpressionFactory;
31 import javax.el.ValueExpression;
32
33
34
35
36 public final class _FacesUtils
37 {
38 private static final Class<?>[] ARGS_SET_VALUE_EXPR = new Class[]
39 {
40 String.class,
41 ValueExpression.class
42 };
43
44 private static final Class<?>[] ARGS_GET_VALUE_EXPR = new Class[]
45 {
46 String.class
47 };
48
49 private static final Method _METHOD_GELC = getMethod(FacesContext.class, "getELContext", (Class[]) null);
50 private static final Method _METHOD_GVE = getMethod(UIComponent.class, "getValueExpression", ARGS_GET_VALUE_EXPR);
51 private static final Method _METHOD_SVE = getMethod(UIComponent.class, "setValueExpression", ARGS_SET_VALUE_EXPR);
52 private static final Method _METHOD_EF = getMethod(Application.class, "getExpressionFactory", (Class[]) null);
53
54 private _FacesUtils()
55 {
56 }
57
58 private static Method getMethod(Class<?> clazz, String name, Class<?>[] args)
59 {
60 try
61 {
62 return clazz.getMethod(name, args);
63 }
64 catch(Exception e)
65 {
66 return null;
67 }
68 }
69
70 private static <T> T invoke(Class<T> retType, Method method, Object target, Object[] args)
71 throws FacesException
72 {
73 try
74 {
75 @SuppressWarnings("unchecked")
76 T result = (T) method.invoke(target, args);
77 return result;
78 }
79 catch(IllegalAccessException e)
80 {
81
82 throw new FacesException("Unable to invoke standard JSF method", e);
83 }
84 catch(InvocationTargetException e)
85 {
86
87 throw new FacesException("Unable to invoke standard JSF method", e);
88 }
89 }
90
91 public static void copyRendered(UIComponent source, UIComponent destination)
92 {
93 boolean renderedSet = false;
94 if (_METHOD_GVE != null)
95 {
96 ValueExpression ve = invoke(ValueExpression.class, _METHOD_GVE, source, new Object[] {"rendered"});
97 if (ve != null)
98 {
99 invoke(Void.class, _METHOD_SVE, destination, new Object[]{"rendered", ve});
100 renderedSet = true;
101 }
102 }
103 else
104 {
105 ValueBinding vb = source.getValueBinding("rendered");
106 if (vb != null)
107 {
108 destination.setValueBinding("rendered", vb);
109 renderedSet = true;
110 }
111 }
112 if (!renderedSet)
113 {
114 destination.setRendered(destination.isRendered());
115 }
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public static void setValueExpression(UIComponent cmp, ELContext elCtx, FacesContext context, String vbString)
132 {
133 Application app = context.getApplication();
134 if (_METHOD_GELC != null)
135 {
136 if (elCtx == null)
137 {
138 elCtx = invoke(ELContext.class, _METHOD_GELC, context, (Object[]) null);
139 }
140
141 ExpressionFactory expressionFactory = invoke(ExpressionFactory.class, _METHOD_EF, app, (Object[]) null);
142 ValueExpression ve = expressionFactory.createValueExpression(elCtx, vbString, Object.class);
143
144 invoke(Void.class, _METHOD_SVE, cmp, new Object[] {"value", ve});
145 }
146 else
147 {
148 ValueBinding vb = app.createValueBinding(vbString);
149 cmp.setValueBinding("value", vb);
150 }
151 }
152 }