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