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

📄 messagetag.java

📁 intra_mart是日本NDD公司开发的著名企业应用解决方案中间件。集成了J2ee服务器
💻 JAVA
字号:
/*
 * MessageTag.java
 *
 * Created on 2003/08/04, 12:00
 */

package jp.co.intra_mart.framework.base.web.tag;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import jp.co.intra_mart.framework.base.message.MessageManager;
import jp.co.intra_mart.framework.base.message.MessageManagerException;
import jp.co.intra_mart.framework.base.service.ServiceManager;
import jp.co.intra_mart.framework.base.service.ServiceManagerException;
import jp.co.intra_mart.framework.system.log.LogManager;
import jp.co.intra_mart.framework.system.log.LogConstant;

/**
 * Message僞僌偱偡丅
 * im-J2EE Framework傪巊梡偡傞応崌偺Message僼儗乕儉儚乕僋偺堦晹偲側傝傑偡丅
 *
 * @author INTRAMART
 * @since 4.2
 */
public class MessageTag extends BodyTagSupport {

    /**
     * MessageManager
     */
    private MessageManager messageManager;

    /**
     * ServiceManager
     */
    private ServiceManager serviceManager;

    /**
     * 僷儔儊乕僞偺廤崌
     */
    private Vector params;

    /**
     * 傾僾儕働乕僔儑儞
     */
    private String application;

    /**
     * 僉乕
     */
    private String key;

    /**
     * 儘働乕儖
     */
    private String locale;

    /**
     * MessageTag傪怴婯偵惗惉偟傑偡丅
     */
    public MessageTag() {
        super();
        try {
            this.messageManager = MessageManager.getMessageManager();
        } catch (MessageManagerException e) {
            String message = null;
            try {
                message =
                    ResourceBundle
                        .getBundle("jp.co.intra_mart.framework.base.web.tag.i18n")
                        .getString("MessageTag.FailedToGetMessageManager");
            } catch (MissingResourceException ex) {
            }
            LogManager.getLogManager().getLogAgent().sendMessage(
                MessageTag.class.getName(),
                LogConstant.LEVEL_ERROR,
                message,
                e);
        }
        try {
            this.serviceManager = ServiceManager.getServiceManager();
        } catch (ServiceManagerException e) {
            String message = null;
            try {
                message =
                    ResourceBundle
                        .getBundle("jp.co.intra_mart.framework.base.web.tag.i18n")
                        .getString("MessageTag.FailedToGetServiceManager");
            } catch (MissingResourceException ex) {
            }
            LogManager.getLogManager().getLogAgent().sendMessage(
                MessageTag.class.getName(),
                LogConstant.LEVEL_ERROR,
                message,
                e);
        }
        this.params = null;
    }

    /**
     * 奐巒僞僌傪専抦偟偨偲偒偵JSP僄儞僕儞偐傜屇偽傟傑偡丅
     * Message僞僌傪夝庍偟傑偡丅
     *
     * @return EVAL_BODY_BUFFERED
     * @throws JspException 僞僌偺夝庍帪偵椺奜偑敪惗
     */
    public int doStartTag() throws JspException {
        this.params = new Vector();

        return EVAL_BODY_BUFFERED;
    }

    /**
     * 廔椆僞僌傪専抦偟偨偲偒偵JSP僄儞僕儞偐傜屇偽傟傑偡丅
     * Message僞僌傪廔椆偟傑偡丅
     *
     * @return EVAL_PAGE
     * @throws JspException 僞僌偺廔椆帪偵椺奜偑敪惗
     */
    public int doEndTag() throws JspException {
        String contextPath = null;
        String serviceServletPath = null;
        String url = null;
        Object[] messageParams = null;
        FrameworkTagParam param = null;
        String destLocaleString = null;
        Locale destLocale = null;

        try {
            // 僷儔儊乕僞
            int paramSize = this.params.size();
            messageParams = new Object[paramSize];

            for (int i = 0; i < paramSize; i++) {
                messageParams[i] = this.params.elementAt(i);
            }

            destLocaleString = getLocale();
            if (destLocaleString == null
                || destLocaleString.trim().equals("")) {
                destLocale =
                    this.serviceManager.getLocale(
                        (HttpServletRequest)pageContext.getRequest(),
                        (HttpServletResponse)pageContext.getResponse());
            } else {
                destLocale = getRealLocale(destLocaleString);
            }

            // Message僞僌奐巒
            pageContext.getOut().print(
                this.messageManager.getMessage(
                    getApplication(),
                    getKey(),
                    messageParams,
                    destLocale));
        } catch (Exception e) {
            throw new FrameworkTagException(e.getMessage(), e);
        }

        return EVAL_PAGE;
    }

    /**
     * 儘働乕儖暥帤楍偐傜幚嵺偺儘働乕儖傪庢摼偟傑偡丅
     * 儘働乕儖暥帤楍偼埲壓偺彂幃偵廬偄傑偡丅<BR>
     * <I>尵岅</I>_<I>崙</I>[_<I>僶儕傾儞僩</I>]<BR>
     * 儘働乕儖暥帤楍偺傾儞僟乕僶乕(_)偼僴僀僼儞(-)偱偁偭偰傕偐傑偄傑偣傫丅
     *
     * @param localeString 儘働乕儖暥帤楍
     * @return 儘働乕儖
     */
    private Locale getRealLocale(String localeString) {
        StringTokenizer tokenizer = new StringTokenizer(localeString, "-_");
        if(tokenizer.countTokens() == 1) {
            String language = tokenizer.nextToken();
            return new Locale(language);
        }else if (tokenizer.countTokens() == 2) {
            String language = tokenizer.nextToken();
            String country = tokenizer.nextToken();
            return new Locale(language, country);
        } else if (tokenizer.countTokens() == 3) {
            String language = tokenizer.nextToken();
            String country = tokenizer.nextToken();
            String variant = tokenizer.nextToken();
            return new Locale(language, country, variant);
        } else {
            String message = null;
            try {
                message =
                    ResourceBundle
                        .getBundle("jp.co.intra_mart.framework.base.web.tag.i18n")
                        .getString("MessageTag.LocaleStringIncorrect");
            } catch (MissingResourceException e) {
            }
            throw new IllegalArgumentException(
                message + " : \"" + localeString + "\"");
        }
    }

    /**
     * 僷儔儊乕僞傪捛壛偟傑偡丅
     *
     * @param value 僷儔儊乕僞偺抣
     */
    public void addParameter(Object value) {
        this.params.add(value);
    }

    /**
     * application傪庢摼偟傑偡丅
     *
     * @return application
     */
    public String getApplication() {
        return this.application;
    }

    /**
     * application傪愝掕偟傑偡丅
     *
     * @param application application
     */
    public void setApplication(String application) {
        this.application = application;
    }

    /**
     * key傪庢摼偟傑偡丅
     *
     * @return key
     */
    public String getKey() {
        return this.key;
    }

    /**
     * key傪愝掕偟傑偡丅
     *
     * @param key key
     */
    public void setKey(String key) {
        this.key = key;
    }

    /**
     * locale傪庢摼偟傑偡丅
     *
     * @return locale
     */
    public String getLocale() {
        return this.locale;
    }

    /**
     * locale傪愝掕偟傑偡丅
     *
     * @param locale locale
     */
    public void setLocale(String locale) {
        this.locale = locale;
    }
}

⌨️ 快捷键说明

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