]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/charset/src/com/ibm/icu/charset/CharsetProviderICU.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / charset / src / com / ibm / icu / charset / CharsetProviderICU.java
1 /**\r
2 *******************************************************************************\r
3 * Copyright (C) 2006-2010, International Business Machines Corporation and    *\r
4 * others. All Rights Reserved.                                                *\r
5 *******************************************************************************\r
6 *\r
7 *******************************************************************************\r
8 */\r
9 \r
10 package com.ibm.icu.charset;\r
11 \r
12 import java.io.IOException;\r
13 import java.nio.charset.Charset;\r
14 import java.nio.charset.UnsupportedCharsetException;\r
15 import java.nio.charset.spi.CharsetProvider;\r
16 import java.util.HashMap;\r
17 import java.util.Iterator;\r
18 import java.util.Map;\r
19 \r
20 import com.ibm.icu.impl.InvalidFormatException;\r
21 \r
22 \r
23 /**\r
24  * A concrete subclass of CharsetProvider for loading and providing charset converters\r
25  * in ICU.\r
26  * @stable ICU 3.6\r
27  */\r
28 public final class CharsetProviderICU extends CharsetProvider{\r
29     private static String optionsString = null;\r
30     private static boolean gettingJavaCanonicalName = false;\r
31     \r
32     /**\r
33      * Default constructor \r
34      * @stable ICU 3.6\r
35      */\r
36     public CharsetProviderICU() {\r
37     }\r
38 \r
39     /**\r
40      * Constructs a charset for the given charset name. \r
41      * Implements the abstract method of super class.\r
42      * @param charsetName charset name\r
43      * @return charset objet for the given charset name, null if unsupported\r
44      * @stable ICU 3.6\r
45      */\r
46     public final Charset charsetForName(String charsetName){\r
47         try{\r
48             // extract the options from the charset name\r
49             charsetName = processOptions(charsetName);\r
50             // get the canonical name\r
51             String icuCanonicalName = getICUCanonicalName(charsetName);      \r
52     \r
53                 // create the converter object and return it\r
54             if(icuCanonicalName==null || icuCanonicalName.length()==0){\r
55                 // Try the original name, may be something added and not in the alias table. \r
56                 // Will get an unsupported encoding exception if it doesn't work.\r
57                 return getCharset(charsetName);\r
58             }\r
59             return getCharset(icuCanonicalName);\r
60         }catch(UnsupportedCharsetException ex){\r
61         }catch(IOException ex){\r
62         }\r
63         return null;\r
64     }\r
65     \r
66     /**\r
67      * Constructs a charset for the given ICU conversion table from the specified class path.\r
68      * Example use: <code>cnv = CharsetProviderICU.charsetForName("myConverter", "com/myCompany/myDataPackage");</code>.\r
69      * In this example myConverter.cnv would exist in the com/myCompany/myDataPackage Java package.\r
70      * Conversion tables can be made with ICU4C's makeconv tool.\r
71      * This function allows you to allows you to load user defined conversion\r
72      * tables that are outside of ICU's core data.\r
73      * @param charsetName The name of the charset conversion table.\r
74      * @param classPath The class path that contain the conversion table.\r
75      * @return charset object for the given charset name, null if unsupported\r
76      * @stable ICU 3.8\r
77      */\r
78     public final Charset charsetForName(String charsetName, String classPath) {\r
79         return charsetForName(charsetName, classPath, null);\r
80     }\r
81     \r
82     /**\r
83      * Constructs a charset for the given ICU conversion table from the specified class path.\r
84      * This function is similar to {@link #charsetForName(String, String)}.\r
85      * @param charsetName The name of the charset conversion table.\r
86      * @param classPath The class path that contain the conversion table.\r
87      * @param loader the class object from which to load the charset conversion table\r
88      * @return charset object for the given charset name, null if unsupported\r
89      * @stable ICU 3.8\r
90      */\r
91     public Charset charsetForName(String charsetName, String classPath, ClassLoader loader) {\r
92         CharsetMBCS cs = null;\r
93         try {\r
94              cs = new CharsetMBCS(charsetName, charsetName, new String[0], classPath, loader);\r
95         } catch (InvalidFormatException e) {\r
96             // return null;\r
97         }\r
98         return cs;\r
99     }\r
100     \r
101     /**\r
102      * Gets the canonical name of the converter as defined by Java\r
103      * @param enc converter name\r
104      * @return canonical name of the converter\r
105      * @internal\r
106      * @deprecated This API is ICU internal only.\r
107      */\r
108      public static final String getICUCanonicalName(String enc)\r
109                                 throws UnsupportedCharsetException{\r
110         String canonicalName = null;\r
111         String ret = null;\r
112         try{\r
113             if(enc!=null){\r
114                  if((canonicalName = UConverterAlias.getCanonicalName(enc, "MIME"))!=null){\r
115                     ret = canonicalName;\r
116                 } else if((canonicalName = UConverterAlias.getCanonicalName(enc, "IANA"))!=null){\r
117                     ret = canonicalName;\r
118                 } else if((canonicalName = UConverterAlias.getAlias(enc, 0))!=null){\r
119                     /* we have some aliases in the form x-blah .. match those */\r
120                     ret = canonicalName;\r
121                 }/*else if((canonicalName = UConverterAlias.getCanonicalName(enc, ""))!=null){\r
122                     ret = canonicalName;\r
123                 }*/else if(enc.indexOf("x-")==0){\r
124                     /* TODO: Match with getJavaCanonicalName method */\r
125                     /*\r
126                     char temp[ UCNV_MAX_CONVERTER_NAME_LENGTH] = {0};\r
127                     strcpy(temp, encName+2);\r
128                     */\r
129                     // Remove the 'x-' and get the ICU canonical name\r
130                     if ((canonicalName = UConverterAlias.getAlias(enc.substring(2), 0))!=null) {\r
131                         ret = canonicalName;\r
132                     } else {\r
133                         ret = "";\r
134                     }\r
135                     \r
136                 }else{\r
137                     /* unsupported encoding */\r
138                    ret = "";\r
139                 }\r
140             }\r
141             return ret;\r
142         }catch(IOException ex){\r
143             throw new UnsupportedCharsetException(enc);\r
144         } \r
145     }\r
146     private static final Charset getCharset(String icuCanonicalName) throws IOException{\r
147        String[] aliases = getAliases(icuCanonicalName);    \r
148        String canonicalName = getJavaCanonicalName(icuCanonicalName);\r
149        \r
150        /* Concat the option string to the icuCanonicalName so that the options can be handled properly\r
151         * by the actual charset.\r
152         * Note: getJavaCanonicalName() may eventually call this method so skip the concatenation part\r
153         * during getJavaCanonicalName() call.\r
154         */\r
155        if (!gettingJavaCanonicalName && optionsString != null) {\r
156            icuCanonicalName = icuCanonicalName.concat(optionsString);\r
157            optionsString = null;\r
158        }\r
159        \r
160        return (CharsetICU.getCharset(icuCanonicalName,canonicalName, aliases));\r
161     }\r
162     /**\r
163      * Gets the canonical name of the converter as defined by Java\r
164      * @param charsetName converter name\r
165      * @return canonical name of the converter\r
166      * @internal\r
167      * @deprecated This API is ICU internal only.\r
168      */\r
169     public static String getJavaCanonicalName(String charsetName){\r
170         /*\r
171         If a charset listed in the IANA Charset Registry is supported by an implementation \r
172         of the Java platform then its canonical name must be the name listed in the registry. \r
173         Many charsets are given more than one name in the registry, in which case the registry \r
174         identifies one of the names as MIME-preferred. If a charset has more than one registry \r
175         name then its canonical name must be the MIME-preferred name and the other names in \r
176         the registry must be valid aliases. If a supported charset is not listed in the IANA \r
177         registry then its canonical name must begin with one of the strings "X-" or "x-".\r
178         */\r
179         if(charsetName==null ){\r
180             return null;\r
181         }  \r
182         try{\r
183             String cName = null;\r
184             /* find out the alias with MIME tag */\r
185             if((cName=UConverterAlias.getStandardName(charsetName, "MIME"))!=null){\r
186             /* find out the alias with IANA tag */\r
187             }else if((cName=UConverterAlias.getStandardName(charsetName, "IANA"))!=null){\r
188             }else {\r
189                 /*  \r
190                     check to see if an alias already exists with x- prefix, if yes then \r
191                     make that the canonical name\r
192                 */\r
193                 int aliasNum = UConverterAlias.countAliases(charsetName);\r
194                 String name;\r
195                 for(int i=0;i<aliasNum;i++){\r
196                     name = UConverterAlias.getAlias(charsetName, i);\r
197                     if(name!=null && name.indexOf("x-")==0){\r
198                         cName = name;\r
199                         break;\r
200                     }\r
201                 }\r
202                 /* last resort just append x- to any of the alias and \r
203                 make it the canonical name */\r
204                 if((cName==null || cName.length()==0)){\r
205                     name = UConverterAlias.getStandardName(charsetName, "UTR22");\r
206                     if(name==null && charsetName.indexOf(",")!=-1){\r
207                         name = UConverterAlias.getAlias(charsetName, 1);\r
208                     }\r
209                     /* if there is no UTR22 canonical name .. then just return itself*/\r
210                     if(name==null){\r
211                         name = charsetName;\r
212                     }\r
213                     cName = "x-"+ name;\r
214                 }\r
215             }\r
216             /* After getting the java canonical name from ICU alias table, get the\r
217              * java canonical name from the current JDK. This is neccessary because\r
218              * different versions of the JVM (Sun and IBM) may have a different\r
219              * canonical name then the one given by ICU. So the java canonical name\r
220              * will depend on the current JVM.  Since java cannot use the ICU canonical \r
221              * we have to try to use a java compatible name.\r
222              */\r
223             if (cName != null) {\r
224                 if (!gettingJavaCanonicalName) {\r
225                     gettingJavaCanonicalName = true;\r
226                     if (Charset.isSupported(cName)) {\r
227                         cName = Charset.forName(cName).name();\r
228                     }\r
229                     gettingJavaCanonicalName = false;\r
230                 }\r
231             }\r
232             return cName;\r
233         }catch (IOException ex){\r
234             \r
235         }\r
236         return null;\r
237      }\r
238 \r
239     /** \r
240      * Gets the aliases associated with the converter name\r
241      * @param encName converter name\r
242      * @return converter names as elements in an object array\r
243      * @internal\r
244      * @deprecated This API is ICU internal only.\r
245      */\r
246     private static final String[] getAliases(String encName)throws IOException{\r
247         String[] ret = null;\r
248         int aliasNum = 0;\r
249         int i=0;\r
250         int j=0;\r
251         String aliasArray[/*50*/] = new String[50];\r
252     \r
253         if(encName != null){\r
254             aliasNum = UConverterAlias.countAliases(encName);\r
255             for(i=0,j=0;i<aliasNum;i++){\r
256                 String name = UConverterAlias.getAlias(encName,i);\r
257                 if(name.indexOf('+')==-1 && name.indexOf(',')==-1){\r
258                     aliasArray[j++]= name;\r
259                 }\r
260             }\r
261             ret = new String[j];\r
262             for(;--j>=0;) {\r
263                 ret[j] = aliasArray[j];\r
264             }\r
265                         \r
266         }\r
267         return (ret);\r
268     \r
269     }\r
270 \r
271     private static final void putCharsets(Map<Charset, String> map){\r
272         int num = UConverterAlias.countAvailable();\r
273         for(int i=0;i<num;i++) {\r
274             String name = UConverterAlias.getAvailableName(i);\r
275             try {\r
276                 Charset cs =  getCharset(name);\r
277                 map.put(cs, getJavaCanonicalName(name));\r
278             }catch(UnsupportedCharsetException ex){\r
279             }catch (IOException e) {\r
280             }\r
281             // add only charsets that can be created!\r
282         }\r
283     }\r
284 \r
285     /**\r
286      * Returns an iterator for the available charsets.\r
287      * Implements the abstract method of super class.\r
288      * @return Iterator the charset name iterator\r
289      * @stable ICU 3.6\r
290      */\r
291     public final Iterator<Charset> charsets(){\r
292         HashMap<Charset, String> map = new HashMap<Charset, String>();\r
293         putCharsets(map);\r
294         return map.keySet().iterator();\r
295     }\r
296     \r
297     /**\r
298      * Gets the canonical names of available converters \r
299      * @return array of available converter names\r
300      * @internal\r
301      * @deprecated This API is ICU internal only.\r
302      */\r
303      public static final String[] getAvailableNames(){\r
304         HashMap<Charset, String> map = new HashMap<Charset, String>();\r
305         putCharsets(map);\r
306         return map.values().toArray(new String[0]);\r
307     }\r
308      \r
309     /**\r
310      * Return all names available\r
311      * @return String[] an array of all available names\r
312      * @internal\r
313      * @deprecated This API is ICU internal only.\r
314      */\r
315      public static final String[] getAllNames(){\r
316         int num = UConverterAlias.countAvailable();\r
317         String[] names = new String[num];\r
318         for(int i=0;i<num;i++) {\r
319             names[i] = UConverterAlias.getAvailableName(i);\r
320         }\r
321         return names;\r
322     }\r
323     \r
324     private static final String processOptions(String charsetName) {\r
325         if (charsetName.indexOf(UConverterConstants.OPTION_SWAP_LFNL_STRING) > -1) {\r
326             /* Remove and save the swap lfnl option string portion of the charset name. */\r
327             optionsString = UConverterConstants.OPTION_SWAP_LFNL_STRING;\r
328             \r
329             charsetName = charsetName.substring(0, charsetName.indexOf(UConverterConstants.OPTION_SWAP_LFNL_STRING));\r
330         }\r
331         \r
332         return charsetName;\r
333     }\r
334 }\r