001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.options.keymap;
043:
044: import java.awt.Color;
045: import java.awt.Font;
046: import java.io.IOException;
047: import java.io.InputStream;
048: import java.io.OutputStream;
049: import java.io.OutputStreamWriter;
050: import java.io.Writer;
051: import java.util.ArrayList;
052: import java.util.HashMap;
053: import java.util.List;
054: import java.util.Map;
055: import org.openide.ErrorManager;
056: import org.openide.filesystems.FileLock;
057:
058: import org.openide.filesystems.FileObject;
059: import org.openide.util.RequestProcessor;
060: import org.openide.xml.XMLUtil;
061: import org.xml.sax.InputSource;
062: import org.xml.sax.SAXException;
063: import org.xml.sax.XMLReader;
064: import org.xml.sax.helpers.DefaultHandler;
065:
066: public class XMLStorage {
067:
068: private static final Map<Color, String> colorToName = new HashMap<Color, String>();
069: private static final Map<String, Color> nameToColor = new HashMap<String, Color>();
070: private static final Map<String, Integer> nameToFontStyle = new HashMap<String, Integer>();
071: private static final Map<Integer, String> fontStyleToName = new HashMap<Integer, String>();
072: static {
073: colorToName.put(Color.black, "black");
074: nameToColor.put("black", Color.black);
075: colorToName.put(Color.blue, "blue");
076: nameToColor.put("blue", Color.blue);
077: colorToName.put(Color.cyan, "cyan");
078: nameToColor.put("cyan", Color.cyan);
079: colorToName.put(Color.darkGray, "darkGray");
080: nameToColor.put("darkGray", Color.darkGray);
081: colorToName.put(Color.gray, "gray");
082: nameToColor.put("gray", Color.gray);
083: colorToName.put(Color.green, "green");
084: nameToColor.put("green", Color.green);
085: colorToName.put(Color.lightGray, "lightGray");
086: nameToColor.put("lightGray", Color.lightGray);
087: colorToName.put(Color.magenta, "magenta");
088: nameToColor.put("magenta", Color.magenta);
089: colorToName.put(Color.orange, "orange");
090: nameToColor.put("orange", Color.orange);
091: colorToName.put(Color.pink, "pink");
092: nameToColor.put("pink", Color.pink);
093: colorToName.put(Color.red, "red");
094: nameToColor.put("red", Color.red);
095: colorToName.put(Color.white, "white");
096: nameToColor.put("white", Color.white);
097: colorToName.put(Color.yellow, "yellow");
098: nameToColor.put("yellow", Color.yellow);
099:
100: nameToFontStyle.put("plain", Integer.valueOf(Font.PLAIN));
101: fontStyleToName.put(Integer.valueOf(Font.PLAIN), "plain");
102: nameToFontStyle.put("bold", Integer.valueOf(Font.BOLD));
103: fontStyleToName.put(Integer.valueOf(Font.BOLD), "bold");
104: nameToFontStyle.put("italic", Integer.valueOf(Font.ITALIC));
105: fontStyleToName.put(Integer.valueOf(Font.ITALIC), "italic");
106: nameToFontStyle.put("bold+italic", Integer.valueOf(Font.BOLD
107: + Font.ITALIC));
108: fontStyleToName.put(Integer.valueOf(Font.BOLD + Font.ITALIC),
109: "bold+italic");
110: }
111:
112: static String colorToString(Color color) {
113: if (colorToName.containsKey(color))
114: return (String) colorToName.get(color);
115: return Integer.toHexString(color.getRGB());
116: }
117:
118: static Color stringToColor(String color) {
119: if (nameToColor.containsKey(color))
120: return (Color) nameToColor.get(color);
121: return new Color((int) Long.parseLong(color, 16));
122: }
123:
124: // generics support methods ................................................
125:
126: private static RequestProcessor requestProcessor = new RequestProcessor(
127: "XMLStorage");
128:
129: static void save(final FileObject fo, final String content) {
130: requestProcessor.post(new Runnable() {
131: public void run() {
132: try {
133: FileLock lock = fo.lock();
134: try {
135: OutputStream os = fo.getOutputStream(lock);
136: Writer writer = new OutputStreamWriter(os,
137: "UTF-8"); // NOI18N
138: try {
139: writer.write(content);
140: } finally {
141: writer.close();
142: }
143: } finally {
144: lock.releaseLock();
145: }
146: } catch (IOException ex) {
147: ErrorManager.getDefault().notify(ex);
148: }
149: }
150: });
151: }
152:
153: static Object load(FileObject fo, Handler handler) {
154: try {
155: XMLReader reader = XMLUtil.createXMLReader();
156: reader.setEntityResolver(handler);
157: reader.setContentHandler(handler);
158: InputStream is = fo.getInputStream();
159: try {
160: reader.parse(new InputSource(is));
161: } finally {
162: is.close();
163: }
164: return handler.getResult();
165: } catch (SAXException ex) {
166: System.out.println("File: " + fo);
167: ex.printStackTrace();
168: return handler.getResult();
169: } catch (IOException ex) {
170: System.out.println("File: " + fo);
171: ex.printStackTrace();
172: return handler.getResult();
173: } catch (Exception ex) {
174: System.out.println("File: " + fo);
175: ex.printStackTrace();
176: return handler.getResult();
177: }
178: }
179:
180: static StringBuffer generateHeader() {
181: StringBuffer sb = new StringBuffer();
182: sb.append("<?xml version=\"1.0\"?>\n\n");
183: return sb;
184: }
185:
186: static void generateFolderStart(StringBuffer sb, String name,
187: Attribs attributes, String indentation) {
188: sb.append(indentation).append('<').append(name);
189: if (attributes != null) {
190: if (!attributes.oneLine)
191: sb.append('\n');
192: else
193: sb.append(' ');
194: generateAttributes(sb, attributes, indentation + " ");
195: if (!attributes.oneLine)
196: sb.append(indentation);
197: sb.append(">\n");
198: } else
199: sb.append(">\n");
200: }
201:
202: static void generateFolderEnd(StringBuffer sb, String name,
203: String indentation) {
204: sb.append(indentation).append("</").append(name).append(">\n");
205: }
206:
207: static void generateLeaf(StringBuffer sb, String name,
208: Attribs attributes, String indentation) {
209: sb.append(indentation).append('<').append(name);
210: if (attributes != null) {
211: if (!attributes.oneLine)
212: sb.append('\n');
213: else
214: sb.append(' ');
215: generateAttributes(sb, attributes, indentation + " ");
216: if (!attributes.oneLine)
217: sb.append(indentation);
218: sb.append("/>\n");
219: } else
220: sb.append("/>\n");
221: }
222:
223: private static void generateAttributes(StringBuffer sb,
224: Attribs attributes, String indentation) {
225: if (attributes == null)
226: return;
227: int i, k = attributes.names.size();
228: for (i = 0; i < k; i++) {
229: if (!attributes.oneLine)
230: sb.append(indentation);
231: sb.append(attributes.names.get(i)).append("=\"").append(
232: attributes.values.get(i)).append('\"');
233: if (!attributes.oneLine)
234: sb.append("\n");
235: else if (i < (k - 1))
236: sb.append(' ');
237: }
238: }
239:
240: static class Handler extends DefaultHandler {
241: private Object result;
242:
243: void setResult(Object result) {
244: this .result = result;
245: }
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: }
|