PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/xml.xdm/src/org/netbeans/modules/xml/xdm/nodes/Convertors.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 385 lines | 235 code | 68 blank | 82 comment | 14 complexity | 6680f0d9b4c6bb13b4b1adb8541fcd5b MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.xml.xdm.nodes;
  45. import java.io.Reader;
  46. import java.io.StringReader;
  47. import java.io.BufferedReader;
  48. import java.io.IOException;
  49. import java.io.EOFException;
  50. import java.util.Iterator;
  51. import javax.swing.text.Document;
  52. import org.xml.sax.InputSource;
  53. import org.openide.ErrorManager;
  54. import org.openide.util.Lookup;
  55. import java.net.URL;
  56. import java.util.Map;
  57. import java.util.TreeMap;
  58. import org.openide.filesystems.FileObject;
  59. import org.openide.loaders.DataObject;
  60. import org.openide.util.NbBundle;
  61. /**
  62. * Set of static methods converting misc data representations.
  63. *
  64. * @author Petr Kuzel
  65. * @version 0.9
  66. */
  67. public final class Convertors {
  68. /**
  69. * Swings document property added by this support.
  70. */
  71. public static final String PROP_DOCUMENT_URL = "doc-url";
  72. /**
  73. * @return current state of Document as string
  74. */
  75. public static String documentToString(final Document doc) {
  76. if (doc == null) throw new NullPointerException();
  77. final String[] str = new String[1];
  78. // safely take the text from the document
  79. Runnable run = new Runnable() {
  80. public void run () {
  81. try {
  82. str[0] = doc.getText(0, doc.getLength());
  83. } catch (javax.swing.text.BadLocationException e) {
  84. // impossible
  85. e.printStackTrace();
  86. }
  87. }
  88. };
  89. doc.render(run);
  90. return str[0];
  91. }
  92. /**
  93. * @return InputSource, a callie SHOULD set systemId if available
  94. */
  95. public static InputSource documentToInputSource(Document doc) {
  96. if (doc == null) throw new NullPointerException();
  97. String text = documentToString(doc);
  98. Reader reader = new StringReader(text);
  99. // our specifics property
  100. String system = (String) doc.getProperty(PROP_DOCUMENT_URL);
  101. // try Swing general property
  102. if (system == null) {
  103. Object obj = doc.getProperty(Document.StreamDescriptionProperty);
  104. if (obj instanceof DataObject) {
  105. try {
  106. DataObject dobj = (DataObject) obj;
  107. FileObject fo = dobj.getPrimaryFile();
  108. URL url = fo.getURL();
  109. system = url.toExternalForm();
  110. } catch (IOException io) {
  111. ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class);
  112. emgr.notify(io);
  113. }
  114. } else {
  115. ErrorManager emgr = (ErrorManager) Lookup.getDefault().lookup(ErrorManager.class);
  116. emgr.log("XML:Convertors:Unknown stream description:" + obj);
  117. }
  118. }
  119. // set something, some parsers are nervous if no system id
  120. if (system == null) {
  121. system = "XML/Core/Convertors/documentToInputSource()"; //NOI18N
  122. }
  123. InputSource in = new InputSource(system); // NOI18N
  124. in.setCharacterStream(reader);
  125. return in;
  126. }
  127. /**
  128. * Wrap reader into buffered one and start reading returning
  129. * String as a EOF is reached.
  130. */
  131. public static String readerToString(Reader reader) throws IOException {
  132. BufferedReader fastReader = new BufferedReader(reader);
  133. StringBuffer buf = new StringBuffer(1024);
  134. try {
  135. for (int i = fastReader.read(); i >= 0; i = fastReader.read()) {
  136. buf.append((char)i);
  137. }
  138. } catch (EOFException eof) {
  139. //expected
  140. }
  141. return buf.toString();
  142. }
  143. /**
  144. */
  145. public static final String iana2java (String iana) {
  146. String java = (String) Convertors.EncodingUtil.getIANA2JavaMap ().get (iana.toUpperCase ());
  147. return java == null ? iana : java;
  148. }
  149. public static final String java2iana (String java) {
  150. String iana = (String) Convertors.EncodingUtil.getJava2IANAMap ().get (java);
  151. return iana == null ? java : iana;
  152. }
  153. //!!! this code is copy pasted from TAX library TreeUtilities
  154. /**
  155. *
  156. */
  157. static class EncodingUtil {
  158. /** IANA to Java encoding mappings */
  159. protected final static Map<String, String> encodingIANA2JavaMap = new TreeMap<String, String>();
  160. /** */
  161. protected final static Map<String, String> encodingIANADescriptionMap = new TreeMap<String, String>();
  162. /** */
  163. protected final static Map<String, String> encodingIANAAliasesMap = new TreeMap<String, String>();
  164. protected final static Map<String, String> encodingJava2IANAMap = new TreeMap<String, String>();
  165. //
  166. // Static initialization
  167. //
  168. static {
  169. encodingIANA2JavaMap.put ("BIG5", "Big5"); // NOI18N
  170. encodingIANADescriptionMap.put ("BIG5", NbBundle.getMessage(Convertors.class, "NAME_BIG5")); // NOI18N
  171. encodingIANAAliasesMap.put ("BIG5", "BIG5"); // NOI18N
  172. encodingIANA2JavaMap.put ("IBM037", "CP037"); // NOI18N
  173. encodingIANADescriptionMap.put ("IBM037", NbBundle.getMessage(Convertors.class, "NAME_IBM037")); // NOI18N
  174. encodingIANAAliasesMap.put ("IBM037", "IBM037"); // NOI18N
  175. encodingIANAAliasesMap.put ("EBCDIC-CP-US", "IBM037"); // NOI18N
  176. encodingIANAAliasesMap.put ("EBCDIC-CP-CA", "IBM037"); // NOI18N
  177. encodingIANAAliasesMap.put ("EBCDIC-CP-NL", "IBM037"); // NOI18N
  178. encodingIANAAliasesMap.put ("EBCDIC-CP-WT", "IBM037"); // NOI18N
  179. encodingIANA2JavaMap.put ("IBM277", "CP277"); // NOI18N
  180. encodingIANADescriptionMap.put ("IBM277", NbBundle.getMessage(Convertors.class, "NAME_IBM277")); // NOI18N
  181. encodingIANAAliasesMap.put ("IBM277", "IBM277"); // NOI18N
  182. encodingIANAAliasesMap.put ("EBCDIC-CP-DK", "IBM277"); // NOI18N
  183. encodingIANAAliasesMap.put ("EBCDIC-CP-NO", "IBM277"); // NOI18N
  184. encodingIANA2JavaMap.put ("IBM278", "CP278"); // NOI18N
  185. encodingIANADescriptionMap.put ("IBM278", NbBundle.getMessage(Convertors.class, "NAME_IBM277")); // NOI18N
  186. encodingIANAAliasesMap.put ("IBM278", "IBM278"); // NOI18N
  187. encodingIANAAliasesMap.put ("EBCDIC-CP-FI", "IBM278"); // NOI18N
  188. encodingIANAAliasesMap.put ("EBCDIC-CP-SE", "IBM278"); // NOI18N
  189. encodingIANA2JavaMap.put ("IBM280", "CP280"); // NOI18N
  190. encodingIANADescriptionMap.put ("IBM280", NbBundle.getMessage(Convertors.class, "NAME_IBM280")); // NOI18N
  191. encodingIANAAliasesMap.put ("IBM280", "IBM280"); // NOI18N
  192. encodingIANAAliasesMap.put ("EBCDIC-CP-IT", "IBM280"); // NOI18N
  193. encodingIANA2JavaMap.put ("IBM284", "CP284"); // NOI18N
  194. encodingIANADescriptionMap.put ("IBM284", NbBundle.getMessage(Convertors.class, "NAME_IBM284")); // NOI18N
  195. encodingIANAAliasesMap.put ("IBM284", "IBM284"); // NOI18N
  196. encodingIANAAliasesMap.put ("EBCDIC-CP-ES", "IBM284"); // NOI18N
  197. encodingIANA2JavaMap.put ("IBM285", "CP285"); // NOI18N
  198. encodingIANADescriptionMap.put ("IBM285", NbBundle.getMessage(Convertors.class, "NAME_IBM285")); // NOI18N
  199. encodingIANAAliasesMap.put ("IBM285", "IBM285"); // NOI18N
  200. encodingIANAAliasesMap.put ("EBCDIC-CP-GB", "IBM285"); // NOI18N
  201. encodingIANA2JavaMap.put ("IBM297", "CP297"); // NOI18N
  202. encodingIANADescriptionMap.put ("IBM297", NbBundle.getMessage(Convertors.class, "NAME_IBM297")); // NOI18N
  203. encodingIANAAliasesMap.put ("IBM297", "IBM297"); // NOI18N
  204. encodingIANAAliasesMap.put ("EBCDIC-CP-FR", "IBM297"); // NOI18N
  205. encodingIANA2JavaMap.put ("IBM424", "CP424"); // NOI18N
  206. encodingIANADescriptionMap.put ("IBM424", NbBundle.getMessage(Convertors.class, "NAME_IBM424")); // NOI18N
  207. encodingIANAAliasesMap.put ("IBM424", "IBM424"); // NOI18N
  208. encodingIANAAliasesMap.put ("EBCDIC-CP-HE", "IBM424"); // NOI18N
  209. encodingIANA2JavaMap.put ("IBM500", "CP500"); // NOI18N
  210. encodingIANADescriptionMap.put ("IBM500", NbBundle.getMessage(Convertors.class, "NAME_IBM500")); // NOI18N
  211. encodingIANAAliasesMap.put ("IBM500", "IBM500"); // NOI18N
  212. encodingIANAAliasesMap.put ("EBCDIC-CP-CH", "IBM500"); // NOI18N
  213. encodingIANAAliasesMap.put ("EBCDIC-CP-BE", "IBM500"); // NOI18N
  214. encodingIANA2JavaMap.put ("IBM870", "CP870"); // NOI18N
  215. encodingIANADescriptionMap.put ("IBM870", NbBundle.getMessage(Convertors.class, "NAME_IBM870")); // NOI18N
  216. encodingIANAAliasesMap.put ("IBM870", "IBM870"); // NOI18N
  217. encodingIANAAliasesMap.put ("EBCDIC-CP-ROECE", "IBM870"); // NOI18N
  218. encodingIANAAliasesMap.put ("EBCDIC-CP-YU", "IBM870"); // NOI18N
  219. encodingIANA2JavaMap.put ("IBM871", "CP871"); // NOI18N
  220. encodingIANADescriptionMap.put ("IBM871", NbBundle.getMessage(Convertors.class, "NAME_IBM871")); // NOI18N
  221. encodingIANAAliasesMap.put ("IBM871", "IBM871"); // NOI18N
  222. encodingIANAAliasesMap.put ("EBCDIC-CP-IS", "IBM871"); // NOI18N
  223. encodingIANA2JavaMap.put ("IBM918", "CP918"); // NOI18N
  224. encodingIANADescriptionMap.put ("IBM918", NbBundle.getMessage(Convertors.class, "NAME_IBM918")); // NOI18N
  225. encodingIANAAliasesMap.put ("IBM918", "IBM918"); // NOI18N
  226. encodingIANAAliasesMap.put ("EBCDIC-CP-AR2", "IBM918"); // NOI18N
  227. encodingIANA2JavaMap.put ("EUC-JP", "EUCJIS"); // NOI18N
  228. encodingIANADescriptionMap.put ("EUC-JP", NbBundle.getMessage(Convertors.class, "NAME_EUC-JP")); // NOI18N
  229. encodingIANAAliasesMap.put ("EUC-JP", "EUC-JP"); // NOI18N
  230. encodingIANA2JavaMap.put ("EUC-KR", "KSC5601"); // NOI18N
  231. encodingIANADescriptionMap.put ("EUC-KR", NbBundle.getMessage(Convertors.class, "NAME_EUC-KR")); // NOI18N
  232. encodingIANAAliasesMap.put ("EUC-KR", "EUC-KR"); // NOI18N
  233. encodingIANA2JavaMap.put ("GB2312", "GB2312"); // NOI18N
  234. encodingIANADescriptionMap.put ("GB2312", NbBundle.getMessage(Convertors.class, "NAME_GB2312")); // NOI18N
  235. encodingIANAAliasesMap.put ("GB2312", "GB2312"); // NOI18N
  236. encodingIANA2JavaMap.put ("ISO-2022-JP", "JIS"); // NOI18N
  237. encodingIANADescriptionMap.put ("ISO-2022-JP", NbBundle.getMessage(Convertors.class, "NAME_ISO-2022-JP")); // NOI18N
  238. encodingIANAAliasesMap.put ("ISO-2022-JP", "ISO-2022-JP"); // NOI18N
  239. encodingIANA2JavaMap.put ("ISO-2022-KR", "ISO2022KR"); // NOI18N
  240. encodingIANADescriptionMap.put ("ISO-2022-KR", NbBundle.getMessage(Convertors.class, "NAME_ISO-2022-KR")); // NOI18N
  241. encodingIANAAliasesMap.put ("ISO-2022-KR", "ISO-2022-KR"); // NOI18N
  242. encodingIANA2JavaMap.put ("ISO-8859-1", "8859_1"); // NOI18N
  243. encodingIANADescriptionMap.put ("ISO-8859-1", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-1")); // NOI18N
  244. encodingIANAAliasesMap.put ("ISO-8859-1", "ISO-8859-1"); // NOI18N
  245. encodingIANAAliasesMap.put ("LATIN1", "ISO-8859-1"); // NOI18N
  246. encodingIANAAliasesMap.put ("L1", "ISO-8859-1"); // NOI18N
  247. encodingIANAAliasesMap.put ("IBM819", "ISO-8859-1"); // NOI18N
  248. encodingIANAAliasesMap.put ("CP819", "ISO-8859-1"); // NOI18N
  249. encodingIANA2JavaMap.put ("ISO-8859-2", "8859_2"); // NOI18N
  250. encodingIANADescriptionMap.put ("ISO-8859-2", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-2")); // NOI18N
  251. encodingIANAAliasesMap.put ("ISO-8859-2", "ISO-8859-2"); // NOI18N
  252. encodingIANAAliasesMap.put ("LATIN2", "ISO-8859-2"); // NOI18N
  253. encodingIANAAliasesMap.put ("L2", "ISO-8859-2"); // NOI18N
  254. encodingIANA2JavaMap.put ("ISO-8859-3", "8859_3"); // NOI18N
  255. encodingIANADescriptionMap.put ("ISO-8859-3", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-3")); // NOI18N
  256. encodingIANAAliasesMap.put ("ISO-8859-3", "ISO-8859-3"); // NOI18N
  257. encodingIANAAliasesMap.put ("LATIN3", "ISO-8859-3"); // NOI18N
  258. encodingIANAAliasesMap.put ("L3", "ISO-8859-3"); // NOI18N
  259. encodingIANA2JavaMap.put ("ISO-8859-4", "8859_4"); // NOI18N
  260. encodingIANADescriptionMap.put ("ISO-8859-4", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-4")); // NOI18N
  261. encodingIANAAliasesMap.put ("ISO-8859-4", "ISO-8859-4"); // NOI18N
  262. encodingIANAAliasesMap.put ("LATIN4", "ISO-8859-4"); // NOI18N
  263. encodingIANAAliasesMap.put ("L4", "ISO-8859-4"); // NOI18N
  264. encodingIANA2JavaMap.put ("ISO-8859-5", "8859_5"); // NOI18N
  265. encodingIANADescriptionMap.put ("ISO-8859-5", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-5")); // NOI18N
  266. encodingIANAAliasesMap.put ("ISO-8859-5", "ISO-8859-5"); // NOI18N
  267. encodingIANAAliasesMap.put ("CYRILLIC", "ISO-8859-5"); // NOI18N
  268. encodingIANA2JavaMap.put ("ISO-8859-6", "8859_6"); // NOI18N
  269. encodingIANADescriptionMap.put ("ISO-8859-6", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-6")); // NOI18N
  270. encodingIANAAliasesMap.put ("ISO-8859-6", "ISO-8859-6"); // NOI18N
  271. encodingIANA2JavaMap.put ("ISO-8859-7", "8859_7"); // NOI18N
  272. encodingIANADescriptionMap.put ("ISO-8859-7", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-7")); // NOI18N
  273. encodingIANAAliasesMap.put ("ISO-8859-7", "ISO-8859-7"); // NOI18N
  274. encodingIANAAliasesMap.put ("GREEK", "ISO-8859-7"); // NOI18N
  275. encodingIANAAliasesMap.put ("GREEK8", "ISO-8859-7"); // NOI18N
  276. encodingIANA2JavaMap.put ("ISO-8859-8", "8859_8"); // NOI18N
  277. encodingIANADescriptionMap.put ("ISO-8859-8", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-8")); // NOI18N
  278. encodingIANAAliasesMap.put ("ISO-8859-8", "ISO-8859-8"); // NOI18N
  279. encodingIANAAliasesMap.put ("HEBREW", "ISO-8859-8"); // NOI18N
  280. encodingIANA2JavaMap.put ("ISO-8859-9", "8859_9"); // NOI18N
  281. encodingIANADescriptionMap.put ("ISO-8859-9", NbBundle.getMessage(Convertors.class, "NAME_ISO-8859-9")); // NOI18N
  282. encodingIANAAliasesMap.put ("ISO-8859-9", "ISO-8859-9"); // NOI18N
  283. encodingIANAAliasesMap.put ("LATIN5", "ISO-8859-9"); // NOI18N
  284. encodingIANAAliasesMap.put ("L5", "ISO-8859-9"); // NOI18N
  285. encodingIANA2JavaMap.put ("KOI8-R", "KOI8_R"); // NOI18N
  286. encodingIANADescriptionMap.put ("KOI8-R", NbBundle.getMessage(Convertors.class, "NAME_KOI8-R")); // NOI18N
  287. encodingIANAAliasesMap.put ("KOI8-R", "KOI8-R"); // NOI18N
  288. encodingIANADescriptionMap.put ("US-ASCII", NbBundle.getMessage(Convertors.class, "NAME_ASCII")); // NOI18N
  289. encodingIANAAliasesMap.put ("ASCII", "US-ASCII"); // NOI18N
  290. encodingIANAAliasesMap.put ("US-ASCII", "US-ASCII"); // NOI18N
  291. encodingIANAAliasesMap.put ("ISO646-US", "US-ASCII"); // NOI18N
  292. encodingIANAAliasesMap.put ("IBM367", "US-ASCII"); // NOI18N
  293. encodingIANAAliasesMap.put ("CP367", "US-ASCII"); // NOI18N
  294. encodingIANA2JavaMap.put ("UTF-8", "UTF8"); // NOI18N
  295. encodingIANADescriptionMap.put ("UTF-8", NbBundle.getMessage(Convertors.class, "NAME_UTF-8")); // NOI18N
  296. encodingIANAAliasesMap.put ("UTF-8", "UTF-8"); // NOI18N
  297. encodingIANA2JavaMap.put ("UTF-16", "Unicode"); // NOI18N
  298. encodingIANADescriptionMap.put ("UTF-16", NbBundle.getMessage(Convertors.class, "NAME_UTF-16")); // NOI18N
  299. encodingIANAAliasesMap.put ("UTF-16", "UTF-16"); // NOI18N
  300. Iterator<String> iter = encodingIANA2JavaMap.keySet().iterator();
  301. String key;
  302. while (iter.hasNext()){
  303. key = iter.next();
  304. encodingJava2IANAMap.put(encodingIANA2JavaMap.get(key), key);
  305. }
  306. encodingIANA2JavaMap.put ("US-ASCII", "8859_1"); // NOI18N
  307. }
  308. /**
  309. */
  310. public static Map getIANA2JavaMap () {
  311. return encodingIANA2JavaMap;
  312. }
  313. public static Map getJava2IANAMap () {
  314. return encodingJava2IANAMap;
  315. }
  316. }
  317. }