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.util;
20  
21  import java.util.Locale;
22  import java.util.StringTokenizer;
23  import java.util.logging.Level;
24  import java.util.logging.Logger;
25  
26  
27  public final class LocaleUtils
28  {
29      //private static final Log log = LogFactory.getLog(LocaleUtils.class);
30      private static final Logger log = Logger.getLogger(LocaleUtils.class.getName());
31  
32      /** Utility class, do not instatiate */
33      private LocaleUtils()
34      {
35          // utility class, do not instantiate
36      }
37  
38      /**
39       * Converts a locale string to <code>Locale</code> class. Accepts both
40       * '_' and '-' as separators for locale components.
41       *
42       * @param localeString string representation of a locale
43       * @return Locale instance, compatible with the string representation
44       */
45      public static Locale toLocale(String localeString)
46      {
47          if ((localeString == null) || (localeString.length() == 0))
48          {
49              Locale locale = Locale.getDefault();
50              if(log.isLoggable(Level.WARNING))
51              {
52                  log.warning("Locale name in faces-config.xml null or empty, setting locale to default locale : "
53                          + locale.toString());
54              }
55              return locale;
56          }
57  
58          int separatorCountry = localeString.indexOf('_');
59          char separator;
60          if (separatorCountry >= 0)
61          {
62              separator = '_';
63          }
64          else
65          {
66              separatorCountry = localeString.indexOf('-');
67              separator = '-';
68          }
69  
70          String language;
71          String country;
72          String variant;
73          if (separatorCountry < 0)
74          {
75              language = localeString;
76              country = "";
77              variant = "";
78          }
79          else
80          {
81              language = localeString.substring(0, separatorCountry);
82  
83              int separatorVariant = localeString.indexOf(separator, separatorCountry + 1);
84              if (separatorVariant < 0)
85              {
86                  country = localeString.substring(separatorCountry + 1);
87                  variant = "";
88              }
89              else
90              {
91                  country = localeString.substring(separatorCountry + 1, separatorVariant);
92                  variant = localeString.substring(separatorVariant + 1);
93              }
94          }
95  
96          return new Locale(language, country, variant);
97      }
98  
99  
100     /**
101      * Convert locale string used by converter tags to locale.
102      *
103      * @param name name of the locale
104      * @return locale specified by the given String
105      *
106      * @see org.apache.myfaces.taglib.core.ConvertDateTimeTag#setConverterLocale
107      * @see org.apache.myfaces.taglib.core.ConvertNumberTag#setConverterLocale
108      */
109     public static Locale converterTagLocaleFromString(String name)
110     {
111         try
112         {
113             Locale locale;
114             StringTokenizer st = new StringTokenizer(name, "_");
115             String language = st.nextToken();
116 
117             if(st.hasMoreTokens())
118             {
119                 String country = st.nextToken();
120 
121                 if(st.hasMoreTokens())
122                 {
123                     String variant = st.nextToken();
124                     locale = new Locale(language, country, variant);
125                 }
126                 else
127                 {
128                     locale = new Locale(language, country);
129                 }
130             }
131             else
132             {
133                 locale = new Locale(language);
134             }
135 
136 
137             return locale;
138         }
139         catch(Exception e)
140         {
141             throw new IllegalArgumentException("Locale parsing exception - " +
142                 "invalid string representation '" + name + "'");
143         }
144     }
145 }