Coverage Report - org.apache.tapestry.web.LocalizedWebContextResourceFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalizedWebContextResourceFinder
0%
0/18
0%
0/8
2.333
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.web;
 16  
 
 17  
 import org.apache.hivemind.util.Defense;
 18  
 import org.apache.hivemind.util.LocalizedNameGenerator;
 19  
 import org.apache.hivemind.util.LocalizedResource;
 20  
 
 21  
 import java.util.Locale;
 22  
 
 23  
 /**
 24  
  * Finds localized resources within a {@link org.apache.tapestry.web.WebContext}..
 25  
  * 
 26  
  * @author Howard Lewis Ship
 27  
  * @since 4.0
 28  
  */
 29  
 
 30  
 public class LocalizedWebContextResourceFinder
 31  
 {
 32  
 
 33  
     private WebContext _context;
 34  
 
 35  
     public LocalizedWebContextResourceFinder(WebContext context)
 36  0
     {
 37  0
         Defense.notNull(context, "context");
 38  
 
 39  0
         _context = context;
 40  0
     }
 41  
 
 42  
     /**
 43  
      * Resolves the resource, returning a path representing the closest match
 44  
      * (with respect to the provided locale). Returns null if no match.
 45  
      * <p>
 46  
      * The provided path is split into a base path and a suffix (at the last
 47  
      * period character). The locale will provide different suffixes to the base
 48  
      * path and the first match is returned.
 49  
      */
 50  
 
 51  
     public LocalizedResource resolve(String contextPath, Locale locale)
 52  
     {
 53  0
         int dotx = contextPath.lastIndexOf('.');
 54  
         String basePath;
 55  
         String suffix;
 56  0
         if (dotx >= 0)
 57  
         {
 58  0
             basePath = contextPath.substring(0, dotx);
 59  0
             suffix = contextPath.substring(dotx);
 60  
         }
 61  
         else
 62  
         {
 63  
             // Resource without extension
 64  0
             basePath = contextPath;
 65  0
             suffix = "";
 66  
         }
 67  
 
 68  0
         LocalizedNameGenerator generator = new LocalizedNameGenerator(basePath,
 69  
                 locale, suffix);
 70  
 
 71  0
         while(generator.more())
 72  
         {
 73  0
             String candidatePath = generator.next();
 74  
 
 75  0
             if (isExistingResource(candidatePath))
 76  0
                 return new LocalizedResource(candidatePath, generator.getCurrentLocale());
 77  0
         }
 78  
 
 79  0
         return null;
 80  
     }
 81  
 
 82  
     private boolean isExistingResource(String path)
 83  
     {
 84  0
         return _context.getResource(path) != null;
 85  
     }
 86  
 }