1 19 20 package org.netbeans.upgrade; 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 <Color ,String > colorToName = new HashMap <Color ,String > (); 48 private static final Map <String , Color > nameToColor = new HashMap <String , Color > (); 49 private static final Map <String , Integer > nameToFontStyle = new HashMap <String , Integer > (); 50 private static final Map <Integer , String > fontStyleToName = new HashMap <Integer , String > (); 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", Integer.valueOf (Font.PLAIN)); 80 fontStyleToName.put (Integer.valueOf (Font.PLAIN), "plain"); 81 nameToFontStyle.put ("bold", Integer.valueOf (Font.BOLD)); 82 fontStyleToName.put (Integer.valueOf (Font.BOLD), "bold"); 83 nameToFontStyle.put ("italic", Integer.valueOf (Font.ITALIC)); 84 fontStyleToName.put (Integer.valueOf (Font.ITALIC), "italic"); 85 nameToFontStyle.put ("bold+italic", Integer.valueOf (Font.BOLD + Font.ITALIC)); 86 fontStyleToName.put (Integer.valueOf (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) throws Exception { 96 if (color.startsWith ("#")) 97 color = color.substring (1); 98 if (nameToColor.containsKey (color)) 99 return (Color ) nameToColor.get (color); 100 try { 101 return new Color ((int) Long.parseLong (color, 16)); 102 } catch (NumberFormatException ex) { 103 throw new Exception (); 104 } 105 } 106 107 108 110 private static RequestProcessor requestProcessor = new RequestProcessor ("XMLStorage"); 111 112 static void save (final FileObject fo, final String content) { 113 if (fo == null) throw new NullPointerException (); 114 if (content == null) throw new NullPointerException (); 115 requestProcessor.post (new Runnable () { 116 public void run () { 117 try { 118 FileLock lock = fo.lock (); 119 try { 120 OutputStream os = fo.getOutputStream (lock); 121 Writer writer = new OutputStreamWriter (os, "UTF-8"); try { 123 writer.write (content); 124 } finally { 125 writer.close (); 126 } 127 } finally { 128 lock.releaseLock (); 129 } 130 } catch (IOException ex) { 131 ErrorManager.getDefault ().notify (ex); 132 } 133 } 134 }); 135 } 136 137 static Object load (InputStream is, String name, Handler handler) { 138 try { 139 try { 140 XMLReader reader = XMLUtil.createXMLReader (); 141 reader.setEntityResolver (handler); 142 reader.setContentHandler (handler); 143 reader.parse (new InputSource (is)); 144 return handler.getResult (); 145 } finally { 146 is.close (); 147 } 148 } catch (SAXException ex) { 149 if (System.getProperty ("org.netbeans.optionsDialog") != null) { 150 System.out.println("File: " + name); 151 ex.printStackTrace (); 152 } 153 return handler.getResult (); 154 } catch (IOException ex) { 155 if (System.getProperty ("org.netbeans.optionsDialog") != null) { 156 System.out.println("File: " + name); 157 ex.printStackTrace (); 158 } 159 return handler.getResult (); 160 } catch (Exception ex) { 161 if (System.getProperty ("org.netbeans.optionsDialog") != null) { 162 System.out.println("File: " + name); 163 ex.printStackTrace (); 164 } 165 return handler.getResult (); 166 } 167 } 168 169 static StringBuffer generateHeader () { 170 StringBuffer sb = new StringBuffer (); 171 sb.append ("<?xml version=\"1.0\"?>\n\n"); 172 return sb; 173 } 174 175 static void generateFolderStart ( 176 StringBuffer sb, 177 String name, 178 Attribs attributes, 179 String indentation 180 ) { 181 sb.append (indentation).append ('<').append (name); 182 if (attributes != null) { 183 if (!attributes.oneLine) sb.append ('\n'); 184 else sb.append (' '); 185 generateAttributes (sb, attributes, indentation + " "); 186 if (!attributes.oneLine) sb.append (indentation); 187 sb.append (">\n"); 188 } else 189 sb.append (">\n"); 190 } 191 192 static void generateFolderEnd (StringBuffer sb, String name, String indentation) { 193 sb.append (indentation).append ("</").append (name).append (">\n"); 194 } 195 196 static void generateLeaf ( 197 StringBuffer sb, 198 String name, 199 Attribs attributes, 200 String indentation 201 ) { 202 sb.append (indentation).append ('<').append (name); 203 if (attributes != null) { 204 if (!attributes.oneLine) sb.append ('\n'); 205 else sb.append (' '); 206 generateAttributes (sb, attributes, indentation + " "); 207 if (!attributes.oneLine) sb.append (indentation); 208 sb.append ("/>\n"); 209 } else 210 sb.append ("/>\n"); 211 } 212 213 private static void generateAttributes ( 214 StringBuffer sb, 215 Attribs attributes, 216 String indentation 217 ) { 218 if (attributes == null) return; 219 int i, k = attributes.names.size (); 220 for (i = 0; i < k; i++) { 221 if (!attributes.oneLine) 222 sb.append (indentation); 223 sb.append (attributes.names.get (i)).append ("=\""). 224 append (attributes.values.get (i)).append ('\"'); 225 if (!attributes.oneLine) 226 sb.append ("\n"); 227 else 228 if (i < (k - 1)) 229 sb.append (' '); 230 } 231 } 232 233 static class Handler extends DefaultHandler { 234 private Object result; 235 void setResult (Object result) { 236 this.result = result; 237 } 238 Object getResult () { 239 return result; 240 } 241 } 242 243 static class Attribs { 244 private List <String > names = new ArrayList <String > (); 245 private List <String > values = new ArrayList <String > (); 246 private boolean oneLine; 247 248 Attribs (boolean oneLine) { 249 this.oneLine = oneLine; 250 } 251 252 void add (String name, String value) { 253 int i = names.indexOf (name); 254 if (i >= 0) { 255 names.remove (i); 256 values.remove (i); 257 } names.add (name); 258 values.add (value); 259 } 260 261 void clear () { 262 names.clear (); 263 values.clear (); 264 } 265 } 266 } 267 | Popular Tags |