1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.component; |
20 | |
|
21 | |
import java.util.Iterator; |
22 | |
|
23 | |
import javax.el.ValueExpression; |
24 | |
import javax.faces.application.FacesMessage; |
25 | |
import javax.faces.context.FacesContext; |
26 | |
import javax.faces.el.EvaluationException; |
27 | |
import javax.faces.el.MethodBinding; |
28 | |
import javax.faces.el.ValueBinding; |
29 | |
import javax.faces.validator.Validator; |
30 | |
import javax.faces.validator.ValidatorException; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
class _ComponentUtils |
39 | |
{ |
40 | 0 | private _ComponentUtils() {} |
41 | |
|
42 | |
static UIComponent findParentNamingContainer(UIComponent component, |
43 | |
boolean returnRootIfNotFound) |
44 | |
{ |
45 | 0 | UIComponent parent = component.getParent(); |
46 | 0 | if (returnRootIfNotFound && parent == null) |
47 | |
{ |
48 | 0 | return component; |
49 | |
} |
50 | 0 | while (parent != null) |
51 | |
{ |
52 | 0 | if (parent instanceof NamingContainer) return parent; |
53 | 0 | if (returnRootIfNotFound) |
54 | |
{ |
55 | 0 | UIComponent nextParent = parent.getParent(); |
56 | 0 | if (nextParent == null) |
57 | |
{ |
58 | 0 | return parent; |
59 | |
} |
60 | 0 | parent = nextParent; |
61 | 0 | } |
62 | |
else |
63 | |
{ |
64 | 0 | parent = parent.getParent(); |
65 | |
} |
66 | |
} |
67 | 0 | return null; |
68 | |
} |
69 | |
|
70 | |
static UIComponent getRootComponent(UIComponent component) |
71 | |
{ |
72 | |
UIComponent parent; |
73 | |
for(;;) |
74 | |
{ |
75 | 0 | parent = component.getParent(); |
76 | 0 | if (parent == null) return component; |
77 | 0 | component = parent; |
78 | |
} |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
static UIComponent findComponent(UIComponent findBase, String id) |
96 | |
{ |
97 | 0 | if (idsAreEqual(id,findBase)) |
98 | |
{ |
99 | 0 | return findBase; |
100 | |
} |
101 | |
|
102 | 0 | for (Iterator it = findBase.getFacetsAndChildren(); it.hasNext(); ) |
103 | |
{ |
104 | 0 | UIComponent childOrFacet = (UIComponent)it.next(); |
105 | 0 | if (!(childOrFacet instanceof NamingContainer)) |
106 | |
{ |
107 | 0 | UIComponent find = findComponent(childOrFacet, id); |
108 | 0 | if (find != null) return find; |
109 | 0 | } |
110 | 0 | else if (idsAreEqual(id,childOrFacet)) |
111 | |
{ |
112 | 0 | return childOrFacet; |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | 0 | return null; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
private static boolean idsAreEqual(String id, UIComponent cmp) |
126 | |
{ |
127 | 0 | if(id.equals(cmp.getId())) |
128 | 0 | return true; |
129 | |
|
130 | 0 | if(cmp instanceof UIData) |
131 | |
{ |
132 | 0 | UIData uiData = ((UIData) cmp); |
133 | |
|
134 | 0 | if(uiData.getRowIndex()==-1) |
135 | |
{ |
136 | 0 | return dynamicIdIsEqual(id,cmp.getId()); |
137 | |
} |
138 | 0 | return id.equals(cmp.getId()+NamingContainer.SEPARATOR_CHAR+uiData.getRowIndex()); |
139 | |
} |
140 | |
|
141 | 0 | return false; |
142 | |
} |
143 | |
|
144 | |
private static boolean dynamicIdIsEqual(String dynamicId, String id) |
145 | |
{ |
146 | 0 | return dynamicId.matches(id+":[0-9]*"); |
147 | |
} |
148 | |
|
149 | |
|
150 | |
static void callValidators(FacesContext context, UIInput input, Object convertedValue) |
151 | |
{ |
152 | |
|
153 | 0 | Validator[] validators = input.getValidators(); |
154 | 0 | for (int i = 0; i < validators.length; i++) |
155 | |
{ |
156 | 0 | Validator validator = validators[i]; |
157 | |
try |
158 | |
{ |
159 | 0 | validator.validate(context, input, convertedValue); |
160 | |
} |
161 | 0 | catch (ValidatorException e) |
162 | |
{ |
163 | 0 | input.setValid(false); |
164 | |
|
165 | 0 | String validatorMessage = input.getValidatorMessage(); |
166 | 0 | if(validatorMessage != null) { |
167 | 0 | context.addMessage(input.getClientId(context), new FacesMessage(FacesMessage.SEVERITY_ERROR,validatorMessage,validatorMessage)); |
168 | |
} else { |
169 | 0 | FacesMessage facesMessage = e.getFacesMessage(); |
170 | 0 | if (facesMessage != null) |
171 | |
{ |
172 | 0 | facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR); |
173 | 0 | context.addMessage(input.getClientId(context), facesMessage); |
174 | |
} |
175 | |
} |
176 | 0 | } |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | 0 | MethodBinding validatorBinding = input.getValidator(); |
182 | 0 | if (validatorBinding != null) |
183 | |
{ |
184 | |
try |
185 | |
{ |
186 | 0 | validatorBinding.invoke(context, |
187 | |
new Object[] {context, input, convertedValue}); |
188 | |
} |
189 | 0 | catch (EvaluationException e) |
190 | |
{ |
191 | 0 | input.setValid(false); |
192 | 0 | Throwable cause = e.getCause(); |
193 | 0 | if (cause instanceof ValidatorException) |
194 | |
{ |
195 | 0 | String validatorMessage = input.getValidatorMessage(); |
196 | 0 | if(validatorMessage != null) { |
197 | 0 | context.addMessage(input.getClientId(context), new FacesMessage(FacesMessage.SEVERITY_ERROR,validatorMessage,validatorMessage)); |
198 | |
} |
199 | |
else { |
200 | 0 | FacesMessage facesMessage = ((ValidatorException)cause).getFacesMessage(); |
201 | 0 | if (facesMessage != null) |
202 | |
{ |
203 | 0 | facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR); |
204 | 0 | context.addMessage(input.getClientId(context), facesMessage); |
205 | |
} |
206 | |
} |
207 | 0 | } |
208 | |
else |
209 | |
{ |
210 | 0 | throw e; |
211 | |
} |
212 | 0 | } |
213 | |
} |
214 | 0 | } |
215 | |
|
216 | |
static String getStringValue(FacesContext context, ValueBinding vb) |
217 | |
{ |
218 | 0 | Object value = vb.getValue(context); |
219 | 0 | if(value == null) |
220 | |
{ |
221 | 0 | return null; |
222 | |
} |
223 | 0 | return value.toString(); |
224 | |
} |
225 | |
|
226 | |
|
227 | |
static <T> T getExpressionValue(UIComponent component, String attribute, T overrideValue, T defaultValue) |
228 | |
{ |
229 | 0 | if (overrideValue != null) |
230 | |
{ |
231 | 0 | return overrideValue; |
232 | |
} |
233 | 0 | ValueExpression ve = component.getValueExpression(attribute); |
234 | 0 | if (ve != null) |
235 | |
{ |
236 | 0 | return (T) ve.getValue(component.getFacesContext().getELContext()); |
237 | |
} |
238 | 0 | return defaultValue; |
239 | |
} |
240 | |
|
241 | |
static String getPathToComponent(UIComponent component) { |
242 | 0 | StringBuffer buf = new StringBuffer(); |
243 | |
|
244 | 0 | if(component == null) |
245 | |
{ |
246 | 0 | buf.append("{Component-Path : "); |
247 | 0 | buf.append("[null]}"); |
248 | 0 | return buf.toString(); |
249 | |
} |
250 | |
|
251 | 0 | getPathToComponent(component,buf); |
252 | |
|
253 | 0 | buf.insert(0,"{Component-Path : "); |
254 | 0 | buf.append("}"); |
255 | |
|
256 | 0 | return buf.toString(); |
257 | |
} |
258 | |
|
259 | |
private static void getPathToComponent(UIComponent component, StringBuffer buf) |
260 | |
{ |
261 | 0 | if(component == null) |
262 | 0 | return; |
263 | |
|
264 | 0 | StringBuffer intBuf = new StringBuffer(); |
265 | |
|
266 | 0 | intBuf.append("[Class: "); |
267 | 0 | intBuf.append(component.getClass().getName()); |
268 | 0 | if(component instanceof UIViewRoot) |
269 | |
{ |
270 | 0 | intBuf.append(",ViewId: "); |
271 | 0 | intBuf.append(((UIViewRoot) component).getViewId()); |
272 | |
} |
273 | |
else |
274 | |
{ |
275 | 0 | intBuf.append(",Id: "); |
276 | 0 | intBuf.append(component.getId()); |
277 | |
} |
278 | 0 | intBuf.append("]"); |
279 | |
|
280 | 0 | buf.insert(0,intBuf.toString()); |
281 | |
|
282 | 0 | getPathToComponent(component.getParent(), buf); |
283 | 0 | } |
284 | |
} |