KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > upgrade > XMLStorage


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.upgrade;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import org.openide.ErrorManager;
34 import org.openide.filesystems.FileLock;
35
36 import org.openide.filesystems.FileObject;
37 import org.openide.util.RequestProcessor;
38 import org.openide.xml.XMLUtil;
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.XMLReader JavaDoc;
42 import org.xml.sax.helpers.DefaultHandler JavaDoc;
43
44
45 public class XMLStorage {
46
47     private static final Map JavaDoc<Color JavaDoc,String JavaDoc> colorToName = new HashMap JavaDoc<Color JavaDoc,String JavaDoc> ();
48     private static final Map JavaDoc<String JavaDoc, Color JavaDoc> nameToColor = new HashMap JavaDoc<String JavaDoc, Color JavaDoc> ();
49     private static final Map JavaDoc<String JavaDoc, Integer JavaDoc> nameToFontStyle = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc> ();
50     private static final Map JavaDoc<Integer JavaDoc, String JavaDoc> fontStyleToName = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc> ();
51     static {
52         colorToName.put (Color.black, "black");
53         nameToColor.put ("black", Color.black);
54         colorToName.put (Color.blue, "blue");
55         nameToColor.put ("blue", Color.blue);
56         colorToName.put (Color.cyan, "cyan");
57         nameToColor.put ("cyan", Color.cyan);
58         colorToName.put (Color.darkGray, "darkGray");
59         nameToColor.put ("darkGray", Color.darkGray);
60         colorToName.put (Color.gray, "gray");
61         nameToColor.put ("gray", Color.gray);
62         colorToName.put (Color.green, "green");
63         nameToColor.put ("green", Color.green);
64         colorToName.put (Color.lightGray, "lightGray");
65         nameToColor.put ("lightGray", Color.lightGray);
66         colorToName.put (Color.magenta, "magenta");
67         nameToColor.put ("magenta", Color.magenta);
68         colorToName.put (Color.orange, "orange");
69         nameToColor.put ("orange", Color.orange);
70         colorToName.put (Color.pink, "pink");
71         nameToColor.put ("pink", Color.pink);
72         colorToName.put (Color.red, "red");
73         nameToColor.put ("red", Color.red);
74         colorToName.put (Color.white, "white");
75         nameToColor.put ("white", Color.white);
76         colorToName.put (Color.yellow, "yellow");
77         nameToColor.put ("yellow", Color.yellow);
78         
79         nameToFontStyle.put ("plain", Integer.valueOf (Font.PLAIN));
80         fontStyleToName.put (Integer.valueOf (Font.PLAIN), "plain");
81         nameToFontStyle.put ("bold", Integer.valueOf (Font.BOLD));
82         fontStyleToName.put (Integer.valueOf (Font.BOLD), "bold");
83         nameToFontStyle.put ("italic", Integer.valueOf (Font.ITALIC));
84         fontStyleToName.put (Integer.valueOf (Font.ITALIC), "italic");
85         nameToFontStyle.put ("bold+italic", Integer.valueOf (Font.BOLD + Font.ITALIC));
86         fontStyleToName.put (Integer.valueOf (Font.BOLD + Font.ITALIC), "bold+italic");
87     }
88     
89     static String JavaDoc colorToString (Color JavaDoc color) {
90     if (colorToName.containsKey (color))
91         return (String JavaDoc) colorToName.get (color);
92     return Integer.toHexString (color.getRGB ());
93     }
94     
95     static Color JavaDoc stringToColor (String JavaDoc color) throws Exception JavaDoc {
96         if (color.startsWith ("#"))
97             color = color.substring (1);
98     if (nameToColor.containsKey (color))
99         return (Color JavaDoc) nameToColor.get (color);
100         try {
101             return new Color JavaDoc ((int) Long.parseLong (color, 16));
102         } catch (NumberFormatException JavaDoc ex) {
103             throw new Exception JavaDoc ();
104         }
105     }
106     
107     
108     // generics support methods ................................................
109

110     private static RequestProcessor requestProcessor = new RequestProcessor ("XMLStorage");
111     
112     static void save (final FileObject fo, final String JavaDoc content) {
113         if (fo == null) throw new NullPointerException JavaDoc ();
114         if (content == null) throw new NullPointerException JavaDoc ();
115         requestProcessor.post (new Runnable JavaDoc () {
116             public void run () {
117                 try {
118                     FileLock lock = fo.lock ();
119                     try {
120                         OutputStream JavaDoc os = fo.getOutputStream (lock);
121                         Writer JavaDoc writer = new OutputStreamWriter JavaDoc (os, "UTF-8"); // NOI18N
122
try {
123                             writer.write (content);
124                         } finally {
125                             writer.close ();
126                         }
127                     } finally {
128                         lock.releaseLock ();
129                     }
130                 } catch (IOException JavaDoc ex) {
131                     ErrorManager.getDefault ().notify (ex);
132                 }
133             }
134         });
135     }
136     
137     static Object JavaDoc load (InputStream JavaDoc is, String JavaDoc name, Handler JavaDoc handler) {
138         try {
139             try {
140                 XMLReader JavaDoc reader = XMLUtil.createXMLReader ();
141                 reader.setEntityResolver (handler);
142                 reader.setContentHandler (handler);
143                 reader.parse (new InputSource JavaDoc (is));
144                 return handler.getResult ();
145             } finally {
146                 is.close ();
147             }
148         } catch (SAXException JavaDoc ex) {
149         if (System.getProperty ("org.netbeans.optionsDialog") != null) {
150                 System.out.println("File: " + name);
151                 ex.printStackTrace ();
152             }
153             return handler.getResult ();
154         } catch (IOException JavaDoc ex) {
155         if (System.getProperty ("org.netbeans.optionsDialog") != null) {
156                 System.out.println("File: " + name);
157                 ex.printStackTrace ();
158             }
159             return handler.getResult ();
160     } catch (Exception JavaDoc ex) {
161         if (System.getProperty ("org.netbeans.optionsDialog") != null) {
162                 System.out.println("File: " + name);
163                 ex.printStackTrace ();
164             }
165             return handler.getResult ();
166         }
167     }
168     
169     static StringBuffer JavaDoc generateHeader () {
170         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
171         sb.append ("<?xml version=\"1.0\"?>\n\n");
172         return sb;
173     }
174     
175     static void generateFolderStart (
176         StringBuffer JavaDoc sb,
177         String JavaDoc name,
178         Attribs attributes,
179         String JavaDoc indentation
180     ) {
181         sb.append (indentation).append ('<').append (name);
182         if (attributes != null) {
183             if (!attributes.oneLine) sb.append ('\n');
184             else sb.append (' ');
185             generateAttributes (sb, attributes, indentation + " ");
186             if (!attributes.oneLine) sb.append (indentation);
187             sb.append (">\n");
188         } else
189             sb.append (">\n");
190     }
191     
192     static void generateFolderEnd (StringBuffer JavaDoc sb, String JavaDoc name, String JavaDoc indentation) {
193         sb.append (indentation).append ("</").append (name).append (">\n");
194     }
195     
196     static void generateLeaf (
197         StringBuffer JavaDoc sb,
198         String JavaDoc name,
199         Attribs attributes,
200         String JavaDoc indentation
201     ) {
202         sb.append (indentation).append ('<').append (name);
203         if (attributes != null) {
204             if (!attributes.oneLine) sb.append ('\n');
205             else sb.append (' ');
206             generateAttributes (sb, attributes, indentation + " ");
207             if (!attributes.oneLine) sb.append (indentation);
208             sb.append ("/>\n");
209         } else
210             sb.append ("/>\n");
211     }
212     
213     private static void generateAttributes (
214         StringBuffer JavaDoc sb,
215         Attribs attributes,
216         String JavaDoc indentation
217     ) {
218         if (attributes == null) return;
219         int i, k = attributes.names.size ();
220         for (i = 0; i < k; i++) {
221             if (!attributes.oneLine)
222                 sb.append (indentation);
223             sb.append (attributes.names.get (i)).append ("=\"").
224                 append (attributes.values.get (i)).append ('\"');
225             if (!attributes.oneLine)
226                 sb.append ("\n");
227             else
228             if (i < (k - 1))
229                 sb.append (' ');
230         }
231     }
232     
233     static class Handler extends DefaultHandler JavaDoc {
234         private Object JavaDoc result;
235         void setResult (Object JavaDoc result) {
236             this.result = result;
237         }
238         Object JavaDoc getResult () {
239             return result;
240         }
241     }
242     
243     static class Attribs {
244         private List JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc> ();
245         private List JavaDoc<String JavaDoc> values = new ArrayList JavaDoc<String JavaDoc> ();
246         private boolean oneLine;
247         
248         Attribs (boolean oneLine) {
249             this.oneLine = oneLine;
250         }
251         
252         void add (String JavaDoc name, String JavaDoc value) {
253             int i = names.indexOf (name);
254             if (i >= 0) {
255                 names.remove (i);
256                 values.remove (i);
257             } names.add (name);
258             values.add (value);
259         }
260         
261         void clear () {
262             names.clear ();
263             values.clear ();
264         }
265     }
266 }
267
Popular Tags