View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.jetspeed.util;
19  
20  import java.util.Locale;
21  import java.util.StringTokenizer;
22  
23  /***
24   * Class to set and get Locale settings for Jetspeed.
25   *          
26   * @author <a href="mailto:roger.ruttimann@earthlink.net">Roger Ruttimann</a>
27   * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
28   * @version $Id: JetspeedLocale.java 516448 2007-03-09 16:25:47Z ate $
29   */
30  public class JetspeedLocale
31  {
32      private static final String DELIM = ",";
33      
34      /***
35       * According to PLT.21.8.1, the default locale should be English.
36       */
37      public static Locale getDefaultLocale()
38      {
39          return Locale.ENGLISH;
40      }
41  
42  
43      /***
44       * Converts Locale to String.
45       * 
46       * @param locale
47       * @return
48       */
49      public static String convertLocaleToString(Locale locale)
50      {
51          if (locale == null)
52          {
53              return null;
54          }
55          String country = locale.getCountry();
56          String language = locale.getLanguage();
57          String variant = locale.getVariant();
58          StringBuffer buffer = new StringBuffer(40);
59          if (language != null)
60          {
61              buffer.append(language);
62          }
63          buffer.append(DELIM);
64  
65          if (country != null)
66          {
67              buffer.append(country);
68          }
69          buffer.append(DELIM);
70  
71          if (variant != null)
72          {
73              buffer.append(variant);
74          }
75  
76          return buffer.toString().trim();
77      }
78  
79      /***
80       * Converts String to Locale.
81       * 
82       * @param localeString
83       * @return
84       */
85      public static Locale convertStringToLocale(String localeString)
86      {
87          if (localeString == null)
88          {
89              return null;
90          }
91          StringTokenizer tokenizer = new StringTokenizer(localeString, DELIM);
92  
93          String language = tokenizer.nextToken().trim();
94          String country = null;
95          String variant = null;
96          if (tokenizer.hasMoreTokens())
97          {
98              country = tokenizer.nextToken().trim();
99          }
100 
101         if (tokenizer.hasMoreTokens())
102         {
103             variant = tokenizer.nextToken().trim();
104         }
105 
106         if (country != null && language != null && variant != null)
107         {
108             return new Locale(language, country, variant);
109         }
110         else if (country != null && language != null)
111         {
112             return new Locale(language, country);
113         }
114         else if (language != null)
115         {
116             return new Locale(language, ""); // JDK 1.3 compatibility
117         }
118         else
119         {
120             return null;
121         }
122 
123     }
124 
125 
126 }