EMMA Coverage Report (generated Fri Jun 19 09:16:10 CEST 2009)
[all classes][org.ktc.rbutils.api.i18n]

COVERAGE SUMMARY FOR SOURCE FILE [LocaleUtils.java]

nameclass, %method, %block, %line, %
LocaleUtils.java100% (1/1)100% (6/6)100% (103/103)100% (31/31)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class LocaleUtils100% (1/1)100% (6/6)100% (103/103)100% (31/31)
<static initializer> 100% (1/1)100% (8/8)100% (1/1)
LocaleUtils (): void 100% (1/1)100% (6/6)100% (2/2)
getEmptyLocale (): Locale 100% (1/1)100% (2/2)100% (1/1)
getLocaleFrom (String): Locale 100% (1/1)100% (26/26)100% (9/9)
removeLocale (String): String 100% (1/1)100% (16/16)100% (5/5)
toLocale (String): Locale 100% (1/1)100% (45/45)100% (13/13)

1/*
2 * Copyright  2005-2006 The RbUtils Project
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 
18// $Id: LocaleUtils.java,v 1.2 2006/09/16 14:31:40 redfish Exp $
19 
20package org.ktc.rbutils.api.i18n;
21 
22import java.util.Locale;
23import java.util.StringTokenizer;
24import org.apache.commons.lang.StringUtils;
25 
26/**
27 * General <code>Locale</code> manipulation utilities.
28 * @since RbUtils 0.5
29 * @version $Revision: 1.2 $
30 * @author moishi
31 */
32public class LocaleUtils {
33    /**
34     * The <code>Locale</code> separator as <code>String</code>.
35     */
36    public static final String LOCALE_SEPARATOR = "_";
37    /**
38     * The <code>Locale</code> separator character.
39     */
40    public static final char LOCALE_SEPARATOR_CHAR = '_';
41    /**
42     * The empty <code>Locale</code>. This is the Locale where language, country and variant are
43     * <code>null</code>.
44     */
45    private static final Locale EMPTY_LOCALE = new Locale(StringUtils.EMPTY, StringUtils.EMPTY,
46                                                          StringUtils.EMPTY);
47 
48    /**
49     * Dummy constructor. This <b>MUST NOT</b> be instanciated.
50     */
51    protected LocaleUtils() {
52        // prevents calls from subclass
53        throw new UnsupportedOperationException();
54    }
55 
56    /**
57     * Returns the <code>Locale</code> represented by a <code>String</code>. <br>
58     * If <code>locale</code> is <code>null</code> or empty, the empty locale is returned.
59     * @param locale the string representation of a locale.
60     * @return the locale represented by a string.
61     */
62    public static Locale toLocale(final String locale) {
63        final Locale aLocale;
64        if (locale == null || StringUtils.EMPTY.equals(locale)) {
65            aLocale = getEmptyLocale();
66        }
67        else {
68            final StringTokenizer st = new StringTokenizer(locale, LOCALE_SEPARATOR);
69            String language = StringUtils.EMPTY;
70            String country = StringUtils.EMPTY;
71            String variant = StringUtils.EMPTY;
72 
73            language = st.nextToken();
74            if (st.hasMoreElements()) {
75                country = st.nextToken();
76                if (st.hasMoreElements()) {
77                    variant = st.nextToken();
78                }
79            }
80            // int localeSepPosition = locale.indexOf(LOCALE_SEPARATOR_CHAR);
81            // String language = StringUtils.EMPTY;
82            // String country = StringUtils.EMPTY;
83            // String variant = StringUtils.EMPTY;
84            //
85            // // No country
86            // if (localeSepPosition == -1) {
87            // language = locale.toString();
88            // }
89            // // Country exists
90            // else {
91            // language = locale.substring(0, localeSepPosition);
92            // final String localeWhitoutCtry = locale.substring(localeSepPosition + 1);
93            // localeSepPosition = localeWhitoutCtry.indexOf(LOCALE_SEPARATOR_CHAR);
94            //
95            // // No variant
96            // if (localeSepPosition == -1) {
97            // country = localeWhitoutCtry.toString();
98            // }
99            // // Variant exists
100            //                else {
101            //                    country = localeWhitoutCtry.substring(0, localeSepPosition);
102            //                    variant = localeWhitoutCtry.substring(localeSepPosition + 1);
103            //                }
104            //            }
105 
106            aLocale = new Locale(language, country, variant);
107        }
108        return aLocale;
109    }
110 
111    /**
112     * Returns the empty locale. This is the locale where language, country and variant are the
113     * empty <code>String</code>.
114     * @return the empty locale.
115     */
116    public static Locale getEmptyLocale() {
117        return EMPTY_LOCALE;
118    }
119 
120    /**
121     * Removes the locale part in a <code>String</code>.
122     * @param string the <code>String</code> from which the locale part will be removed.
123     * @return string without its locale part; <code>null</code> if the String parameter is
124     *         <code>null</code>.
125     */
126    public static String removeLocale(final String string) {
127        final String stringWithoutLocale;
128        if (string == null) {
129            stringWithoutLocale = null;
130        }
131        else {
132            final StringTokenizer st = new StringTokenizer(string, LOCALE_SEPARATOR);
133            stringWithoutLocale = st.nextToken();
134            // final int firstLocalSep = string.indexOf(LOCALE_SEPARATOR_CHAR);
135            //
136            // // checks language existence
137            // if (firstLocalSep != -1) {
138            // stringWithoutLocale = string.substring(0, firstLocalSep);
139            // }
140            // else {
141            //                stringWithoutLocale = string;
142            //            }
143        }
144 
145        return stringWithoutLocale;
146    }
147 
148    /**
149     * Returns the locale part of a <code>String</code>. <br>
150     * If <code>string</code> is <code>null</code> or does not have any locale part, the empty
151     * locale is returned.
152     * @param string the <code>String</code> from which the locale part will be removed.
153     * @return the locale part of the string.
154     */
155    public static Locale getLocaleFrom(final String string) {
156        final Locale locale;
157        if (string == null) {
158            locale = getEmptyLocale();
159        }
160        else {
161            final int localeSepPosition = string.indexOf(LocaleUtils.LOCALE_SEPARATOR_CHAR);
162 
163            // Locale exists
164            if (localeSepPosition != -1) {
165                final String localeAsString = string.substring(localeSepPosition + 1);
166                locale = toLocale(localeAsString);
167            }
168            else {
169                locale = getEmptyLocale();
170            }
171        }
172        return locale;
173    }
174 
175}

[all classes][org.ktc.rbutils.api.i18n]
EMMA 2.0.5312 (C) Vladimir Roubtsov