1 16 package org.apache.myfaces.renderkit; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 21 import javax.faces.FacesException; 22 import javax.faces.component.UIOutput; 23 import javax.faces.component.UISelectMany; 24 import javax.faces.context.FacesContext; 25 import javax.faces.convert.Converter; 26 import javax.faces.convert.ConverterException; 27 import javax.faces.el.ValueBinding; 28 import java.lang.reflect.Array ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 32 53 class _SharedRendererUtils 54 { 55 static Converter findUIOutputConverter(FacesContext facesContext, UIOutput component) 56 { 57 61 Converter converter = component.getConverter(); 62 if (converter != null) return converter; 63 64 ValueBinding vb = component.getValueBinding("value"); 66 if (vb == null) return null; 67 68 Class valueType = vb.getType(facesContext); 69 if (valueType == null) return null; 70 71 if (String .class.equals(valueType)) return null; if (Object .class.equals(valueType)) return null; 74 try 75 { 76 return facesContext.getApplication().createConverter(valueType); 77 } 78 catch (FacesException e) 79 { 80 log(facesContext, "No Converter for type " + valueType.getName() + " found", e); 81 return null; 82 } 83 } 84 85 static Object getConvertedUISelectManyValue(FacesContext facesContext, 86 UISelectMany component, 87 String [] submittedValue) 88 throws ConverterException 89 { 90 94 if (submittedValue == null) throw new NullPointerException ("submittedValue"); 95 96 ValueBinding vb = component.getValueBinding("value"); 97 Class valueType = null; 98 Class arrayComponentType = null; 99 if (vb != null) 100 { 101 valueType = vb.getType(facesContext); 102 if (valueType != null && valueType.isArray()) 103 { 104 arrayComponentType = valueType.getComponentType(); 105 } 106 } 107 108 Converter converter = component.getConverter(); 109 if (converter == null) 110 { 111 if (valueType == null) 112 { 113 return submittedValue; 116 } 117 118 if (List .class.isAssignableFrom(valueType)) 119 { 120 int len = submittedValue.length; 124 List lst = new ArrayList (len); 125 for (int i = 0; i < len; i++) 126 { 127 lst.add(submittedValue[i]); 128 } 129 return lst; 130 } 131 132 if (arrayComponentType == null) 133 { 134 throw new IllegalArgumentException ("ValueBinding for UISelectMany must be of type List or Array"); 135 } 136 137 if (String .class.equals(arrayComponentType)) return submittedValue; if (Object .class.equals(arrayComponentType)) return submittedValue; 140 try 141 { 142 converter = facesContext.getApplication().createConverter(arrayComponentType); 143 } 144 catch (FacesException e) 145 { 146 log(facesContext, "No Converter for type " + arrayComponentType.getName() + " found", e); 147 return submittedValue; 148 } 149 } 150 151 if (valueType == null) 153 { 154 int len = submittedValue.length; 157 Object [] convertedValues = new Object [len]; 158 for (int i = 0; i < len; i++) 159 { 160 convertedValues[i] 161 = converter.getAsObject(facesContext, component, submittedValue[i]); 162 } 163 return convertedValues; 164 } 165 166 if (List .class.isAssignableFrom(valueType)) 167 { 168 int len = submittedValue.length; 172 List lst = new ArrayList (len); 173 for (int i = 0; i < len; i++) 174 { 175 lst.add(converter.getAsObject(facesContext, component, submittedValue[i])); 176 } 177 return lst; 178 } 179 180 if (arrayComponentType == null) 181 { 182 throw new IllegalArgumentException ("ValueBinding for UISelectMany must be of type List or Array"); 183 } 184 185 if (arrayComponentType.isPrimitive()) 186 { 187 int len = submittedValue.length; 189 Object convertedValues = Array.newInstance(arrayComponentType, len); 190 for (int i = 0; i < len; i++) 191 { 192 Array.set(convertedValues, i, 193 converter.getAsObject(facesContext, component, submittedValue[i])); 194 } 195 return convertedValues; 196 } 197 else 198 { 199 int len = submittedValue.length; 201 Object [] convertedValues = new Object [len]; 202 for (int i = 0; i < len; i++) 203 { 204 convertedValues[i] 205 = converter.getAsObject(facesContext, component, submittedValue[i]); 206 } 207 return convertedValues; 208 } 209 } 210 211 212 213 private static final Log log = LogFactory.getLog(_SharedRendererUtils.class); 214 215 218 private static void log(FacesContext context, String msg, Exception e) 219 { 220 log.error(msg, e); 221 } 222 } 223 | Popular Tags |