⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tableproperties.java

📁 dispalytag的源码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    /**     * Property <code>comparator.default</code>.  If present, will use use as the classname of the default comparator.     * Will be overriden by column level comparators.     */    public static final String PROPERTY_DEFAULT_COMPARATOR = "comparator.default"; //$NON-NLS-1$    // </JBN>    /**     * Separator char used in property names.     */    private static final char SEP = '.';    /**     * logger.     */    private static Log log = LogFactory.getLog(TableProperties.class);    /**     * The userProperties are local, non-default properties; these settings override the defaults from     * displaytag.properties and TableTag.properties.     */    private static Properties userProperties = new Properties();    /**     * Configured resource provider. If no ResourceProvider is configured, an no-op one is used. This instance is     * initialized at first use and shared.     */    private static I18nResourceProvider resourceProvider;    /**     * Configured locale resolver.     */    private static LocaleResolver localeResolver;    /**     * TableProperties for each locale are loaded as needed, and cloned for public usage.     */    private static Map prototypes = new HashMap();    /**     * Loaded properties (defaults from defaultProperties + custom from bundle).     */    private Properties properties;    /**     * The locale for these properties.     */    private Locale locale;    /**     * Cache for dinamically instantiated object (request factory, decorator factory).     */    private Map objectCache = new HashMap();    /**     * Setter for I18nResourceProvider. A resource provider is usually set using displaytag properties, this accessor is     * needed for tests.     * @param provider I18nResourceProvider instance     */    protected static void setResourceProvider(I18nResourceProvider provider)    {        resourceProvider = provider;    }    /**     * Setter for LocaleResolver. A locale resolver is usually set using displaytag properties, this accessor is needed     * for tests.     * @param resolver LocaleResolver instance     */    protected static void setLocaleResolver(LocaleResolver resolver)    {        localeResolver = resolver;    }    /**     * Loads default properties (TableTag.properties).     * @return loaded properties     * @throws TablePropertiesLoadException if default properties file can't be found     */    private static Properties loadBuiltInProperties() throws TablePropertiesLoadException    {        Properties defaultProperties = new Properties();        try        {            InputStream is = TableProperties.class.getResourceAsStream(DEFAULT_FILENAME);            if (is == null)            {                throw new TablePropertiesLoadException(TableProperties.class, DEFAULT_FILENAME, null);            }            defaultProperties.load(is);        }        catch (IOException e)        {            throw new TablePropertiesLoadException(TableProperties.class, DEFAULT_FILENAME, e);        }        return defaultProperties;    }    /**     * Loads user properties (displaytag.properties) according to the given locale. User properties are not guarantee to     * exist, so the method can return <code>null</code> (no exception will be thrown).     * @param locale requested Locale     * @return loaded properties     */    private static ResourceBundle loadUserProperties(Locale locale)    {        ResourceBundle bundle = null;        try        {            bundle = ResourceBundle.getBundle(LOCAL_PROPERTIES, locale);        }        catch (MissingResourceException e)        {            // if no resource bundle is found, try using the context classloader            try            {                bundle = ResourceBundle.getBundle(LOCAL_PROPERTIES, locale, Thread                    .currentThread()                    .getContextClassLoader());            }            catch (MissingResourceException mre)            {                if (log.isDebugEnabled())                {                    log.debug(Messages.getString("TableProperties.propertiesnotfound", //$NON-NLS-1$                        new Object[]{mre.getMessage()}));                }            }        }        return bundle;    }    /**     * Returns the configured Locale Resolver. This method is called before the loading of localized properties.     * @return LocaleResolver instance.     * @throws TablePropertiesLoadException if the default <code>TableTag.properties</code> file is not found.     */    public static LocaleResolver getLocaleResolverInstance() throws TablePropertiesLoadException    {        if (localeResolver == null)        {            // special handling, table properties is not yet instantiated            String className = null;            ResourceBundle defaultUserProperties = loadUserProperties(Locale.getDefault());            // if available, user properties have higher precedence            if (defaultUserProperties != null)            {                try                {                    className = defaultUserProperties.getString(PROPERTY_CLASS_LOCALERESOLVER);                }                catch (MissingResourceException e)                {                    // no problem                }            }            // still null? load defaults            if (className == null)            {                Properties defaults = loadBuiltInProperties();                className = defaults.getProperty(PROPERTY_CLASS_LOCALERESOLVER);            }            if (className != null)            {                try                {                    Class classProperty = ReflectHelper.classForName(className);                    localeResolver = (LocaleResolver) classProperty.newInstance();                    log.info(Messages.getString("TableProperties.classinitializedto", //$NON-NLS-1$                        new Object[]{ClassUtils.getShortClassName(LocaleResolver.class), className}));                }                catch (Throwable e)                {                    log.warn(Messages.getString("TableProperties.errorloading", //$NON-NLS-1$                        new Object[]{                            ClassUtils.getShortClassName(LocaleResolver.class),                            e.getClass().getName(),                            e.getMessage()}));                }            }            else            {                log.info(Messages.getString("TableProperties.noconfigured", //$NON-NLS-1$                    new Object[]{ClassUtils.getShortClassName(LocaleResolver.class)}));            }            // still null?            if (localeResolver == null)            {                // fallback locale resolver                localeResolver = new LocaleResolver()                {                    public Locale resolveLocale(HttpServletRequest request)                    {                        return request.getLocale();                    }                };            }        }        return localeResolver;    }    /**     * Initialize a new TableProperties loading the default properties file and the user defined one. There is no     * caching used here, caching is assumed to occur in the getInstance factory method.     * @param myLocale the locale we are in     * @throws TablePropertiesLoadException for errors during loading of properties files     */    private TableProperties(Locale myLocale) throws TablePropertiesLoadException    {        this.locale = myLocale;        // default properties will not change unless this class is reloaded        Properties defaultProperties = loadBuiltInProperties();        properties = new Properties(defaultProperties);        addProperties(myLocale);        // Now copy in the user properties (properties file set by calling setUserProperties()).        // note setUserProperties() MUST BE CALLED before the first TableProperties instantation        Enumeration keys = userProperties.keys();        while (keys.hasMoreElements())        {            String key = (String) keys.nextElement();            if (key != null)            {                properties.setProperty(key, (String) userProperties.get(key));            }        }    }    /**     * Try to load the properties from the local properties file, displaytag.properties, and merge them into the     * existing properties.     * @param userLocale the locale from which the properties are to be loaded     */    private void addProperties(Locale userLocale)    {        ResourceBundle bundle = loadUserProperties(userLocale);        if (bundle != null)        {            Enumeration keys = bundle.getKeys();            while (keys.hasMoreElements())            {                String key = (String) keys.nextElement();                properties.setProperty(key, bundle.getString(key));            }        }    }    /**     * Clones the properties as well.     * @return a new clone of oneself     */    protected Object clone()    {        TableProperties twin;        try        {            twin = (TableProperties) super.clone();        }        catch (CloneNotSupportedException e)        {            // should never happen            throw new UnhandledException(e);        }        twin.properties = (Properties) this.properties.clone();        return twin;    }    /**     * Returns a new TableProperties instance for the given locale.     * @param request HttpServletRequest needed to extract the locale to use. If null the default locale will be used.     * @return TableProperties instance     */    public static TableProperties getInstance(HttpServletRequest request)    {        Locale locale;        if (request != null)        {            locale = getLocaleResolverInstance().resolveLocale(request);        }        else        {            // for some configuration parameters locale doesn't matter            locale = Locale.getDefault();        }        TableProperties props = (TableProperties) prototypes.get(locale);        if (props == null)        {            TableProperties lprops = new TableProperties(locale);            prototypes.put(locale, lprops);            props = lprops;        }        return (TableProperties) props.clone();    }    /**     * Unload all cached properties. This will not clear properties set by by setUserProperties; you must clear those     * manually.     */    public static void clearProperties()    {        prototypes.clear();    }    /**     * Local, non-default properties; these settings override the defaults from displaytag.properties and     * TableTag.properties. Please note that the values are copied in, so that multiple calls with non-overlapping     * properties will be merged, not overwritten. Note: setUserProperties() MUST BE CALLED before the first     * TableProperties instantation.     * @param overrideProperties - The local, non-default properties     */    public static void setUserProperties(Properties overrideProperties)    {        // copy keys here, so that this can be invoked more than once from different sources.        // if default properties are not yet loaded they will be copied in constructor        Enumeration keys = overrideProperties.keys();        while (keys.hasMoreElements())        {            String key = (String) keys.nextElement();            if (key != null)            {                userProperties.setProperty(key, (String) overrideProperties.get(key));            }        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -