KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > Collation


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import java.text.Collator JavaDoc;
35 import java.util.Locale JavaDoc;
36
37 import org.hsqldb.lib.HashMap;
38 import org.hsqldb.lib.StringUtil;
39 import org.hsqldb.lib.java.JavaSystem;
40
41 /**
42  * Implementation of collation support for all CHAR and VARCHAR data.
43  *
44  * @author frank.schoenheit@sun.com
45  * @author fredt@users
46  * @version 1.8.0
47  * @since 1.8.0
48  */

49 public class Collation {
50
51     static final HashMap nameToJavaName = new HashMap(101);
52
53     static {
54         nameToJavaName.put("Afrikaans", "af-ZA");
55         nameToJavaName.put("Amharic", "am-ET");
56         nameToJavaName.put("Arabic", "ar");
57         nameToJavaName.put("Assamese", "as-IN");
58         nameToJavaName.put("Azerbaijani_Latin", "az-AZ");
59         nameToJavaName.put("Azerbaijani_Cyrillic", "az-cyrillic");
60         nameToJavaName.put("Belarusian", "be-BY");
61         nameToJavaName.put("Bulgarian", "bg-BG");
62         nameToJavaName.put("Bengali", "bn-IN");
63         nameToJavaName.put("Tibetan", "bo-CN");
64         nameToJavaName.put("Bosnian", "bs-BA");
65         nameToJavaName.put("Catalan", "ca-ES");
66         nameToJavaName.put("Czech", "cs-CZ");
67         nameToJavaName.put("Welsh", "cy-GB");
68         nameToJavaName.put("Danish", "da-DK");
69         nameToJavaName.put("German", "de-DE");
70         nameToJavaName.put("Greek", "el-GR");
71         nameToJavaName.put("Latin1_General", "en-US");
72         nameToJavaName.put("Spanish", "es-ES");
73         nameToJavaName.put("Estonian", "et-EE");
74         nameToJavaName.put("Basque", "eu");
75         nameToJavaName.put("Finnish", "fi-FI");
76         nameToJavaName.put("French", "fr-FR");
77         nameToJavaName.put("Guarani", "gn-PY");
78         nameToJavaName.put("Gujarati", "gu-IN");
79         nameToJavaName.put("Hausa", "ha-NG");
80         nameToJavaName.put("Hebrew", "he-IL");
81         nameToJavaName.put("Hindi", "hi-IN");
82         nameToJavaName.put("Croatian", "hr-HR");
83         nameToJavaName.put("Hungarian", "hu-HU");
84         nameToJavaName.put("Armenian", "hy-AM");
85         nameToJavaName.put("Indonesian", "id-ID");
86         nameToJavaName.put("Igbo", "ig-NG");
87         nameToJavaName.put("Icelandic", "is-IS");
88         nameToJavaName.put("Italian", "it-IT");
89         nameToJavaName.put("Inuktitut", "iu-CA");
90         nameToJavaName.put("Japanese", "ja-JP");
91         nameToJavaName.put("Georgian", "ka-GE");
92         nameToJavaName.put("Kazakh", "kk-KZ");
93         nameToJavaName.put("Khmer", "km-KH");
94         nameToJavaName.put("Kannada", "kn-IN");
95         nameToJavaName.put("Korean", "ko-KR");
96         nameToJavaName.put("Konkani", "kok-IN");
97         nameToJavaName.put("Kashmiri", "ks");
98         nameToJavaName.put("Kirghiz", "ky-KG");
99         nameToJavaName.put("Lao", "lo-LA");
100         nameToJavaName.put("Lithuanian", "lt-LT");
101         nameToJavaName.put("Latvian", "lv-LV");
102         nameToJavaName.put("Maori", "mi-NZ");
103         nameToJavaName.put("Macedonian", "mk-MK");
104         nameToJavaName.put("Malayalam", "ml-IN");
105         nameToJavaName.put("Mongolian", "mn-MN");
106         nameToJavaName.put("Manipuri", "mni-IN");
107         nameToJavaName.put("Marathi", "mr-IN");
108         nameToJavaName.put("Malay", "ms-MY");
109         nameToJavaName.put("Maltese", "mt-MT");
110         nameToJavaName.put("Burmese", "my-MM");
111         nameToJavaName.put("Danish_Norwegian", "nb-NO");
112         nameToJavaName.put("Nepali", "ne-NP");
113         nameToJavaName.put("Dutch", "nl-NL");
114         nameToJavaName.put("Norwegian", "nn-NO");
115         nameToJavaName.put("Oriya", "or-IN");
116         nameToJavaName.put("Punjabi", "pa-IN");
117         nameToJavaName.put("Polish", "pl-PL");
118         nameToJavaName.put("Pashto", "ps-AF");
119         nameToJavaName.put("Portuguese", "pt-PT");
120         nameToJavaName.put("Romanian", "ro-RO");
121         nameToJavaName.put("Russian", "ru-RU");
122         nameToJavaName.put("Sanskrit", "sa-IN");
123         nameToJavaName.put("Sindhi", "sd-IN");
124         nameToJavaName.put("Slovak", "sk-SK");
125         nameToJavaName.put("Slovenian", "sl-SI");
126         nameToJavaName.put("Somali", "so-SO");
127         nameToJavaName.put("Albanian", "sq-AL");
128         nameToJavaName.put("Serbian_Cyrillic", "sr-YU");
129         nameToJavaName.put("Serbian_Latin", "sh-BA");
130         nameToJavaName.put("Swedish", "sv-SE");
131         nameToJavaName.put("Swahili", "sw-KE");
132         nameToJavaName.put("Tamil", "ta-IN");
133         nameToJavaName.put("Telugu", "te-IN");
134         nameToJavaName.put("Tajik", "tg-TJ");
135         nameToJavaName.put("Thai", "th-TH");
136         nameToJavaName.put("Turkmen", "tk-TM");
137         nameToJavaName.put("Tswana", "tn-BW");
138         nameToJavaName.put("Turkish", "tr-TR");
139         nameToJavaName.put("Tatar", "tt-RU");
140         nameToJavaName.put("Ukrainian", "uk-UA");
141         nameToJavaName.put("Urdu", "ur-PK");
142         nameToJavaName.put("Uzbek_Latin", "uz-UZ");
143         nameToJavaName.put("Venda", "ven-ZA");
144         nameToJavaName.put("Vietnamese", "vi-VN");
145         nameToJavaName.put("Yoruba", "yo-NG");
146         nameToJavaName.put("Chinese", "zh-CN");
147         nameToJavaName.put("Zulu", "zu-ZA");
148     }
149
150     String JavaDoc name;
151     Collator JavaDoc collator;
152     Locale JavaDoc locale;
153
154     public Collation() {
155         locale = Locale.ENGLISH;
156     }
157
158     public org.hsqldb.lib.Iterator getCollationsIterator() {
159         return nameToJavaName.keySet().iterator();
160     }
161
162     public org.hsqldb.lib.Iterator getLocalesIterator() {
163         return nameToJavaName.values().iterator();
164     }
165
166     public void setCollationAsLocale() {
167
168         Locale JavaDoc locale = Locale.getDefault();
169         String JavaDoc language = locale.getDisplayLanguage(Locale.ENGLISH);
170
171         try {
172             setCollation(language);
173         } catch (HsqlException e) {}
174     }
175
176     void setCollation(String JavaDoc name) throws HsqlException {
177
178         String JavaDoc jname = (String JavaDoc) Collation.nameToJavaName.get(name);
179
180         if (jname == null) {
181             throw Trace.error(Trace.INVALID_COLLATION_NAME_NO_SUBCLASS);
182         }
183
184         this.name = name;
185
186         String JavaDoc[] parts = StringUtil.split(jname, "-");
187         String JavaDoc language = parts[0];
188         String JavaDoc country = parts.length == 2 ? parts[1]
189                                               : "";
190
191         locale = new Locale JavaDoc(language, country);
192         collator = Collator.getInstance(locale);
193     }
194
195     /**
196      * returns -1, 0 or +1
197      */

198     int compare(String JavaDoc a, String JavaDoc b) {
199
200         int i;
201
202         if (collator == null) {
203             i = a.compareTo(b);
204         } else {
205             i = collator.compare(a, b);
206         }
207
208         return (i == 0) ? 0
209                         : (i < 0 ? -1
210                                  : 1);
211     }
212
213     int compareIgnoreCase(String JavaDoc a, String JavaDoc b) {
214
215         int i;
216
217         if (collator == null) {
218             i = JavaSystem.CompareIngnoreCase(a, b);
219         } else {
220             i = collator.compare(toUpperCase(a), toUpperCase(b));
221         }
222
223         return (i == 0) ? 0
224                         : (i < 0 ? -1
225                                  : 1);
226     }
227
228     String JavaDoc toUpperCase(String JavaDoc s) {
229         return s.toUpperCase(locale);
230     }
231 }
232
Popular Tags