1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.localization

File LocaleUtils.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
16
3
1
94
37
9
0.56
5.33
3
3

Classes

Class Line # Actions
LocaleUtils 32 16 0% 9 2
0.925925992.6%
 

Contributing tests

This file is covered by 64 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.localization;
21   
22    import java.util.Locale;
23   
24    import org.apache.commons.lang3.StringUtils;
25   
26    /**
27    * Extends {@link org.apache.commons.lang3.LocaleUtils} with more tools.
28    *
29    * @version $Id: f41368dd83ab79f2b0d1e9d915aaa75ddf5538fb $
30    * @since 5.1RC1
31    */
 
32    public class LocaleUtils extends org.apache.commons.lang3.LocaleUtils
33    {
34    /**
35    * @param locale the locale
36    * @return the parent locale
37    */
 
38  1565634 toggle public static Locale getParentLocale(Locale locale)
39    {
40  1565634 String language = locale.getLanguage();
41  1565634 String country = locale.getCountry();
42  1565633 String variant = locale.getVariant();
43   
44  1565651 if (StringUtils.isEmpty(language)) {
45  936775 return null;
46    }
47   
48  628870 if (StringUtils.isEmpty(country)) {
49  628856 return Locale.ROOT;
50    }
51   
52  15 if (StringUtils.isEmpty(variant)) {
53  15 return new Locale(language);
54    }
55   
56  0 return new Locale(language, country);
57    }
58   
59    /**
60    * Extends {@link org.apache.commons.lang3.LocaleUtils} which return {@link Locale#ROOT} for an empty string.
61    *
62    * @param str the locale String to convert, null returns null
63    * @return a Locale, null if null input
64    * @throws IllegalArgumentException if the string is an invalid format
65    * @since 5.2M1
66    */
 
67  167490 toggle public static Locale toLocale(String str)
68    {
69  167476 if (str != null && str.isEmpty()) {
70  7087 return Locale.ROOT;
71    }
72   
73  160388 return org.apache.commons.lang3.LocaleUtils.toLocale(str);
74    }
75   
76    /**
77    * Same as {@link #toLocale(String)} but it never throws an exception. It returns the passed fallback locale in case
78    * the given string locale has an invalid format.
79    *
80    * @param str the locale string to convert, null returns null
81    * @param fallback the locale to return as fallback in case the given locale string has an invalid format
82    * @return a Locale, null if null input
83    * @see #toLocale(String)
84    * @since 6.0M1
85    */
 
86  67721 toggle public static Locale toLocale(String str, Locale fallback)
87    {
88  67710 try {
89  67700 return toLocale(str);
90    } catch (Exception e) {
91  1 return fallback;
92    }
93    }
94    }