1 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 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; } 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 context.addMessage(input.getClientId(context), facesMessage); 142 } 143 else 144 { 145 } 147 } 149 } 150 151 MethodBinding validatorBinding = input.getValidator(); 152 if (validatorBinding != null) 153 { 154 try 155 { 156 validatorBinding.invoke(context, 157 new Object [] {context, input, convertedValue}); 158 } 159 catch (EvaluationException e) 160 { 161 input.setValid(false); 162 Throwable 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 } 174 } 176 else 177 { 178 throw e; 179 } 180 } 181 } 182 } 183 184 185 } 186 | Popular Tags |