001: /*
002: * Copyright 2000-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.wsrp4j.util;
018:
019: import java.util.ArrayList;
020: import java.util.Locale;
021:
022: public class LocaleHelper {
023: private final static String LOCALE_SEPARATOR = "-";
024:
025: /**
026: * Gets the language code from a locales string representation, assuming
027: * that the provided locale has a format like [2 char language code]"-"[2 char country code].
028: *
029: * @param locale The locale as <code>String</code>.
030: * @return The language code.
031: **/
032: public static String getLanguageCode(String locale) {
033:
034: String code = "";
035:
036: int pos = locale.indexOf(LOCALE_SEPARATOR);
037:
038: if (pos != -1) {
039: code = locale.substring(0, pos);
040:
041: } else if (locale.length() == 2) {
042: //see if locale is language only
043: code = locale;
044:
045: } else if ((pos = locale.indexOf("_")) != -1) {
046:
047: // see if separator is "_" instead of "-"
048: code = locale.substring(0, pos);
049:
050: }
051:
052: return code;
053: }
054:
055: /**
056: * Gets the country code from a locales string representation, assuming
057: * that the provided locale has a format like [2 char language code]"-"[2 char country code].
058: *
059: * @param locale The locale as <code>String</code>.
060: * @return The country code.
061: **/
062: public static String getCountryCode(String locale) {
063:
064: String code = "";
065:
066: int pos = locale.indexOf(LOCALE_SEPARATOR);
067:
068: if (pos != -1) {
069: code = locale.substring(pos + 1, locale.length());
070:
071: } else if ((pos = locale.indexOf("_")) != -1) {
072:
073: // see if separator is "_" instead of "-"
074: code = locale.substring(0, pos);
075: }
076:
077: return code;
078: }
079:
080: /**
081: * Convinence method to create a <code>Locale</code> object from a locales
082: * string representation, assuming that the provided locale has a format like
083: * [2 char language code]"-"[2 char country code].
084: *
085: * @param locale The locale as <code>String</code>.
086: * @return The corresponding <code>Locale</code> object.
087: **/
088: public static Locale getLocale(String locale) {
089: return new Locale(getLanguageCode(locale),
090: getCountryCode(locale));
091: }
092:
093: /**
094: * The method takes a given <code>Locale</code> and returns a string
095: * representation which is conform to the WSRP standard.
096: *
097: * @param locale The <code>Locale</code> to be formatted.
098: * @return A string representation of the given locale which is conform to the
099: * WSRP standard.
100: **/
101: public static String getWsrpLocale(Locale locale) {
102: if (locale == null)
103: return null;
104:
105: String lang = locale.getLanguage();
106: String country = locale.getCountry();
107:
108: return lang
109: + ((country != null && !country.equals("")) ? "-"
110: + country : "");
111: }
112:
113: /**
114: * Convinence method to create a <code>Locale</code> object array from a locales
115: * string array representation, assuming that the provided locale has a format like
116: * [2 char language code]"-"[2 char country code].
117: *
118: * @param locales The locale as <code>String</code>.
119: * @return The corresponding <code>Locale</code> object.
120: **/
121: public static Locale[] getLocales(String[] locales) {
122:
123: ArrayList list = new ArrayList();
124:
125: for (int i = 0; i < locales.length; i++) {
126: list.add(getLocale(locales[i]));
127: }
128:
129: Locale[] typedArray = new Locale[locales.length];
130:
131: return (Locale[]) list.toArray(typedArray);
132: }
133: }
|