/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package javax.servlet.jsp.jstl.fmt;

import java.util.*;
import java.text.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.jstl.core.Config;
import org.apache.taglibs.standard.tag.common.fmt.BundleSupport;
import org.apache.taglibs.standard.tag.common.fmt.MessageSupport;
import org.apache.taglibs.standard.resources.Resources;

/**
 * <p> 便利なメソッドを通して、リソースバンドルのためのロケール決定ロジックを表面化するクラスです。</p>
 *
 * <p>このクラスは、地域対応メッセージを生成しなければならないタグハンドラの実装に役立つでしょう。
 * 例えば、エラーページ上でユーザーに役立つ情報をじかに伝えるための Exception メッセージに有効でしょう。</p>
 *
 * @author Jan Luehe
 */

public class LocaleSupport {

    /**
     * <p> 指定されたキーに合致する地域対応メッセージを返します。</p>
     *
     * <p> 指定されたキーは、コンフィグレーションで設定されている
     * <tt>javax.servlet.jsp.jstl.fmt.localizationContext</tt>
     * より返された、デフォルトの I18N ローカライゼーションコンテキストの持つリソースバンドルより検索していきます、</p>
     *
     * <p> コンフィグレーションが未設定の場合、
     * デフォルトの I18N ローカライゼーションコンテキストでリソースバンドルを持たない場合、もしくは、
     * 指定されたキーがリソースバンドルで未定義であった場合、
     * "???&lt;key&gt;???"という文字列が返され、"&lt;key&gt;"には、指定されたキーがそのまま入ります。</p>
     *
     * @param pageContext 指定されたキーに合致する地域対応メッセージが格納されている JSP ページコンテキスト。
     * @param key メッセージキー。
     *
     * @return 指定されたキーに合致する地域対応メッセージ。
     */
    public static String getLocalizedMessage(PageContext pageContext,
                                             String key) {
	return getLocalizedMessage(pageContext, key, null, null);
    }

    /**
     * <p> 指定されたキーに合致する地域対応メッセージを返します。</p>
     *
     * <p> 指定されたキーは、指定された基底のリソースバンドルより検索していきます。</p>
     *
     * <p> 指定された基底のリソースバンドルが存在しなかったり、指定されたキーがリソースバンドルで未定義であった場合、
     * "???&lt;key&gt;???"という文字列が返され、"&lt;key&gt;"には、指定されたキーがそのまま入ります。</p>
     *
     * @param pageContext 指定されたキーに合致する地域対応メッセージが格納されている JSP ページコンテキスト。
     * @param key メッセージキー。
     * @param basename 基底のリソースバンドル名。
     *
     * @return 指定されたキーに合致する地域対応メッセージ。
     */
    public static String getLocalizedMessage(PageContext pageContext,
                                             String key,
                                             String basename) {
	return getLocalizedMessage(pageContext, key, null, basename);
    }

    /**
     * <p> 指定されたキーに合致する地域対応メッセージを返し、
     * <tt>args</tt> で指定された引数を元に、パラメータ置換処理を実行します。</p>
     *
     * <p> パラメータ置換処理がどのように実装されているかについては、
     * &lt;fmt:message&gt; アクションの仕様を参照してください。</p>
     *
     * <p> 地域対応メッセージは、
     * {@link #getLocalizedMessage(javax.servlet.jsp.PageContext,java.lang.String) getLocalizedMessage(pageContext, key)}
     * より返されます。</p>
     *
     * @param pageContext 指定されたキーに合致する地域対応メッセージが格納されている JSP ページコンテキスト。
     * @param key メッセージキー。
     * @param args パラメータ置換のために渡される引数。
     *
     * @return 指定されたキーに合致する地域対応メッセージ。
     */
    public static String getLocalizedMessage(PageContext pageContext,
                                             String key,
                                             Object[] args) {
	return getLocalizedMessage(pageContext, key, args, null);
    }

    /**
     * <p> 指定されたキーに合致する地域対応メッセージを返し、
     * <tt>args</tt> で指定された引数を元に、パラメータ置換処理を実行します。</p>
     *
     * <p>パラメータ置換処理がどのように実装されているかについては、
     * &lt;fmt:message&gt; アクションの仕様を参照してください。</p>
     *
     * <p> 地域対応メッセージは、
     * {@link #getLocalizedMessage(javax.servlet.jsp.PageContext,java.lang.String, java.lang.String) getLocalizedMessage(pageContext, key, basename)}
     * より返されます。</p>
     *
     * @param pageContext 指定されたキーに合致する地域対応メッセージが格納されている JSP ページコンテキスト。
     * @param key メッセージキー。
     * @param args パラメータ置換のために渡される引数。
     * @param basename 基底のリソースバンドル名
     *
     * @return 指定されたキーに合致する地域対応メッセージ。
     */
    public static String getLocalizedMessage(PageContext pc,
                                             String key,
                                             Object[] args,
                                             String basename) {
	LocalizationContext locCtxt = null;
	String message = MessageSupport.UNDEFINED_KEY + key
	    + MessageSupport.UNDEFINED_KEY;

	if (basename != null) {
	    locCtxt = BundleSupport.getLocalizationContext(pc, basename);
	} else {
	    locCtxt = BundleSupport.getLocalizationContext(pc);
	}

	if (locCtxt != null) {
	    ResourceBundle bundle = locCtxt.getResourceBundle();
	    if (bundle != null) {
		try {
		    message = bundle.getString(key);
		    if (args != null) {
			MessageFormat formatter = new MessageFormat("");
			if (locCtxt.getLocale() != null) {
			    formatter.setLocale(locCtxt.getLocale());
			}
			formatter.applyPattern(message);
			message = formatter.format(args);
		    }
		} catch (MissingResourceException mre) {
		}
	    }
	}

	return message;
    }
}

