Coverage Report - org.apache.turbine.util.LocaleUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LocaleUtils
92%
35/38
75%
6/8
2,5
 
 1  
 package org.apache.turbine.util;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.Locale;
 23  
 
 24  
 import org.apache.commons.lang3.StringUtils;
 25  
 import org.apache.fulcrum.mimetype.MimeTypeService;
 26  
 import org.apache.logging.log4j.LogManager;
 27  
 import org.apache.logging.log4j.Logger;
 28  
 import org.apache.turbine.Turbine;
 29  
 import org.apache.turbine.TurbineConstants;
 30  
 import org.apache.turbine.services.ServiceManager;
 31  
 import org.apache.turbine.services.TurbineServices;
 32  
 
 33  
 /**
 34  
  * This class provides utilities for handling locales and charsets
 35  
  *
 36  
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
 37  
  */
 38  0
 public class LocaleUtils
 39  
 {
 40  
     /** Logging */
 41  78
     private static final Logger log = LogManager.getLogger(LocaleUtils.class);
 42  
 
 43  
     /** The default locale. */
 44  78
     private static Locale defaultLocale = null;
 45  
 
 46  
     /** The default charset. */
 47  78
     private static String defaultCharSet = null;
 48  
 
 49  
     /**
 50  
      * Returns the default input encoding for the servlet.
 51  
      *
 52  
      * @return the default input encoding.
 53  
      */
 54  
     public static String getDefaultInputEncoding()
 55  
     {
 56  
         // Get the default input defaultEncoding
 57  105
         String inputEncoding = Turbine.getConfiguration()
 58  105
                 .getString(TurbineConstants.PARAMETER_ENCODING_KEY,
 59  
                         TurbineConstants.PARAMETER_ENCODING_DEFAULT);
 60  
 
 61  105
         log.debug("Input Encoding has been set to {}", inputEncoding);
 62  
 
 63  105
         return inputEncoding;
 64  
     }
 65  
 
 66  
     /**
 67  
      * Gets the default locale defined by properties named "locale.default.lang"
 68  
      * and "locale.default.country".
 69  
      *
 70  
      * This changed from earlier Turbine versions that you can rely on
 71  
      * getDefaultLocale() to never return null.
 72  
      *
 73  
      * @return A Locale object.
 74  
      */
 75  
     public static Locale getDefaultLocale()
 76  
     {
 77  3
         if (defaultLocale == null)
 78  
         {
 79  
             /* Get the default locale and cache it in a static variable. */
 80  3
             String lang = Turbine.getConfiguration()
 81  3
                     .getString(TurbineConstants.LOCALE_DEFAULT_LANGUAGE_KEY,
 82  
                             TurbineConstants.LOCALE_DEFAULT_LANGUAGE_DEFAULT);
 83  
 
 84  3
             String country = Turbine.getConfiguration()
 85  3
                     .getString(TurbineConstants.LOCALE_DEFAULT_COUNTRY_KEY,
 86  
                             TurbineConstants.LOCALE_DEFAULT_COUNTRY_DEFAULT);
 87  
 
 88  
             // We ensure that lang and country is never null
 89  3
             defaultLocale = new Locale(lang, country);
 90  
         }
 91  
 
 92  3
         return defaultLocale;
 93  
     }
 94  
 
 95  
     /**
 96  
      * Gets the default charset defined by a property named
 97  
      * "locale.default.charset"
 98  
      *
 99  
      * @return the name of the default charset or null.
 100  
      */
 101  
     public static String getDefaultCharSet()
 102  
     {
 103  9
         if (defaultCharSet == null)
 104  
         {
 105  
             /* Get the default charset and cache it in a static variable. */
 106  6
             defaultCharSet = Turbine.getConfiguration()
 107  6
                     .getString(TurbineConstants.LOCALE_DEFAULT_CHARSET_KEY,
 108  
                             TurbineConstants.LOCALE_DEFAULT_CHARSET_DEFAULT);
 109  6
             log.debug("defaultCharSet = {} (From Properties)", defaultCharSet);
 110  
         }
 111  
 
 112  9
         String charset = defaultCharSet;
 113  
 
 114  9
         if (StringUtils.isEmpty(charset)) // can happen if set explicitly in the configuration
 115  
         {
 116  3
             log.debug("Default charset is empty!");
 117  
             /* Default charset isn't specified, get the locale specific one. */
 118  3
             Locale locale = getDefaultLocale();
 119  3
             log.debug("Locale is {}", locale);
 120  
 
 121  3
             if (!locale.equals(Locale.US))
 122  
             {
 123  3
                 log.debug("We don't have US Locale!");
 124  3
                 ServiceManager serviceManager = TurbineServices.getInstance();
 125  3
                 MimeTypeService mimeTypeService = null;
 126  
                 try
 127  
                 {
 128  3
                     mimeTypeService = (MimeTypeService) serviceManager.getService(MimeTypeService.ROLE);
 129  
                 }
 130  0
                 catch (Exception e)
 131  
                 {
 132  0
                     throw new RuntimeException(e);
 133  3
                 }
 134  3
                 charset = mimeTypeService.getCharSet(locale);
 135  
 
 136  3
                 log.debug("Charset now {}", charset);
 137  
             }
 138  
         }
 139  
 
 140  9
         log.debug("Returning default Charset of {}", charset);
 141  9
         return charset;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Gets the charset defined by a property named "locale.override.charset"
 146  
      * This property has no default. If it exists, the output charset is always
 147  
      * set to its value
 148  
      *
 149  
      * @return the name of the override charset or null.
 150  
      */
 151  
     public static String getOverrideCharSet()
 152  
     {
 153  192
         return Turbine.getConfiguration()
 154  96
                 .getString(TurbineConstants.LOCALE_OVERRIDE_CHARSET_KEY);
 155  
     }
 156  
 }