KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

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

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

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