1. Project Clover database Sat Feb 2 2019 06:45:20 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 63 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  155749611 toggle public static Locale getParentLocale(Locale locale)
39    {
40  155749676 String language = locale.getLanguage();
41  155749717 String country = locale.getCountry();
42  155749726 String variant = locale.getVariant();
43   
44  155750045 if (StringUtils.isEmpty(language)) {
45  101094441 return null;
46    }
47   
48  54655034 if (StringUtils.isEmpty(country)) {
49  54654957 return Locale.ROOT;
50    }
51   
52  16 if (StringUtils.isEmpty(variant)) {
53  16 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  2097563 toggle public static Locale toLocale(String str)
68    {
69  2097732 if (str != null && str.isEmpty()) {
70  53509 return Locale.ROOT;
71    }
72   
73  2044334 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  698067 toggle public static Locale toLocale(String str, Locale fallback)
87    {
88  698397 try {
89  698816 return toLocale(str);
90    } catch (Exception e) {
91  1 return fallback;
92    }
93    }
94    }