XMLStorage.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.upgrade;
  45. import java.awt.Color;
  46. import java.awt.Font;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.OutputStream;
  50. import java.io.OutputStreamWriter;
  51. import java.io.Writer;
  52. import java.util.ArrayList;
  53. import java.util.HashMap;
  54. import java.util.List;
  55. import java.util.Map;
  56. import org.openide.ErrorManager;
  57. import org.openide.filesystems.FileLock;
  58. import org.openide.filesystems.FileObject;
  59. import org.openide.util.RequestProcessor;
  60. import org.openide.xml.XMLUtil;
  61. import org.xml.sax.InputSource;
  62. import org.xml.sax.SAXException;
  63. import org.xml.sax.XMLReader;
  64. import org.xml.sax.helpers.DefaultHandler;
  65. public class XMLStorage {
  66. private static final Map<Color,String> colorToName = new HashMap<Color,String> ();
  67. private static final Map<String, Color> nameToColor = new HashMap<String, Color> ();
  68. private static final Map<String, Integer> nameToFontStyle = new HashMap<String, Integer> ();
  69. private static final Map<Integer, String> fontStyleToName = new HashMap<Integer, String> ();
  70. static {
  71. colorToName.put (Color.black, "black");
  72. nameToColor.put ("black", Color.black);
  73. colorToName.put (Color.blue, "blue");
  74. nameToColor.put ("blue", Color.blue);
  75. colorToName.put (Color.cyan, "cyan");
  76. nameToColor.put ("cyan", Color.cyan);
  77. colorToName.put (Color.darkGray, "darkGray");
  78. nameToColor.put ("darkGray", Color.darkGray);
  79. colorToName.put (Color.gray, "gray");
  80. nameToColor.put ("gray", Color.gray);
  81. colorToName.put (Color.green, "green");
  82. nameToColor.put ("green", Color.green);
  83. colorToName.put (Color.lightGray, "lightGray");
  84. nameToColor.put ("lightGray", Color.lightGray);
  85. colorToName.put (Color.magenta, "magenta");
  86. nameToColor.put ("magenta", Color.magenta);
  87. colorToName.put (Color.orange, "orange");
  88. nameToColor.put ("orange", Color.orange);
  89. colorToName.put (Color.pink, "pink");
  90. nameToColor.put ("pink", Color.pink);
  91. colorToName.put (Color.red, "red");
  92. nameToColor.put ("red", Color.red);
  93. colorToName.put (Color.white, "white");
  94. nameToColor.put ("white", Color.white);
  95. colorToName.put (Color.yellow, "yellow");
  96. nameToColor.put ("yellow", Color.yellow);
  97. nameToFontStyle.put ("plain", Integer.valueOf (Font.PLAIN));
  98. fontStyleToName.put (Integer.valueOf (Font.PLAIN), "plain");
  99. nameToFontStyle.put ("bold", Integer.valueOf (Font.BOLD));
  100. fontStyleToName.put (Integer.valueOf (Font.BOLD), "bold");
  101. nameToFontStyle.put ("italic", Integer.valueOf (Font.ITALIC));
  102. fontStyleToName.put (Integer.valueOf (Font.ITALIC), "italic");
  103. nameToFontStyle.put ("bold+italic", Integer.valueOf (Font.BOLD + Font.ITALIC));
  104. fontStyleToName.put (Integer.valueOf (Font.BOLD + Font.ITALIC), "bold+italic");
  105. }
  106. static String colorToString (Color color) {
  107. if (colorToName.containsKey (color))
  108. return (String) colorToName.get (color);
  109. return Integer.toHexString (color.getRGB ());
  110. }
  111. static Color stringToColor (String color) throws Exception {
  112. if (color.startsWith ("#"))
  113. color = color.substring (1);
  114. if (nameToColor.containsKey (color))
  115. return (Color) nameToColor.get (color);
  116. try {
  117. return new Color ((int) Long.parseLong (color, 16));
  118. } catch (NumberFormatException ex) {
  119. throw new Exception ();
  120. }
  121. }
  122. // generics support methods ................................................
  123. private static RequestProcessor requestProcessor = new RequestProcessor ("XMLStorage");
  124. static void save (final FileObject fo, final String content) {
  125. if (fo == null) throw new NullPointerException ();
  126. if (content == null) throw new NullPointerException ();
  127. requestProcessor.post (new Runnable () {
  128. public void run () {
  129. try {
  130. FileLock lock = fo.lock ();
  131. try {
  132. OutputStream os = fo.getOutputStream (lock);
  133. Writer writer = new OutputStreamWriter (os, "UTF-8"); // NOI18N
  134. try {
  135. writer.write (content);
  136. } finally {
  137. writer.close ();
  138. }
  139. } finally {
  140. lock.releaseLock ();
  141. }
  142. } catch (IOException ex) {
  143. ErrorManager.getDefault ().notify (ex);
  144. }
  145. }
  146. });
  147. }
  148. static Object load (InputStream is, String name, Handler handler) {
  149. try {
  150. try {
  151. XMLReader reader = XMLUtil.createXMLReader ();
  152. reader.setEntityResolver (handler);
  153. reader.setContentHandler (handler);
  154. reader.parse (new InputSource (is));
  155. return handler.getResult ();
  156. } finally {
  157. is.close ();
  158. }
  159. } catch (SAXException ex) {
  160. if (System.getProperty ("org.netbeans.optionsDialog") != null) {
  161. System.out.println("File: " + name);
  162. ex.printStackTrace ();
  163. }
  164. return handler.getResult ();
  165. } catch (IOException ex) {
  166. if (System.getProperty ("org.netbeans.optionsDialog") != null) {
  167. System.out.println("File: " + name);
  168. ex.printStackTrace ();
  169. }
  170. return handler.getResult ();
  171. } catch (Exception ex) {
  172. if (System.getProperty ("org.netbeans.optionsDialog") != null) {
  173. System.out.println("File: " + name);
  174. ex.printStackTrace ();
  175. }
  176. return handler.getResult ();
  177. }
  178. }
  179. static StringBuffer generateHeader () {
  180. StringBuffer sb = new StringBuffer ();
  181. sb.append ("<?xml version=\"1.0\"?>\n\n");
  182. return sb;
  183. }
  184. static void generateFolderStart (
  185. StringBuffer sb,
  186. String name,
  187. Attribs attributes,
  188. String indentation
  189. ) {
  190. sb.append (indentation).append ('<').append (name);
  191. if (attributes != null) {
  192. if (!attributes.oneLine) sb.append ('\n');
  193. else sb.append (' ');
  194. generateAttributes (sb, attributes, indentation + " ");
  195. if (!attributes.oneLine) sb.append (indentation);
  196. sb.append (">\n");
  197. } else
  198. sb.append (">\n");
  199. }
  200. static void generateFolderEnd (StringBuffer sb, String name, String indentation) {
  201. sb.append (indentation).append ("</").append (name).append (">\n");
  202. }
  203. static void generateLeaf (
  204. StringBuffer sb,
  205. String name,
  206. Attribs attributes,
  207. String indentation
  208. ) {
  209. sb.append (indentation).append ('<').append (name);
  210. if (attributes != null) {
  211. if (!attributes.oneLine) sb.append ('\n');
  212. else sb.append (' ');
  213. generateAttributes (sb, attributes, indentation + " ");
  214. if (!attributes.oneLine) sb.append (indentation);
  215. sb.append ("/>\n");
  216. } else
  217. sb.append ("/>\n");
  218. }
  219. private static void generateAttributes (
  220. StringBuffer sb,
  221. Attribs attributes,
  222. String indentation
  223. ) {
  224. if (attributes == null) return;
  225. int i, k = attributes.names.size ();
  226. for (i = 0; i < k; i++) {
  227. if (!attributes.oneLine)
  228. sb.append (indentation);
  229. sb.append (attributes.names.get (i)).append ("=\"").
  230. append (attributes.values.get (i)).append ('\"');
  231. if (!attributes.oneLine)
  232. sb.append ("\n");
  233. else
  234. if (i < (k - 1))
  235. sb.append (' ');
  236. }
  237. }
  238. static class Handler extends DefaultHandler {
  239. private Object result;
  240. void setResult (Object result) {
  241. this.result = result;
  242. }
  243. Object getResult () {
  244. return result;
  245. }
  246. }
  247. static class Attribs {
  248. private List<String> names = new ArrayList<String> ();
  249. private List<String> values = new ArrayList<String> ();
  250. private boolean oneLine;
  251. Attribs (boolean oneLine) {
  252. this.oneLine = oneLine;
  253. }
  254. void add (String name, String value) {
  255. int i = names.indexOf (name);
  256. if (i >= 0) {
  257. names.remove (i);
  258. values.remove (i);
  259. } names.add (name);
  260. values.add (value);
  261. }
  262. void clear () {
  263. names.clear ();
  264. values.clear ();
  265. }
  266. }
  267. }