KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > keymap > 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.modules.options.keymap;
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 colorToName = new HashMap JavaDoc ();
48     private static final Map JavaDoc nameToColor = new HashMap JavaDoc ();
49     private static final Map JavaDoc nameToFontStyle = new HashMap JavaDoc ();
50     private static final Map JavaDoc fontStyleToName = new HashMap 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", new Integer JavaDoc (Font.PLAIN));
80         fontStyleToName.put (new Integer JavaDoc (Font.PLAIN), "plain");
81         nameToFontStyle.put ("bold", new Integer JavaDoc (Font.BOLD));
82         fontStyleToName.put (new Integer JavaDoc (Font.BOLD), "bold");
83         nameToFontStyle.put ("italic", new Integer JavaDoc (Font.ITALIC));
84         fontStyleToName.put (new Integer JavaDoc (Font.ITALIC), "italic");
85         nameToFontStyle.put ("bold+italic", new Integer JavaDoc (Font.BOLD + Font.ITALIC));
86         fontStyleToName.put (new Integer JavaDoc (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) {
96     if (nameToColor.containsKey (color))
97         return (Color JavaDoc) nameToColor.get (color);
98     return new Color JavaDoc ((int) Long.parseLong (color, 16));
99     }
100     
101     
102     // generics support methods ................................................
103

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