1 16 package org.apache.myfaces.wap.renderkit; 17 18 import java.lang.reflect.Array ; 19 import java.net.URLEncoder ; 20 import java.util.ArrayList ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.faces.FacesException; 28 import javax.faces.application.ViewHandler; 29 import javax.faces.component.EditableValueHolder; 30 import javax.faces.component.UIComponent; 31 import javax.faces.component.UISelectMany; 32 import javax.faces.component.ValueHolder; 33 import javax.faces.context.FacesContext; 34 import javax.faces.context.ResponseWriter; 35 import javax.faces.convert.Converter; 36 import javax.faces.el.ValueBinding; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 50 51 public class RendererUtils { 52 private static Log log = LogFactory.getLog(RendererUtils.class); 53 54 public static String getCurrentUrl(FacesContext context){ 55 ViewHandler viewHandler = context.getApplication().getViewHandler(); 56 String viewId = context.getViewRoot().getViewId(); 57 String actionUrl = viewHandler.getActionURL(context, viewId); 58 String urlLink = context.getExternalContext().encodeActionURL(actionUrl); 59 return(urlLink); 60 } 61 62 63 public static void writeAttribute(String attribute, Object value, ResponseWriter writer){ 64 log.debug("attribute " + attribute + ": " + value); 65 66 try { 67 if (value != null) 68 writer.writeAttribute(attribute, value, null); 69 } catch (java.io.IOException ex) { 70 log.error("Error write attribute '" + attribute + " value: '" + value + "'", ex.getCause()); 71 } 72 } 73 74 75 public static Object convertToObject(FacesContext context, UIComponent component){ 76 if (!(component instanceof EditableValueHolder)) 77 throw new IllegalArgumentException ("Expected component of type EditableValueHolder. Cannot convert submitted value."); 78 79 EditableValueHolder holder = (EditableValueHolder)component; 80 81 if (!(holder.getSubmittedValue() instanceof String )) 82 throw new ClassCastException ("Submitted value must be a String value."); 83 84 String submittedValue = (String )holder.getSubmittedValue(); 85 Converter conv = holder.getConverter(); 86 87 if (conv == null) return(submittedValue); 89 90 return(conv.getAsObject(context, component, submittedValue)); 91 } 92 93 94 public static String convertToString(FacesContext facesContext, UIComponent component) { 95 if (!(component instanceof ValueHolder)) { 96 throw new IllegalArgumentException ("Component is not a ValueHolder"); 97 } 98 99 if (component instanceof EditableValueHolder) { 100 Object submittedValue = ((EditableValueHolder)component).getSubmittedValue(); 101 if (submittedValue != null) { 102 if (submittedValue instanceof String ) { 103 return (String )submittedValue; 104 } 105 else { 106 throw new IllegalArgumentException ("Expected submitted value of type String"); 107 } 108 } 109 } 110 111 Object value = ((ValueHolder)component).getValue(); 112 113 Converter converter = ((ValueHolder)component).getConverter(); 114 if (converter == null && value != null) { 115 if (value instanceof String ) { 116 return (String ) value; 117 } 118 119 try { 120 converter = facesContext.getApplication().createConverter(value.getClass()); 121 } 122 catch (FacesException e) { 123 log.error("No converter for class " + value.getClass().getName() + " found (component id=" + component.getId() + ")."); 124 } 126 } 127 128 if (converter == null) { 129 if (value == null) { 130 return ""; 131 } 132 else { 133 return value.toString(); 134 } 135 } 136 else { 137 return converter.getAsString(facesContext, component, value); 138 } 139 } 140 141 public static Object convertUISelectManyToObject(FacesContext context, UISelectMany component, Object value) { 142 if (!(value instanceof String [])) 143 throw new ClassCastException ("Selected value must be a String[] type."); 144 145 String [] submittedValue = (String [])value; 146 147 ValueBinding vb = component.getValueBinding("value"); 148 Class valueType = null; 149 Class arrayComponentType = null; 150 if (vb != null) { 151 valueType = vb.getType(context); 152 if (valueType != null && valueType.isArray()) { 153 arrayComponentType = valueType.getComponentType(); 154 } 155 } 156 157 Converter converter = component.getConverter(); 158 if (converter == null) { 159 if (valueType == null) { 160 return submittedValue; 163 } 164 165 if (List .class.isAssignableFrom(valueType)) { 166 int len = submittedValue.length; 170 List lst = new ArrayList (len); 171 for (int i = 0; i < len; i++) { 172 lst.add(submittedValue[i]); 173 } 174 return lst; 175 } 176 177 if (arrayComponentType == null) { 178 throw new IllegalArgumentException ("ValueBinding for UISelectMany must be of type List or Array"); 179 } 180 181 if (String .class.equals(arrayComponentType)) return submittedValue; if (Object .class.equals(arrayComponentType)) return submittedValue; 184 try { 185 converter = context.getApplication().createConverter(arrayComponentType); 186 } 187 catch (FacesException e) { 188 log.error("No Converter for type " + arrayComponentType.getName() + " found"); 189 return submittedValue; 190 } 191 } 192 193 if (valueType == null) { 195 int len = submittedValue.length; 198 Object [] convertedValues = new Object [len]; 199 for (int i = 0; i < len; i++) { 200 convertedValues[i] = converter.getAsObject(context, component, submittedValue[i]); 201 } 202 return convertedValues; 203 } 204 205 if (List .class.isAssignableFrom(valueType)) { 206 int len = submittedValue.length; 210 List lst = new ArrayList (len); 211 for (int i = 0; i < len; i++) { 212 lst.add(converter.getAsObject(context, component, submittedValue[i])); 213 } 214 return lst; 215 } 216 217 if (arrayComponentType == null) { 218 throw new IllegalArgumentException ("ValueBinding for UISelectMany must be of type List or Array"); 219 } 220 221 if (arrayComponentType.isPrimitive()) { 222 int len = submittedValue.length; 224 Object convertedValues = Array.newInstance(arrayComponentType, len); 225 for (int i = 0; i < len; i++) { 226 Array.set(convertedValues, i, converter.getAsObject(context, component, submittedValue[i])); 227 } 228 return convertedValues; 229 } 230 else { 231 int len = submittedValue.length; 233 Object [] convertedValues = new Object [len]; 234 for (int i = 0; i < len; i++) { 235 convertedValues[i] = converter.getAsObject(context, component, submittedValue[i]); 236 } 237 return convertedValues; 238 } 239 } 240 241 public static void renderChildren(FacesContext context, UIComponent component) throws java.io.IOException { 242 if (component != null && component.isRendered()){ 243 Iterator iter = component.getChildren().iterator(); 244 while (iter.hasNext()){ 245 UIComponent child = (UIComponent)iter.next(); 246 renderChild(context, child); 247 } 248 } 249 } 250 251 public static void renderChild(FacesContext context, UIComponent child) throws java.io.IOException { 252 if (child == null || !child.isRendered()) return; 253 254 child.encodeBegin(context); 255 if (child.getRendersChildren()) { 256 child.encodeChildren(context); 257 } 258 else { 259 renderChildren(context, child); 260 } 261 child.encodeEnd(context); 262 263 if (!child.isRendered()) 264 { 265 return; 266 } 267 } 268 269 270 public static String insertGetParam(String href, String param, String value, String encoding) throws java.io.IOException { 271 href += (href.indexOf('?') == -1) ? "?" : "&"; 272 273 href += URLEncoder.encode(param, encoding); 274 href += '='; 275 href += URLEncoder.encode(value, encoding); 276 277 return (href); 278 } 279 280 284 public static Set getInputTags(List components){ 285 Set inputTags = new HashSet (); 286 287 Iterator iter = components.iterator(); 288 289 while (iter.hasNext()){ 290 UIComponent comp = (UIComponent)iter.next(); 291 String family = comp.getFamily(); 292 log.debug("processing component family:" + family); 293 294 if (isInputComponent(family)){ 295 inputTags.add(comp); 296 } 297 } 298 return(inputTags); 299 } 300 301 302 private static boolean isInputComponent(String name){ 303 String INPUT_COMPONENTS_FAMILY[] = {"UIInput", "UISelectItems", "UISelectBoolean", "UISelectOne", "UISelectMany"}; 304 305 for (int i = 0; i < INPUT_COMPONENTS_FAMILY.length; i++){ 306 if (name != null && name.equals(INPUT_COMPONENTS_FAMILY[i])) 307 return (true); 308 } 309 return (false); 310 } 311 312 public static String getAttribute(UIComponent comp, String attribute){ 313 Map attrs = comp.getAttributes(); 314 315 Iterator iter = attrs.keySet().iterator(); 316 while (iter.hasNext()) 317 log.debug("attr:" + iter.next()); 318 319 String value = (String )attrs.get(attribute); 320 if (value != null){ 321 log.debug("Contains name:" + value); 322 return(value); 323 } 324 325 return(null); 326 } 327 } 328 | Popular Tags |