1 61 62 package org.jaxen.function.ext; 63 64 import java.util.List ; 65 import java.util.Locale ; 66 import java.util.StringTokenizer ; 67 68 import org.jaxen.Function; 69 import org.jaxen.Navigator; 70 import org.jaxen.function.StringFunction; 71 72 81 public abstract class LocaleFunctionSupport implements Function 82 { 83 84 96 protected Locale getLocale(Object value, Navigator navigator) 97 { 98 if (value instanceof Locale ) 99 { 100 return (Locale ) value; 101 } 102 else if(value instanceof List ) 103 { 104 List list = (List ) value; 105 if ( ! list.isEmpty() ) 106 { 107 return getLocale( list.get(0), navigator ); 108 } 109 } 110 else { 111 String text = StringFunction.evaluate( value, navigator ); 112 if (text != null && text.length() > 0) 113 { 114 return findLocale( text ); 115 } 116 } 117 return null; 118 } 119 120 129 protected Locale findLocale(String localeText) { 130 StringTokenizer tokens = new StringTokenizer ( localeText, "-" ); 131 if (tokens.hasMoreTokens()) 132 { 133 String language = tokens.nextToken(); 134 if (! tokens.hasMoreTokens()) 135 { 136 return findLocaleForLanguage(language); 137 } 138 else 139 { 140 String country = tokens.nextToken(); 141 if (! tokens.hasMoreTokens()) 142 { 143 return new Locale (language, country); 144 } 145 else 146 { 147 String variant = tokens.nextToken(); 148 return new Locale (language, country, variant); 149 } 150 } 151 } 152 return null; 153 } 154 155 163 protected Locale findLocaleForLanguage(String language) { 164 Locale [] locales = Locale.getAvailableLocales(); 165 for ( int i = 0, size = locales.length; i < size; i++ ) 166 { 167 Locale locale = locales[i]; 168 if ( language.equals( locale.getLanguage() ) ) 169 { 170 String country = locale.getCountry(); 171 if ( country == null || country.length() == 0 ) 172 { 173 String variant = locale.getVariant(); 174 if ( variant == null || variant.length() == 0 ) 175 { 176 return locale; 177 } 178 } 179 } 180 } 181 return null; 182 } 183 } 184 | Popular Tags |