1 19 20 package org.netbeans.modules.options.keymap; 21 22 import java.awt.Color ; 23 import java.awt.Font ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.io.OutputStreamWriter ; 28 import java.io.Writer ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.List ; 32 import java.util.Map ; 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 ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.XMLReader ; 42 import org.xml.sax.helpers.DefaultHandler ; 43 44 45 public class XMLStorage { 46 47 private static final Map colorToName = new HashMap (); 48 private static final Map nameToColor = new HashMap (); 49 private static final Map nameToFontStyle = new HashMap (); 50 private static final Map fontStyleToName = new HashMap (); 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 (Font.PLAIN)); 80 fontStyleToName.put (new Integer (Font.PLAIN), "plain"); 81 nameToFontStyle.put ("bold", new Integer (Font.BOLD)); 82 fontStyleToName.put (new Integer (Font.BOLD), "bold"); 83 nameToFontStyle.put ("italic", new Integer (Font.ITALIC)); 84 fontStyleToName.put (new Integer (Font.ITALIC), "italic"); 85 nameToFontStyle.put ("bold+italic", new Integer (Font.BOLD + Font.ITALIC)); 86 fontStyleToName.put (new Integer (Font.BOLD + Font.ITALIC), "bold+italic"); 87 } 88 89 static String colorToString (Color color) { 90 if (colorToName.containsKey (color)) 91 return (String ) colorToName.get (color); 92 return Integer.toHexString (color.getRGB ()); 93 } 94 95 static Color stringToColor (String color) { 96 if (nameToColor.containsKey (color)) 97 return (Color ) nameToColor.get (color); 98 return new Color ((int) Long.parseLong (color, 16)); 99 } 100 101 102 104 private static RequestProcessor requestProcessor = new RequestProcessor ("XMLStorage"); 105 106 static void save (final FileObject fo, final String content) { 107 requestProcessor.post (new Runnable () { 108 public void run () { 109 try { 110 FileLock lock = fo.lock (); 111 try { 112 OutputStream os = fo.getOutputStream (lock); 113 Writer writer = new OutputStreamWriter (os, "UTF-8"); try { 115 writer.write (content); 116 } finally { 117 writer.close (); 118 } 119 } finally { 120 lock.releaseLock (); 121 } 122 } catch (IOException ex) { 123 ErrorManager.getDefault ().notify (ex); 124 } 125 } 126 }); 127 } 128 129 static Object load (FileObject fo, Handler handler) { 130 try { 131 XMLReader reader = XMLUtil.createXMLReader (); 132 reader.setEntityResolver (handler); 133 reader.setContentHandler (handler); 134 InputStream is = fo.getInputStream (); 135 try { 136 reader.parse (new InputSource (is)); 137 } finally { 138 is.close (); 139 } 140 return handler.getResult (); 141 } catch (SAXException ex) { 142 System.out.println("File: " + fo); 143 ex.printStackTrace (); 144 return handler.getResult (); 145 } catch (IOException ex) { 146 System.out.println("File: " + fo); 147 ex.printStackTrace (); 148 return handler.getResult (); 149 } catch (Exception ex) { 150 System.out.println("File: " + fo); 151 ex.printStackTrace (); 152 return handler.getResult (); 153 } 154 } 155 156 static StringBuffer generateHeader () { 157 StringBuffer sb = new StringBuffer (); 158 sb.append ("<?xml version=\"1.0\"?>\n\n"); 159 return sb; 160 } 161 162 static void generateFolderStart ( 163 StringBuffer sb, 164 String name, 165 Attribs attributes, 166 String 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 sb, String name, String indentation) { 180 sb.append (indentation).append ("</").append (name).append (">\n"); 181 } 182 183 static void generateLeaf ( 184 StringBuffer sb, 185 String name, 186 Attribs attributes, 187 String 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 sb, 202 Attribs attributes, 203 String 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 { 221 private Object result; 222 void setResult (Object result) { 223 this.result = result; 224 } 225 Object getResult () { 226 return result; 227 } 228 } 229 230 static class Attribs { 231 private List names = new ArrayList (); 232 private List values = new ArrayList (); 233 private boolean oneLine; 234 235 Attribs (boolean oneLine) { 236 this.oneLine = oneLine; 237 } 238 239 void add (String name, String 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 |