KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > LocaleHelper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.repository;
17
18 import java.util.Locale JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 /**
22  * Helps converting between Locale objects and strings.
23  */

24 public class LocaleHelper {
25     public static Locale JavaDoc parseLocale(String JavaDoc localeString) {
26         StringTokenizer JavaDoc localeParser = new StringTokenizer JavaDoc(localeString, "-_");
27
28         String JavaDoc lang = null, country = null, variant = null;
29
30         if (localeParser.hasMoreTokens())
31             lang = localeParser.nextToken();
32         if (localeParser.hasMoreTokens())
33             country = localeParser.nextToken();
34         if (localeParser.hasMoreTokens())
35             variant = localeParser.nextToken();
36
37         if (lang != null && country != null && variant != null)
38             return new Locale JavaDoc(lang, country, variant);
39         else if (lang != null && country != null)
40             return new Locale JavaDoc(lang, country);
41         else if (lang != null)
42             return new Locale JavaDoc(lang);
43         else
44             return new Locale JavaDoc("");
45     }
46
47     public static String JavaDoc getString(Locale JavaDoc locale) {
48         boolean hasLanguage = !locale.getLanguage().equals("");
49         boolean hasCountry = !locale.getCountry().equals("");
50         boolean hasVariant = !locale.getVariant().equals("");
51
52         if (hasLanguage && hasCountry && hasVariant)
53             return locale.getLanguage() + '-' + locale.getCountry() + '-' + locale.getVariant();
54         else if (hasLanguage && hasCountry)
55             return locale.getLanguage() + '-' + locale.getCountry();
56         else if (hasLanguage)
57             return locale.getLanguage();
58         else
59             return "";
60     }
61 }
62
Popular Tags