~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to core/options/keymap/src/org/netbeans/modules/options/keymap/XMLStorage.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.options.keymap;
 
43
 
 
44
import java.awt.Color;
 
45
import java.awt.Font;
 
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;
 
54
import java.util.Map;
 
55
import org.openide.ErrorManager;
 
56
import org.openide.filesystems.FileLock;
 
57
 
 
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
 
 
66
 
 
67
public class XMLStorage {
 
68
 
 
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> ();
 
73
    static {
 
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);
 
100
        
 
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");
 
109
    }
 
110
    
 
111
    static String colorToString (Color color) {
 
112
        if (colorToName.containsKey (color))
 
113
            return (String) colorToName.get (color);
 
114
        return Integer.toHexString (color.getRGB ());
 
115
    }
 
116
    
 
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));
 
121
    }
 
122
    
 
123
    
 
124
    // generics support methods ................................................
 
125
    
 
126
    private static RequestProcessor requestProcessor = new RequestProcessor ("XMLStorage");
 
127
    
 
128
    static void save (final FileObject fo, final String content) {
 
129
        requestProcessor.post (new Runnable () {
 
130
            public void run () {
 
131
                try {
 
132
                    FileLock lock = fo.lock ();
 
133
                    try {
 
134
                        OutputStream os = fo.getOutputStream (lock);
 
135
                        Writer writer = new OutputStreamWriter (os, "UTF-8"); // NOI18N
 
136
                        try {
 
137
                            writer.write (content);
 
138
                        } finally {
 
139
                            writer.close ();
 
140
                        } 
 
141
                    } finally {
 
142
                        lock.releaseLock ();
 
143
                    }
 
144
                } catch (IOException ex) {
 
145
                    ErrorManager.getDefault ().notify (ex);
 
146
                }
 
147
            }
 
148
        });
 
149
    }
 
150
    
 
151
    static Object load (FileObject fo, Handler handler) {
 
152
        try {
 
153
            XMLReader reader = XMLUtil.createXMLReader ();
 
154
            reader.setEntityResolver (handler);
 
155
            reader.setContentHandler (handler);
 
156
            InputStream is = fo.getInputStream ();
 
157
            try {
 
158
                reader.parse (new InputSource (is));
 
159
            } finally {
 
160
                is.close ();
 
161
            }
 
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 ();
 
175
        }
 
176
    }
 
177
    
 
178
    static StringBuffer generateHeader () {
 
179
        StringBuffer sb = new StringBuffer ();
 
180
        sb.append ("<?xml version=\"1.0\"?>\n\n");
 
181
        return sb;
 
182
    }
 
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
    
 
201
    static void generateFolderEnd (StringBuffer sb, String name, String indentation) {
 
202
        sb.append (indentation).append ("</").append (name).append (">\n");
 
203
    }
 
204
    
 
205
    static void generateLeaf (
 
206
        StringBuffer sb, 
 
207
        String name, 
 
208
        Attribs attributes, 
 
209
        String indentation
 
210
    ) {
 
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);
 
217
            sb.append ("/>\n");
 
218
        } else
 
219
            sb.append ("/>\n");
 
220
    }
 
221
    
 
222
    private static void generateAttributes (
 
223
        StringBuffer sb, 
 
224
        Attribs attributes, 
 
225
        String indentation
 
226
    ) {
 
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)
 
235
                sb.append ("\n");
 
236
            else
 
237
            if (i < (k - 1))
 
238
                sb.append (' ');
 
239
        }
 
240
    }
 
241
    
 
242
    static class Handler extends DefaultHandler {
 
243
        private Object result;
 
244
        void setResult (Object result) {
 
245
            this.result = result;
 
246
        }
 
247
        Object getResult () {
 
248
            return result;
 
249
        }
 
250
    }
 
251
    
 
252
    static class Attribs {
 
253
        private List<String> names = new ArrayList<String> ();
 
254
        private List<String> values = new ArrayList<String> ();
 
255
        private boolean oneLine;
 
256
        
 
257
        Attribs (boolean oneLine) {
 
258
            this.oneLine = oneLine;
 
259
        }
 
260
        
 
261
        void add (String name, String value) {
 
262
            int i = names.indexOf (name);
 
263
            if (i >= 0) {
 
264
                names.remove (i);
 
265
                values.remove (i);
 
266
            }
 
267
            names.add (name);
 
268
            values.add (value);
 
269
        }
 
270
        
 
271
        void clear () {
 
272
            names.clear ();
 
273
            values.clear ();
 
274
        }
 
275
    }
 
276
}