001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.faces;
018:
019: import org.apache.cocoon.faces.taglib.UIComponentTag;
020: import org.apache.cocoon.taglib.Tag;
021:
022: import javax.faces.FacesException;
023: import javax.faces.component.UIComponent;
024: import javax.faces.context.FacesContext;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: /**
029: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
030: * @version CVS $Id: FacesUtils.java 433543 2006-08-22 06:22:54Z crossley $
031: */
032: public class FacesUtils {
033:
034: /**
035: * Key of the {@link FacesContext} in the object model.
036: */
037: private static final String FACES_CONTEXT_OBJECT = "javax.faces.webapp.FACES_CONTEXT";
038:
039: /**
040: * Find current FacesContext, and store it in the objectModel
041: */
042: public static FacesContext getFacesContext(Tag tag, Map objectModel) {
043: FacesContext context = (FacesContext) objectModel
044: .get(FACES_CONTEXT_OBJECT);
045: if (context == null) {
046: context = FacesContext.getCurrentInstance();
047: if (context == null) {
048: throw new FacesException("Tag <"
049: + tag.getClass().getName() + "> "
050: + "could not find current FacesContext");
051: }
052: objectModel.put(FACES_CONTEXT_OBJECT, context);
053: }
054:
055: return context;
056: }
057:
058: /**
059: * Find child component by ID
060: */
061: public static UIComponent getChild(UIComponent component, String id) {
062: for (Iterator kids = component.getChildren().iterator(); kids
063: .hasNext();) {
064: UIComponent kid = (UIComponent) kids.next();
065: if (id.equals(kid.getId())) {
066: return kid;
067: }
068: }
069:
070: return null;
071: }
072:
073: /**
074: * Remove child component by ID
075: */
076: public static UIComponent removeChild(UIComponent component,
077: String id) {
078: UIComponent kid = getChild(component, id);
079: if (kid != null) {
080: component.getChildren().remove(kid);
081: }
082:
083: return kid;
084: }
085:
086: /**
087: * Is this an expression?
088: */
089: public static boolean isExpression(String value) {
090: if (value == null) {
091: return false;
092: }
093:
094: int i = value.indexOf("#{");
095: return i != -1 && i < value.indexOf('}');
096: }
097:
098: /**
099: * Evaluate expression
100: */
101: public static Object evaluate(FacesContext context, String value) {
102: if (isExpression(value)) {
103: return context.getApplication().createValueBinding(value)
104: .getValue(context);
105: }
106:
107: return value;
108: }
109:
110: /**
111: *
112: */
113: public static UIComponentTag findParentUIComponentTag(Tag tag) {
114: Tag parent = tag;
115: do {
116: parent = parent.getParent();
117: } while (parent != null && !(parent instanceof UIComponentTag));
118:
119: return (UIComponentTag) parent;
120: }
121: }
|