1 19 20 package org.netbeans.modules.xml.schema.core; 21 22 import java.io.Reader ; 23 import java.io.StringReader ; 24 import java.io.BufferedReader ; 25 import java.io.IOException ; 26 import java.io.EOFException ; 27 import java.util.Iterator ; 28 import javax.swing.text.Document ; 29 import org.xml.sax.InputSource ; 30 import org.openide.ErrorManager; 31 import org.openide.util.Lookup; 32 import java.net.URL ; 33 import java.util.Map ; 34 import java.util.TreeMap ; 35 import org.openide.filesystems.FileObject; 36 import org.openide.loaders.DataObject; 37 import org.openide.util.NbBundle; 38 39 45 public final class Convertors { 46 49 public static final String PROP_DOCUMENT_URL = "doc-url"; 50 51 52 55 public static String documentToString(final Document doc) { 56 57 if (doc == null) throw new NullPointerException (); 58 59 final String [] str = new String [1]; 60 61 Runnable run = new Runnable () { 63 public void run () { 64 try { 65 str[0] = doc.getText(0, doc.getLength()); 66 } catch (javax.swing.text.BadLocationException e) { 67 e.printStackTrace(); 69 } 70 } 71 }; 72 73 doc.render(run); 74 return str[0]; 75 76 } 77 78 81 public static InputSource documentToInputSource(Document doc) { 82 83 if (doc == null) throw new NullPointerException (); 84 85 String text = documentToString(doc); 86 Reader reader = new StringReader (text); 87 88 String system = (String ) doc.getProperty(PROP_DOCUMENT_URL); 90 91 if (system == null) { 93 Object obj = doc.getProperty(Document.StreamDescriptionProperty); 94 if (obj instanceof DataObject) { 95 try { 96 DataObject dobj = (DataObject) obj; 97 FileObject fo = dobj.getPrimaryFile(); 98 URL url = fo.getURL(); 99 system = url.toExternalForm(); 100 } catch (IOException 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 if (system == null) { 112 system = "XML/Core/Convertors/documentToInputSource()"; } 114 115 InputSource in = new InputSource (system); in.setCharacterStream(reader); 117 return in; 118 } 119 120 121 125 public static String readerToString(Reader reader) throws IOException { 126 127 BufferedReader fastReader = new BufferedReader (reader); 128 StringBuffer buf = new StringBuffer (1024); 129 try { 130 for (int i = fastReader.read(); i >= 0; i = fastReader.read()) { 131 buf.append((char)i); 132 } 133 } catch (EOFException eof) { 134 } 136 137 return buf.toString(); 138 } 139 140 142 public static final String iana2java (String iana) { 143 String java = (String ) Convertors.EncodingUtil.getIANA2JavaMap ().get (iana.toUpperCase ()); 144 return java == null ? iana : java; 145 } 146 147 public static final String java2iana (String java) { 148 String iana = (String ) Convertors.EncodingUtil.getJava2IANAMap ().get (java); 149 return iana == null ? java : iana; 150 } 151 152 153 155 158 static class EncodingUtil { 159 160 161 protected final static Map <String , String > encodingIANA2JavaMap = new TreeMap <String , String >(); 162 163 164 protected final static Map <String , String > encodingIANADescriptionMap = new TreeMap <String , String >(); 165 166 167 protected final static Map <String , String > encodingIANAAliasesMap = new TreeMap <String , String >(); 168 169 protected final static Map <String , String > encodingJava2IANAMap = new TreeMap <String , String >(); 170 171 175 static { 176 encodingIANA2JavaMap.put ("BIG5", "Big5"); encodingIANADescriptionMap.put ("BIG5", NbBundle.getMessage(Convertors.class, "NAME_BIG5")); encodingIANAAliasesMap.put ("BIG5", "BIG5"); 180 encodingIANA2JavaMap.put ("IBM037", "CP037"); encodingIANADescriptionMap.put ("IBM037", NbBundle.getMessage(Convertors.class, "NAME_IBM037")); encodingIANAAliasesMap.put ("IBM037", "IBM037"); encodingIANAAliasesMap.put ("EBCDIC-CP-US", "IBM037"); encodingIANAAliasesMap.put ("EBCDIC-CP-CA", "IBM037"); encodingIANAAliasesMap.put ("EBCDIC-CP-NL", "IBM037"); encodingIANAAliasesMap.put ("EBCDIC-CP-WT", "IBM037"); 188 encodingIANA2JavaMap.put ("IBM277", "CP277"); encodingIANADescriptionMap.put ("IBM277", NbBundle.getMessage(Convertors.class, "NAME_IBM277")); encodingIANAAliasesMap.put ("IBM277", "IBM277"); encodingIANAAliasesMap.put ("EBCDIC-CP-DK", "IBM277"); encodingIANAAliasesMap.put ("EBCDIC-CP-NO", "IBM277"); 194 encodingIANA2JavaMap.put ("IBM278", "CP278"); encodingIANADescriptionMap.put ("IBM278", NbBundle.getMessage(Convertors.class, "NAME_IBM277")); encodingIANAAliasesMap.put ("IBM278", "IBM278"); encodingIANAAliasesMap.put ("EBCDIC-CP-FI", "IBM278"); encodingIANAAliasesMap.put ("EBCDIC-CP-SE", "IBM278"); 200 encodingIANA2JavaMap.put ("IBM280", "CP280"); encodingIANADescriptionMap.put ("IBM280", NbBundle.getMessage(Convertors.class, "NAME_IBM280")); encodingIANAAliasesMap.put ("IBM280", "IBM280"); encodingIANAAliasesMap.put ("EBCDIC-CP-IT", "IBM280"); 205 encodingIANA2JavaMap.put ("IBM284", "CP284"); encodingIANADescriptionMap.put ("IBM284", NbBundle.getMessage(Convertors.class, "NAME_IBM284")); encodingIANAAliasesMap.put ("IBM284", "IBM284"); encodingIANAAliasesMap.put ("EBCDIC-CP-ES", "IBM284"); 210 encodingIANA2JavaMap.put ("IBM285", "CP285"); encodingIANADescriptionMap.put ("IBM285", NbBundle.getMessage(Convertors.class, "NAME_IBM285")); encodingIANAAliasesMap.put ("IBM285", "IBM285"); encodingIANAAliasesMap.put ("EBCDIC-CP-GB", "IBM285"); 215 encodingIANA2JavaMap.put ("IBM297", "CP297"); encodingIANADescriptionMap.put ("IBM297", NbBundle.getMessage(Convertors.class, "NAME_IBM297")); encodingIANAAliasesMap.put ("IBM297", "IBM297"); encodingIANAAliasesMap.put ("EBCDIC-CP-FR", "IBM297"); 220 encodingIANA2JavaMap.put ("IBM424", "CP424"); encodingIANADescriptionMap.put ("IBM424", NbBundle.getMessage(Convertors.class, "NAME_IBM424")); encodingIANAAliasesMap.put ("IBM424", "IBM424"); encodingIANAAliasesMap.put ("EBCDIC-CP-HE", "IBM424"); 225 encodingIANA2JavaMap.put ("IBM500", "CP500"); encodingIANADescriptionMap.put ("IBM500", NbBundle.getMessage(Convertors.class, "NAME_IBM500")); encodingIANAAliasesMap.put ("IBM500", "IBM500"); encodingIANAAliasesMap.put ("EBCDIC-CP-CH", "IBM500"); encodingIANAAliasesMap.put ("EBCDIC-CP-BE", "IBM500"); 231 encodingIANA2JavaMap.put ("IBM870", "CP870"); encodingIANADescriptionMap.put ("IBM870", NbBundle.getMessage(Convertors.class, "NAME_IBM870")); encodingIANAAliasesMap.put ("IBM870", "IBM870"); encodingIANAAliasesMap.put ("EBCDIC-CP-ROECE", "IBM870"); encodingIANAAliasesMap.put ("EBCDIC-CP-YU", "IBM870"); 237 encodingIANA2JavaMap.put ("IBM871", "CP871"); encodingIANADescriptionMap.put ("IBM871", NbBundle.getMessage(Convertors.class, "NAME_IBM871")); encodingIANAAliasesMap.put ("IBM871", "IBM871"); encodingIANAAliasesMap.put ("EBCDIC-CP-IS", "IBM871"); 242 encodingIANA2JavaMap.put ("IBM918", "CP918"); encodingIANADescriptionMap.put ("IBM918", NbBundle.getMessage(Convertors.class, "NAME_IBM918")); encodingIANAAliasesMap.put ("IBM918", "IBM918"); encodingIANAAliasesMap.put ("EBCDIC-CP-AR2", "IBM918"); 247 encodingIANA2JavaMap.put ("EUC-JP", "EUCJIS"); encodingIANADescriptionMap.put ("EUC-JP", NbBundle.getMessage(Convertors.class, "NAME_EUC-JP")); encodingIANAAliasesMap.put ("EUC-JP", "EUC-JP"); 251 encodingIANA2JavaMap.put ("EUC-KR", "KSC5601"); encodingIANADescriptionMap.put ("EUC-KR", NbBundle.getMessage(Convertors.class, "NAME_EUC-KR")); encodingIANAAliasesMap.put ("EUC-KR", "EUC-KR"); 255 encodingIANA2JavaMap.put ("GB2312", "GB2312"); encodingIANADescriptionMap.put ("GB2312", NbBundle.getMessage(Convertors.class, "NAME_GB2312")); encodingIANAAliasesMap.put ("GB2312", "GB2312"); 259 encodingIANA2JavaMap.put ("ISO-2022-JP", "JIS"); encodingIANADescriptionMap.put ("ISO-2022-JP", NbBundle.getMessage(Convertors.class, "NAME_ISO-2022-JP")); encodingIANAAliasesMap.put ("ISO-2022-JP", "ISO-2022-JP"); 263 encodingIANA2JavaMap.put ("ISO-2022-KR", "ISO2022KR"); encodingIANADescriptionMap.put ("ISO-2022-KR", NbBundle.getMessage(Convertors.class, "NAME_ISO-2022-KR")); encodingIANAAliasesMap.put ("ISO-2022-KR", "ISO-2022-KR"); 267 encodingIANA2JavaMap.put ("ISO-8859-1", "8859_1"); encodingIANADescriptionMap.put ("ISO-8859-1", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-1")); encodingIANAAliasesMap.put ("ISO-8859-1", "ISO-8859-1"); encodingIANAAliasesMap.put ("LATIN1", "ISO-8859-1"); encodingIANAAliasesMap.put ("L1", "ISO-8859-1"); encodingIANAAliasesMap.put ("IBM819", "ISO-8859-1"); encodingIANAAliasesMap.put ("CP819", "ISO-8859-1"); 275 encodingIANA2JavaMap.put ("ISO-8859-2", "8859_2"); encodingIANADescriptionMap.put ("ISO-8859-2", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-2")); encodingIANAAliasesMap.put ("ISO-8859-2", "ISO-8859-2"); encodingIANAAliasesMap.put ("LATIN2", "ISO-8859-2"); encodingIANAAliasesMap.put ("L2", "ISO-8859-2"); 281 encodingIANA2JavaMap.put ("ISO-8859-3", "8859_3"); encodingIANADescriptionMap.put ("ISO-8859-3", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-3")); encodingIANAAliasesMap.put ("ISO-8859-3", "ISO-8859-3"); encodingIANAAliasesMap.put ("LATIN3", "ISO-8859-3"); encodingIANAAliasesMap.put ("L3", "ISO-8859-3"); 287 encodingIANA2JavaMap.put ("ISO-8859-4", "8859_4"); encodingIANADescriptionMap.put ("ISO-8859-4", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-4")); encodingIANAAliasesMap.put ("ISO-8859-4", "ISO-8859-4"); encodingIANAAliasesMap.put ("LATIN4", "ISO-8859-4"); encodingIANAAliasesMap.put ("L4", "ISO-8859-4"); 293 encodingIANA2JavaMap.put ("ISO-8859-5", "8859_5"); encodingIANADescriptionMap.put ("ISO-8859-5", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-5")); encodingIANAAliasesMap.put ("ISO-8859-5", "ISO-8859-5"); encodingIANAAliasesMap.put ("CYRILLIC", "ISO-8859-5"); 298 encodingIANA2JavaMap.put ("ISO-8859-6", "8859_6"); encodingIANADescriptionMap.put ("ISO-8859-6", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-6")); encodingIANAAliasesMap.put ("ISO-8859-6", "ISO-8859-6"); 302 encodingIANA2JavaMap.put ("ISO-8859-7", "8859_7"); encodingIANADescriptionMap.put ("ISO-8859-7", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-7")); encodingIANAAliasesMap.put ("ISO-8859-7", "ISO-8859-7"); encodingIANAAliasesMap.put ("GREEK", "ISO-8859-7"); encodingIANAAliasesMap.put ("GREEK8", "ISO-8859-7"); 308 encodingIANA2JavaMap.put ("ISO-8859-8", "8859_8"); encodingIANADescriptionMap.put ("ISO-8859-8", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-8")); encodingIANAAliasesMap.put ("ISO-8859-8", "ISO-8859-8"); encodingIANAAliasesMap.put ("HEBREW", "ISO-8859-8"); 313 encodingIANA2JavaMap.put ("ISO-8859-9", "8859_9"); encodingIANADescriptionMap.put ("ISO-8859-9", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-9")); encodingIANAAliasesMap.put ("ISO-8859-9", "ISO-8859-9"); encodingIANAAliasesMap.put ("LATIN5", "ISO-8859-9"); encodingIANAAliasesMap.put ("L5", "ISO-8859-9"); 319 encodingIANA2JavaMap.put ("KOI8-R", "KOI8_R"); encodingIANADescriptionMap.put ("KOI8-R", NbBundle.getMessage(Convertors.class, "NAME_KOI8-R")); encodingIANAAliasesMap.put ("KOI8-R", "KOI8-R"); 323 encodingIANADescriptionMap.put ("US-ASCII", NbBundle.getMessage(Convertors.class, "NAME_ASCII")); encodingIANAAliasesMap.put ("ASCII", "US-ASCII"); encodingIANAAliasesMap.put ("US-ASCII", "US-ASCII"); encodingIANAAliasesMap.put ("ISO646-US", "US-ASCII"); encodingIANAAliasesMap.put ("IBM367", "US-ASCII"); encodingIANAAliasesMap.put ("CP367", "US-ASCII"); 330 encodingIANA2JavaMap.put ("UTF-8", "UTF8"); encodingIANADescriptionMap.put ("UTF-8", NbBundle.getMessage(Convertors.class, "NAME_UTF-8")); encodingIANAAliasesMap.put ("UTF-8", "UTF-8"); 334 encodingIANA2JavaMap.put ("UTF-16", "Unicode"); encodingIANADescriptionMap.put ("UTF-16", NbBundle.getMessage(Convertors.class, "NAME_UTF-16")); encodingIANAAliasesMap.put ("UTF-16", "UTF-16"); 338 339 Iterator <String > iter = encodingIANA2JavaMap.keySet().iterator(); 340 String 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"); } 348 349 350 352 public static Map getIANA2JavaMap () { 353 return encodingIANA2JavaMap; 354 } 355 356 public static Map getJava2IANAMap () { 357 return encodingJava2IANAMap; 358 } 359 } 360 } 361 | Popular Tags |