1 30 31 32 package org.hsqldb; 33 34 import java.text.Collator ; 35 import java.util.Locale ; 36 37 import org.hsqldb.lib.HashMap; 38 import org.hsqldb.lib.StringUtil; 39 import org.hsqldb.lib.java.JavaSystem; 40 41 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 name; 151 Collator collator; 152 Locale 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 locale = Locale.getDefault(); 169 String language = locale.getDisplayLanguage(Locale.ENGLISH); 170 171 try { 172 setCollation(language); 173 } catch (HsqlException e) {} 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 191 locale = new Locale (language, country); 192 collator = Collator.getInstance(locale); 193 } 194 195 198 int compare(String a, String 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 a, String 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 toUpperCase(String s) { 229 return s.toUpperCase(locale); 230 } 231 } 232 | Popular Tags |