Coverage report

  %line %branch
org.apache.turbine.services.localization.LocaleDetector
0% 
0% 

 1  
 package org.apache.turbine.services.localization;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * 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, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.util.Locale;
 20  
 import java.util.StringTokenizer;
 21  
 
 22  
 import org.apache.commons.configuration.Configuration;
 23  
 
 24  
 import org.apache.turbine.Turbine;
 25  
 import org.apache.turbine.util.RunData;
 26  
 
 27  
 /**
 28  
  * This class returns a Locale object based on the HTTP
 29  
  * Accept-Language header.
 30  
  *
 31  
  * This class is based on examples from Jason Hunter's book <i>Java
 32  
  * Servlet Programming</i>.
 33  
  *
 34  
  * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
 35  
  * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
 36  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 37  
  * @version $Id: LocaleDetector.java 264148 2005-08-29 14:21:04Z henning $
 38  
  * @deprecated Use LocaleTokenizer instead.
 39  
  */
 40  0
 public class LocaleDetector
 41  
 {
 42  
     /**
 43  
      * Attempts to pull the "Accept-Language" header out of the
 44  
      * HttpServletRequest object and then parse it.  If the header is
 45  
      * not present, it will return a null Locale.
 46  
      *
 47  
      * @param data Turbine information.
 48  
      * @return A Locale.
 49  
      */
 50  
     public static Locale getLocale(RunData data)
 51  
     {
 52  0
         String header = data.getRequest().getHeader("Accept-Language");
 53  0
         if (header == null || header.length() == 0)
 54  0
             return null;
 55  0
         return getLocale(header);
 56  
     }
 57  
 
 58  
     /**
 59  
      * This method parses the Accept-Language header and attempts to
 60  
      * create a Locale out of it.
 61  
      *
 62  
      * @param languageHeader A String with the language header.
 63  
      * @return A Locale.
 64  
      */
 65  
     public static Locale getLocale(String languageHeader)
 66  
     {
 67  0
         Configuration conf = Turbine.getConfiguration();
 68  
 
 69  
 
 70  
         // return a "default" locale
 71  0
         if (languageHeader == null ||
 72  
                 languageHeader.trim().equals(""))
 73  
         {
 74  0
             return new Locale(
 75  
                     conf.getString("locale.default.language", "en"),
 76  
                     conf.getString("locale.default.country", "US"));
 77  
         }
 78  
 
 79  
         // The HTTP Accept-Header is something like
 80  
         //
 81  
         // "en, es;q=0.8, zh-TW;q=0.1"
 82  0
         StringTokenizer tokenizer = new StringTokenizer(languageHeader, ",");
 83  
 
 84  
         // while ( tokenizer.hasMoreTokens() )
 85  
         // {
 86  0
         String language = tokenizer.nextToken();
 87  
         // This should never be true but just in case
 88  
         // if ( !language.trim().equals("") )
 89  0
         return getLocaleForLanguage(language.trim());
 90  
         // }
 91  
     }
 92  
 
 93  
     /**
 94  
      * This method creates a Locale from the language.
 95  
      *
 96  
      * @param language A String with the language.
 97  
      * @return A Locale.
 98  
      */
 99  
     private static Locale getLocaleForLanguage(String language)
 100  
     {
 101  
         Locale locale;
 102  
         int semi, dash;
 103  
 
 104  
         // Cut off any q-value that comes after a semicolon.
 105  0
         if ((semi = language.indexOf(';')) != -1)
 106  
         {
 107  0
             language = language.substring(0, semi);
 108  
         }
 109  
 
 110  0
         language = language.trim();
 111  
 
 112  
         // Create a Locale from the language.  A dash may separate the
 113  
         // language from the country.
 114  0
         if ((dash = language.indexOf('-')) == -1)
 115  
         {
 116  
             // No dash means no country.
 117  0
             locale = new Locale(language, "");
 118  
         }
 119  
         else
 120  
         {
 121  0
             locale = new Locale(language.substring(0, dash),
 122  
                     language.substring(dash + 1));
 123  
         }
 124  
 
 125  0
         return locale;
 126  
     }
 127  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.