001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.insync;
042:
043: import java.io.Reader;
044: import java.io.StringReader;
045: import java.io.BufferedReader;
046: import java.io.IOException;
047: import java.io.EOFException;
048: import java.util.Iterator;
049:
050: import javax.swing.text.Document;
051: import org.openide.util.NbBundle;
052:
053: import org.xml.sax.InputSource;
054:
055: import org.openide.ErrorManager;
056: import org.openide.util.Lookup;
057: import java.net.URL;
058: import java.util.Map;
059: import java.util.TreeMap;
060:
061: import org.openide.filesystems.FileObject;
062: import org.openide.loaders.DataObject;
063:
064: /**
065: * Set of static methods converting misc data representations.
066: *
067: * @author Petr Kuzel
068: * @version 0.9
069: */
070: public final class Convertors {
071:
072: /**
073: * @return current state of Document as string
074: */
075: public static String documentToString(final Document doc) {
076:
077: if (doc == null)
078: throw new NullPointerException();
079:
080: final String[] str = new String[1];
081:
082: // safely take the text from the document
083: Runnable run = new Runnable() {
084: public void run() {
085: try {
086: str[0] = doc.getText(0, doc.getLength());
087: } catch (javax.swing.text.BadLocationException e) {
088: // impossible
089: e.printStackTrace();
090: }
091: }
092: };
093:
094: doc.render(run);
095: return str[0];
096:
097: }
098:
099: /**
100: * @return InputSource, a callie SHOULD set systemId if available
101: */
102: /*public static InputSource documentToInputSource(Document doc) {
103:
104: if (doc == null) throw new NullPointerException();
105:
106: String text = documentToString(doc);
107: Reader reader = new StringReader(text);
108:
109: // our specifics property
110: String system = (String) doc.getProperty(TextEditorSupport.PROP_DOCUMENT_URL);
111:
112: // try Swing general property
113: if (system == null) {
114: Object obj = doc.getProperty(Document.StreamDescriptionProperty);
115: if (obj instanceof DataObject) {
116: try {
117: DataObject dobj = (DataObject) obj;
118: FileObject fo = dobj.getPrimaryFile();
119: URL url = fo.getURL();
120: system = url.toExternalForm();
121: } catch (IOException io) {
122: ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class);
123: emgr.notify(io);
124: }
125: } else {
126: ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class);
127: emgr.log("XML:Convertors:Unknown stream description:" + obj);
128: }
129: }
130:
131: // set something, some parsers are nervous if no system id
132: if (system == null) {
133: system = "XML/Core/Convertors/documentToInputSource()"; //NOI18N
134: }
135:
136: InputSource in = new InputSource(system); // NOI18N
137: in.setCharacterStream(reader);
138: return in;
139: }*/
140:
141: /**
142: * Wrap reader into buffered one and start reading returning
143: * String as a EOF is reached.
144: */
145: public static String readerToString(Reader reader)
146: throws IOException {
147:
148: BufferedReader fastReader = new BufferedReader(reader);
149: StringBuffer buf = new StringBuffer(1024);
150: try {
151: for (int i = fastReader.read(); i >= 0; i = fastReader
152: .read()) {
153: buf.append((char) i);
154: }
155: } catch (EOFException eof) {
156: //expected
157: }
158:
159: return buf.toString();
160: }
161:
162: /**
163: */
164: public static final String iana2java(String iana) {
165: String java = (String) Convertors.EncodingUtil
166: .getIANA2JavaMap().get(iana.toUpperCase());
167: return java == null ? iana : java;
168: }
169:
170: public static final String java2iana(String java) {
171: String iana = (String) Convertors.EncodingUtil
172: .getJava2IANAMap().get(java);
173: return iana == null ? java : iana;
174: }
175:
176: //!!! this code is copy pasted from TAX library TreeUtilities
177:
178: /**
179: *
180: */
181: static class EncodingUtil {
182:
183: /** IANA to Java encoding mappings */
184: protected final static Map encodingIANA2JavaMap = new TreeMap();
185:
186: /** */
187: protected final static Map encodingIANADescriptionMap = new TreeMap();
188:
189: /** */
190: protected final static Map encodingIANAAliasesMap = new TreeMap();
191:
192: protected final static Map encodingJava2IANAMap = new TreeMap();
193:
194: //
195: // Static initialization
196: //
197:
198: static {
199: encodingIANA2JavaMap.put("BIG5", "Big5"); // NOI18N
200: encodingIANADescriptionMap.put("BIG5",
201: getString("NAME_BIG5")); // NOI18N
202: encodingIANAAliasesMap.put("BIG5", "BIG5"); // NOI18N
203:
204: encodingIANA2JavaMap.put("IBM037", "CP037"); // NOI18N
205: encodingIANADescriptionMap.put("IBM037",
206: getString("NAME_IBM037")); // NOI18N
207: encodingIANAAliasesMap.put("IBM037", "IBM037"); // NOI18N
208: encodingIANAAliasesMap.put("EBCDIC-CP-US", "IBM037"); // NOI18N
209: encodingIANAAliasesMap.put("EBCDIC-CP-CA", "IBM037"); // NOI18N
210: encodingIANAAliasesMap.put("EBCDIC-CP-NL", "IBM037"); // NOI18N
211: encodingIANAAliasesMap.put("EBCDIC-CP-WT", "IBM037"); // NOI18N
212:
213: encodingIANA2JavaMap.put("IBM277", "CP277"); // NOI18N
214: encodingIANADescriptionMap.put("IBM277",
215: getString("NAME_IBM277")); // NOI18N
216: encodingIANAAliasesMap.put("IBM277", "IBM277"); // NOI18N
217: encodingIANAAliasesMap.put("EBCDIC-CP-DK", "IBM277"); // NOI18N
218: encodingIANAAliasesMap.put("EBCDIC-CP-NO", "IBM277"); // NOI18N
219:
220: encodingIANA2JavaMap.put("IBM278", "CP278"); // NOI18N
221: encodingIANADescriptionMap.put("IBM278",
222: getString("NAME_IBM277")); // NOI18N
223: encodingIANAAliasesMap.put("IBM278", "IBM278"); // NOI18N
224: encodingIANAAliasesMap.put("EBCDIC-CP-FI", "IBM278"); // NOI18N
225: encodingIANAAliasesMap.put("EBCDIC-CP-SE", "IBM278"); // NOI18N
226:
227: encodingIANA2JavaMap.put("IBM280", "CP280"); // NOI18N
228: encodingIANADescriptionMap.put("IBM280",
229: getString("NAME_IBM280")); // NOI18N
230: encodingIANAAliasesMap.put("IBM280", "IBM280"); // NOI18N
231: encodingIANAAliasesMap.put("EBCDIC-CP-IT", "IBM280"); // NOI18N
232:
233: encodingIANA2JavaMap.put("IBM284", "CP284"); // NOI18N
234: encodingIANADescriptionMap.put("IBM284",
235: getString("NAME_IBM284")); // NOI18N
236: encodingIANAAliasesMap.put("IBM284", "IBM284"); // NOI18N
237: encodingIANAAliasesMap.put("EBCDIC-CP-ES", "IBM284"); // NOI18N
238:
239: encodingIANA2JavaMap.put("IBM285", "CP285"); // NOI18N
240: encodingIANADescriptionMap.put("IBM285",
241: getString("NAME_IBM285")); // NOI18N
242: encodingIANAAliasesMap.put("IBM285", "IBM285"); // NOI18N
243: encodingIANAAliasesMap.put("EBCDIC-CP-GB", "IBM285"); // NOI18N
244:
245: encodingIANA2JavaMap.put("IBM297", "CP297"); // NOI18N
246: encodingIANADescriptionMap.put("IBM297",
247: getString("NAME_IBM297")); // NOI18N
248: encodingIANAAliasesMap.put("IBM297", "IBM297"); // NOI18N
249: encodingIANAAliasesMap.put("EBCDIC-CP-FR", "IBM297"); // NOI18N
250:
251: encodingIANA2JavaMap.put("IBM424", "CP424"); // NOI18N
252: encodingIANADescriptionMap.put("IBM424",
253: getString("NAME_IBM424")); // NOI18N
254: encodingIANAAliasesMap.put("IBM424", "IBM424"); // NOI18N
255: encodingIANAAliasesMap.put("EBCDIC-CP-HE", "IBM424"); // NOI18N
256:
257: encodingIANA2JavaMap.put("IBM500", "CP500"); // NOI18N
258: encodingIANADescriptionMap.put("IBM500",
259: getString("NAME_IBM500")); // NOI18N
260: encodingIANAAliasesMap.put("IBM500", "IBM500"); // NOI18N
261: encodingIANAAliasesMap.put("EBCDIC-CP-CH", "IBM500"); // NOI18N
262: encodingIANAAliasesMap.put("EBCDIC-CP-BE", "IBM500"); // NOI18N
263:
264: encodingIANA2JavaMap.put("IBM870", "CP870"); // NOI18N
265: encodingIANADescriptionMap.put("IBM870",
266: getString("NAME_IBM870")); // NOI18N
267: encodingIANAAliasesMap.put("IBM870", "IBM870"); // NOI18N
268: encodingIANAAliasesMap.put("EBCDIC-CP-ROECE", "IBM870"); // NOI18N
269: encodingIANAAliasesMap.put("EBCDIC-CP-YU", "IBM870"); // NOI18N
270:
271: encodingIANA2JavaMap.put("IBM871", "CP871"); // NOI18N
272: encodingIANADescriptionMap.put("IBM871",
273: getString("NAME_IBM871")); // NOI18N
274: encodingIANAAliasesMap.put("IBM871", "IBM871"); // NOI18N
275: encodingIANAAliasesMap.put("EBCDIC-CP-IS", "IBM871"); // NOI18N
276:
277: encodingIANA2JavaMap.put("IBM918", "CP918"); // NOI18N
278: encodingIANADescriptionMap.put("IBM918",
279: getString("NAME_IBM918")); // NOI18N
280: encodingIANAAliasesMap.put("IBM918", "IBM918"); // NOI18N
281: encodingIANAAliasesMap.put("EBCDIC-CP-AR2", "IBM918"); // NOI18N
282:
283: encodingIANA2JavaMap.put("EUC-JP", "EUCJIS"); // NOI18N
284: encodingIANADescriptionMap.put("EUC-JP",
285: getString("NAME_EUC-JP")); // NOI18N
286: encodingIANAAliasesMap.put("EUC-JP", "EUC-JP"); // NOI18N
287:
288: encodingIANA2JavaMap.put("EUC-KR", "KSC5601"); // NOI18N
289: encodingIANADescriptionMap.put("EUC-KR",
290: getString("NAME_EUC-KR")); // NOI18N
291: encodingIANAAliasesMap.put("EUC-KR", "EUC-KR"); // NOI18N
292:
293: encodingIANA2JavaMap.put("GB2312", "GB2312"); // NOI18N
294: encodingIANADescriptionMap.put("GB2312",
295: getString("NAME_GB2312")); // NOI18N
296: encodingIANAAliasesMap.put("GB2312", "GB2312"); // NOI18N
297:
298: encodingIANA2JavaMap.put("ISO-2022-JP", "JIS"); // NOI18N
299: encodingIANADescriptionMap.put("ISO-2022-JP",
300: getString("NAME_ISO-2022-JP")); // NOI18N
301: encodingIANAAliasesMap.put("ISO-2022-JP", "ISO-2022-JP"); // NOI18N
302:
303: encodingIANA2JavaMap.put("ISO-2022-KR", "ISO2022KR"); // NOI18N
304: encodingIANADescriptionMap.put("ISO-2022-KR",
305: getString("NAME_ISO-2022-KR")); // NOI18N
306: encodingIANAAliasesMap.put("ISO-2022-KR", "ISO-2022-KR"); // NOI18N
307:
308: encodingIANA2JavaMap.put("ISO-8859-1", "8859_1"); // NOI18N
309: encodingIANADescriptionMap.put("ISO-8859-1",
310: getString("NAME_ISO-8859-1")); // NOI18N
311: encodingIANAAliasesMap.put("ISO-8859-1", "ISO-8859-1"); // NOI18N
312: encodingIANAAliasesMap.put("LATIN1", "ISO-8859-1"); // NOI18N
313: encodingIANAAliasesMap.put("L1", "ISO-8859-1"); // NOI18N
314: encodingIANAAliasesMap.put("IBM819", "ISO-8859-1"); // NOI18N
315: encodingIANAAliasesMap.put("CP819", "ISO-8859-1"); // NOI18N
316:
317: encodingIANA2JavaMap.put("ISO-8859-2", "8859_2"); // NOI18N
318: encodingIANADescriptionMap.put("ISO-8859-2",
319: getString("NAME_ISO-8859-2")); // NOI18N
320: encodingIANAAliasesMap.put("ISO-8859-2", "ISO-8859-2"); // NOI18N
321: encodingIANAAliasesMap.put("LATIN2", "ISO-8859-2"); // NOI18N
322: encodingIANAAliasesMap.put("L2", "ISO-8859-2"); // NOI18N
323:
324: encodingIANA2JavaMap.put("ISO-8859-3", "8859_3"); // NOI18N
325: encodingIANADescriptionMap.put("ISO-8859-3",
326: getString("NAME_ISO-8859-3")); // NOI18N
327: encodingIANAAliasesMap.put("ISO-8859-3", "ISO-8859-3"); // NOI18N
328: encodingIANAAliasesMap.put("LATIN3", "ISO-8859-3"); // NOI18N
329: encodingIANAAliasesMap.put("L3", "ISO-8859-3"); // NOI18N
330:
331: encodingIANA2JavaMap.put("ISO-8859-4", "8859_4"); // NOI18N
332: encodingIANADescriptionMap.put("ISO-8859-4",
333: getString("NAME_ISO-8859-4")); // NOI18N
334: encodingIANAAliasesMap.put("ISO-8859-4", "ISO-8859-4"); // NOI18N
335: encodingIANAAliasesMap.put("LATIN4", "ISO-8859-4"); // NOI18N
336: encodingIANAAliasesMap.put("L4", "ISO-8859-4"); // NOI18N
337:
338: encodingIANA2JavaMap.put("ISO-8859-5", "8859_5"); // NOI18N
339: encodingIANADescriptionMap.put("ISO-8859-5",
340: getString("NAME_ISO-8859-5")); // NOI18N
341: encodingIANAAliasesMap.put("ISO-8859-5", "ISO-8859-5"); // NOI18N
342: encodingIANAAliasesMap.put("CYRILLIC", "ISO-8859-5"); // NOI18N
343:
344: encodingIANA2JavaMap.put("ISO-8859-6", "8859_6"); // NOI18N
345: encodingIANADescriptionMap.put("ISO-8859-6",
346: getString("NAME_ISO-8859-6")); // NOI18N
347: encodingIANAAliasesMap.put("ISO-8859-6", "ISO-8859-6"); // NOI18N
348:
349: encodingIANA2JavaMap.put("ISO-8859-7", "8859_7"); // NOI18N
350: encodingIANADescriptionMap.put("ISO-8859-7",
351: getString("NAME_ISO-8859-7")); // NOI18N
352: encodingIANAAliasesMap.put("ISO-8859-7", "ISO-8859-7"); // NOI18N
353: encodingIANAAliasesMap.put("GREEK", "ISO-8859-7"); // NOI18N
354: encodingIANAAliasesMap.put("GREEK8", "ISO-8859-7"); // NOI18N
355:
356: encodingIANA2JavaMap.put("ISO-8859-8", "8859_8"); // NOI18N
357: encodingIANADescriptionMap.put("ISO-8859-8",
358: getString("NAME_ISO-8859-8")); // NOI18N
359: encodingIANAAliasesMap.put("ISO-8859-8", "ISO-8859-8"); // NOI18N
360: encodingIANAAliasesMap.put("HEBREW", "ISO-8859-8"); // NOI18N
361:
362: encodingIANA2JavaMap.put("ISO-8859-9", "8859_9"); // NOI18N
363: encodingIANADescriptionMap.put("ISO-8859-9",
364: getString("NAME_ISO-8859-9")); // NOI18N
365: encodingIANAAliasesMap.put("ISO-8859-9", "ISO-8859-9"); // NOI18N
366: encodingIANAAliasesMap.put("LATIN5", "ISO-8859-9"); // NOI18N
367: encodingIANAAliasesMap.put("L5", "ISO-8859-9"); // NOI18N
368:
369: encodingIANA2JavaMap.put("KOI8-R", "KOI8_R"); // NOI18N
370: encodingIANADescriptionMap.put("KOI8-R",
371: getString("NAME_KOI8-R")); // NOI18N
372: encodingIANAAliasesMap.put("KOI8-R", "KOI8-R"); // NOI18N
373:
374: encodingIANADescriptionMap.put("US-ASCII",
375: getString("NAME_ASCII")); // NOI18N
376: encodingIANAAliasesMap.put("ASCII", "US-ASCII"); // NOI18N
377: encodingIANAAliasesMap.put("US-ASCII", "US-ASCII"); // NOI18N
378: encodingIANAAliasesMap.put("ISO646-US", "US-ASCII"); // NOI18N
379: encodingIANAAliasesMap.put("IBM367", "US-ASCII"); // NOI18N
380: encodingIANAAliasesMap.put("CP367", "US-ASCII"); // NOI18N
381:
382: encodingIANA2JavaMap.put("UTF-8", "UTF8"); // NOI18N
383: encodingIANADescriptionMap.put("UTF-8",
384: getString("NAME_UTF-8")); // NOI18N
385: encodingIANAAliasesMap.put("UTF-8", "UTF-8"); // NOI18N
386:
387: encodingIANA2JavaMap.put("UTF-16", "Unicode"); // NOI18N
388: encodingIANADescriptionMap.put("UTF-16",
389: getString("NAME_UTF-16")); // NOI18N
390: encodingIANAAliasesMap.put("UTF-16", "UTF-16"); // NOI18N
391:
392: Iterator iter = encodingIANA2JavaMap.keySet().iterator();
393: Object key;
394: while (iter.hasNext()) {
395: key = iter.next();
396: encodingJava2IANAMap.put(encodingIANA2JavaMap.get(key),
397: key);
398: }
399:
400: encodingIANA2JavaMap.put("US-ASCII", "8859_1"); // NOI18N
401: }
402:
403: /**
404: * Get localized string from package bundle.
405: * @param key Key identifing localized value.
406: * @return localized value.
407: */
408: private static String getString(String key) {
409: if (key == null)
410: throw new NullPointerException();
411: return NbBundle.getMessage(Convertors.class, key);
412: }
413:
414: /**
415: */
416: public static Map getIANA2JavaMap() {
417: return encodingIANA2JavaMap;
418: }
419:
420: public static Map getJava2IANAMap() {
421: return encodingJava2IANAMap;
422: }
423:
424: } // end: class EncodingUtil
425:
426: }
|