Coverage Report - org.apache.myfaces.shared_impl.util.LocaleUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LocaleUtils
0%
0/38
0%
0/16
4.667
 
 1  
 /*
 2  
  *  Licensed to the Apache Software Foundation (ASF) under one
 3  
  *  or more contributor license agreements.  See the NOTICE file
 4  
  *  distributed with this work for additional information
 5  
  *  regarding copyright ownership.  The ASF licenses this file
 6  
  *  to you under the Apache License, Version 2.0 (the
 7  
  *  "License"); you may not use this file except in compliance
 8  
  *  with the License.  You may obtain a copy of the License at
 9  
  * 
 10  
  *  http://www.apache.org/licenses/LICENSE-2.0
 11  
  * 
 12  
  *  Unless required by applicable law or agreed to in writing,
 13  
  *  software distributed under the License is distributed on an
 14  
  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  *  KIND, either express or implied.  See the License for the
 16  
  *  specific language governing permissions and limitations
 17  
  *  under the License.
 18  
  */
 19  
 package org.apache.myfaces.shared_impl.util;
 20  
 
 21  
 import java.util.Locale;
 22  
 import java.util.StringTokenizer;
 23  
 
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 
 28  
 /**
 29  
  * @author Anton Koinov (latest modification by $Author$)
 30  
  * @version $Revision$ $Date$
 31  
  */
 32  
 public final class LocaleUtils
 33  
 {
 34  0
     private static final Log log = LogFactory.getLog(LocaleUtils.class);
 35  
 
 36  
     /** Utility class, do not instatiate */
 37  
     private LocaleUtils()
 38  0
     {
 39  
         // utility class, do not instantiate
 40  0
     }
 41  
 
 42  
     /**
 43  
      * Converts a locale string to <code>Locale</code> class. Accepts both
 44  
      * '_' and '-' as separators for locale components.
 45  
      *
 46  
      * @param localeString string representation of a locale
 47  
      * @return Locale instance, compatible with the string representation
 48  
      */
 49  
     public static Locale toLocale(String localeString)
 50  
     {
 51  0
         if ((localeString == null) || (localeString.length() == 0))
 52  
         {
 53  0
             Locale locale = Locale.getDefault();
 54  0
             if(log.isWarnEnabled())
 55  0
                 log.warn("Locale name in faces-config.xml null or empty, setting locale to default locale : "+locale.toString());
 56  0
             return locale;
 57  
         }
 58  
 
 59  0
         int separatorCountry = localeString.indexOf('_');
 60  
         char separator;
 61  0
         if (separatorCountry >= 0) {
 62  0
             separator = '_';
 63  
         }
 64  
         else
 65  
         {
 66  0
             separatorCountry = localeString.indexOf('-');
 67  0
             separator = '-';
 68  
         }
 69  
 
 70  
         String language, country, variant;
 71  0
         if (separatorCountry < 0)
 72  
         {
 73  0
             language = localeString;
 74  0
             country = variant = "";
 75  
         }
 76  
         else
 77  
         {
 78  0
             language = localeString.substring(0, separatorCountry);
 79  
 
 80  0
             int separatorVariant = localeString.indexOf(separator, separatorCountry + 1);
 81  0
             if (separatorVariant < 0)
 82  
             {
 83  0
                 country = localeString.substring(separatorCountry + 1);
 84  0
                 variant = "";
 85  
             }
 86  
             else
 87  
             {
 88  0
                 country = localeString.substring(separatorCountry + 1, separatorVariant);
 89  0
                 variant = localeString.substring(separatorVariant + 1);
 90  
             }
 91  
         }
 92  
 
 93  0
         return new Locale(language, country, variant);
 94  
     }
 95  
 
 96  
 
 97  
     /**
 98  
      * Convert locale string used by converter tags to locale.
 99  
      *
 100  
      * @param name name of the locale
 101  
      * @return locale specified by the given String
 102  
      *
 103  
      * @see org.apache.myfaces.taglib.core.ConvertDateTimeTag#setConverterLocale
 104  
      * @see org.apache.myfaces.taglib.core.ConvertNumberTag#setConverterLocale
 105  
      */
 106  
     public static Locale converterTagLocaleFromString(String name)
 107  
     {
 108  
         try
 109  
         {
 110  
             Locale locale;
 111  0
             StringTokenizer st = new StringTokenizer(name, "_");
 112  0
             String language = st.nextToken();
 113  
 
 114  0
             if(st.hasMoreTokens())
 115  
             {
 116  0
                 String country = st.nextToken();
 117  
 
 118  0
                 if(st.hasMoreTokens())
 119  
                 {
 120  0
                     String variant = st.nextToken();
 121  0
                     locale = new Locale(language, country, variant);
 122  0
                 }
 123  
                 else
 124  
                 {
 125  0
                     locale = new Locale(language, country);
 126  
                 }
 127  0
             }
 128  
             else
 129  
             {
 130  0
                 locale = new Locale(language);
 131  
             }
 132  
 
 133  
 
 134  0
             return locale;
 135  
         }
 136  0
         catch(Exception e)
 137  
         {
 138  0
             throw new IllegalArgumentException("Locale parsing exception - " +
 139  
                 "invalid string representation '" + name + "'");
 140  
         }
 141  
     }
 142  
 }