KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > FacesUtils


1 /*
2  * Copyright 1999-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 org.apache.cocoon.faces;
17
18 import org.apache.cocoon.faces.taglib.UIComponentTag;
19 import org.apache.cocoon.taglib.Tag;
20
21 import javax.faces.FacesException;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
29  * @version CVS $Id: FacesUtils.java 46253 2004-09-17 14:36:29Z vgritsenko $
30  */

31 public class FacesUtils {
32
33     /**
34      * Key of the {@link FacesContext} in the object model.
35      */

36     private static final String JavaDoc FACES_CONTEXT_OBJECT = "javax.faces.webapp.FACES_CONTEXT";
37
38     /**
39      * Find current FacesContext, and store it in the objectModel
40      */

41     public static FacesContext getFacesContext(Tag tag, Map JavaDoc objectModel) {
42         FacesContext context = (FacesContext) objectModel.get(FACES_CONTEXT_OBJECT);
43         if (context == null) {
44             context = FacesContext.getCurrentInstance();
45             if (context == null) {
46                 throw new FacesException("Tag <" + tag.getClass().getName() + "> " +
47                                          "could not find current FacesContext");
48             }
49             objectModel.put(FACES_CONTEXT_OBJECT, context);
50         }
51
52         return context;
53     }
54
55     /**
56      * Find child component by ID
57      */

58     public static UIComponent getChild(UIComponent component, String JavaDoc id) {
59         for (Iterator JavaDoc kids = component.getChildren().iterator(); kids.hasNext();) {
60             UIComponent kid = (UIComponent) kids.next();
61             if (id.equals(kid.getId())) {
62                 return kid;
63             }
64         }
65
66         return null;
67     }
68
69     /**
70      * Remove child component by ID
71      */

72     public static UIComponent removeChild(UIComponent component, String JavaDoc id) {
73         UIComponent kid = getChild(component, id);
74         if (kid != null) {
75             component.getChildren().remove(kid);
76         }
77
78         return kid;
79     }
80
81     /**
82      * Is this an expression?
83      */

84     public static boolean isExpression(String JavaDoc value) {
85         if (value == null) {
86             return false;
87         }
88
89         int i = value.indexOf("#{");
90         return i != -1 && i < value.indexOf('}');
91     }
92
93     /**
94      * Evaluate expression
95      */

96     public static Object JavaDoc evaluate(FacesContext context, String JavaDoc value) {
97         if (isExpression(value)) {
98             return context.getApplication().createValueBinding(value).getValue(context);
99         }
100
101         return value;
102     }
103
104     /**
105      *
106      */

107     public static UIComponentTag findParentUIComponentTag(Tag tag) {
108         Tag parent = tag;
109         do {
110             parent = parent.getParent();
111         } while (parent != null && !(parent instanceof UIComponentTag));
112
113         return (UIComponentTag) parent;
114     }
115 }
116
Popular Tags