Clover coverage report - Code Coverage for hivemind release 1.1-beta-2
Coverage timestamp: Tue Jun 28 2005 10:28:23 EDT
file stats: LOC: 79   Methods: 3
NCLOC: 36   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LocalizedContextResourceFinder.java 100% 100% 100% 100%
coverage
 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.hivemind.util;
 16   
 17    import java.net.MalformedURLException;
 18    import java.util.Locale;
 19   
 20    import javax.servlet.ServletContext;
 21   
 22    /**
 23    * Finds localized resources within the web application context.
 24    * <p>
 25    * Originally part of Tapestry 3.0.
 26    *
 27    * @see javax.servlet.ServletContext
 28    * @author Howard Lewis Ship
 29    * @since 1.1
 30    */
 31   
 32    public class LocalizedContextResourceFinder
 33    {
 34    private ServletContext _context;
 35   
 36  6 public LocalizedContextResourceFinder(ServletContext context)
 37    {
 38  6 _context = context;
 39    }
 40   
 41    /**
 42    * Resolves the resource, returning a path representing the closest match (with respect to the
 43    * provided locale). Returns null if no match.
 44    * <p>
 45    * The provided path is split into a base path and a suffix (at the last period character). The
 46    * locale will provide different suffixes to the base path and the first match is returned.
 47    */
 48   
 49  6 public LocalizedResource resolve(String contextPath, Locale locale)
 50    {
 51  6 int dotx = contextPath.lastIndexOf('.');
 52  6 String basePath = contextPath.substring(0, dotx);
 53  6 String suffix = contextPath.substring(dotx);
 54   
 55  6 LocalizedNameGenerator generator = new LocalizedNameGenerator(basePath, locale, suffix);
 56   
 57  6 while (generator.more())
 58    {
 59  11 String candidatePath = generator.next();
 60   
 61  11 if (isExistingResource(candidatePath))
 62  3 return new LocalizedResource(candidatePath, generator.getCurrentLocale());
 63    }
 64   
 65  3 return null;
 66    }
 67   
 68  11 private boolean isExistingResource(String path)
 69    {
 70  11 try
 71    {
 72  11 return _context.getResource(path) != null;
 73    }
 74    catch (MalformedURLException ex)
 75    {
 76  2 return false;
 77    }
 78    }
 79    }