KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > component > _SharedRendererUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * The util methods in this class are shared between the javax.faces.component package and
29  * the org.apache.myfaces.renderkit package.
30  * Please note: Any changes here must also apply to the class in the other package!
31  *
32  * @author Manfred Geiler (latest modification by $Author: manolito $)
33  * @version $Revision: 1.6 $ $Date: 2005/04/06 19:39:30 $
34  * $Log: _SharedRendererUtils.java,v $
35  * Revision 1.6 2005/04/06 19:39:30 manolito
36  * MYFACES-157 patch removed, not necessary because of MYFACES-149 fix
37  *
38  * Revision 1.5 2005/04/06 10:21:55 manolito
39  * MYFACES-149 fix for NullPointerException in _SharedRendererUtils.getConvertedUISelectManyValue
40  *
41  * Revision 1.4 2005/03/31 22:47:14 schof
42  * Whitney Hunter's patch for MYFACES-157
43  *
44  * Revision 1.3 2004/10/13 11:50:59 matze
45  * renamed packages to org.apache
46  *
47  * Revision 1.2 2004/07/01 22:00:50 mwessendorf
48  * ASF switch
49  *
50  * Revision 1.1 2004/04/06 13:03:35 manolito
51  * x-checked getConvertedValue method in api and impl
52  *
53  */

54 class _SharedRendererUtils
55 {
56     static Converter findUIOutputConverter(FacesContext facesContext, UIOutput component)
57     {
58         // Attention!
59
// This code is duplicated in myfaces implementation renderkit package.
60
// If you change something here please do the same in the other class!
61

62         Converter converter = component.getConverter();
63         if (converter != null) return converter;
64
65         //Try to find out by value binding
66
ValueBinding vb = component.getValueBinding("value");
67         if (vb == null) return null;
68
69         Class JavaDoc valueType = vb.getType(facesContext);
70         if (valueType == null) return null;
71
72         if (String JavaDoc.class.equals(valueType)) return null; //No converter needed for String type
73
if (Object JavaDoc.class.equals(valueType)) return null; //There is no converter for Object class
74

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 JavaDoc getConvertedUISelectManyValue(FacesContext facesContext,
87                                                 UISelectMany component,
88                                                 String JavaDoc[] submittedValue)
89             throws ConverterException
90     {
91         // Attention!
92
// This code is duplicated in myfaces implementation renderkit package.
93
// If you change something here please do the same in the other class!
94

95         if (submittedValue == null) throw new NullPointerException JavaDoc("submittedValue");
96
97         ValueBinding vb = component.getValueBinding("value");
98         Class JavaDoc valueType = null;
99         Class JavaDoc 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                 // No converter, and no idea of expected type
115
// --> return the submitted String array
116
return submittedValue;
117             }
118
119             if (List JavaDoc.class.isAssignableFrom(valueType))
120             {
121                 // expected type is a List
122
// --> according to javadoc of UISelectMany we assume that the element type
123
// is java.lang.String, and copy the String array to a new List
124
int len = submittedValue.length;
125                 List JavaDoc lst = new ArrayList JavaDoc(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 JavaDoc("ValueBinding for UISelectMany must be of type List or Array");
136             }
137
138             if (String JavaDoc.class.equals(arrayComponentType)) return submittedValue; //No conversion needed for String type
139
if (Object JavaDoc.class.equals(arrayComponentType)) return submittedValue; //No conversion for Object class
140

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         // Now, we have a converter...
153
if (valueType == null)
154         {
155             // ...but have no idea of expected type
156
// --> so let's convert it to an Object array
157
int len = submittedValue.length;
158             Object JavaDoc[] convertedValues = new Object JavaDoc[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 JavaDoc.class.isAssignableFrom(valueType))
168         {
169             // Curious case: According to specs we should assume, that the element type
170
// of this List is java.lang.String. But there is a Converter set for this
171
// component. Because the user must know what he is doing, we will convert the values.
172
int len = submittedValue.length;
173             List JavaDoc lst = new ArrayList JavaDoc(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 JavaDoc("ValueBinding for UISelectMany must be of type List or Array");
184         }
185
186         if (arrayComponentType.isPrimitive())
187         {
188             //primitive array
189
int len = submittedValue.length;
190             Object JavaDoc 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             //Object array
201
int len = submittedValue.length;
202             Object JavaDoc[] convertedValues = new Object JavaDoc[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     /**
215      * This method is different in the two versions of _SharedRendererUtils.
216      */

217     private static void log(FacesContext context, String JavaDoc msg, Exception JavaDoc e)
218     {
219         context.getExternalContext().log(msg, e);
220     }
221 }
222
Popular Tags