KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > http > AcceptLanguage


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.tomcat.util.http;
19
20 import java.util.Enumeration JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * Util to process the "Accept-Language" header. Used by facade to implement
28  * getLocale() and by StaticInterceptor.
29  *
30  * Not optimized - it's very slow.
31  *
32  * @author James Duncan Davidson [duncan@eng.sun.com]
33  * @author James Todd [gonzo@eng.sun.com]
34  * @author Jason Hunter [jch@eng.sun.com]
35  * @author Harish Prabandham
36  * @author costin@eng.sun.com
37  */

38 public class AcceptLanguage {
39
40     public static Locale JavaDoc getLocale(String JavaDoc acceptLanguage) {
41     if( acceptLanguage == null ) return Locale.getDefault();
42
43         Hashtable JavaDoc languages = new Hashtable JavaDoc();
44         Vector JavaDoc quality=new Vector JavaDoc();
45         processAcceptLanguage(acceptLanguage, languages,quality);
46
47         if (languages.size() == 0) return Locale.getDefault();
48
49         Vector JavaDoc l = new Vector JavaDoc();
50         extractLocales( languages,quality, l);
51
52         return (Locale JavaDoc)l.elementAt(0);
53     }
54
55     public static Enumeration JavaDoc getLocales(String JavaDoc acceptLanguage) {
56         // Short circuit with an empty enumeration if null header
57
if (acceptLanguage == null) {
58             Vector JavaDoc v = new Vector JavaDoc();
59             v.addElement(Locale.getDefault());
60             return v.elements();
61         }
62     
63         Hashtable JavaDoc languages = new Hashtable JavaDoc();
64         Vector JavaDoc quality=new Vector JavaDoc();
65         processAcceptLanguage(acceptLanguage, languages , quality);
66
67         if (languages.size() == 0) {
68             Vector JavaDoc v = new Vector JavaDoc();
69             v.addElement(Locale.getDefault());
70             return v.elements();
71         }
72         Vector JavaDoc l = new Vector JavaDoc();
73         extractLocales( languages, quality , l);
74         return l.elements();
75     }
76
77     private static void processAcceptLanguage( String JavaDoc acceptLanguage,
78                           Hashtable JavaDoc languages, Vector JavaDoc q)
79     {
80         StringTokenizer JavaDoc languageTokenizer =
81             new StringTokenizer JavaDoc(acceptLanguage, ",");
82
83         while (languageTokenizer.hasMoreTokens()) {
84             String JavaDoc language = languageTokenizer.nextToken().trim();
85             int qValueIndex = language.indexOf(';');
86             int qIndex = language.indexOf('q');
87             int equalIndex = language.indexOf('=');
88             Double JavaDoc qValue = new Double JavaDoc(1);
89
90             if (qValueIndex > -1 &&
91                     qValueIndex < qIndex &&
92                     qIndex < equalIndex) {
93                 String JavaDoc qValueStr = language.substring(qValueIndex + 1);
94                 language = language.substring(0, qValueIndex);
95                 qValueStr = qValueStr.trim().toLowerCase();
96                 qValueIndex = qValueStr.indexOf('=');
97                 qValue = new Double JavaDoc(0);
98                 if (qValueStr.startsWith("q") &&
99                     qValueIndex > -1) {
100                     qValueStr = qValueStr.substring(qValueIndex + 1);
101                     try {
102                         qValue = new Double JavaDoc(qValueStr.trim());
103                     } catch (NumberFormatException JavaDoc nfe) {
104                     }
105                 }
106             }
107
108             // XXX
109
// may need to handle "*" at some point in time
110

111             if (! language.equals("*")) {
112                 String JavaDoc key = qValue.toString();
113                 Vector JavaDoc v;
114                 if (languages.containsKey(key)) {
115                     v = (Vector JavaDoc)languages.get(key) ;
116                 } else {
117                     v= new Vector JavaDoc();
118                     q.addElement(qValue);
119                 }
120                 v.addElement(language);
121                 languages.put(key, v);
122             }
123         }
124     }
125
126     private static void extractLocales(Hashtable JavaDoc languages, Vector JavaDoc q,Vector JavaDoc l)
127     {
128         // XXX We will need to order by q value Vector in the Future ?
129
Enumeration JavaDoc e = q.elements();
130         while (e.hasMoreElements()) {
131             Vector JavaDoc v =
132                 (Vector JavaDoc)languages.get(((Double JavaDoc)e.nextElement()).toString());
133             Enumeration JavaDoc le = v.elements();
134             while (le.hasMoreElements()) {
135                 String JavaDoc language = (String JavaDoc)le.nextElement();
136                 String JavaDoc 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 JavaDoc(language, country));
143             }
144         }
145     }
146
147
148 }
149
Popular Tags