1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.wsrp4j.util;
18  
19  import java.util.ArrayList;
20  import java.util.Locale;
21  
22  public class LocaleHelper {
23      private final static String LOCALE_SEPARATOR = "-";
24  
25      /**
26       * Gets the language code from a locales string representation, assuming
27       * that the provided locale has a format like [2 char language code]"-"[2 char country code].
28       * 
29       * @param locale The locale as <code>String</code>.
30       * @return The language code.
31       **/
32      public static String getLanguageCode(String locale) {
33  
34          String code = "";
35  
36          int pos = locale.indexOf(LOCALE_SEPARATOR);
37  
38          if (pos != -1) {
39              code = locale.substring(0, pos);
40  
41          }
42          else if (locale.length() == 2) {
43              //see if locale is language only
44              code = locale;
45  
46          }
47          else if ((pos = locale.indexOf("_")) != -1) {
48  
49              // see if separator is "_" instead of "-"
50              code = locale.substring(0, pos);
51  
52          }
53  
54          return code;
55      }
56  
57      /**
58       * Gets the country code from a locales string representation, assuming
59       * that the provided locale has a format like [2 char language code]"-"[2 char country code].
60       * 
61       * @param locale The locale as <code>String</code>.
62       * @return The country code.
63       **/
64      public static String getCountryCode(String locale) {
65  
66          String code = "";
67  
68          int pos = locale.indexOf(LOCALE_SEPARATOR);
69  
70          if (pos != -1) {
71              code = locale.substring(pos + 1, locale.length());
72  
73          }
74          else if ((pos = locale.indexOf("_")) != -1) {
75  
76              // see if separator is "_" instead of "-"
77              code = locale.substring(0, pos);
78          }
79  
80          return code;
81      }
82  
83      /**
84       * Convinence method to create a <code>Locale</code> object from a locales
85       * string representation, assuming that the provided locale has a format like 
86       * [2 char language code]"-"[2 char country code].
87       *
88       * @param locale The locale as <code>String</code>.
89       * @return The corresponding <code>Locale</code> object.
90       **/
91      public static Locale getLocale(String locale) {
92          return new Locale(getLanguageCode(locale), getCountryCode(locale));
93      }
94  
95      /**
96       * The method takes a given <code>Locale</code> and returns a string
97       * representation which is conform to the WSRP standard.
98       * 
99       * @param locale The <code>Locale</code> to be formatted.
100      * @return A string representation of the given locale which is conform to the 
101      *          WSRP standard.    
102      **/
103     public static String getWsrpLocale(Locale locale) {
104         if (locale == null)
105             return null;
106 
107         String lang = locale.getLanguage();
108         String country = locale.getCountry();
109 
110         return lang
111                 + ((country != null && !country.equals("")) ? "-" + country
112                         : "");
113     }
114 
115     /**
116      * Convinence method to create a <code>Locale</code> object array from a locales
117      * string array representation, assuming that the provided locale has a format like 
118      * [2 char language code]"-"[2 char country code].
119      *
120      * @param locales The locale as <code>String</code>.
121      * @return The corresponding <code>Locale</code> object.
122      **/
123     public static Locale[] getLocales(String[] locales) {
124 
125         ArrayList list = new ArrayList();
126 
127         for (int i = 0; i < locales.length; i++) {
128             list.add(getLocale(locales[i]));
129         }
130 
131         Locale[] typedArray = new Locale[locales.length];
132 
133         return (Locale[]) list.toArray(typedArray);
134     }
135 }