📄 localebeanutilsbean.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.beanutils.locale;import org.apache.commons.beanutils.*;import org.apache.commons.beanutils.ContextClassLoaderLocal;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import java.beans.IndexedPropertyDescriptor;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.util.Locale;/** * <p>Utility methods for populating JavaBeans properties * via reflection in a locale-dependent manner.</p> * * @author Craig R. McClanahan * @author Ralph Schaer * @author Chris Audley * @author Rey Fran鏾is * @author Gregor Ra齧an * @author Yauheny Mikulski * @since 1.7 */public class LocaleBeanUtilsBean extends BeanUtilsBean { /** * Contains <code>LocaleBeanUtilsBean</code> instances indexed by context classloader. */ private static final ContextClassLoaderLocal localeBeansByClassLoader = new ContextClassLoaderLocal() { // Creates the default instance used when the context classloader is unavailable protected Object initialValue() { return new LocaleBeanUtilsBean(); } }; /** Gets singleton instance */ public synchronized static LocaleBeanUtilsBean getLocaleBeanUtilsInstance() { return (LocaleBeanUtilsBean)localeBeansByClassLoader.get(); } /** * Sets the instance which provides the functionality for {@link LocaleBeanUtils}. * This is a pseudo-singleton - an single instance is provided per (thread) context classloader. * This mechanism provides isolation for web apps deployed in the same container. */ public synchronized static void setInstance(LocaleBeanUtilsBean newInstance) { localeBeansByClassLoader.set(newInstance); } /** All logging goes through this logger */ private static Log log = LogFactory.getLog(LocaleBeanUtilsBean.class); // ----------------------------------------------------- Instance Variables /** Convertor used by this class */ private LocaleConvertUtilsBean localeConvertUtils; // --------------------------------------------------------- Constructors /** Construct instance with standard conversion bean */ public LocaleBeanUtilsBean() { this.localeConvertUtils = new LocaleConvertUtilsBean(); } /** * Construct instance that uses given locale conversion * * @param localeConvertUtils use this <code>localeConvertUtils</code> to perform * conversions * @param convertUtilsBean use this for standard conversions * @param propertyUtilsBean use this for property conversions */ public LocaleBeanUtilsBean( LocaleConvertUtilsBean localeConvertUtils, ConvertUtilsBean convertUtilsBean, PropertyUtilsBean propertyUtilsBean) { super(convertUtilsBean, propertyUtilsBean); this.localeConvertUtils = localeConvertUtils; } /** * Construct instance that uses given locale conversion * * @param localeConvertUtils use this <code>localeConvertUtils</code> to perform * conversions */ public LocaleBeanUtilsBean(LocaleConvertUtilsBean localeConvertUtils) { this.localeConvertUtils = localeConvertUtils; } // --------------------------------------------------------- Public Methods /** Gets the bean instance used for conversions */ public LocaleConvertUtilsBean getLocaleConvertUtils() { return localeConvertUtils; } /** * Gets the default Locale */ public Locale getDefaultLocale() { return getLocaleConvertUtils().getDefaultLocale(); } /** * Sets the default Locale */ public void setDefaultLocale(Locale locale) { getLocaleConvertUtils().setDefaultLocale(locale); } /** * Is the pattern to be applied localized * (Indicate whether the pattern is localized or not) */ public boolean getApplyLocalized() { return getLocaleConvertUtils().getApplyLocalized(); } /** * Sets whether the pattern is applied localized * (Indicate whether the pattern is localized or not) */ public void setApplyLocalized(boolean newApplyLocalized) { getLocaleConvertUtils().setApplyLocalized(newApplyLocalized); } // --------------------------------------------------------- Public Methods /** * Return the value of the specified locale-sensitive indexed property * of the specified bean, as a String. The zero-relative index of the * required value must be included (in square brackets) as a suffix to * the property name, or <code>IllegalArgumentException</code> will be * thrown. * * @param bean Bean whose property is to be extracted * @param name <code>propertyname[index]</code> of the property value * to be extracted * @param pattern The convertion pattern * * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception InvocationTargetException if the property accessor method * throws an exception * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public String getIndexedProperty( Object bean, String name, String pattern) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = getPropertyUtils().getIndexedProperty(bean, name); return getLocaleConvertUtils().convert(value, pattern); } /** * Return the value of the specified locale-sensitive indexed property * of the specified bean, as a String using the default convertion pattern of * the corresponding {@link LocaleConverter}. The zero-relative index * of the required value must be included (in square brackets) as a suffix * to the property name, or <code>IllegalArgumentException</code> will be thrown. * * @param bean Bean whose property is to be extracted * @param name <code>propertyname[index]</code> of the property value * to be extracted * * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception InvocationTargetException if the property accessor method * throws an exception * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public String getIndexedProperty( Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { return getIndexedProperty(bean, name, null); } /** * Return the value of the specified locale-sensetive indexed property * of the specified bean, as a String using the specified convertion pattern. * The index is specified as a method parameter and * must *not* be included in the property name expression * * @param bean Bean whose property is to be extracted * @param name Simple property name of the property value to be extracted * @param index Index of the property value to be extracted * @param pattern The convertion pattern * * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception InvocationTargetException if the property accessor method * throws an exception * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public String getIndexedProperty(Object bean, String name, int index, String pattern) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = getPropertyUtils().getIndexedProperty(bean, name, index); return getLocaleConvertUtils().convert(value, pattern); } /** * Return the value of the specified locale-sensetive indexed property * of the specified bean, as a String using the default convertion pattern of * the corresponding {@link LocaleConverter}. * The index is specified as a method parameter and * must *not* be included in the property name expression * * @param bean Bean whose property is to be extracted * @param name Simple property name of the property value to be extracted * @param index Index of the property value to be extracted * * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception InvocationTargetException if the property accessor method * throws an exception * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public String getIndexedProperty(Object bean, String name, int index) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { return getIndexedProperty(bean, name, index, null); } /** * Return the value of the specified simple locale-sensitive property * of the specified bean, converted to a String using the specified * convertion pattern. * * @param bean Bean whose property is to be extracted * @param name Name of the property to be extracted * @param pattern The convertion pattern * * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception InvocationTargetException if the property accessor method * throws an exception * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public String getSimpleProperty(Object bean, String name, String pattern) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = getPropertyUtils().getSimpleProperty(bean, name); return getLocaleConvertUtils().convert(value, pattern); } /** * Return the value of the specified simple locale-sensitive property * of the specified bean, converted to a String using the default * convertion pattern of the corresponding {@link LocaleConverter}. * * @param bean Bean whose property is to be extracted * @param name Name of the property to be extracted * * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception InvocationTargetException if the property accessor method * throws an exception * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public String getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { return getSimpleProperty(bean, name, null); } /** * Return the value of the specified mapped locale-sensitive property * of the specified bean, as a String using the specified convertion pattern. * The key is specified as a method parameter and must *not* be included in * the property name expression.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -