001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jetspeed.util;
019:
020: import java.util.Locale;
021: import java.util.StringTokenizer;
022:
023: /**
024: * Class to set and get Locale settings for Jetspeed.
025: *
026: * @author <a href="mailto:roger.ruttimann@earthlink.net">Roger Ruttimann</a>
027: * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
028: * @version $Id: JetspeedLocale.java 516448 2007-03-09 16:25:47Z ate $
029: */
030: public class JetspeedLocale {
031: private static final String DELIM = ",";
032:
033: /**
034: * According to PLT.21.8.1, the default locale should be English.
035: */
036: public static Locale getDefaultLocale() {
037: return Locale.ENGLISH;
038: }
039:
040: /**
041: * Converts Locale to String.
042: *
043: * @param locale
044: * @return
045: */
046: public static String convertLocaleToString(Locale locale) {
047: if (locale == null) {
048: return null;
049: }
050: String country = locale.getCountry();
051: String language = locale.getLanguage();
052: String variant = locale.getVariant();
053: StringBuffer buffer = new StringBuffer(40);
054: if (language != null) {
055: buffer.append(language);
056: }
057: buffer.append(DELIM);
058:
059: if (country != null) {
060: buffer.append(country);
061: }
062: buffer.append(DELIM);
063:
064: if (variant != null) {
065: buffer.append(variant);
066: }
067:
068: return buffer.toString().trim();
069: }
070:
071: /**
072: * Converts String to Locale.
073: *
074: * @param localeString
075: * @return
076: */
077: public static Locale convertStringToLocale(String localeString) {
078: if (localeString == null) {
079: return null;
080: }
081: StringTokenizer tokenizer = new StringTokenizer(localeString,
082: DELIM);
083:
084: String language = tokenizer.nextToken().trim();
085: String country = null;
086: String variant = null;
087: if (tokenizer.hasMoreTokens()) {
088: country = tokenizer.nextToken().trim();
089: }
090:
091: if (tokenizer.hasMoreTokens()) {
092: variant = tokenizer.nextToken().trim();
093: }
094:
095: if (country != null && language != null && variant != null) {
096: return new Locale(language, country, variant);
097: } else if (country != null && language != null) {
098: return new Locale(language, country);
099: } else if (language != null) {
100: return new Locale(language, ""); // JDK 1.3 compatibility
101: } else {
102: return null;
103: }
104:
105: }
106:
107: }
|