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          Locale locale = Locale.forLanguageTag(localeString);
59          if (locale != null && !locale.getLanguage().isEmpty())
60          {
61              return locale;
62          }
63  
64          int separatorCountry = localeString.indexOf('_');
65          char separator;
66          if (separatorCountry >= 0)
67          {
68              separator = '_';
69          }
70          else
71          {
72              separatorCountry = localeString.indexOf('-');
73              separator = '-';
74          }
75  
76          String language;
77          String country;
78          String variant;
79          if (separatorCountry < 0)
80          {
81              language = localeString;
82              country = "";
83              variant = "";
84          }
85          else
86          {
87              language = localeString.substring(0, separatorCountry);
88  
89              int separatorVariant = localeString.indexOf(separator, separatorCountry + 1);
90              if (separatorVariant < 0)
91              {
92                  country = localeString.substring(separatorCountry + 1);
93                  variant = "";
94              }
95              else
96              {
97                  country = localeString.substring(separatorCountry + 1, separatorVariant);
98                  variant = localeString.substring(separatorVariant + 1);
99              }
100         }
101 
102         return new Locale(language, country, variant);
103     }
104 
105 
106     /**
107      * Convert locale string used by converter tags to locale.
108      *
109      * @param name name of the locale
110      * @return locale specified by the given String
111      */
112     public static Locale converterTagLocaleFromString(String name)
113     {
114         try
115         {
116             Locale locale;
117             StringTokenizer st = new StringTokenizer(name, "_");
118             String language = st.nextToken();
119 
120             if(st.hasMoreTokens())
121             {
122                 String country = st.nextToken();
123 
124                 if(st.hasMoreTokens())
125                 {
126                     String variant = st.nextToken();
127                     locale = new Locale(language, country, variant);
128                 }
129                 else
130                 {
131                     locale = new Locale(language, country);
132                 }
133             }
134             else
135             {
136                 locale = new Locale(language);
137             }
138 
139 
140             return locale;
141         }
142         catch(Exception e)
143         {
144             throw new IllegalArgumentException("Locale parsing exception - " +
145                 "invalid string representation '" + name + "'");
146         }
147     }
148 }