1 /*
2 * Copyright 2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package javax.faces.component;
17
18 import javax.faces.application.FacesMessage;
19 import javax.faces.context.FacesContext;
20 import javax.faces.el.EvaluationException;
21 import javax.faces.el.MethodBinding;
22 import javax.faces.validator.Validator;
23 import javax.faces.validator.ValidatorException;
24 import java.util.Iterator;
25
26 /**
27 * @author Manfred Geiler (latest modification by $Author: schof $)
28 * @version $Revision: 264794 $ $Date: 2005-08-30 11:19:58 -0400 (Tue, 30 Aug 2005) $
29 */
30 class _ComponentUtils
31 {
32 private _ComponentUtils() {}
33
34 static UIComponent findParentNamingContainer(UIComponent component,
35 boolean returnRootIfNotFound)
36 {
37 UIComponent parent = component.getParent();
38 if (returnRootIfNotFound && parent == null)
39 {
40 return component;
41 }
42 while (parent != null)
43 {
44 if (parent instanceof NamingContainer) return parent;
45 if (returnRootIfNotFound)
46 {
47 UIComponent nextParent = parent.getParent();
48 if (nextParent == null)
49 {
50 return parent; //Root
51 }
52 parent = nextParent;
53 }
54 else
55 {
56 parent = parent.getParent();
57 }
58 }
59 return null;
60 }
61
62 static UIComponent getRootComponent(UIComponent component)
63 {
64 UIComponent parent;
65 for(;;)
66 {
67 parent = component.getParent();
68 if (parent == null) return component;
69 component = parent;
70 }
71 }
72
73 static UIComponent findComponent(UIComponent findBase, String id)
74 {
75 if (idsAreEqual(id,findBase))
76 {
77 return findBase;
78 }
79
80 for (Iterator it = findBase.getFacetsAndChildren(); it.hasNext(); )
81 {
82 UIComponent childOrFacet = (UIComponent)it.next();
83 if (!(childOrFacet instanceof NamingContainer))
84 {
85 UIComponent find = findComponent(childOrFacet, id);
86 if (find != null) return find;
87 }
88 else if (idsAreEqual(id,childOrFacet))
89 {
90 return childOrFacet;
91 }
92 }
93
94 return null;
95 }
96
97 private static boolean idsAreEqual(String id, UIComponent cmp)
98 {
99 if(id.equals(cmp.getId()))
100 return true;
101
102 if(cmp instanceof UIData)
103 {
104 UIData uiData = ((UIData) cmp);
105
106 if(uiData.getRowIndex()==-1)
107 {
108 return dynamicIdIsEqual(id,cmp.getId());
109 }
110 else
111 {
112 return id.equals(cmp.getId()+"_"+uiData.getRowIndex());
113 }
114 }
115
116 return false;
117 }
118
119 private static boolean dynamicIdIsEqual(String dynamicId, String id)
120 {
121 return dynamicId.matches(id+"_[0-9]*");
122 }
123
124
125 static void callValidators(FacesContext context, UIInput input, Object convertedValue)
126 {
127 Validator[] validators = input.getValidators();
128 for (int i = 0; i < validators.length; i++)
129 {
130 Validator validator = validators[i];
131 try
132 {
133 validator.validate(context, input, convertedValue);
134 }
135 catch (ValidatorException e)
136 {
137 input.setValid(false);
138 FacesMessage facesMessage = e.getFacesMessage();
139 if (facesMessage != null)
140 {
141 facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
142 context.addMessage(input.getClientId(context), facesMessage);
143 }
144 }
145 }
146
147 MethodBinding validatorBinding = input.getValidator();
148 if (validatorBinding != null)
149 {
150 try
151 {
152 validatorBinding.invoke(context,
153 new Object[] {context, input, convertedValue});
154 }
155 catch (EvaluationException e)
156 {
157 input.setValid(false);
158 Throwable cause = e.getCause();
159 if (cause instanceof ValidatorException)
160 {
161 FacesMessage facesMessage = ((ValidatorException)cause).getFacesMessage();
162 if (facesMessage != null)
163 {
164 facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
165 context.addMessage(input.getClientId(context), facesMessage);
166 }
167 }
168 else
169 {
170 throw e;
171 }
172 }
173 }
174 }
175
176
177 }