1 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 ; 25 import java.util.Map ; 26 27 31 public class FacesUtils { 32 33 36 private static final String FACES_CONTEXT_OBJECT = "javax.faces.webapp.FACES_CONTEXT"; 37 38 41 public static FacesContext getFacesContext(Tag tag, Map 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 58 public static UIComponent getChild(UIComponent component, String id) { 59 for (Iterator 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 72 public static UIComponent removeChild(UIComponent component, String id) { 73 UIComponent kid = getChild(component, id); 74 if (kid != null) { 75 component.getChildren().remove(kid); 76 } 77 78 return kid; 79 } 80 81 84 public static boolean isExpression(String 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 96 public static Object evaluate(FacesContext context, String value) { 97 if (isExpression(value)) { 98 return context.getApplication().createValueBinding(value).getValue(context); 99 } 100 101 return value; 102 } 103 104 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 |