001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb;
032:
033: import java.text.Collator;
034: import java.util.Locale;
035:
036: import org.hsqldb.lib.HashMap;
037: import org.hsqldb.lib.StringUtil;
038: import org.hsqldb.lib.java.JavaSystem;
039:
040: /**
041: * Implementation of collation support for all CHAR and VARCHAR data.
042: *
043: * @author frank.schoenheit@sun.com
044: * @author fredt@users
045: * @version 1.8.0
046: * @since 1.8.0
047: */
048: public class Collation {
049:
050: static final HashMap nameToJavaName = new HashMap(101);
051:
052: static {
053: nameToJavaName.put("Afrikaans", "af-ZA");
054: nameToJavaName.put("Amharic", "am-ET");
055: nameToJavaName.put("Arabic", "ar");
056: nameToJavaName.put("Assamese", "as-IN");
057: nameToJavaName.put("Azerbaijani_Latin", "az-AZ");
058: nameToJavaName.put("Azerbaijani_Cyrillic", "az-cyrillic");
059: nameToJavaName.put("Belarusian", "be-BY");
060: nameToJavaName.put("Bulgarian", "bg-BG");
061: nameToJavaName.put("Bengali", "bn-IN");
062: nameToJavaName.put("Tibetan", "bo-CN");
063: nameToJavaName.put("Bosnian", "bs-BA");
064: nameToJavaName.put("Catalan", "ca-ES");
065: nameToJavaName.put("Czech", "cs-CZ");
066: nameToJavaName.put("Welsh", "cy-GB");
067: nameToJavaName.put("Danish", "da-DK");
068: nameToJavaName.put("German", "de-DE");
069: nameToJavaName.put("Greek", "el-GR");
070: nameToJavaName.put("Latin1_General", "en-US");
071: nameToJavaName.put("Spanish", "es-ES");
072: nameToJavaName.put("Estonian", "et-EE");
073: nameToJavaName.put("Basque", "eu");
074: nameToJavaName.put("Finnish", "fi-FI");
075: nameToJavaName.put("French", "fr-FR");
076: nameToJavaName.put("Guarani", "gn-PY");
077: nameToJavaName.put("Gujarati", "gu-IN");
078: nameToJavaName.put("Hausa", "ha-NG");
079: nameToJavaName.put("Hebrew", "he-IL");
080: nameToJavaName.put("Hindi", "hi-IN");
081: nameToJavaName.put("Croatian", "hr-HR");
082: nameToJavaName.put("Hungarian", "hu-HU");
083: nameToJavaName.put("Armenian", "hy-AM");
084: nameToJavaName.put("Indonesian", "id-ID");
085: nameToJavaName.put("Igbo", "ig-NG");
086: nameToJavaName.put("Icelandic", "is-IS");
087: nameToJavaName.put("Italian", "it-IT");
088: nameToJavaName.put("Inuktitut", "iu-CA");
089: nameToJavaName.put("Japanese", "ja-JP");
090: nameToJavaName.put("Georgian", "ka-GE");
091: nameToJavaName.put("Kazakh", "kk-KZ");
092: nameToJavaName.put("Khmer", "km-KH");
093: nameToJavaName.put("Kannada", "kn-IN");
094: nameToJavaName.put("Korean", "ko-KR");
095: nameToJavaName.put("Konkani", "kok-IN");
096: nameToJavaName.put("Kashmiri", "ks");
097: nameToJavaName.put("Kirghiz", "ky-KG");
098: nameToJavaName.put("Lao", "lo-LA");
099: nameToJavaName.put("Lithuanian", "lt-LT");
100: nameToJavaName.put("Latvian", "lv-LV");
101: nameToJavaName.put("Maori", "mi-NZ");
102: nameToJavaName.put("Macedonian", "mk-MK");
103: nameToJavaName.put("Malayalam", "ml-IN");
104: nameToJavaName.put("Mongolian", "mn-MN");
105: nameToJavaName.put("Manipuri", "mni-IN");
106: nameToJavaName.put("Marathi", "mr-IN");
107: nameToJavaName.put("Malay", "ms-MY");
108: nameToJavaName.put("Maltese", "mt-MT");
109: nameToJavaName.put("Burmese", "my-MM");
110: nameToJavaName.put("Danish_Norwegian", "nb-NO");
111: nameToJavaName.put("Nepali", "ne-NP");
112: nameToJavaName.put("Dutch", "nl-NL");
113: nameToJavaName.put("Norwegian", "nn-NO");
114: nameToJavaName.put("Oriya", "or-IN");
115: nameToJavaName.put("Punjabi", "pa-IN");
116: nameToJavaName.put("Polish", "pl-PL");
117: nameToJavaName.put("Pashto", "ps-AF");
118: nameToJavaName.put("Portuguese", "pt-PT");
119: nameToJavaName.put("Romanian", "ro-RO");
120: nameToJavaName.put("Russian", "ru-RU");
121: nameToJavaName.put("Sanskrit", "sa-IN");
122: nameToJavaName.put("Sindhi", "sd-IN");
123: nameToJavaName.put("Slovak", "sk-SK");
124: nameToJavaName.put("Slovenian", "sl-SI");
125: nameToJavaName.put("Somali", "so-SO");
126: nameToJavaName.put("Albanian", "sq-AL");
127: nameToJavaName.put("Serbian_Cyrillic", "sr-YU");
128: nameToJavaName.put("Serbian_Latin", "sh-BA");
129: nameToJavaName.put("Swedish", "sv-SE");
130: nameToJavaName.put("Swahili", "sw-KE");
131: nameToJavaName.put("Tamil", "ta-IN");
132: nameToJavaName.put("Telugu", "te-IN");
133: nameToJavaName.put("Tajik", "tg-TJ");
134: nameToJavaName.put("Thai", "th-TH");
135: nameToJavaName.put("Turkmen", "tk-TM");
136: nameToJavaName.put("Tswana", "tn-BW");
137: nameToJavaName.put("Turkish", "tr-TR");
138: nameToJavaName.put("Tatar", "tt-RU");
139: nameToJavaName.put("Ukrainian", "uk-UA");
140: nameToJavaName.put("Urdu", "ur-PK");
141: nameToJavaName.put("Uzbek_Latin", "uz-UZ");
142: nameToJavaName.put("Venda", "ven-ZA");
143: nameToJavaName.put("Vietnamese", "vi-VN");
144: nameToJavaName.put("Yoruba", "yo-NG");
145: nameToJavaName.put("Chinese", "zh-CN");
146: nameToJavaName.put("Zulu", "zu-ZA");
147: }
148:
149: String name;
150: Collator collator;
151: Locale locale;
152:
153: public Collation() {
154: locale = Locale.ENGLISH;
155: }
156:
157: public org.hsqldb.lib.Iterator getCollationsIterator() {
158: return nameToJavaName.keySet().iterator();
159: }
160:
161: public org.hsqldb.lib.Iterator getLocalesIterator() {
162: return nameToJavaName.values().iterator();
163: }
164:
165: public void setCollationAsLocale() {
166:
167: Locale locale = Locale.getDefault();
168: String language = locale.getDisplayLanguage(Locale.ENGLISH);
169:
170: try {
171: setCollation(language);
172: } catch (HsqlException e) {
173: }
174: }
175:
176: void setCollation(String name) throws HsqlException {
177:
178: String jname = (String) 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[] parts = StringUtil.split(jname, "-");
187: String language = parts[0];
188: String country = parts.length == 2 ? parts[1] : "";
189:
190: locale = new Locale(language, country);
191: collator = Collator.getInstance(locale);
192: }
193:
194: /**
195: * returns -1, 0 or +1
196: */
197: int compare(String a, String b) {
198:
199: int i;
200:
201: if (collator == null) {
202: i = a.compareTo(b);
203: } else {
204: i = collator.compare(a, b);
205: }
206:
207: return (i == 0) ? 0 : (i < 0 ? -1 : 1);
208: }
209:
210: int compareIgnoreCase(String a, String b) {
211:
212: int i;
213:
214: if (collator == null) {
215: i = JavaSystem.CompareIngnoreCase(a, b);
216: } else {
217: i = collator.compare(toUpperCase(a), toUpperCase(b));
218: }
219:
220: return (i == 0) ? 0 : (i < 0 ? -1 : 1);
221: }
222:
223: String toUpperCase(String s) {
224: return s.toUpperCase(locale);
225: }
226: }
|