2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.options.keymap;
44
import java.awt.Color;
46
import java.io.IOException;
47
import java.io.InputStream;
48
import java.io.OutputStream;
49
import java.io.OutputStreamWriter;
50
import java.io.Writer;
51
import java.util.ArrayList;
52
import java.util.HashMap;
53
import java.util.List;
55
import org.openide.ErrorManager;
56
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;
67
public class XMLStorage {
69
private static final Map<Color, String> colorToName = new HashMap<Color, String> ();
70
private static final Map<String, Color> nameToColor = new HashMap<String, Color> ();
71
private static final Map<String, Integer> nameToFontStyle = new HashMap<String, Integer> ();
72
private static final Map<Integer, String> fontStyleToName = new HashMap<Integer, String> ();
74
colorToName.put (Color.black, "black");
75
nameToColor.put ("black", Color.black);
76
colorToName.put (Color.blue, "blue");
77
nameToColor.put ("blue", Color.blue);
78
colorToName.put (Color.cyan, "cyan");
79
nameToColor.put ("cyan", Color.cyan);
80
colorToName.put (Color.darkGray, "darkGray");
81
nameToColor.put ("darkGray", Color.darkGray);
82
colorToName.put (Color.gray, "gray");
83
nameToColor.put ("gray", Color.gray);
84
colorToName.put (Color.green, "green");
85
nameToColor.put ("green", Color.green);
86
colorToName.put (Color.lightGray, "lightGray");
87
nameToColor.put ("lightGray", Color.lightGray);
88
colorToName.put (Color.magenta, "magenta");
89
nameToColor.put ("magenta", Color.magenta);
90
colorToName.put (Color.orange, "orange");
91
nameToColor.put ("orange", Color.orange);
92
colorToName.put (Color.pink, "pink");
93
nameToColor.put ("pink", Color.pink);
94
colorToName.put (Color.red, "red");
95
nameToColor.put ("red", Color.red);
96
colorToName.put (Color.white, "white");
97
nameToColor.put ("white", Color.white);
98
colorToName.put (Color.yellow, "yellow");
99
nameToColor.put ("yellow", Color.yellow);
101
nameToFontStyle.put ("plain", Integer.valueOf (Font.PLAIN));
102
fontStyleToName.put (Integer.valueOf (Font.PLAIN), "plain");
103
nameToFontStyle.put ("bold", Integer.valueOf (Font.BOLD));
104
fontStyleToName.put (Integer.valueOf (Font.BOLD), "bold");
105
nameToFontStyle.put ("italic", Integer.valueOf (Font.ITALIC));
106
fontStyleToName.put (Integer.valueOf (Font.ITALIC), "italic");
107
nameToFontStyle.put ("bold+italic", Integer.valueOf (Font.BOLD + Font.ITALIC));
108
fontStyleToName.put (Integer.valueOf (Font.BOLD + Font.ITALIC), "bold+italic");
111
static String colorToString (Color color) {
112
if (colorToName.containsKey (color))
113
return (String) colorToName.get (color);
114
return Integer.toHexString (color.getRGB ());
117
static Color stringToColor (String color) {
118
if (nameToColor.containsKey (color))
119
return (Color) nameToColor.get (color);
120
return new Color ((int) Long.parseLong (color, 16));
124
// generics support methods ................................................
126
private static RequestProcessor requestProcessor = new RequestProcessor ("XMLStorage");
128
static void save (final FileObject fo, final String content) {
129
requestProcessor.post (new Runnable () {
132
FileLock lock = fo.lock ();
134
OutputStream os = fo.getOutputStream (lock);
135
Writer writer = new OutputStreamWriter (os, "UTF-8"); // NOI18N
137
writer.write (content);
144
} catch (IOException ex) {
145
ErrorManager.getDefault ().notify (ex);
151
static Object load (FileObject fo, Handler handler) {
153
XMLReader reader = XMLUtil.createXMLReader ();
154
reader.setEntityResolver (handler);
155
reader.setContentHandler (handler);
156
InputStream is = fo.getInputStream ();
158
reader.parse (new InputSource (is));
162
return handler.getResult ();
163
} catch (SAXException ex) {
164
System.out.println("File: " + fo);
165
ex.printStackTrace ();
166
return handler.getResult ();
167
} catch (IOException ex) {
168
System.out.println("File: " + fo);
169
ex.printStackTrace ();
170
return handler.getResult ();
171
} catch (Exception ex) {
172
System.out.println("File: " + fo);
173
ex.printStackTrace ();
174
return handler.getResult ();
178
static StringBuffer generateHeader () {
179
StringBuffer sb = new StringBuffer ();
180
sb.append ("<?xml version=\"1.0\"?>\n\n");
184
static void generateFolderStart (
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);
201
static void generateFolderEnd (StringBuffer sb, String name, String indentation) {
202
sb.append (indentation).append ("</").append (name).append (">\n");
205
static void generateLeaf (
211
sb.append (indentation).append ('<').append (name);
212
if (attributes != null) {
213
if (!attributes.oneLine) sb.append ('\n');
214
else sb.append (' ');
215
generateAttributes (sb, attributes, indentation + " ");
216
if (!attributes.oneLine) sb.append (indentation);
222
private static void generateAttributes (
227
if (attributes == null) return;
228
int i, k = attributes.names.size ();
229
for (i = 0; i < k; i++) {
230
if (!attributes.oneLine)
231
sb.append (indentation);
232
sb.append (attributes.names.get (i)).append ("=\"").
233
append (attributes.values.get (i)).append ('\"');
234
if (!attributes.oneLine)
242
static class Handler extends DefaultHandler {
243
private Object result;
244
void setResult (Object result) {
245
this.result = result;
247
Object getResult () {
252
static class Attribs {
253
private List<String> names = new ArrayList<String> ();
254
private List<String> values = new ArrayList<String> ();
255
private boolean oneLine;
257
Attribs (boolean oneLine) {
258
this.oneLine = oneLine;
261
void add (String name, String value) {
262
int i = names.indexOf (name);