KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > core > Convertors


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.core;
21
22 import java.io.Reader JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.EOFException JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.openide.ErrorManager;
31 import org.openide.util.Lookup;
32 import java.net.URL JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.TreeMap JavaDoc;
35 import org.openide.filesystems.FileObject;
36 import org.openide.loaders.DataObject;
37 import org.openide.util.NbBundle;
38
39 /**
40  * Set of static methods converting misc data representations.
41  *
42  * @author Petr Kuzel
43  * @version 0.9
44  */

45 public final class Convertors {
46     /**
47      * Swings document property added by this support.
48      */

49     public static final String JavaDoc PROP_DOCUMENT_URL = "doc-url";
50
51
52     /**
53      * @return current state of Document as string
54      */

55     public static String JavaDoc documentToString(final Document JavaDoc doc) {
56
57         if (doc == null) throw new NullPointerException JavaDoc();
58         
59         final String JavaDoc[] str = new String JavaDoc[1];
60
61         // safely take the text from the document
62
Runnable JavaDoc run = new Runnable JavaDoc() {
63             public void run () {
64                 try {
65                     str[0] = doc.getText(0, doc.getLength());
66                 } catch (javax.swing.text.BadLocationException JavaDoc e) {
67                     // impossible
68
e.printStackTrace();
69                 }
70             }
71         };
72
73         doc.render(run);
74         return str[0];
75         
76     }
77     
78     /**
79      * @return InputSource, a callie SHOULD set systemId if available
80      */

81     public static InputSource JavaDoc documentToInputSource(Document JavaDoc doc) {
82         
83         if (doc == null) throw new NullPointerException JavaDoc();
84         
85         String JavaDoc text = documentToString(doc);
86         Reader JavaDoc reader = new StringReader JavaDoc(text);
87         
88         // our specifics property
89
String JavaDoc system = (String JavaDoc) doc.getProperty(PROP_DOCUMENT_URL);
90         
91         // try Swing general property
92
if (system == null) {
93             Object JavaDoc obj = doc.getProperty(Document.StreamDescriptionProperty);
94             if (obj instanceof DataObject) {
95                 try {
96                         DataObject dobj = (DataObject) obj;
97                         FileObject fo = dobj.getPrimaryFile();
98                         URL JavaDoc url = fo.getURL();
99                         system = url.toExternalForm();
100                 } catch (IOException JavaDoc io) {
101                     ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class);
102                     emgr.notify(io);
103                 }
104             } else {
105                 ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class);
106                 emgr.log("XML:Convertors:Unknown stream description:" + obj);
107             }
108         }
109
110         // set something, some parsers are nervous if no system id
111
if (system == null) {
112             system = "XML/Core/Convertors/documentToInputSource()"; //NOI18N
113
}
114         
115         InputSource JavaDoc in = new InputSource JavaDoc(system); // NOI18N
116
in.setCharacterStream(reader);
117         return in;
118     }
119
120
121     /**
122      * Wrap reader into buffered one and start reading returning
123      * String as a EOF is reached.
124      */

125     public static String JavaDoc readerToString(Reader JavaDoc reader) throws IOException JavaDoc {
126         
127         BufferedReader JavaDoc fastReader = new BufferedReader JavaDoc(reader);
128         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(1024);
129         try {
130             for (int i = fastReader.read(); i >= 0; i = fastReader.read()) {
131                 buf.append((char)i);
132             }
133         } catch (EOFException JavaDoc eof) {
134             //expected
135
}
136
137         return buf.toString();
138     }
139
140     /**
141      */

142     public static final String JavaDoc iana2java (String JavaDoc iana) {
143         String JavaDoc java = (String JavaDoc) Convertors.EncodingUtil.getIANA2JavaMap ().get (iana.toUpperCase ());
144         return java == null ? iana : java;
145     }
146     
147     public static final String JavaDoc java2iana (String JavaDoc java) {
148         String JavaDoc iana = (String JavaDoc) Convertors.EncodingUtil.getJava2IANAMap ().get (java);
149         return iana == null ? java : iana;
150     }
151
152
153     //!!! this code is copy pasted from TAX library TreeUtilities
154

