KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > app > servlet > AcceptLanguage


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17
18 package org.alfresco.web.app.servlet;
19
20 import java.util.Locale JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 /**
24  * Accept Language Class
25  *
26  * <p>Holds the details of an accepted language from a HTTP Accept-Language header
27  *
28  * @author gkspencer
29  */

30 class AcceptLanguage
31 {
32     // Language name
33

34     private String JavaDoc m_language;
35     
36     // Quality
37

38     private float m_quality = 1.0f;
39     
40     /**
41      * Class constructor
42      *
43      * @param lang String
44      * @param quality float
45      */

46     public AcceptLanguage(String JavaDoc lang, float quality)
47     {
48         // Convert the language to Java format
49

50         m_language = lang.replace('-', '_');
51         m_quality = quality;
52     }
53     
54     /**
55      * Return the language
56      *
57      * @return String
58      */

59     public final String JavaDoc getLanguage()
60     {
61         return m_language;
62     }
63     
64     /**
65      * Return the quality
66      *
67      * @return float
68      */

69     public final float getQuality()
70     {
71         return m_quality;
72     }
73     
74     /**
75      * Create a locale for this language
76      *
77      * @return Locale
78      */

79     public final Locale JavaDoc createLocale()
80     {
81         return createLocale(getLanguage());
82     }
83     
84     /**
85      * Create a locale for this language
86      *
87      * @param locName String
88      * @return Locale
89      */

90     public final static Locale JavaDoc createLocale(String JavaDoc locName)
91     {
92         Locale JavaDoc locale = null;
93         
94         StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(locName, "_");
95         int tokens = t.countTokens();
96         if (tokens == 1)
97         {
98            locale = new Locale JavaDoc(locName);
99         }
100         else if (tokens == 2)
101         {
102            locale = new Locale JavaDoc(t.nextToken(), t.nextToken());
103         }
104         else if (tokens == 3)
105         {
106            locale = new Locale JavaDoc(t.nextToken(), t.nextToken(), t.nextToken());
107         }
108         
109         return locale;
110     }
111     
112     /**
113      * Return the accept language as a string
114      *
115      * @return String
116      */

117     public String JavaDoc toString()
118     {
119         StringBuilder JavaDoc str = new StringBuilder JavaDoc();
120         
121         str.append("[");
122         str.append(getLanguage());
123         str.append(",");
124         str.append(getQuality());
125         str.append("]");
126         
127         return str.toString();
128     }
129 }
130
Popular Tags