1 package org.apache.turbine.services.localization;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.ArrayList;
58 import java.util.Collections;
59 import java.util.Iterator;
60 import java.util.Locale;
61 import java.util.NoSuchElementException;
62 import java.util.StringTokenizer;
63
64 /***
65 * Parses the HTTP <code>Accept-Language</code> header as per section
66 * 14.4 of RFC 2068 (HTTP 1.1 header field definitions).
67 *
68 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
69 * @version $Id: LocaleTokenizer.java,v 1.1 2001/10/13 23:15:24 dlr Exp $
70 */
71 public class LocaleTokenizer
72 implements Iterator
73 {
74 /***
75 * Separates elements of the <code>Accept-Language</code> HTTP
76 * header.
77 */
78 private static final String LOCALE_SEPARATOR = ",";
79
80 /***
81 * Separates locale from quality within elements.
82 */
83 private static final char QUALITY_SEPARATOR = ';';
84
85 /***
86 * The default quality value for an <code>AcceptLanguage</code>
87 * object.
88 */
89 private static final Float DEFAULT_QUALITY = new Float(1.0f);
90
91 /***
92 * The parsed locales.
93 */
94 private ArrayList locales = new ArrayList(3);
95
96 /***
97 * Parses the <code>Accept-Language</code> header.
98 *
99 * @param header The <code>Accept-Language</code> header
100 * (i.e. <code>en, es;q=0.8, zh-TW;q=0.1</code>).
101 */
102 public LocaleTokenizer(String header)
103 {
104 StringTokenizer tok = new StringTokenizer(header, LOCALE_SEPARATOR);
105 while (tok.hasMoreTokens())
106 {
107 AcceptLanguage acceptLang = new AcceptLanguage();
108 String element = tok.nextToken().trim();
109 int index;
110
111 // Record and cut off any quality value that comes after a
112 // semi-colon.
113 if ( (index = element.indexOf(QUALITY_SEPARATOR)) != -1 )
114 {
115 String q = element.substring(index);
116 element = element.substring(0, index);
117 if ( (index = q.indexOf('=')) != -1 )
118 {
119 try
120 {
121 acceptLang.quality =
122 Float.valueOf(q.substring(index + 1));
123 }
124 catch (NumberFormatException useDefault)
125 {
126 }
127 }
128 }
129
130 element = element.trim();
131
132 // Create a Locale from the language. A dash may separate the
133 // language from the country.
134 if ( (index = element.indexOf('-')) == -1 )
135 {
136 // No dash means no country.
137 acceptLang.locale = new Locale(element, "");
138 }
139 else
140 {
141 acceptLang.locale = new Locale(element.substring(0, index),
142 element.substring(index + 1));
143 }
144
145 locales.add(acceptLang);
146 }
147
148 // Sort by quality in descending order.
149 Collections.sort(locales, Collections.reverseOrder());
150 }
151
152 /***
153 * @return Whether there are more locales.
154 */
155 public boolean hasNext()
156 {
157 return !locales.isEmpty();
158 }
159
160 /***
161 * Creates a <code>Locale</code> from the next element of the
162 * <code>Accept-Language</code> header.
163 *
164 * @return The next highest-rated <code>Locale</code>.
165 * @throws NoSuchElementException No more locales.
166 */
167 public Object next()
168 {
169 if (locales.isEmpty())
170 {
171 throw new NoSuchElementException();
172 }
173 return ((AcceptLanguage) locales.remove(0)).locale;
174 }
175
176 /***
177 * Not implemented.
178 */
179 public final void remove()
180 {
181 throw new UnsupportedOperationException(getClass().getName() +
182 " does not support remove()");
183 }
184
185 /***
186 * Struct representing an element of the HTTP
187 * <code>Accept-Language</code> header.
188 */
189 private class AcceptLanguage implements Comparable
190 {
191 /***
192 * The language and country.
193 */
194 Locale locale;
195
196 /***
197 * The quality of our locale (as values approach
198 * <code>1.0</code>, they indicate increased user preference).
199 */
200 Float quality = DEFAULT_QUALITY;
201
202 public final int compareTo(Object acceptLang)
203 {
204 return quality.compareTo( ((AcceptLanguage) acceptLang).quality );
205 }
206 }
207 }
This page was automatically generated by Maven