01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package org.outerj.daisy.repository;
17:
18: import java.util.Locale;
19: import java.util.StringTokenizer;
20:
21: /**
22: * Helps converting between Locale objects and strings.
23: */
24: public final class LocaleHelper {
25: public static Locale parseLocale(String localeString) {
26: StringTokenizer localeParser = new StringTokenizer(
27: localeString, "-_");
28:
29: String lang = null, country = null, variant = null;
30:
31: if (localeParser.hasMoreTokens())
32: lang = localeParser.nextToken();
33: if (localeParser.hasMoreTokens())
34: country = localeParser.nextToken();
35: if (localeParser.hasMoreTokens())
36: variant = localeParser.nextToken();
37:
38: if (lang != null && country != null && variant != null)
39: return new Locale(lang, country, variant);
40: else if (lang != null && country != null)
41: return new Locale(lang, country);
42: else if (lang != null)
43: return new Locale(lang);
44: else
45: return new Locale("");
46: }
47:
48: public static String getString(Locale locale) {
49: boolean hasLanguage = !locale.getLanguage().equals("");
50: boolean hasCountry = !locale.getCountry().equals("");
51: boolean hasVariant = !locale.getVariant().equals("");
52:
53: if (hasLanguage && hasCountry && hasVariant)
54: return locale.getLanguage() + '-' + locale.getCountry()
55: + '-' + locale.getVariant();
56: else if (hasLanguage && hasCountry)
57: return locale.getLanguage() + '-' + locale.getCountry();
58: else if (hasLanguage)
59: return locale.getLanguage();
60: else
61: return "";
62: }
63: }
|