package org.apache.maven.xdoc.util;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * Utility class to handle the <code>java.util.Locale</code> object.
 * 
 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton </a>
 * @version $Id:$
 */
public final class LocaleUtil extends Object {

    /**
     * Converts a locale code like "en", "en_US" or "en_US_win" to a <code>java.util.Locale</code>
     * object. <br>
     * If localeCode = "default", return the current value of the default locale for this instance
     * of the Java Virtual Machine.
     * 
     * @see <a
     *      href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Locale.html">java.util.Locale#getDefault()
     *      </a>
     * @param localeCode
     *            the locale code string.
     * @return a java.util.Locale object instancied or null if errors occurred
     */
    public static Locale codeToLocale(final String localeCode) {
        if (localeCode == null) {
            return null;
        }

        if (localeCode.toLowerCase().equals("default")) {
            return Locale.getDefault();
        }

        Locale locale = null;
        String language = "";
        String country = "";
        String variant = "";

        StringTokenizer tokenizer = new StringTokenizer(localeCode, "_");
        if (tokenizer.countTokens() > 3) {
            System.out.println("The locale code=[" + localeCode + "] is not a valid java.util.Locale");
            return null;
        }

        if (tokenizer.hasMoreTokens()) {
            language = tokenizer.nextToken();
            if (tokenizer.hasMoreTokens()) {
                country = tokenizer.nextToken();
                if (tokenizer.hasMoreTokens()) {
                    variant = tokenizer.nextToken();
                }
            }
        }

        locale = new Locale(language, country, variant);

        return locale;
    }

    /**
     * Parse a comma separated list of locale codes and converts to an array of
     * <code>java.util.Locale</code> object.
     * 
     * @param localeCodes
     *            a comma separated list of locale codes.
     * @return a array of java.util.Locale object instancied or null if errors occurred
     */
    public static Locale[] codesToLocales(final String localeCodes) {
        if (localeCodes == null) {
            return null;
        }

        List arrays = new ArrayList();
        StringTokenizer strToken = new StringTokenizer(localeCodes, ", ");

        while (strToken.hasMoreTokens()) {
            final String locale = strToken.nextToken().trim();

            final Locale currentLocale = codeToLocale(locale);
            if (!Arrays.asList(Locale.getAvailableLocales()).contains(currentLocale)) {
                System.out.println("The current locale parsed defined by '" + locale
                        + "' is not available in the Java version " + System.getProperty("java.version") + " from "
                        + System.getProperty("java.vendor"));
            }
            arrays.add(currentLocale);
        }

        return (Locale[]) arrays.toArray(new Locale[0]);
    }

    /**
     * Returns the name for the <code>locale</code>. This will be the values returned by
     * <code>java.util.Locale.getDisplayName()</code>, but the first character will be in upper
     * case.
     * 
     * @param locale
     *            The locale to display
     * @return The display name of the locale with the first character in upper case
     */
    public static String displayName(final Locale locale) {
        return displayName(locale, null);
    }

    /**
     * Returns the name for the <code>locale</code>. This will be the values returned by
     * <code>java.util.Locale.getDisplayName()</code>, but the first character will be in upper
     * case. The display name can be in a locale wanted.
     * 
     * @param locale
     *            The locale to display
     * @param localeWanted
     *            The locale wanted to display the locale
     * @return The display name of the locale with the first character in upper case
     */
    public static String displayName(final Locale locale, final Locale localeWanted) {
        if (locale == null) {
            return null;
        }

        String localeAsStr = null;
        if (localeWanted == null) {
            localeAsStr = locale.getDisplayName();
        } else {
            localeAsStr = locale.getDisplayName(localeWanted);
        }
        char[] letters = localeAsStr.toCharArray();
        letters[0] = Character.toUpperCase(letters[0]);

        return new String(letters);
    }
}