View Javadoc

1   package org.apache.maven.xdoc.util;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.Arrays;
24  import java.util.StringTokenizer;
25  
26  /**
27   * Utility class to handle the <code>java.util.Locale</code> object.
28   * 
29   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton </a>
30   * @version $Id: LocaleUtil.java 532339 2007-04-25 12:28:56Z ltheussl $
31   */
32  public final class LocaleUtil extends Object {
33  
34      /**
35       * Converts a locale code like "en", "en_US" or "en_US_win" to a <code>java.util.Locale</code>
36       * object. <br>
37       * If localeCode = "default", return the current value of the default locale for this instance
38       * of the Java Virtual Machine.
39       * 
40       * @see <a
41       *      href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Locale.html">java.util.Locale#getDefault()
42       *      </a>
43       * @param localeCode
44       *            the locale code string.
45       * @return a java.util.Locale object instancied or null if errors occurred
46       */
47      public static Locale codeToLocale(final String localeCode) {
48          if (localeCode == null) {
49              return null;
50          }
51  
52          if (localeCode.toLowerCase().equals("default")) {
53              return Locale.getDefault();
54          }
55  
56          Locale locale = null;
57          String language = "";
58          String country = "";
59          String variant = "";
60  
61          StringTokenizer tokenizer = new StringTokenizer(localeCode, "_");
62          if (tokenizer.countTokens() > 3) {
63              System.out.println("The locale code=[" + localeCode + "] is not a valid java.util.Locale");
64              return null;
65          }
66  
67          if (tokenizer.hasMoreTokens()) {
68              language = tokenizer.nextToken();
69              if (tokenizer.hasMoreTokens()) {
70                  country = tokenizer.nextToken();
71                  if (tokenizer.hasMoreTokens()) {
72                      variant = tokenizer.nextToken();
73                  }
74              }
75          }
76  
77          locale = new Locale(language, country, variant);
78  
79          return locale;
80      }
81  
82      /**
83       * Parse a comma separated list of locale codes and converts to an array of
84       * <code>java.util.Locale</code> object.
85       * 
86       * @param localeCodes
87       *            a comma separated list of locale codes.
88       * @return a array of java.util.Locale object instancied or null if errors occurred
89       */
90      public static Locale[] codesToLocales(final String localeCodes) {
91          if (localeCodes == null) {
92              return null;
93          }
94  
95          List arrays = new ArrayList();
96          StringTokenizer strToken = new StringTokenizer(localeCodes, ", ");
97  
98          while (strToken.hasMoreTokens()) {
99              final String locale = strToken.nextToken().trim();
100 
101             final Locale currentLocale = codeToLocale(locale);
102             if (!Arrays.asList(Locale.getAvailableLocales()).contains(currentLocale)) {
103                 System.out.println("The current locale parsed defined by '" + locale
104                         + "' is not available in the Java version " + System.getProperty("java.version") + " from "
105                         + System.getProperty("java.vendor"));
106             }
107             arrays.add(currentLocale);
108         }
109 
110         return (Locale[]) arrays.toArray(new Locale[0]);
111     }
112 
113     /**
114      * Returns the name for the <code>locale</code>. This will be the values returned by
115      * <code>java.util.Locale.getDisplayName()</code>, but the first character will be in upper
116      * case.
117      * 
118      * @param locale
119      *            The locale to display
120      * @return The display name of the locale with the first character in upper case
121      */
122     public static String displayName(final Locale locale) {
123         return displayName(locale, null);
124     }
125 
126     /**
127      * Returns the name for the <code>locale</code>. This will be the values returned by
128      * <code>java.util.Locale.getDisplayName()</code>, but the first character will be in upper
129      * case. The display name can be in a locale wanted.
130      * 
131      * @param locale
132      *            The locale to display
133      * @param localeWanted
134      *            The locale wanted to display the locale
135      * @return The display name of the locale with the first character in upper case
136      */
137     public static String displayName(final Locale locale, final Locale localeWanted) {
138         if (locale == null) {
139             return null;
140         }
141 
142         String localeAsStr = null;
143         if (localeWanted == null) {
144             localeAsStr = locale.getDisplayName();
145         } else {
146             localeAsStr = locale.getDisplayName(localeWanted);
147         }
148         char[] letters = localeAsStr.toCharArray();
149         letters[0] = Character.toUpperCase(letters[0]);
150 
151         return new String(letters);
152     }
153 }