1 19 20 package org.netbeans.upgrade; 21 22 import java.awt.Color ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.MissingResourceException ; 34 import java.util.ResourceBundle ; 35 import javax.swing.text.AttributeSet ; 36 import javax.swing.text.SimpleAttributeSet ; 37 import javax.swing.text.StyleConstants ; 38 import org.netbeans.upgrade.XMLStorage.Attribs; 39 import org.openide.ErrorManager; 40 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileSystem; 43 import org.openide.filesystems.Repository; 44 import org.openide.util.NbBundle; 45 import org.xml.sax.Attributes ; 46 import org.xml.sax.InputSource ; 47 import org.xml.sax.SAXException ; 48 49 50 56 class ColoringStorage { 57 58 59 61 static Map loadColorings ( 62 InputStream is, 63 String name 64 ) { 65 return (Map ) XMLStorage.load (is, name, new ColoringsReader ()); 66 } 67 68 private static class ColoringsReader extends XMLStorage.Handler { 69 private Map <String , SimpleAttributeSet > colorings = new HashMap <String , SimpleAttributeSet > (); 70 private SimpleAttributeSet last; 71 72 Object getResult () { 73 return colorings; 74 } 75 76 public void startElement ( 77 String uri, 78 String localName, 79 String name, 80 Attributes attributes 81 ) throws SAXException { 82 try { 83 if (name.equals ("fontscolors")) { 84 } else 85 if (name.equals ("fontcolor")) { 86 String n = (String ) attributes.getValue ("syntaxName"); 87 if (n == null) 88 n = (String ) attributes.getValue ("name"); 89 if (n == null) { 90 System.out.println("no syntaxName " + attributes); 91 return; 92 } 93 SimpleAttributeSet a = new SimpleAttributeSet (); 94 a.addAttribute ( 95 StyleConstants.NameAttribute, 96 n 97 ); 98 if (attributes.getValue ("bgColor") != null) 99 a.addAttribute ( 100 StyleConstants.Background, 101 XMLStorage.stringToColor (attributes.getValue ("bgColor")) 102 ); 103 if (attributes.getValue ("foreColor") != null) 104 a.addAttribute ( 105 StyleConstants.Foreground, 106 XMLStorage.stringToColor (attributes.getValue ("foreColor")) 107 ); 108 if (attributes.getValue ("underline") != null) 109 a.addAttribute ( 110 StyleConstants.Underline, 111 XMLStorage.stringToColor (attributes.getValue ("underline")) 112 ); 113 if (attributes.getValue ("strikeThrough") != null) 114 a.addAttribute ( 115 StyleConstants.StrikeThrough, 116 XMLStorage.stringToColor (attributes.getValue ("strikeThrough")) 117 ); 118 if (attributes.getValue ("waveUnderlined") != null) 119 a.addAttribute ( 120 "waveUnderlined", 121 XMLStorage.stringToColor (attributes.getValue ("waveUnderlined")) 122 ); 123 if (attributes.getValue ("default") != null) 124 a.addAttribute ( 125 "default", 126 (String ) attributes.getValue ("default") 127 ); 128 colorings.put (n, a); 129 last = a; 130 } else 131 if (name.equals ("font")) { 132 if (attributes.getValue ("name") != null) 133 last.addAttribute ( 134 StyleConstants.FontFamily, 135 attributes.getValue ("name") 136 ); 137 if (attributes.getValue ("size") != null) 138 try { 139 last.addAttribute ( 140 StyleConstants.FontSize, 141 Integer.decode (attributes.getValue ("size")) 142 ); 143 } catch (NumberFormatException ex) { 144 ex.printStackTrace (); 145 } 146 if (attributes.getValue ("style") != null) { 147 if (attributes.getValue ("style").indexOf ("bold") >= 0) 148 last.addAttribute ( 149 StyleConstants.Bold, 150 Boolean.TRUE 151 ); 152 if (attributes.getValue ("style").indexOf ("italic") >= 0) 153 last.addAttribute ( 154 StyleConstants.Italic, 155 Boolean.TRUE 156 ); 157 } 158 } 159 } catch (Exception ex) { 160 ErrorManager.getDefault ().notify (ex); 161 } 162 } 163 164 public InputSource resolveEntity (String pubid, String sysid) { 165 return new InputSource ( 166 new java.io.ByteArrayInputStream (new byte [0]) 167 ); 168 } 169 } 170 171 172 174 static void saveColorings (FileObject fo, Collection colorings) { 175 final StringBuffer sb = XMLStorage.generateHeader (); 176 XMLStorage.generateFolderStart (sb, "fontscolors", null, ""); 177 Iterator it = colorings.iterator (); 178 while (it.hasNext ()) { 179 AttributeSet category = (AttributeSet ) it.next (); 180 Attribs attributes = new Attribs (true); 181 attributes.add ( 182 "name", 183 (String ) category.getAttribute (StyleConstants.NameAttribute) 184 ); 185 if (category.isDefined (StyleConstants.Foreground)) 186 attributes.add ( 187 "foreColor", 188 XMLStorage.colorToString ( 189 (Color ) category.getAttribute (StyleConstants.Foreground) 190 ) 191 ); 192 if (category.isDefined (StyleConstants.Background)) 193 attributes.add ( 194 "bgColor", 195 XMLStorage.colorToString ( 196 (Color ) category.getAttribute (StyleConstants.Background) 197 ) 198 ); 199 if (category.isDefined (StyleConstants.StrikeThrough)) 200 attributes.add ( 201 "strikeThrough", 202 XMLStorage.colorToString ( 203 (Color ) category.getAttribute (StyleConstants.StrikeThrough) 204 ) 205 ); 206 if (category.isDefined ("waveUnderlined")) 207 attributes.add ( 208 "waveUnderlined", 209 XMLStorage.colorToString ( 210 (Color ) category.getAttribute ("waveUnderlined") 211 ) 212 ); 213 if (category.isDefined (StyleConstants.Underline)) 214 attributes.add ( 215 "underline", 216 XMLStorage.colorToString ( 217 (Color ) category.getAttribute (StyleConstants.Underline) 218 ) 219 ); 220 if (category.isDefined ("default")) 221 attributes.add ( 222 "default", 223 (String ) category.getAttribute ("default") 224 ); 225 if ( category.isDefined (StyleConstants.FontFamily) || 226 category.isDefined (StyleConstants.FontSize) || 227 category.isDefined (StyleConstants.Bold) || 228 category.isDefined (StyleConstants.Italic) 229 ) { 230 XMLStorage.generateFolderStart (sb, "fontcolor", attributes, " "); 231 attributes = new Attribs (true); 232 if (category.isDefined (StyleConstants.FontFamily)) 233 attributes.add ( 234 "name", 235 (String ) category.getAttribute (StyleConstants.FontFamily) 236 ); 237 if (category.isDefined (StyleConstants.FontSize)) 238 attributes.add ( 239 "size", 240 "" + category.getAttribute (StyleConstants.FontSize) 241 ); 242 if (category.isDefined (StyleConstants.Bold) || 243 category.isDefined (StyleConstants.Italic) 244 ) { 245 Boolean bold = Boolean.FALSE, italic = Boolean.FALSE; 246 if (category.isDefined (StyleConstants.Bold)) 247 bold = (Boolean ) category.getAttribute (StyleConstants.Bold); 248 if (category.isDefined (StyleConstants.Italic)) 249 italic = (Boolean ) category.getAttribute (StyleConstants.Italic); 250 attributes.add ("style", 251 bold.booleanValue () ? 252 (italic.booleanValue () ? 253 "bold+italic" : 254 "bold") : 255 (italic.booleanValue () ? 256 "italic" : "plain") 257 ); 258 } 259 XMLStorage.generateLeaf (sb, "font", attributes, " "); 260 XMLStorage.generateFolderEnd (sb, "fontcolor", " "); 261 } else 262 XMLStorage.generateLeaf (sb, "fontcolor", attributes, " "); 263 } 264 XMLStorage.generateFolderEnd (sb, "fontscolors", ""); 265 XMLStorage.save (fo, new String (sb)); 266 } 267 268 271 private static String getFolderName ( 272 String [] mimeTypes, 273 String profile 274 ) { 275 StringBuffer sb = new StringBuffer (); 276 sb.append ("Editors"); 277 int i, k = mimeTypes.length; 278 for (i = 0; i < k; i++) 279 sb.append ('/').append (mimeTypes [i]); 280 if (profile != null) 281 sb.append ('/').append (profile); 282 return sb.append ('/').toString (); 283 } 284 285 288 private static FileObject createFileObject ( 289 FileObject root, 290 String [] mimeTypes, 291 String profile, 292 String fileName 293 ) { 294 FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 295 try { 296 FileObject fo = getFO (fs.getRoot (), "Editors"); 297 int i, k = mimeTypes.length; 298 for (i = 0; i < k; i++) 299 fo = getFO (fo, mimeTypes [i]); 300 if (profile != null) 301 fo = getFO (fo, profile); 302 if (fileName == null) 303 return fo; 304 FileObject fo1 = fo.getFileObject (fileName); 305 if (fo1 != null) return fo1; 306 return fo.createData (fileName); 307 } catch (IOException ex) { 308 ErrorManager.getDefault ().notify (ex); 309 return null; 310 } 311 } 312 313 private static FileObject getFO (FileObject fo, String next) throws IOException { 314 FileObject fo1 = fo.getFileObject (next); 315 if (fo1 == null) 316 return fo.createFolder (next); 317 return fo1; 318 } 319 } 320 | Popular Tags |