155     /**
156      *
157      */

158     static class EncodingUtil {
159
160         /** IANA to Java encoding mappings */
161         protected final static Map JavaDoc<String JavaDoc, String JavaDoc> encodingIANA2JavaMap = new TreeMap JavaDoc<String JavaDoc, String JavaDoc>();
162
163         /** */
164         protected final static Map JavaDoc<String JavaDoc, String JavaDoc> encodingIANADescriptionMap = new TreeMap JavaDoc<String JavaDoc, String JavaDoc>();
165
166         /** */
167         protected final static Map JavaDoc<String JavaDoc, String JavaDoc> encodingIANAAliasesMap = new TreeMap JavaDoc<String JavaDoc, String JavaDoc>();
168         
169         protected final static Map JavaDoc<String JavaDoc, String JavaDoc> encodingJava2IANAMap = new TreeMap JavaDoc<String JavaDoc, String JavaDoc>();
170
171         //
172
// Static initialization
173
//
174

175         static {
176             encodingIANA2JavaMap.put ("BIG5", "Big5"); // NOI18N
177
encodingIANADescriptionMap.put ("BIG5", NbBundle.getMessage(Convertors.class, "NAME_BIG5")); // NOI18N
178
encodingIANAAliasesMap.put ("BIG5", "BIG5"); // NOI18N
179

180             encodingIANA2JavaMap.put ("IBM037", "CP037"); // NOI18N
181
encodingIANADescriptionMap.put ("IBM037", NbBundle.getMessage(Convertors.class, "NAME_IBM037")); // NOI18N
182
encodingIANAAliasesMap.put ("IBM037", "IBM037"); // NOI18N
183
encodingIANAAliasesMap.put ("EBCDIC-CP-US", "IBM037"); // NOI18N
184
encodingIANAAliasesMap.put ("EBCDIC-CP-CA", "IBM037"); // NOI18N
185
encodingIANAAliasesMap.put ("EBCDIC-CP-NL", "IBM037"); // NOI18N
186
encodingIANAAliasesMap.put ("EBCDIC-CP-WT", "IBM037"); // NOI18N
187

188             encodingIANA2JavaMap.put ("IBM277", "CP277"); // NOI18N
189
encodingIANADescriptionMap.put ("IBM277", NbBundle.getMessage(Convertors.class, "NAME_IBM277")); // NOI18N
190
encodingIANAAliasesMap.put ("IBM277", "IBM277"); // NOI18N
191
encodingIANAAliasesMap.put ("EBCDIC-CP-DK", "IBM277"); // NOI18N
192
encodingIANAAliasesMap.put ("EBCDIC-CP-NO", "IBM277"); // NOI18N
193

194             encodingIANA2JavaMap.put ("IBM278", "CP278"); // NOI18N
195
encodingIANADescriptionMap.put ("IBM278", NbBundle.getMessage(Convertors.class, "NAME_IBM277")); // NOI18N
196
encodingIANAAliasesMap.put ("IBM278", "IBM278"); // NOI18N
197
encodingIANAAliasesMap.put ("EBCDIC-CP-FI", "IBM278"); // NOI18N
198
encodingIANAAliasesMap.put ("EBCDIC-CP-SE", "IBM278"); // NOI18N
199

200             encodingIANA2JavaMap.put ("IBM280", "CP280"); // NOI18N
201
encodingIANADescriptionMap.put ("IBM280", NbBundle.getMessage(Convertors.class, "NAME_IBM280")); // NOI18N
202
encodingIANAAliasesMap.put ("IBM280", "IBM280"); // NOI18N
203
encodingIANAAliasesMap.put ("EBCDIC-CP-IT", "IBM280"); // NOI18N
204

205             encodingIANA2JavaMap.put ("IBM284", "CP284"); // NOI18N
206
encodingIANADescriptionMap.put ("IBM284", NbBundle.getMessage(Convertors.class, "NAME_IBM284")); // NOI18N
207
encodingIANAAliasesMap.put ("IBM284", "IBM284"); // NOI18N
208
encodingIANAAliasesMap.put ("EBCDIC-CP-ES", "IBM284"); // NOI18N
209

210             encodingIANA2JavaMap.put ("IBM285", "CP285"); // NOI18N
211
encodingIANADescriptionMap.put ("IBM285", NbBundle.getMessage(Convertors.class, "NAME_IBM285")); // NOI18N
212
encodingIANAAliasesMap.put ("IBM285", "IBM285"); // NOI18N
213
encodingIANAAliasesMap.put ("EBCDIC-CP-GB", "IBM285"); // NOI18N
214

215             encodingIANA2JavaMap.put ("IBM297", "CP297"); // NOI18N
216
encodingIANADescriptionMap.put ("IBM297", NbBundle.getMessage(Convertors.class, "NAME_IBM297")); // NOI18N
217
encodingIANAAliasesMap.put ("IBM297", "IBM297"); // NOI18N
218
encodingIANAAliasesMap.put ("EBCDIC-CP-FR", "IBM297"); // NOI18N
219

220             encodingIANA2JavaMap.put ("IBM424", "CP424"); // NOI18N
221
encodingIANADescriptionMap.put ("IBM424", NbBundle.getMessage(Convertors.class, "NAME_IBM424")); // NOI18N
222
encodingIANAAliasesMap.put ("IBM424", "IBM424"); // NOI18N
223
encodingIANAAliasesMap.put ("EBCDIC-CP-HE", "IBM424"); // NOI18N
224

225             encodingIANA2JavaMap.put ("IBM500", "CP500"); // NOI18N
226
encodingIANADescriptionMap.put ("IBM500", NbBundle.getMessage(Convertors.class, "NAME_IBM500")); // NOI18N
227
encodingIANAAliasesMap.put ("IBM500", "IBM500"); // NOI18N
228
encodingIANAAliasesMap.put ("EBCDIC-CP-CH", "IBM500"); // NOI18N
229
encodingIANAAliasesMap.put ("EBCDIC-CP-BE", "IBM500"); // NOI18N
230

231             encodingIANA2JavaMap.put ("IBM870", "CP870"); // NOI18N
232
encodingIANADescriptionMap.put ("IBM870", NbBundle.getMessage(Convertors.class, "NAME_IBM870")); // NOI18N
233
encodingIANAAliasesMap.put ("IBM870", "IBM870"); // NOI18N
234
encodingIANAAliasesMap.put ("EBCDIC-CP-ROECE", "IBM870"); // NOI18N
235
encodingIANAAliasesMap.put ("EBCDIC-CP-YU", "IBM870"); // NOI18N
236

237             encodingIANA2JavaMap.put ("IBM871", "CP871"); // NOI18N
238
encodingIANADescriptionMap.put ("IBM871", NbBundle.getMessage(Convertors.class, "NAME_IBM871")); // NOI18N
239
encodingIANAAliasesMap.put ("IBM871", "IBM871"); // NOI18N
240
encodingIANAAliasesMap.put ("EBCDIC-CP-IS", "IBM871"); // NOI18N
241

242             encodingIANA2JavaMap.put ("IBM918", "CP918"); // NOI18N
243
encodingIANADescriptionMap.put ("IBM918", NbBundle.getMessage(Convertors.class, "NAME_IBM918")); // NOI18N
244
encodingIANAAliasesMap.put ("IBM918", "IBM918"); // NOI18N
245
encodingIANAAliasesMap.put ("EBCDIC-CP-AR2", "IBM918"); // NOI18N
246

247             encodingIANA2JavaMap.put ("EUC-JP", "EUCJIS"); // NOI18N
248
encodingIANADescriptionMap.put ("EUC-JP", NbBundle.getMessage(Convertors.class, "NAME_EUC-JP")); // NOI18N
249
encodingIANAAliasesMap.put ("EUC-JP", "EUC-JP"); // NOI18N
250

251             encodingIANA2JavaMap.put ("EUC-KR", "KSC5601"); // NOI18N
252
encodingIANADescriptionMap.put ("EUC-KR", NbBundle.getMessage(Convertors.class, "NAME_EUC-KR")); // NOI18N
253
encodingIANAAliasesMap.put ("EUC-KR", "EUC-KR"); // NOI18N
254

255             encodingIANA2JavaMap.put ("GB2312", "GB2312"); // NOI18N
256
encodingIANADescriptionMap.put ("GB2312", NbBundle.getMessage(Convertors.class, "NAME_GB2312")); // NOI18N
257
encodingIANAAliasesMap.put ("GB2312", "GB2312"); // NOI18N
258

259             encodingIANA2JavaMap.put ("ISO-2022-JP", "JIS"); // NOI18N
260
encodingIANADescriptionMap.put ("ISO-2022-JP", NbBundle.getMessage(Convertors.class, "NAME_ISO-2022-JP")); // NOI18N
261
encodingIANAAliasesMap.put ("ISO-2022-JP", "ISO-2022-JP"); // NOI18N
262

263             encodingIANA2JavaMap.put ("ISO-2022-KR", "ISO2022KR"); // NOI18N
264
encodingIANADescriptionMap.put ("ISO-2022-KR", NbBundle.getMessage(Convertors.class, "NAME_ISO-2022-KR")); // NOI18N
265
encodingIANAAliasesMap.put ("ISO-2022-KR", "ISO-2022-KR"); // NOI18N
266

267             encodingIANA2JavaMap.put ("ISO-8859-1", "8859_1"); // NOI18N
268
encodingIANADescriptionMap.put ("ISO-8859-1", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-1")); // NOI18N
269
encodingIANAAliasesMap.put ("ISO-8859-1", "ISO-8859-1"); // NOI18N
270
encodingIANAAliasesMap.put ("LATIN1", "ISO-8859-1"); // NOI18N
271
encodingIANAAliasesMap.put ("L1", "ISO-8859-1"); // NOI18N
272
encodingIANAAliasesMap.put ("IBM819", "ISO-8859-1"); // NOI18N
273
encodingIANAAliasesMap.put ("CP819", "ISO-8859-1"); // NOI18N
274

275             encodingIANA2JavaMap.put ("ISO-8859-2", "8859_2"); // NOI18N
276
encodingIANADescriptionMap.put ("ISO-8859-2", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-2")); // NOI18N
277
encodingIANAAliasesMap.put ("ISO-8859-2", "ISO-8859-2"); // NOI18N
278
encodingIANAAliasesMap.put ("LATIN2", "ISO-8859-2"); // NOI18N
279
encodingIANAAliasesMap.put ("L2", "ISO-8859-2"); // NOI18N
280

281             encodingIANA2JavaMap.put ("ISO-8859-3", "8859_3"); // NOI18N
282
encodingIANADescriptionMap.put ("ISO-8859-3", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-3")); // NOI18N
283
encodingIANAAliasesMap.put ("ISO-8859-3", "ISO-8859-3"); // NOI18N
284
encodingIANAAliasesMap.put ("LATIN3", "ISO-8859-3"); // NOI18N
285
encodingIANAAliasesMap.put ("L3", "ISO-8859-3"); // NOI18N
286

287             encodingIANA2JavaMap.put ("ISO-8859-4", "8859_4"); // NOI18N
288
encodingIANADescriptionMap.put ("ISO-8859-4", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-4")); // NOI18N
289
encodingIANAAliasesMap.put ("ISO-8859-4", "ISO-8859-4"); // NOI18N
290
encodingIANAAliasesMap.put ("LATIN4", "ISO-8859-4"); // NOI18N
291
encodingIANAAliasesMap.put ("L4", "ISO-8859-4"); // NOI18N
292

293             encodingIANA2JavaMap.put ("ISO-8859-5", "8859_5"); // NOI18N
294
encodingIANADescriptionMap.put ("ISO-8859-5", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-5")); // NOI18N
295
encodingIANAAliasesMap.put ("ISO-8859-5", "ISO-8859-5"); // NOI18N
296
encodingIANAAliasesMap.put ("CYRILLIC", "ISO-8859-5"); // NOI18N
297

298             encodingIANA2JavaMap.put ("ISO-8859-6", "8859_6"); // NOI18N
299
encodingIANADescriptionMap.put ("ISO-8859-6", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-6")); // NOI18N
300
encodingIANAAliasesMap.put ("ISO-8859-6", "ISO-8859-6"); // NOI18N
301

302             encodingIANA2JavaMap.put ("ISO-8859-7", "8859_7"); // NOI18N
303
encodingIANADescriptionMap.put ("ISO-8859-7", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-7")); // NOI18N
304
encodingIANAAliasesMap.put ("ISO-8859-7", "ISO-8859-7"); // NOI18N
305
encodingIANAAliasesMap.put ("GREEK", "ISO-8859-7"); // NOI18N
306
encodingIANAAliasesMap.put ("GREEK8", "ISO-8859-7"); // NOI18N
307

308             encodingIANA2JavaMap.put ("ISO-8859-8", "8859_8"); // NOI18N
309
encodingIANADescriptionMap.put ("ISO-8859-8", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-8")); // NOI18N
310
encodingIANAAliasesMap.put ("ISO-8859-8", "ISO-8859-8"); // NOI18N
311
encodingIANAAliasesMap.put ("HEBREW", "ISO-8859-8"); // NOI18N
312

313             encodingIANA2JavaMap.put ("ISO-8859-9", "8859_9"); // NOI18N
314
encodingIANADescriptionMap.put ("ISO-8859-9", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-9")); // NOI18N
315
encodingIANAAliasesMap.put ("ISO-8859-9", "ISO-8859-9"); // NOI18N
316
encodingIANAAliasesMap.put ("LATIN5", "ISO-8859-9"); // NOI18N
317
encodingIANAAliasesMap.put ("L5", "ISO-8859-9"); // NOI18N
318

319             encodingIANA2JavaMap.put ("KOI8-R", "KOI8_R"); // NOI18N
320
encodingIANADescriptionMap.put ("KOI8-R", NbBundle.getMessage(Convertors.class, "NAME_KOI8-R")); // NOI18N
321
encodingIANAAliasesMap.put ("KOI8-R", "KOI8-R"); // NOI18N
322

323             encodingIANADescriptionMap.put ("US-ASCII", NbBundle.getMessage(Convertors.class, "NAME_ASCII")); // NOI18N
324
encodingIANAAliasesMap.put ("ASCII", "US-ASCII"); // NOI18N
325
encodingIANAAliasesMap.put ("US-ASCII", "US-ASCII"); // NOI18N
326
encodingIANAAliasesMap.put ("ISO646-US", "US-ASCII"); // NOI18N
327
encodingIANAAliasesMap.put ("IBM367", "US-ASCII"); // NOI18N
328
encodingIANAAliasesMap.put ("CP367", "US-ASCII"); // NOI18N
329

330             encodingIANA2JavaMap.put ("UTF-8", "UTF8"); // NOI18N
331
encodingIANADescriptionMap.put ("UTF-8", NbBundle.getMessage(Convertors.class, "NAME_UTF-8")); // NOI18N
332
encodingIANAAliasesMap.put ("UTF-8", "UTF-8"); // NOI18N
333

334             encodingIANA2JavaMap.put ("UTF-16", "Unicode"); // NOI18N
335
encodingIANADescriptionMap.put ("UTF-16", NbBundle.getMessage(Convertors.class, "NAME_UTF-16")); // NOI18N
336
encodingIANAAliasesMap.put ("UTF-16", "UTF-16"); // NOI18N
337

338             
339             Iterator JavaDoc<String JavaDoc> iter = encodingIANA2JavaMap.keySet().iterator();
340             String JavaDoc key;
341             while (iter.hasNext()){
342                 key = iter.next();
343                 encodingJava2IANAMap.put(encodingIANA2JavaMap.get(key), key);
344             }
345             
346             encodingIANA2JavaMap.put ("US-ASCII", "8859_1"); // NOI18N
347
}
348
349
350         /**
351          */

352         public static Map JavaDoc getIANA2JavaMap () {
353             return encodingIANA2JavaMap;
354         }
355         
356         public static Map JavaDoc getJava2IANAMap () {
357             return encodingJava2IANAMap;
358         }
359     }
360 }
361
Popular Tags