1 87 88 package org.opencms.i18n; 89 90 import org.opencms.main.CmsIllegalArgumentException; 91 import org.opencms.main.CmsRuntimeException; 92 import org.opencms.main.OpenCms; 93 import org.opencms.util.CmsStringUtil; 94 95 import java.util.ArrayList ; 96 import java.util.Collections ; 97 import java.util.Iterator ; 98 import java.util.List ; 99 import java.util.Locale ; 100 import java.util.NoSuchElementException ; 101 102 import javax.servlet.http.HttpServletRequest ; 103 104 115 public class CmsAcceptLanguageHeaderParser implements Iterator { 116 117 120 class AcceptLanguage implements Comparable { 121 122 123 Locale m_locale; 124 125 126 Float m_quality = DEFAULT_QUALITY; 127 128 131 public final int compareTo(Object acceptLang) { 132 133 return m_quality.compareTo(((AcceptLanguage)acceptLang).m_quality); 134 } 135 } 136 137 138 public static final String ACCEPT_LANGUAGE = "Accept-Language"; 139 140 141 protected static final Float DEFAULT_QUALITY = new Float (1.0f); 142 143 144 private static final char LOCALE_SEPARATOR = ','; 145 146 147 private static final char QUALITY_SEPARATOR = ';'; 148 149 150 private List m_acceptLanguage = new ArrayList (3); 151 152 153 private List m_locales; 154 155 161 public CmsAcceptLanguageHeaderParser(HttpServletRequest req, Locale defaultLocale) { 162 163 this(req.getHeader(ACCEPT_LANGUAGE), defaultLocale); 164 } 165 166 172 public CmsAcceptLanguageHeaderParser(String header, Locale defaultLocale) { 173 174 if (header == null) { 177 m_locales = new ArrayList (); 178 m_locales.add(defaultLocale); 179 } else { 180 List tokens = CmsStringUtil.splitAsList(header, LOCALE_SEPARATOR, true); 181 Iterator it = tokens.iterator(); 182 while (it.hasNext()) { 183 AcceptLanguage acceptLang = new AcceptLanguage(); 184 String element = (String )it.next(); 185 int index; 186 187 index = element.indexOf(QUALITY_SEPARATOR); 189 if (index != -1) { 190 String q = element.substring(index); 191 element = element.substring(0, index); 192 index = q.indexOf('='); 193 if (index != -1) { 194 try { 195 acceptLang.m_quality = Float.valueOf(q.substring(index + 1)); 196 } catch (NumberFormatException useDefault) { 197 } 199 } 200 } 201 202 element = element.trim(); 203 204 index = element.indexOf('-'); 206 if (index == -1) { 207 acceptLang.m_locale = new Locale (element, ""); 209 } else { 210 acceptLang.m_locale = new Locale (element.substring(0, index), element.substring(index + 1)); 211 } 212 213 m_acceptLanguage.add(acceptLang); 214 } 215 216 Collections.sort(m_acceptLanguage, Collections.reverseOrder()); 218 219 m_locales = new ArrayList (m_acceptLanguage.size()); 221 Iterator i = m_acceptLanguage.iterator(); 222 while (i.hasNext()) { 223 AcceptLanguage lang = (AcceptLanguage)i.next(); 224 m_locales.add(lang.m_locale); 225 } 226 } 227 228 } 229 230 235 public static String createLanguageHeader() { 236 237 String header; 238 239 List defaultLocales = OpenCms.getLocaleManager().getDefaultLocales(); 241 Iterator i = defaultLocales.iterator(); 242 header = ""; 243 while (i.hasNext()) { 244 Locale loc = (Locale )i.next(); 245 header += loc.getLanguage() + ", "; 246 } 247 header = header.substring(0, header.length() - 2); 248 return header; 249 } 250 251 256 public List getAcceptedLocales() { 257 258 return m_locales; 259 } 260 261 264 public boolean hasNext() { 265 266 return !m_acceptLanguage.isEmpty(); 267 } 268 269 274 public Object next() { 275 276 if (m_acceptLanguage.isEmpty()) { 277 throw new NoSuchElementException (); 278 } 279 return ((AcceptLanguage)m_acceptLanguage.remove(0)).m_locale; 280 } 281 282 288 public final void remove() throws CmsIllegalArgumentException { 289 290 throw new CmsRuntimeException(org.opencms.db.Messages.get().container( 291 org.opencms.db.Messages.ERR_UNSUPPORTED_OPERATION_2, 292 getClass().getName(), 293 "remove()")); 294 } 295 } | Popular Tags |