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

📄 utilproperties.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        } catch (Exception e) {
            Debug.log(e.getMessage(), module);
        }
        return value == null ? "" : value.trim();
    }

    /** Returns the value of a split property name from the specified resource/properties file
     * Rather than specifying the property name the value of a name.X property is specified which
     * will correspond to a value.X property whose value will be returned. X is a number from 1 to
     * whatever and all values are checked until a name.X for a certain X is not found.
     * @param url URL object specifying the location of the resource
     * @param name The name of the split property in the properties file
     * @return The value of the split property from the properties file
     */
    public static String getSplitPropertyValue(URL url, String name) {
        if (url == null) return "";
        if (name == null || name.length() <= 0) return "";

        FlexibleProperties properties = (FlexibleProperties) urlCache.get(url);

        if (properties == null) {
            try {
                properties = FlexibleProperties.makeFlexibleProperties(url);
                urlCache.put(url, properties);
            } catch (MissingResourceException e) {
                Debug.log(e.getMessage(), module);
            }
        }
        if (properties == null) {
            Debug.log("[UtilProperties.getPropertyValue] could not find resource: " + url, module);
            return null;
        }

        String value = null;

        try {
            int curIdx = 1;
            String curName = null;

            while ((curName = properties.getProperty("name." + curIdx)) != null) {
                if (name.equals(curName)) {
                    value = properties.getProperty("value." + curIdx);
                    break;
                }
                curIdx++;
            }
        } catch (Exception e) {
            Debug.log(e.getMessage(), module);
        }
        return value == null ? "" : value.trim();
    }
    
    
    // ========= Locale & Resource Based Methods ==========

    /** Returns the value of the specified property name from the specified resource/properties file corresponding to the given locale.
     *  <br>
     *  <br> Two reasons why we do not use the FlexibleProperties class for this:
     *  <ul>
     *    <li>Doesn't support flexible locale based naming: try fname_locale (5 letter), then fname_locale (2 letter lang only), then fname</li>
     *    <li>Does not support parent properties/bundles so that if the fname_locale5 file doesn't have it then fname_locale2 is tried, then the fname bundle</li>
     *  </ul>
     * @param resource The name of the resource - can be a file, class, or URL
     * @param name The name of the property in the properties file
     * @param locale The locale that the given resource will correspond to
     * @return The value of the property in the properties file
     */
    public static String getMessage(String resource, String name, Locale locale) {
        if (resource == null || resource.length() <= 0) return "";
        if (name == null || name.length() <= 0) return "";

        ResourceBundle bundle = getResourceBundle(resource, locale);
        if (bundle == null) return "";

        String value = null;
        try {
            value = bundle.getString(name);
        } catch (Exception e) {
            Debug.log(e.getMessage(), module);
        }
        return value == null ? "" : value.trim();
    }

    /** Returns the value of the specified property name from the specified resource/properties file corresponding 
     * to the given locale and replacing argument place holders with the given arguments using the MessageFormat class
     * @param resource The name of the resource - can be a file, class, or URL
     * @param name The name of the property in the properties file
     * @param locale The locale that the given resource will correspond to
     * @param arguments An array of Objects to insert into the message argument place holders
     * @return The value of the property in the properties file
     */
    public static String getMessage(String resource, String name, Object[] arguments, Locale locale) {
        String value = getMessage(resource, name, locale);
        
        if (value == null || value.length() == 0) {
            return "";
        } else {
            if (arguments != null && arguments.length > 0) {
                value = MessageFormat.format(value, arguments);
            }   
            return value;
        }
    }

    /** Returns the value of the specified property name from the specified resource/properties file corresponding 
     * to the given locale and replacing argument place holders with the given arguments using the MessageFormat class
     * @param resource The name of the resource - can be a file, class, or URL
     * @param name The name of the property in the properties file
     * @param locale The locale that the given resource will correspond to
     * @param arguments A List of Objects to insert into the message argument place holders
     * @return The value of the property in the properties file
     */
    public static String getMessage(String resource, String name, List arguments, Locale locale) {
        String value = getMessage(resource, name, locale);
        
        if (value == null || value.length() == 0) {
            return "";
        } else {
            if (arguments != null && arguments.size() > 0) {
                value = MessageFormat.format(value, arguments.toArray());
            }   
            return value;
        }
    }

    /** Returns the value of the specified property name from the specified resource/properties file corresponding 
     * to the given locale and replacing argument place holders with the given arguments using the FlexibleStringExpander class
     * @param resource The name of the resource - can be a file, class, or URL
     * @param name The name of the property in the properties file
     * @param locale The locale that the given resource will correspond to
     * @param context A Map of Objects to insert into the message place holders using the ${} syntax of the FlexibleStringExpander
     * @return The value of the property in the properties file
     */
    public static String getMessage(String resource, String name, Map context, Locale locale) {
        String value = getMessage(resource, name, locale);
        
        if (value == null || value.length() == 0) {
            return "";
        } else {
            if (context != null && context.size() > 0) {
                value = FlexibleStringExpander.expandString(value, context);
            }   
            return value;
        }
    }

    /** Returns the specified resource/properties file as a ResourceBundle
     * @param resource The name of the resource - can be a file, class, or URL
     * @param locale The locale that the given resource will correspond to
     * @return The ResourceBundle
     */
    public static ResourceBundle getResourceBundle(String resource, Locale locale) {
        Map bundleMap = getResourceBundleMap(resource, locale);
        if (bundleMap == null) {
            return null;
        }
        return (ResourceBundle) bundleMap.get("_RESOURCE_BUNDLE_");
    }
    
    /** Returns the specified resource/properties file as a Map with the original ResourceBundle in the Map under the key _RESOURCE_BUNDLE_
     * @param resource The name of the resource - can be a file, class, or URL
     * @param locale The locale that the given resource will correspond to
     * @return Map containing all entries in The ResourceBundle
     */
    public static Map getResourceBundleMap(String resource, Locale locale) {
        if (locale == null) {
            throw new IllegalArgumentException("Locale cannot be null");
        }
        
        String resourceCacheKey = resource + "_" + locale.toString();        
        Map bundleMap = (Map) bundleLocaleCache.get(resourceCacheKey);

        if (bundleMap == null) {
            ResourceBundle bundle = getBaseResourceBundle(resource, locale);
            bundleMap = resourceBundleToMap(bundle);
            if (bundleMap != null) {
                bundleLocaleCache.put(resourceCacheKey, bundleMap);
            }
        }
        return bundleMap;
    }

    protected static ResourceBundle getBaseResourceBundle(String resource, Locale locale) {
        if (resource == null || resource.length() <= 0) return null;
        if (locale == null) locale = Locale.getDefault();

        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        ResourceBundle bundle = null;
        try {
            bundle = ResourceBundle.getBundle(resource, locale, loader);
        } catch (MissingResourceException e) {
            Debug.log("[UtilProperties.getPropertyValue] could not find resource: " + resource + " for locale " + locale.toString() + ": " + e.toString(), module);
            return null;
        }
        if (bundle == null) {
            Debug.log("[UtilProperties.getPropertyValue] could not find resource: " + resource + " for locale " + locale.toString(), module);
            return null;
        }
        
        return bundle;
    }
    
    protected static Map resourceBundleToMap(ResourceBundle bundle) {
        if (bundle == null) {
            return new HashMap();
        }
        // NOTE: this should return all keys, including keys from parent ResourceBundles, if not then something else must be done here...
        Enumeration keyNum = bundle.getKeys();
        Map resourceBundleMap = new HashMap();
        while (keyNum.hasMoreElements()) {
            String key = (String) keyNum.nextElement();
            //resourceBundleMap.put(key, bundle.getObject(key));
            Object value = bundle.getObject(key);
            resourceBundleMap.put(key, value);
        }
        resourceBundleMap.put("_RESOURCE_BUNDLE_", bundle);
        return resourceBundleMap;
    }
    
    /** Returns the specified resource/properties file
     *
     * NOTE: This is NOT fully implemented yet to fulfill all of the requirements
     *  for i18n messages. Do NOT use.
     *
     * To be used in an i18n context this still needs to be extended quite 
     *  a bit. The behavior needed is that for each getMessage the most specific 
     *  locale (with fname_en_US for instance) is searched first, then the next 
     *  less specific (fname_en for instance), then without the locale if it is
     *  still not found (plain fname for example, not that these examples would
     *  have .properties appended to them).
     * This would be accomplished by returning the following structure:
     *    1. Get "fname" FlexibleProperties object
     *    2. Get the "fname_en" FlexibleProperties object and if the "fname" one 
     *      is not null, set it as the default/parent of the "fname_en" object
     *    3. Get the "fname_en_US" FlexibleProperties object and if the 
     *      "fname_en" one is not null, set it as the default/parent of the 
     *      "fname_en_US" object; if the "fname_en" one is null, but the "fname"
     *      one is not, set the "fname" object as the default/parent of the 
     *      "fname_en_US" object
     * Then return the fname_en_US object if not null, else the fname_en, else the fname.
     *
     * To make this all more fun, the default locale should be the parent of 
     *  the "fname" object in this example so that there is an even higher 
     *  chance of finding something for each request.
     *
     * For efficiency all of these should be cached indendependently so the same
     *  instance can be shared, speeding up loading time/efficiency.
     *
     * All of this should work with the setDefaultProperties method of the 
     *  FlexibleProperties class, but it should be tested and updated as 
     *  necessary. It's a bit tricky, so chances are it won't work as desired...
     *
     * @param resource The name of the resource - can be a file, class, or URL
     * @param locale The locale that the given resource will correspond to
     * @return The Properties class
     */
    public static Properties getProperties(String resource, Locale locale) {
        if (resource == null || resource.length() <= 0) return null;
        if (locale == null) locale = Locale.getDefault();
        
        String localeString = locale.toString();
        String resourceLocale = resource + "_" + localeString;
        Properties properties = (FlexibleProperties) resourceCache.get(resourceLocale);

        if (properties == null) {
            try {
                URL url = UtilURL.fromResource(resourceLocale);
                if (url == null) {
                    properties = getProperties(resource);
                } else {
                    properties = FlexibleProperties.makeFlexibleProperties(url);
                }
            } catch (MissingResourceException e) {
                Debug.log(e.getMessage(), module);
            }
            resourceCache.put(resourceLocale, properties);
        }      
        
        if (properties == null)
            Debug.logInfo("[UtilProperties.getProperties] could not find resource: " + resource + ", locale: " + locale, module);

        return properties;
    }
}

⌨️ 快捷键说明

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