001    package org.apache.myfaces.tobago.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.lang.StringUtils;
021    
022    import java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.List;
025    import java.util.Locale;
026    
027    public class LocaleUtils {
028    
029      private LocaleUtils() {
030      }
031    
032      public static Locale createLocale(String value) {
033        Locale locale = null;
034        String[] strings = StringUtils.split(value, "_");
035        switch (strings.length) {
036          case 1:
037            locale = new Locale(strings[0]);
038            break;
039          case 2:
040            locale = new Locale(strings[0], strings[1]);
041            break;
042          case 3:
043            locale = new Locale(strings[0], strings[1], strings[2]);
044            break;
045          default:
046            // TODO
047        }
048        return locale;
049      }
050    
051      /**
052       * @deprecated since 1.5
053       */
054      @Deprecated
055      public static List<Locale> getLocaleList(Locale locale) {
056    
057        String string = locale.toString();
058        List<Locale> locales = new ArrayList<Locale>(3);
059        locales.add(locale);
060        int underscore;
061        while ((underscore = string.lastIndexOf('_')) > 0) {
062          string = string.substring(0, underscore);
063          locales.add(createLocale(string));
064        }
065    
066        return locales;
067      }
068    
069      /**
070       * Returns a list of strings, which are used as suffix for resources from resource manager.
071       * 
072       * Sample: "de_DE" -> "_de_DE", "_de", "" 
073       */
074      public static List<String> getLocaleSuffixList(Locale locale) {
075    
076        if (locale == null) {
077          return Arrays.asList("");
078        }
079        
080        String string = locale.toString();
081        String prefix = "_";
082        List<String> locales = new ArrayList<String>(4);
083        locales.add(prefix + string);
084        int underscore;
085        while ((underscore = string.lastIndexOf('_')) > 0) {
086          string = string.substring(0, underscore);
087          locales.add(prefix + string);
088        }
089    
090        locales.add(""); // default suffix is nothing
091    
092        return locales;
093      }
094    
095    }