Coverage report

  %line %branch
org.apache.jetspeed.localization.impl.SimplifiedLocalizationValveImpl
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.jetspeed.localization.impl;
 18  
 
 19  
 import java.util.Enumeration;
 20  
 import java.util.Locale;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 import org.apache.jetspeed.PortalReservedParameters;
 25  
 import org.apache.jetspeed.i18n.CurrentLocale;
 26  
 import org.apache.jetspeed.pipeline.PipelineException;
 27  
 import org.apache.jetspeed.pipeline.valve.AbstractValve;
 28  
 import org.apache.jetspeed.pipeline.valve.LocalizationValve;
 29  
 import org.apache.jetspeed.pipeline.valve.ValveContext;
 30  
 import org.apache.jetspeed.request.RequestContext;
 31  
 
 32  
 /**
 33  
  * LocalizationValveImpl
 34  
  * 
 35  
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
 36  
  * @version $Id: LocalizationValveImpl.java 378091 2006-02-15 21:12:28Z taylor $
 37  
  */
 38  
 public class SimplifiedLocalizationValveImpl extends AbstractValve implements LocalizationValve
 39  
 {
 40  0
     private static final Log log = LogFactory.getLog(LocalizationValveImpl.class);
 41  0
     private Locale defaultLocale = null;
 42  
     
 43  0
     public SimplifiedLocalizationValveImpl() {}
 44  
     
 45  
     public SimplifiedLocalizationValveImpl(String defaultLanguage)
 46  0
     {
 47  0
         String language = defaultLanguage != null ? defaultLanguage.trim() : "";
 48  0
         if (language.length()>0)
 49  
         {
 50  
             // Code taken from LocaleSelectorPorltet
 51  0
             String country = "";
 52  0
             String variant = "";
 53  0
             int countryIndex = language.indexOf('_');
 54  0
             if (countryIndex > -1)
 55  
             {
 56  0
                 country = language.substring(countryIndex + 1).trim();
 57  0
                 language = language.substring(0, countryIndex).trim();
 58  0
                 int vDash = country.indexOf("_");
 59  0
                 if (vDash > 0)
 60  
                 {
 61  0
                     String cTemp = country.substring(0, vDash);
 62  0
                     variant = country.substring(vDash + 1);
 63  0
                     country = cTemp;
 64  
                 }
 65  
             }
 66  
 
 67  0
             defaultLocale = new Locale(language, country, variant);
 68  0
             if ( defaultLocale.getLanguage().length() == 0 )
 69  
             {
 70  
                 // not a valid language
 71  0
                 defaultLocale = null;
 72  0
                 log.warn("Invalid or unrecognized default language: "+language);
 73  
             }
 74  
             else
 75  
             {
 76  0
                 log.info("Default language set: "+defaultLocale);
 77  
             }
 78  
                 
 79  
         }
 80  0
     }
 81  
     
 82  
     /*
 83  
      * (non-Javadoc)
 84  
      * 
 85  
      * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext,
 86  
      *      org.apache.jetspeed.pipeline.valve.ValveContext)
 87  
      */
 88  
     public void invoke( RequestContext request, ValveContext context ) throws PipelineException
 89  
     {
 90  0
         Locale locale = (Locale)request.getRequest().getSession().getAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
 91  
         
 92  0
         if ( locale == null && defaultLocale != class="keyword">null )
 93  
         {
 94  0
             locale = defaultLocale;
 95  
         }
 96  
 
 97  0
         if (locale == null)
 98  
         {
 99  0
             locale = request.getRequest().getLocale();
 100  
         }
 101  
         
 102  0
         if (locale == null)
 103  
         {
 104  0
             Enumeration preferedLocales = request.getRequest().getLocales();
 105  0
             while (preferedLocales.hasMoreElements() && locale == null)
 106  
             {
 107  0
                 locale = (Locale) preferedLocales.nextElement();
 108  
             }
 109  
         }
 110  
 
 111  0
         if (locale == null)
 112  
         {
 113  0
             locale = Locale.getDefault();
 114  
         }
 115  
 
 116  0
         request.setLocale(locale);
 117  0
         request.getRequest().setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale);
 118  0
         request.getRequest().getSession().setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale);
 119  0
         CurrentLocale.set(locale);
 120  
        
 121  
         // Pass control to the next Valve in the Pipeline
 122  0
         context.invokeNext(request);
 123  
 
 124  0
     }
 125  
 
 126  
     public String toString()
 127  
     {
 128  0
         return "LocalizationValve";
 129  
     }
 130  
 
 131  
 }

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