View Javadoc

1   /*
2    * Copyright 2001-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   
17  package org.apache.commons.beanutils.locale;
18  
19  
20  import org.apache.commons.beanutils.*;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import java.beans.IndexedPropertyDescriptor;
25  import java.beans.PropertyDescriptor;
26  import java.lang.reflect.InvocationTargetException;
27  import java.util.Locale;
28  
29  
30  /**
31   * <p>Utility methods for populating JavaBeans properties
32   * via reflection in a locale-dependent manner.</p>
33   *
34   * <p>The implementations for these methods are provided by <code>LocaleBeanUtilsBean</code>.
35   * For more details see {@link LocaleBeanUtilsBean}.</p>
36   *
37   * @author Craig R. McClanahan
38   * @author Ralph Schaer
39   * @author Chris Audley
40   * @author Rey Fran?ois
41   * @author Gregor Ra?man
42   * @author Yauheny Mikulski
43   */
44  
45  public class LocaleBeanUtils extends BeanUtils {
46  
47  
48      // ----------------------------------------------------- Instance Variables
49  
50      /** All logging goes through this logger */
51      private static Log log = LogFactory.getLog(LocaleBeanUtils.class);
52  
53      /**
54       * <p>Gets the locale used when no locale is passed.</p>
55       *
56       * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
57       *
58       * @see LocaleBeanUtilsBean#getDefaultLocale()
59       */
60      public static Locale getDefaultLocale() {
61  
62          return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getDefaultLocale();
63      }
64  
65  
66      /**
67       * <p>Sets the locale used when no locale is passed.</p>
68       *
69       * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
70       *
71       * @see LocaleBeanUtilsBean#setDefaultLocale(Locale)
72       */
73      public static void setDefaultLocale(Locale locale) {
74  
75          LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().setDefaultLocale(locale);
76      }
77  
78      /**
79       * <p>Gets whether the pattern is localized or not.</p>
80       *
81       * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
82       *
83       * @see LocaleBeanUtilsBean#getApplyLocalized()
84       */
85      public static boolean getApplyLocalized() {
86  
87          return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getApplyLocalized();
88      }
89  
90      /**
91       * <p>Sets whether the pattern is localized or not.</p>
92       *
93       * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
94       *
95       * @see LocaleBeanUtilsBean#setApplyLocalized(boolean)
96       */
97      public static void setApplyLocalized(boolean newApplyLocalized) {
98  
99          LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().setApplyLocalized(newApplyLocalized);
100     }
101 
102 
103     // --------------------------------------------------------- Public Methods
104 
105     /**
106      * <p>Return the value of the specified locale-sensitive indexed property
107      * of the specified bean, as a String.</p>
108      *
109      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
110      *
111      * @see LocaleBeanUtilsBean#getIndexedProperty(Object, String, String)
112      */
113     public static String getIndexedProperty(Object bean, String name, String pattern)
114             throws IllegalAccessException, InvocationTargetException,
115             NoSuchMethodException {
116 
117         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getIndexedProperty(bean, name, pattern);
118     }
119 
120     /**
121      * Return the value of the specified locale-sensitive indexed property
122      * of the specified bean, as a String using the default convertion pattern of
123      * the corresponding {@link LocaleConverter}.
124      *
125      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
126      *
127      * @see LocaleBeanUtilsBean#getIndexedProperty(Object, String)
128      */
129     public static String getIndexedProperty(Object bean, String name)
130             throws IllegalAccessException, InvocationTargetException,
131             NoSuchMethodException {
132 
133         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getIndexedProperty(bean, name);
134     }
135 
136     /**
137      * <p>Return the value of the specified locale-sensetive indexed property
138      * of the specified bean, as a String using the specified convertion pattern.</p>
139      *
140      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
141      *
142      * @see LocaleBeanUtilsBean#getIndexedProperty(Object, String, int, String)
143      */
144     public static String getIndexedProperty(Object bean,
145                                             String name, int index, String pattern)
146             throws IllegalAccessException, InvocationTargetException,
147             NoSuchMethodException {
148 
149         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getIndexedProperty(bean, name, index, pattern);
150     }
151 
152     /**
153      * <p>Return the value of the specified locale-sensetive indexed property
154      * of the specified bean, as a String using the default convertion pattern of
155      * the corresponding {@link LocaleConverter}.</p>
156      *
157      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
158      *
159      * @see LocaleBeanUtilsBean#getIndexedProperty(Object, String, int)
160      */
161     public static String getIndexedProperty(Object bean,
162                                             String name, int index)
163             throws IllegalAccessException, InvocationTargetException,
164             NoSuchMethodException {
165         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getIndexedProperty(bean, name, index);
166     }
167 
168     /**
169      * <p>Return the value of the specified simple locale-sensitive property
170      * of the specified bean, converted to a String using the specified
171      * convertion pattern.</p>
172      *
173      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
174      *
175      * @see LocaleBeanUtilsBean#getSimpleProperty(Object, String, String)
176      */
177     public static String getSimpleProperty(Object bean, String name, String pattern)
178             throws IllegalAccessException, InvocationTargetException,
179             NoSuchMethodException {
180 
181         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getSimpleProperty(bean, name, pattern);
182     }
183 
184     /**
185      * <p>Return the value of the specified simple locale-sensitive property
186      * of the specified bean, converted to a String using the default
187      * convertion pattern of the corresponding {@link LocaleConverter}.</p>
188      *
189      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
190      *
191      * @see LocaleBeanUtilsBean#getSimpleProperty(Object, String)
192      */
193     public static String getSimpleProperty(Object bean, String name)
194             throws IllegalAccessException, InvocationTargetException,
195             NoSuchMethodException {
196 
197         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getSimpleProperty(bean, name);
198     }
199 
200     /**
201      * <p>Return the value of the specified mapped locale-sensitive property
202      * of the specified bean, as a String using the specified convertion pattern.</p>
203      *
204      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
205      *
206      * @see LocaleBeanUtilsBean#getMappedProperty(Object, String, String, String)
207      */
208     public static String getMappedProperty(Object bean,
209                                            String name, String key, String pattern)
210             throws IllegalAccessException, InvocationTargetException,
211             NoSuchMethodException {
212 
213         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getMappedProperty(bean, name, key, pattern);
214     }
215 
216     /**
217      * <p>Return the value of the specified mapped locale-sensitive property
218      * of the specified bean, as a String
219      * The key is specified as a method parameter and must *not* be included
220      * in the property name expression.</p>
221      *
222      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
223      *
224      * @see LocaleBeanUtilsBean#getMappedProperty(Object, String, String)
225      */
226     public static String getMappedProperty(Object bean,
227                                            String name, String key)
228             throws IllegalAccessException, InvocationTargetException,
229             NoSuchMethodException {
230 
231         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getMappedProperty(bean, name, key);
232     }
233 
234 
235     /**
236      * <p>Return the value of the specified locale-sensitive mapped property
237      * of the specified bean, as a String using the specified pattern.</p>
238      *
239      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
240      *
241      * @see LocaleBeanUtilsBean#getMappedPropertyLocale(Object, String, String)
242      */
243     public static String getMappedPropertyLocale(Object bean, String name, String pattern)
244             throws IllegalAccessException, InvocationTargetException,
245             NoSuchMethodException {
246 
247         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getMappedPropertyLocale(bean, name, pattern);
248     }
249 
250 
251     /**
252      * <p>Return the value of the specified locale-sensitive mapped property
253      * of the specified bean, as a String using the default
254      * convertion pattern of the corresponding {@link LocaleConverter}.</p>
255      *
256      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
257      *
258      * @see LocaleBeanUtilsBean#getMappedProperty(Object, String)
259      */
260     public static String getMappedProperty(Object bean, String name)
261             throws IllegalAccessException, InvocationTargetException,
262             NoSuchMethodException {
263 
264         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getMappedProperty(bean, name);
265     }
266 
267     /**
268      * <p>Return the value of the (possibly nested) locale-sensitive property
269      * of the specified name, for the specified bean,
270      * as a String using the specified pattern.</p>
271      *
272      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
273      *
274      * @see LocaleBeanUtilsBean#getNestedProperty(Object, String, String)
275      */
276     public static String getNestedProperty(Object bean, String name, String pattern)
277             throws IllegalAccessException, InvocationTargetException,
278             NoSuchMethodException {
279 
280         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getNestedProperty(bean, name, pattern);
281     }
282 
283     /**
284      * <p>Return the value of the (possibly nested) locale-sensitive property
285      * of the specified name.</p>
286      *
287      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
288      *
289      * @see LocaleBeanUtilsBean#getNestedProperty(Object, String)
290      */
291     public static String getNestedProperty(Object bean, String name)
292             throws IllegalAccessException, InvocationTargetException,
293             NoSuchMethodException {
294 
295         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getNestedProperty(bean, name);
296     }
297 
298     /**
299      * <p>Return the value of the specified locale-sensitive property
300      * of the specified bean.</p>
301      *
302      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
303      *
304      * @see LocaleBeanUtilsBean#getProperty(Object, String, String)
305      */
306     public static String getProperty(Object bean, String name, String pattern)
307             throws IllegalAccessException, InvocationTargetException,
308             NoSuchMethodException {
309 
310         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getProperty(bean, name, pattern);
311     }
312 
313     /**
314      * <p>Return the value of the specified locale-sensitive property
315      * of the specified bean.</p>
316      *
317      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
318      *
319      * @see LocaleBeanUtilsBean#getProperty(Object, String)
320      */
321     public static String getProperty(Object bean, String name)
322             throws IllegalAccessException, InvocationTargetException,
323             NoSuchMethodException {
324 
325         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getProperty(bean, name);
326     }
327 
328     /**
329      * <p>Set the specified locale-sensitive property value, performing type
330      * conversions as required to conform to the type of the destination property
331      * using the default convertion pattern of the corresponding {@link LocaleConverter}.</p>
332      *
333      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
334      *
335      * @see LocaleBeanUtilsBean#setProperty(Object, String, Object)
336      */
337     public static void setProperty(Object bean, String name, Object value)
338             throws IllegalAccessException, InvocationTargetException {
339 
340         LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().setProperty(bean, name, value);
341     }
342 
343     /**
344      * <p>Set the specified locale-sensitive property value, performing type
345      * conversions as required to conform to the type of the destination
346      * property using the specified convertion pattern.</p>
347      *
348      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
349      *
350      * @see LocaleBeanUtilsBean#setProperty(Object, String, Object, String)
351      */
352     public static void setProperty(Object bean, String name, Object value, String pattern)
353             throws IllegalAccessException, InvocationTargetException {
354 
355         LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().setProperty(bean, name, value, pattern);
356      }
357 
358     /**
359      * <p>Calculate the property type.</p>
360      *
361      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
362      *
363      * @see LocaleBeanUtilsBean#definePropertyType(Object, String, String)
364      */
365     protected static Class definePropertyType(Object target, String name, String propName)
366             throws IllegalAccessException, InvocationTargetException {
367 
368         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().definePropertyType(target, name, propName);
369     }
370 
371     /**
372      * <p>Convert the specified value to the required type using the
373      * specified convertion pattern.</p>
374      *
375      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
376      *
377      * @see LocaleBeanUtilsBean#convert(Class, int, Object, String)
378      */
379     protected static Object convert(Class type, int index, Object value, String pattern) {
380 
381         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().convert(type, index, value, pattern);
382     }
383 
384     /**
385      * <p>Convert the specified value to the required type.</p>
386      *
387      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
388      *
389      * @see LocaleBeanUtilsBean#convert(Class, int, Object)
390      */
391     protected static Object convert(Class type, int index, Object value) {
392 
393         return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().convert(type, index, value);
394     }
395 
396     /**
397      * <p>Invoke the setter method.</p>
398      *
399      * <p>For more details see <code>LocaleBeanUtilsBean</code></p>
400      *
401      * @see LocaleBeanUtilsBean#invokeSetter(Object, String, String, int, Object)
402      */
403     protected static void invokeSetter(Object target, String propName, String key, int index, Object newValue)
404             throws IllegalAccessException, InvocationTargetException {
405 
406        LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().invokeSetter(target, propName, key, index, newValue);
407     }
408 
409     /**
410      * Resolve any nested expression to get the actual target bean.
411      *
412      * @deprecated moved into <code>LocaleBeanUtilsBean</code>
413      * @param bean The bean
414      * @param name The property name
415      *
416      * @exception IllegalAccessException if the caller does not have
417      *  access to the property accessor method
418      * @exception InvocationTargetException if the property accessor method
419      *  throws an exception
420      */
421     protected static Descriptor calculate(Object bean, String name)
422             throws IllegalAccessException, InvocationTargetException {
423 
424         String propName = null;          // Simple name of target property
425         int index = -1;                  // Indexed subscript value (if any)
426         String key = null;               // Mapped key value (if any)
427 
428         Object target = bean;
429         int delim = name.lastIndexOf(PropertyUtils.NESTED_DELIM);
430         if (delim >= 0) {
431             try {
432                 target =
433                         PropertyUtils.getProperty(bean, name.substring(0, delim));
434             }
435             catch (NoSuchMethodException e) {
436                 return null; // Skip this property setter
437             }
438             name = name.substring(delim + 1);
439             if (log.isTraceEnabled()) {
440                 log.trace("    Target bean = " + target);
441                 log.trace("    Target name = " + name);
442             }
443         }
444 
445         // Calculate the property name, index, and key values
446         propName = name;
447         int i = propName.indexOf(PropertyUtils.INDEXED_DELIM);
448         if (i >= 0) {
449             int k = propName.indexOf(PropertyUtils.INDEXED_DELIM2);
450             try {
451                 index =
452                         Integer.parseInt(propName.substring(i + 1, k));
453             }
454             catch (NumberFormatException e) {
455                 ;
456             }
457             propName = propName.substring(0, i);
458         }
459         int j = propName.indexOf(PropertyUtils.MAPPED_DELIM);
460         if (j >= 0) {
461             int k = propName.indexOf(PropertyUtils.MAPPED_DELIM2);
462             try {
463                 key = propName.substring(j + 1, k);
464             }
465             catch (IndexOutOfBoundsException e) {
466                 ;
467             }
468             propName = propName.substring(0, j);
469         }
470         return new Descriptor(target, name, propName, key, index);
471     }
472 
473     /** @deprecated moved into <code>LocaleBeanUtils</code> */
474     protected static class Descriptor {
475 
476         private int index = -1;    // Indexed subscript value (if any)
477         private String name;
478         private String propName;   // Simple name of target property
479         private String key;        // Mapped key value (if any)
480         private Object target;
481 
482         public Descriptor(Object target, String name, String propName, String key, int index) {
483 
484             setTarget(target);
485             setName(name);
486             setPropName(propName);
487             setKey(key);
488             setIndex(index);
489         }
490 
491         public Object getTarget() {
492             return target;
493         }
494 
495         public void setTarget(Object target) {
496             this.target = target;
497         }
498 
499         public String getKey() {
500             return key;
501         }
502 
503         public void setKey(String key) {
504             this.key = key;
505         }
506 
507         public int getIndex() {
508             return index;
509         }
510 
511         public void setIndex(int index) {
512             this.index = index;
513         }
514 
515         public String getName() {
516             return name;
517         }
518 
519         public void setName(String name) {
520             this.name = name;
521         }
522 
523         public String getPropName() {
524             return propName;
525         }
526 
527         public void setPropName(String propName) {
528             this.propName = propName;
529         }
530     }
531 }
532 
533