ivaneeo's blog

自由的力量,自由的生活。

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
package net.sourceforge.jtds.jdbc;

import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Map;
import java.util.ResourceBundle;

public final class Messages {
  private static final String DEFAULT_RESOURCE = "net.sourceforge.jtds.jdbc.Messages";  //默认得资源
  private static ResourceBundle defaultResource;  //和locale的绑定

  private Messages() {
    }

    public static String get(String key) {
        return get(key, null);
    }

    public static String get(String key, Object param1) {
        Object args[] = {param1};
        return get(key, args);
    }

    static String get(String key, Object param1, Object param2) {
        Object args[] = {param1, param2};
        return get(key, args);
    }

    private static String get(String key, Object[] arguments) {
      try {
            ResourceBundle bundle = loadResourceBundle();
            String formatString = bundle.getString(key);
            // No need for any formatting if no parameters are specified
            if (arguments == null || arguments.length == 0) {
                return formatString;
            } else {
                MessageFormat formatter = new MessageFormat(formatString);
                return formatter.format(arguments);
            }
        } catch (java.util.MissingResourceException mre) {
            throw new RuntimeException("No message resource found for message property " + key);
        }
    }

    private static ResourceBundle loadResourceBundle() {
        if (defaultResource == null) {
            defaultResource = ResourceBundle.getBundle(DEFAULT_RESOURCE);
        }
        return defaultResource;
    }

    static void loadDriverProperties(Map propertyMap, Map descriptionMap) {
        final ResourceBundle bundle = loadResourceBundle();
        final Enumeration keys = bundle.getKeys();
        while (keys.hasMoreElements()) {
            final String key = (String) keys.nextElement();
            final String descriptionPrefix = "prop.desc.";
            final String propertyPrefix = "prop.";
            if (key.startsWith(descriptionPrefix)) {
                descriptionMap.put(key.substring(descriptionPrefix.length()), bundle.getString(key));
            }
            else if (key.startsWith(propertyPrefix)) {
                propertyMap.put(key.substring(propertyPrefix.length()), bundle.getString(key));
            }
        }
    }
}

上面代码中默认得绑定名为:"net.sourceforge.jtds.jdbc.Messages".其实就是以工程为根目录,寻找文件Messages.properties.这里查找的方式和类是一样的.比如:"net.sourceforge.jtds.jdbc.Messages",就是工程根目录下的net/sourceforge/jtds/jdbc/下的Messages.properties文件.
这个文件定义很多的属性,要得到只要用get方法.

  注意这里的defaultResource = ResourceBundle.getBundle(DEFAULT_RESOURCE);没有设定Locale值,所以文件名为Messages.properties.如果设置了Locale值的话,例如:defaultResource = ResourceBundle.getBundle(DEFAULT_RESOURCE, Locale.ENGLISH);那么它就会去查找文件Messages_en.properties.其他类似加后缀(Locale.CHINA是Messages_zh.properties).

  关于java.text.MessageFormat类,下面通过一个例子就可以说明:
    MessageFormat mf = new MessageFormat("You have {0} messages.");
    Object[] arguments = {"no"};
    System.out.println(mf.format(arguments));  //"You have no messages."

  关于String.startsWith(String prex)是测试字符串是否以prex开头.
posted on 2005-08-04 14:43 ivaneeo 阅读(403) 评论(0)  编辑  收藏 所属分类: java魔力

只有注册用户登录后才能发表评论。


网站导航: