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

📄 messageresources.java

📁 这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用struts,hebinate,xml等技术,有丰富的tag,role,navigation,session,dictio
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Returns a text message for the specified key, for the default Locale.
     * A null string result will be returned by this method if no relevant
     * message resource is found for this key or Locale, if the
     * <code>returnNull</code> property is set.  Otherwise, an appropriate
     * error message will be returned.
     * <p>
     * This method must be implemented by a concrete subclass.
     *
     * @param locale The requested message Locale, or <code>null</code>
     *  for the system default Locale
     * @param key The message key to look up
     */
    public abstract String getMessage(Locale locale, String key);


    /**
     * Returns a text message after parametric replacement of the specified
     * parameter placeholders.  A null string result will be returned by
     * this method if no resource bundle has been configured.
     *
     * @param locale The requested message Locale, or <code>null</code>
     *  for the system default Locale
     * @param key The message key to look up
     * @param args An array of replacement parameters for placeholders
     */
    public String getMessage(Locale locale, String key, Object args[]) {

	// Cache MessageFormat instances as they are accessed
        if (locale == null)
            locale = defaultLocale;
	MessageFormat format = null;
	String formatKey = messageKey(locale, key);
	synchronized (formats) {
	    format = (MessageFormat) formats.get(formatKey);
	    if (format == null) {
		String formatString = getMessage(locale, key);
		if (formatString == null) {
		    if (returnNull)
			return (null);
		    else
			return ("???" + formatKey + "???");
		}
		format = new MessageFormat(escape(formatString));
		formats.put(formatKey, format);
	    }

	}
	return (format.format(args));

    }


    /**
     * Returns a text message after parametric replacement of the specified
     * parameter placeholders.  A null string result will never be returned
     * by this method.
     *
     * @param locale The requested message Locale, or <code>null</code>
     *  for the system default Locale
     * @param key The message key to look up
     * @param arg0 The replacement for placeholder {0} in the message
     */
    public String getMessage(Locale locale, String key, Object arg0) {

	Object args[] = new Object[1];
	args[0] = arg0;
	return (getMessage(locale, key, args));

    }


    /**
     * Returns a text message after parametric replacement of the specified
     * parameter placeholders.  A null string result will never be returned
     * by this method.
     *
     * @param locale The requested message Locale, or <code>null</code>
     *  for the system default Locale
     * @param key The message key to look up
     * @param arg0 The replacement for placeholder {0} in the message
     * @param arg1 The replacement for placeholder {1} in the message
     */
    public String getMessage(Locale locale,
			     String key, Object arg0, Object arg1) {

	Object args[] = new Object[2];
	args[0] = arg0;
	args[1] = arg1;
	return (getMessage(locale, key, args));

    }


    /**
     * Returns a text message after parametric replacement of the specified
     * parameter placeholders.  A null string result will never be returned
     * by this method.
     *
     * @param locale The requested message Locale, or <code>null</code>
     *  for the system default Locale
     * @param key The message key to look up
     * @param arg0 The replacement for placeholder {0} in the message
     * @param arg1 The replacement for placeholder {1} in the message
     * @param arg2 The replacement for placeholder {2} in the message
     */
    public String getMessage(Locale locale,
			     String key, Object arg0, Object arg1,
			     Object arg2) {

	Object args[] = new Object[3];
	args[0] = arg0;
	args[1] = arg1;
	args[2] = arg2;
	return (getMessage(locale, key, args));

    }


    /**
     * Returns a text message after parametric replacement of the specified
     * parameter placeholders.  A null string result will never be returned
     * by this method.
     *
     * @param locale The requested message Locale, or <code>null</code>
     *  for the system default Locale
     * @param key The message key to look up
     * @param arg0 The replacement for placeholder {0} in the message
     * @param arg1 The replacement for placeholder {1} in the message
     * @param arg2 The replacement for placeholder {2} in the message
     * @param arg3 The replacement for placeholder {3} in the message
     */
    public String getMessage(Locale locale,
			     String key, Object arg0, Object arg1,
			     Object arg2, Object arg3) {

	Object args[] = new Object[4];
	args[0] = arg0;
	args[1] = arg1;
	args[2] = arg2;
	args[3] = arg3;
	return (getMessage(locale, key, args));

    }


    /**
     * Return <code>true</code> if there is a defined message for the specified
     * key in the system default locale.
     *
     * @param key The message key to look up
     */
    public boolean isPresent(String key) {

        return (isPresent(null, key));

    }


    /**
     * Return <code>true</code> if there is a defined message for the specified
     * key in the specified Locale.
     *
     * @param locale The requested message Locale, or <code>null</code>
     *  for the system default Locale
     * @param key The message key to look up
     */
    public boolean isPresent(Locale locale, String key) {

        String message = getMessage(locale, key);
        if (message == null)
            return (false);
        else if (message.startsWith("???") && message.endsWith("???"))
            return (false); // FIXME - Only valid for default implementation
        else
            return (true);

    }


    // ------------------------------------------------------ Protected Methods


    /**
     * Escape any single quote characters that are included in the specified
     * message string.
     *
     * @param string The string to be escaped
     */
    protected String escape(String string) {

        if ((string == null) || (string.indexOf('\'') < 0))
            return (string);
        int n = string.length();
        StringBuffer sb = new StringBuffer(n);
        for (int i = 0; i < n; i++) {
            char ch = string.charAt(i);
            if (ch == '\'')
                sb.append('\'');
            sb.append(ch);
        }
        return (sb.toString());

    }


    /**
     * Compute and return a key to be used in caching information by a Locale.
     * <strong>NOTE</strong> - The locale key for the default Locale in our
     * environment is a zero length String.
     *
     * @param locale The locale for which a key is desired
     */
    protected String localeKey(Locale locale) {

        if (locale == null)
            return ("");
        //        else if (locale.equals(defaultLocale))
        //            return ("");
        else
            return (locale.toString());

    }


    /**
     * Compute and return a key to be used in caching information
     * by Locale and message key.
     *
     * @param locale The Locale for which this format key is calculated
     * @param key The message key for which this format key is calculated
     */
    protected String messageKey(Locale locale, String key) {

        return (localeKey(locale) + "." + key);

    }


    /**
     * Compute and return a key to be used in caching information
     * by locale key and message key.
     *
     * @param localeKey The locale key for which this cache key is calculated
     * @param key The message key for which this cache key is calculated
     */
    protected String messageKey(String localeKey, String key) {

        return (localeKey + "." + key);

    }


    // --------------------------------------------------------- Static Methods


    /**
     * The default MessageResourcesFactory used to create MessageResources
     * instances.
     */
    protected static MessageResourcesFactory defaultFactory = null;


    /**
     * Create and return an instance of <code>MessageResources</code> for the
     * created by the default <code>MessageResourcesFactory</code>.
     *
     * @param config Configuration parameter for this message bundle.
     */
    public synchronized static MessageResources
        getMessageResources(String config) {

        if (defaultFactory == null)
            defaultFactory = MessageResourcesFactory.createFactory();
        return defaultFactory.createResources(config);

    }


    /**
     * Log a message to the Writer that has been configured for our use.
     *
     * @param message The message to be logged
     */
    public void log(String message) {
        log.debug(message);
    }


    /**
     * Log a message and exception to the Writer that has been configured
     * for our use.
     *
     * @param message The message to be logged
     * @param throwable The exception to be logged
     */
    public void log(String message, Throwable throwable) {
        log.debug(message, throwable);
    }


}

⌨️ 快捷键说明

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