001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.shared_impl.util;
017:
018: import java.util.Locale;
019: import java.util.StringTokenizer;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /**
025: * @author Anton Koinov (latest modification by $Author$)
026: * @version $Revision$ $Date$
027: */
028: public final class LocaleUtils {
029: private static final Log log = LogFactory.getLog(LocaleUtils.class);
030:
031: /** Utility class, do not instatiate */
032: private LocaleUtils() {
033: // utility class, do not instantiate
034: }
035:
036: /**
037: * Converts a locale string to <code>Locale</code> class. Accepts both
038: * '_' and '-' as separators for locale components.
039: *
040: * @param localeString string representation of a locale
041: * @return Locale instance, compatible with the string representation
042: */
043: public static Locale toLocale(String localeString) {
044: if ((localeString == null) || (localeString.length() == 0)) {
045: Locale locale = Locale.getDefault();
046: if (log.isWarnEnabled())
047: log
048: .warn("Locale name in faces-config.xml null or empty, setting locale to default locale : "
049: + locale.toString());
050: return locale;
051: }
052:
053: int separatorCountry = localeString.indexOf('_');
054: char separator;
055: if (separatorCountry >= 0) {
056: separator = '_';
057: } else {
058: separatorCountry = localeString.indexOf('-');
059: separator = '-';
060: }
061:
062: String language, country, variant;
063: if (separatorCountry < 0) {
064: language = localeString;
065: country = variant = "";
066: } else {
067: language = localeString.substring(0, separatorCountry);
068:
069: int separatorVariant = localeString.indexOf(separator,
070: separatorCountry + 1);
071: if (separatorVariant < 0) {
072: country = localeString.substring(separatorCountry + 1);
073: variant = "";
074: } else {
075: country = localeString.substring(separatorCountry + 1,
076: separatorVariant);
077: variant = localeString.substring(separatorVariant + 1);
078: }
079: }
080:
081: return new Locale(language, country, variant);
082: }
083:
084: /**
085: * Convert locale string used by converter tags to locale.
086: *
087: * @param name name of the locale
088: * @return locale specified by the given String
089: *
090: * @see org.apache.myfaces.taglib.core.ConvertDateTimeTag#setConverterLocale
091: * @see org.apache.myfaces.taglib.core.ConvertNumberTag#setConverterLocale
092: */
093: public static Locale converterTagLocaleFromString(String name) {
094: try {
095: Locale locale;
096: StringTokenizer st = new StringTokenizer(name, "_");
097: String language = st.nextToken();
098:
099: if (st.hasMoreTokens()) {
100: String country = st.nextToken();
101:
102: if (st.hasMoreTokens()) {
103: String variant = st.nextToken();
104: locale = new Locale(language, country, variant);
105: } else {
106: locale = new Locale(language, country);
107: }
108: } else {
109: locale = new Locale(language);
110: }
111:
112: return locale;
113: } catch (Exception e) {
114: throw new IllegalArgumentException(
115: "Locale parsing exception - "
116: + "invalid string representation '" + name
117: + "'");
118: }
119: }
120: }
|