001    // Copyright 2008 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.util;
016    
017    import java.util.Locale;
018    
019    /**
020     * Contains code borrowed from <a href="http://commons.apache.org/lang/">commons-lang</a>.
021     */
022    public class LocaleUtils
023    {
024        /**
025         * <p>Converts a String to a Locale.</p> <p/> <p>This method takes the string format of a locale and creates the
026         * locale object from it.</p> <p/>
027         * <pre>
028         *   LocaleUtils.toLocale("en")         = new Locale("en", "")
029         *   LocaleUtils.toLocale("en_GB")      = new Locale("en", "GB")
030         *   LocaleUtils.toLocale("en_GB_xxx")  = new Locale("en", "GB", "xxx")   (#)
031         * </pre>
032         * <p/> <p>(#) The behaviour of the JDK variant constructor changed between JDK1.3 and JDK1.4. In JDK1.3, the
033         * constructor upper cases the variant, in JDK1.4, it doesn't. Thus, the result from getVariant() may vary depending
034         * on your JDK.</p> <p/> <p>This method validates the input strictly. The language code must be lowercase. The
035         * country code must be uppercase. The separator must be an underscore. The length must be correct. </p>
036         *
037         * @param input the locale String to convert, null returns null
038         * @return a Locale, null if null input
039         * @throws IllegalArgumentException if the string is an invalid format
040         */
041        public static Locale toLocale(String input)
042        {
043            if (input == null)
044                return null;
045    
046            int len = input.length();
047            if (len != 2 && len != 5 && len < 7)
048                fail(input);
049    
050            char ch0 = input.charAt(0);
051            char ch1 = input.charAt(1);
052    
053            if (ch0 < 'a' || ch0 > 'z' || ch1 < 'a' || ch1 > 'z')
054                fail(input);
055    
056            if (len == 2)
057                return new Locale(input, "");
058    
059            if (input.charAt(2) != '_')
060                fail(input);
061    
062            char ch3 = input.charAt(3);
063            if (ch3 == '_')
064                return new Locale(input.substring(0, 2), "", input.substring(4));
065    
066            char ch4 = input.charAt(4);
067            if (ch3 < 'A' || ch3 > 'Z' || ch4 < 'A' || ch4 > 'Z')
068                fail(input);
069    
070            if (len == 5)
071                return new Locale(input.substring(0, 2), input.substring(3, 5));
072    
073            if (input.charAt(5) != '_')
074                fail(input);
075    
076            return new Locale(input.substring(0, 2), input.substring(3, 5), input.substring(6));
077        }
078    
079        private static void fail(String input)
080        {
081            throw new IllegalArgumentException(String.format("Unable to convert '%s' to a Locale instance.", input));
082        }
083    
084    }