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