KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > component > _ComponentUtils


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 JavaDoc;
25
26 /**
27  * @author Manfred Geiler (latest modification by $Author: mmarinschek $)
28  * @version $Revision: 1.6 $ $Date: 2005/01/26 13:55:15 $
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 JavaDoc id)
74     {
75         if (idsAreEqual(id,findBase))
76         {
77             return findBase;
78         }
79
80         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc dynamicId, String JavaDoc id)
120     {
121         return dynamicId.matches(id+"_[0-9]*");
122     }
123
124
125     static void callValidators(FacesContext context, UIInput input, Object JavaDoc 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                     context.addMessage(input.getClientId(context), facesMessage);
142                 }
143                 else
144                 {
145                     //TODO: specification? add a general message?
146
}
147                 //TODO: specification? should we abort validation immediately
148
}
149         }
150
151         MethodBinding validatorBinding = input.getValidator();
152         if (validatorBinding != null)
153         {
154             try
155             {
156                 validatorBinding.invoke(context,
157                                         new Object JavaDoc[] {context, input, convertedValue});
158             }
159             catch (EvaluationException e)
160             {
161                 input.setValid(false);
162                 Throwable JavaDoc cause = e.getCause();
163                 if (cause instanceof ValidatorException)
164                 {
165                     FacesMessage facesMessage = ((ValidatorException)cause).getFacesMessage();
166                     if (facesMessage != null)
167                     {
168                         context.addMessage(input.getClientId(context), facesMessage);
169                     }
170                     else
171                     {
172                         //TODO: specification? add a general message?
173
}
174                     //TODO: specification? should we abort validation immediately
175
}
176                 else
177                 {
178                     throw e;
179                 }
180             }
181         }
182     }
183
184
185 }
186
Popular Tags