View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  package org.apache.myfaces.shared_orchestra.util;
    20
    21  import java.util.Locale;
    22  import java.util.StringTokenizer;
    23
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26
    27
    28  /**
    29   * @author Anton Koinov (latest modification by $Author$)
    30   * @version $Revision$ $Date$
    31   */
    32  public final class LocaleUtils
    33  {
    34      private static final Log log = LogFactory.getLog(LocaleUtils.class);
    35
    36      /** Utility class, do not instatiate */
    37      private LocaleUtils()
    38      {
    39          // utility class, do not instantiate
    40      }
    41
    42      /**
    43       * Converts a locale string to <code>Locale</code> class. Accepts both
    44       * '_' and '-' as separators for locale components.
    45       *
    46       * @param localeString string representation of a locale
    47       * @return Locale instance, compatible with the string representation
    48       */
    49      public static Locale toLocale(String localeString)
    50      {
    51          if ((localeString == null) || (localeString.length() == 0))
    52          {
    53              Locale locale = Locale.getDefault();
    54              if(log.isWarnEnabled())
    55                  log.warn("Locale name in faces-config.xml null or empty, setting locale to default locale : "+locale.toString());
    56              return locale;
    57          }
    58
    59          int separatorCountry = localeString.indexOf('_');
    60          char separator;
    61          if (separatorCountry >= 0) {
    62              separator = '_';
    63          }
    64          else
    65          {
    66              separatorCountry = localeString.indexOf('-');
    67              separator = '-';
    68          }
    69
    70          String language, country, variant;
    71          if (separatorCountry < 0)
    72          {
    73              language = localeString;
    74              country = variant = "";
    75          }
    76          else
    77          {
    78              language = localeString.substring(0, separatorCountry);
    79
    80              int separatorVariant = localeString.indexOf(separator, separatorCountry + 1);
    81              if (separatorVariant < 0)
    82              {
    83                  country = localeString.substring(separatorCountry + 1);
    84                  variant = "";
    85              }
    86              else
    87              {
    88                  country = localeString.substring(separatorCountry + 1, separatorVariant);
    89                  variant = localeString.substring(separatorVariant + 1);
    90              }
    91          }
    92
    93          return new Locale(language, country, variant);
    94      }
    95
    96
    97      /**
    98       * Convert locale string used by converter tags to locale.
    99       *
    100      * @param name name of the locale
    101      * @return locale specified by the given String
    102      *
    103      * @see org.apache.myfaces.taglib.core.ConvertDateTimeTag#setConverterLocale
    104      * @see org.apache.myfaces.taglib.core.ConvertNumberTag#setConverterLocale
    105      */
    106     public static Locale converterTagLocaleFromString(String name)
    107     {
    108         try
    109         {
    110             Locale locale;
    111             StringTokenizer st = new StringTokenizer(name, "_");
    112             String language = st.nextToken();
    113
    114             if(st.hasMoreTokens())
    115             {
    116                 String country = st.nextToken();
    117
    118                 if(st.hasMoreTokens())
    119                 {
    120                     String variant = st.nextToken();
    121                     locale = new Locale(language, country, variant);
    122                 }
    123                 else
    124                 {
    125                     locale = new Locale(language, country);
    126                 }
    127             }
    128             else
    129             {
    130                 locale = new Locale(language);
    131             }
    132
    133
    134             return locale;
    135         }
    136         catch(Exception e)
    137         {
    138             throw new IllegalArgumentException("Locale parsing exception - " +
    139                 "invalid string representation '" + name + "'");
    140         }
    141     }
    142 }