| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LocaleUtils |
|
| 8.0;8 |
| 1 | // Copyright 2008 The Apache Software Foundation | |
| 2 | // | |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | // you may not use this file except in compliance with the License. | |
| 5 | // You may obtain a copy of the License at | |
| 6 | // | |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | // | |
| 9 | // Unless required by applicable law or agreed to in writing, software | |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 | // See the License for the specific language governing permissions and | |
| 13 | // limitations under the License. | |
| 14 | ||
| 15 | package org.apache.tapestry5.internal.util; | |
| 16 | ||
| 17 | import java.util.Locale; | |
| 18 | ||
| 19 | /** | |
| 20 | * Contains code borrowed from <a href="http://commons.apache.org/lang/">commons-lang</a>. | |
| 21 | */ | |
| 22 | 0 | public class LocaleUtils |
| 23 | { | |
| 24 | /** | |
| 25 | * <p>Converts a String to a Locale.</p> <p/> <p>This method takes the string format of a locale and creates the | |
| 26 | * locale object from it.</p> <p/> | |
| 27 | * <pre> | |
| 28 | * LocaleUtils.toLocale("en") = new Locale("en", "") | |
| 29 | * LocaleUtils.toLocale("en_GB") = new Locale("en", "GB") | |
| 30 | * LocaleUtils.toLocale("en_GB_xxx") = new Locale("en", "GB", "xxx") (#) | |
| 31 | * </pre> | |
| 32 | * <p/> <p>(#) The behaviour of the JDK variant constructor changed between JDK1.3 and JDK1.4. In JDK1.3, the | |
| 33 | * constructor upper cases the variant, in JDK1.4, it doesn't. Thus, the result from getVariant() may vary depending | |
| 34 | * on your JDK.</p> <p/> <p>This method validates the input strictly. The language code must be lowercase. The | |
| 35 | * country code must be uppercase. The separator must be an underscore. The length must be correct. </p> | |
| 36 | * | |
| 37 | * @param input the locale String to convert, null returns null | |
| 38 | * @return a Locale, null if null input | |
| 39 | * @throws IllegalArgumentException if the string is an invalid format | |
| 40 | */ | |
| 41 | public static Locale toLocale(String input) | |
| 42 | { | |
| 43 | 52 | if (input == null) |
| 44 | 2 | return null; |
| 45 | ||
| 46 | 50 | int len = input.length(); |
| 47 | 50 | if (len != 2 && len != 5 && len < 7) |
| 48 | 8 | fail(input); |
| 49 | ||
| 50 | 42 | char ch0 = input.charAt(0); |
| 51 | 42 | char ch1 = input.charAt(1); |
| 52 | ||
| 53 | 42 | if (ch0 < 'a' || ch0 > 'z' || ch1 < 'a' || ch1 > 'z') |
| 54 | 10 | fail(input); |
| 55 | ||
| 56 | 32 | if (len == 2) |
| 57 | 10 | return new Locale(input, ""); |
| 58 | ||
| 59 | 22 | if (input.charAt(2) != '_') |
| 60 | 2 | fail(input); |
| 61 | ||
| 62 | 20 | char ch3 = input.charAt(3); |
| 63 | 20 | if (ch3 == '_') |
| 64 | 0 | return new Locale(input.substring(0, 2), "", input.substring(4)); |
| 65 | ||
| 66 | 20 | char ch4 = input.charAt(4); |
| 67 | 20 | if (ch3 < 'A' || ch3 > 'Z' || ch4 < 'A' || ch4 > 'Z') |
| 68 | 8 | fail(input); |
| 69 | ||
| 70 | 12 | if (len == 5) |
| 71 | 4 | return new Locale(input.substring(0, 2), input.substring(3, 5)); |
| 72 | ||
| 73 | 8 | if (input.charAt(5) != '_') |
| 74 | 2 | fail(input); |
| 75 | ||
| 76 | 6 | return new Locale(input.substring(0, 2), input.substring(3, 5), input.substring(6)); |
| 77 | } | |
| 78 | ||
| 79 | private static void fail(String input) | |
| 80 | { | |
| 81 | 30 | throw new IllegalArgumentException(String.format("Unable to convert '%s' to a Locale instance.", input)); |
| 82 | } | |
| 83 | ||
| 84 | } |