Coverage Report - de.smartics.properties.spi.core.context.LocaleFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
LocaleFinder
0%
0/35
0%
0/10
2,4
LocaleFinder$1
0%
0/2
N/A
N/A
2,4
 
 1  
 /*
 2  
  * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package de.smartics.properties.spi.core.context;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collections;
 20  
 import java.util.Comparator;
 21  
 import java.util.HashSet;
 22  
 import java.util.List;
 23  
 import java.util.Locale;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.commons.io.FilenameUtils;
 27  
 import org.apache.commons.lang.LocaleUtils;
 28  
 
 29  
 import de.smartics.properties.api.core.domain.ConfigException;
 30  
 import de.smartics.properties.api.core.domain.PropertiesContext;
 31  
 import de.smartics.util.lang.ClassPathContext;
 32  
 import de.smartics.util.util.GenericServiceFactory;
 33  
 
 34  
 /**
 35  
  * Finds all supported locales.
 36  
  */
 37  
 final class LocaleFinder
 38  
 {
 39  
   // ********************************* Fields *********************************
 40  
 
 41  
   // --- constants ------------------------------------------------------------
 42  
 
 43  
   /**
 44  
    * Helper to get listing of a folder on the class path.
 45  
    */
 46  
   private static final ClassPathListing CLASS_PATH_LISTING;
 47  
 
 48  
   // --- members --------------------------------------------------------------
 49  
 
 50  
   /**
 51  
    * Locale comparator.
 52  
    */
 53  0
   private static final Comparator<Locale> COMPARATOR = new Comparator<Locale>()
 54  0
   {
 55  
     @Override
 56  
     public int compare(final Locale o1, final Locale o2)
 57  
     {
 58  0
       return o1.toString().compareTo(o2.toString());
 59  
     }
 60  
   };
 61  
 
 62  
   // ****************************** Initializer *******************************
 63  
 
 64  
   static
 65  
   {
 66  0
     ClassPathListing service = null;
 67  
     try
 68  
     {
 69  0
       service =
 70  
           new GenericServiceFactory<ClassPathListing>(ClassPathListing.class)
 71  
               .create();
 72  
     }
 73  0
     catch (final IllegalStateException e)
 74  
     {
 75  0
       service = new JarAndFileClassPathListing();
 76  0
     }
 77  
 
 78  0
     CLASS_PATH_LISTING = service;
 79  0
   }
 80  
 
 81  
   // ****************************** Constructors ******************************
 82  
 
 83  
   /**
 84  
    * Default constructor.
 85  
    */
 86  
   LocaleFinder()
 87  0
   {
 88  0
   }
 89  
 
 90  
   // ****************************** Inner Classes *****************************
 91  
 
 92  
   // ********************************* Methods ********************************
 93  
 
 94  
   // --- init -----------------------------------------------------------------
 95  
 
 96  
   // --- get&set --------------------------------------------------------------
 97  
 
 98  
   // --- business -------------------------------------------------------------
 99  
 
 100  
   List<Locale> find(final String systemId, final ClassPathContext context)
 101  
     throws ConfigException
 102  
   {
 103  0
     final Set<Locale> locales = new HashSet<Locale>();
 104  0
     locales.add(new Locale(""));
 105  
 
 106  0
     final List<String> listing =
 107  
         CLASS_PATH_LISTING.list(context,
 108  
             PropertiesContext.META_INF_PROPERTY_SET_REPORT);
 109  
 
 110  0
     for (final String path : listing)
 111  
     {
 112  0
       final Locale locale = calcLocale(path);
 113  0
       if (locale != null)
 114  
       {
 115  0
         locales.add(locale);
 116  
       }
 117  0
     }
 118  
 
 119  0
     final List<Locale> list = new ArrayList<Locale>(locales);
 120  0
     Collections.sort(list, COMPARATOR);
 121  0
     return list;
 122  
   }
 123  
 
 124  
   Locale calcLocale(final String urlString)
 125  
   {
 126  0
     final String fileName = FilenameUtils.getBaseName(urlString);
 127  0
     final int urlStringLength = urlString.length();
 128  0
     int separatorIndex = fileName.indexOf('_');
 129  0
     Locale locale = null;
 130  
 
 131  0
     while (separatorIndex != -1)
 132  
     {
 133  0
       final String localeString = fileName.substring(separatorIndex + 1);
 134  0
       locale = constructLocale(localeString);
 135  0
       separatorIndex =
 136  
           locale != null || separatorIndex + 1 >= urlStringLength ? -1
 137  
               : fileName.indexOf('_', separatorIndex + 1);
 138  0
     }
 139  0
     return locale;
 140  
   }
 141  
 
 142  
   private Locale constructLocale(final String localeString)
 143  
   {
 144  
     try
 145  
     {
 146  0
       final Locale locale = LocaleUtils.toLocale(localeString);
 147  0
       return locale;
 148  
     }
 149  0
     catch (final IllegalArgumentException e)
 150  
     {
 151  0
       return null;
 152  
     }
 153  
   }
 154  
 
 155  
   // --- object basics --------------------------------------------------------
 156  
 
 157  
 }