1 17 18 package org.apache.tomcat.util.http; 19 20 import java.util.Enumeration ; 21 import java.util.Hashtable ; 22 import java.util.Locale ; 23 import java.util.StringTokenizer ; 24 import java.util.Vector ; 25 26 38 public class AcceptLanguage { 39 40 public static Locale getLocale(String acceptLanguage) { 41 if( acceptLanguage == null ) return Locale.getDefault(); 42 43 Hashtable languages = new Hashtable (); 44 Vector quality=new Vector (); 45 processAcceptLanguage(acceptLanguage, languages,quality); 46 47 if (languages.size() == 0) return Locale.getDefault(); 48 49 Vector l = new Vector (); 50 extractLocales( languages,quality, l); 51 52 return (Locale )l.elementAt(0); 53 } 54 55 public static Enumeration getLocales(String acceptLanguage) { 56 if (acceptLanguage == null) { 58 Vector v = new Vector (); 59 v.addElement(Locale.getDefault()); 60 return v.elements(); 61 } 62 63 Hashtable languages = new Hashtable (); 64 Vector quality=new Vector (); 65 processAcceptLanguage(acceptLanguage, languages , quality); 66 67 if (languages.size() == 0) { 68 Vector v = new Vector (); 69 v.addElement(Locale.getDefault()); 70 return v.elements(); 71 } 72 Vector l = new Vector (); 73 extractLocales( languages, quality , l); 74 return l.elements(); 75 } 76 77 private static void processAcceptLanguage( String acceptLanguage, 78 Hashtable languages, Vector q) 79 { 80 StringTokenizer languageTokenizer = 81 new StringTokenizer (acceptLanguage, ","); 82 83 while (languageTokenizer.hasMoreTokens()) { 84 String language = languageTokenizer.nextToken().trim(); 85 int qValueIndex = language.indexOf(';'); 86 int qIndex = language.indexOf('q'); 87 int equalIndex = language.indexOf('='); 88 Double qValue = new Double (1); 89 90 if (qValueIndex > -1 && 91 qValueIndex < qIndex && 92 qIndex < equalIndex) { 93 String qValueStr = language.substring(qValueIndex + 1); 94 language = language.substring(0, qValueIndex); 95 qValueStr = qValueStr.trim().toLowerCase(); 96 qValueIndex = qValueStr.indexOf('='); 97 qValue = new Double (0); 98 if (qValueStr.startsWith("q") && 99 qValueIndex > -1) { 100 qValueStr = qValueStr.substring(qValueIndex + 1); 101 try { 102 qValue = new Double (qValueStr.trim()); 103 } catch (NumberFormatException nfe) { 104 } 105 } 106 } 107 108 111 if (! language.equals("*")) { 112 String key = qValue.toString(); 113 Vector v; 114 if (languages.containsKey(key)) { 115 v = (Vector )languages.get(key) ; 116 } else { 117 v= new Vector (); 118 q.addElement(qValue); 119 } 120 v.addElement(language); 121 languages.put(key, v); 122 } 123 } 124 } 125 126 private static void extractLocales(Hashtable languages, Vector q,Vector l) 127 { 128 Enumeration e = q.elements(); 130 while (e.hasMoreElements()) { 131 Vector v = 132 (Vector )languages.get(((Double )e.nextElement()).toString()); 133 Enumeration le = v.elements(); 134 while (le.hasMoreElements()) { 135 String language = (String )le.nextElement(); 136 String country = ""; 137 int countryIndex = language.indexOf("-"); 138 if (countryIndex > -1) { 139 country = language.substring(countryIndex + 1).trim(); 140 language = language.substring(0, countryIndex).trim(); 141 } 142 l.addElement(new Locale (language, country)); 143 } 144 } 145 } 146 147 148 } 149 | Popular Tags |