1 19 20 package org.netbeans.modules.editor.settings.storage; 21 22 import java.awt.Color ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import javax.swing.text.AttributeSet ; 32 import javax.swing.text.SimpleAttributeSet ; 33 import javax.swing.text.StyleConstants ; 34 import org.netbeans.api.editor.mimelookup.MimePath; 35 import org.netbeans.api.editor.settings.AttributesUtilities; 36 import org.netbeans.api.editor.settings.EditorStyleConstants; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileSystem; 39 import org.openide.filesystems.Repository; 40 import org.openide.xml.XMLUtil; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.Node ; 44 import org.xml.sax.Attributes ; 45 import org.xml.sax.InputSource ; 46 import org.xml.sax.SAXException ; 47 48 49 55 final class ColoringStorage { 56 57 private static final Logger LOG = Logger.getLogger(ColoringStorage.class.getName()); 58 59 62 static final String ALL_LANGUAGES_FILE_NAME = "defaultColoring.xml"; static final String COLORING_FILE_NAME = "coloring.xml"; static final String HIGHLIGHTING_FILE_NAME = "editorColoring.xml"; 66 67 static final String DEFAULTS_FOLDER = "Defaults"; 69 private static final String E_ROOT = "fontscolors"; private static final String E_FONTCOLOR = "fontcolor"; private static final String E_FONT = "font"; private static final String A_NAME = "name"; private static final String A_FOREGROUND = "foreColor"; private static final String A_BACKGROUND = "bgColor"; private static final String A_STRIKETHROUGH = "strikeThrough"; private static final String A_WAVEUNDERLINE = "waveUnderlined"; private static final String A_UNDERLINE = "underline"; private static final String A_DEFAULT = "default"; private static final String A_SIZE = "size"; private static final String A_STYLE = "style"; private static final String V_BOLD_ITALIC = "bold+italic"; private static final String V_BOLD = "bold"; private static final String V_ITALIC = "italic"; private static final String V_PLAIN = "plain"; 86 private static final String PUBLIC_ID = "-//NetBeans//DTD Editor Fonts and Colors settings 1.1//EN"; private static final String SYSTEM_ID = "http://www.netbeans.org/dtds/EditorFontsColors-1_1.dtd"; 89 private ColoringStorage() { 90 } 91 92 94 public static Map <String , AttributeSet > loadColorings( 95 MimePath mimePath, 96 String profile, 97 boolean colorings, boolean defaults ) { 100 String fileName = determineFileName(mimePath, colorings); 101 102 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 104 FileObject fo = null; 105 106 if (!defaults) { 107 fo = fs.findResource(Utils.getFileName(mimePath, profile, fileName)); 108 } 109 110 if (fo == null) { 111 fo = fs.findResource(Utils.getFileName( 112 mimePath, profile, DEFAULTS_FOLDER + "/" + fileName)); } 114 115 if (fo == null) { 116 return null; 117 } else { 118 List l = (List ) XMLStorage.load(fo, new ColoringsReader()); 119 120 FileObject basicName = fs.findResource( 122 Utils.getFileName(mimePath, EditorSettingsImpl.DEFAULT_PROFILE, DEFAULTS_FOLDER + "/" + fileName) ); 124 125 Map <String , AttributeSet > m = new HashMap <String , AttributeSet >(); 126 Iterator it = l.iterator(); 127 while (it.hasNext()) { 128 SimpleAttributeSet as = (SimpleAttributeSet ) it.next(); 129 String name = (String ) as.getAttribute(StyleConstants.NameAttribute); 130 String displayName = Utils.getLocalizedName(basicName, name, name); 131 132 as.addAttribute(EditorStyleConstants.DisplayName, displayName); 133 m.put(name, AttributesUtilities.createImmutable(as)); 134 } 135 136 return m; 137 } 138 } 139 140 private static class ColoringsReader extends XMLStorage.Handler { 141 private List <AttributeSet > colorings = new ArrayList <AttributeSet >(); 142 143 public Object getResult () { 144 return colorings; 145 } 146 147 public void startElement ( 148 String uri, 149 String localName, 150 String name, 151 Attributes attributes 152 ) throws SAXException { 153 try { 154 if (name.equals(E_ROOT)) { 155 157 } else if (name.equals(E_FONTCOLOR)) { 158 SimpleAttributeSet a = new SimpleAttributeSet (); 159 String value; 160 161 a.addAttribute(StyleConstants.NameAttribute, attributes.getValue(A_NAME)); 162 163 value = attributes.getValue(A_BACKGROUND); 164 if (value != null) { 165 a.addAttribute(StyleConstants.Background, Utils.stringToColor(value)); 166 } 167 168 value = attributes.getValue(A_FOREGROUND); 169 if (value != null) { 170 a.addAttribute(StyleConstants.Foreground, Utils.stringToColor(value)); 171 } 172 173 value = attributes.getValue(A_UNDERLINE); 174 if (value != null) { 175 a.addAttribute(StyleConstants.Underline, Utils.stringToColor(value)); 176 } 177 178 value = attributes.getValue(A_STRIKETHROUGH); 179 if (value != null) { 180 a.addAttribute(StyleConstants.StrikeThrough, Utils.stringToColor(value)); 181 } 182 183 value = attributes.getValue(A_WAVEUNDERLINE); 184 if (value != null) { 185 a.addAttribute(EditorStyleConstants.WaveUnderlineColor, Utils.stringToColor(value)); 186 } 187 188 value = attributes.getValue(A_DEFAULT); 189 if (value != null) { 190 a.addAttribute(EditorStyleConstants.Default, value); 191 } 192 193 colorings.add (a); 194 195 } else if (name.equals(E_FONT)) { 196 SimpleAttributeSet a = (SimpleAttributeSet ) colorings.get(colorings.size() - 1); 197 String value; 198 199 value = attributes.getValue(A_NAME); 200 if (value != null) { 201 a.addAttribute(StyleConstants.FontFamily, value); 202 } 203 204 value = attributes.getValue(A_SIZE); 205 if (value != null) { 206 try { 207 a.addAttribute(StyleConstants.FontSize, Integer.decode(value)); 208 } catch (NumberFormatException ex) { 209 LOG.log(Level.WARNING, value + " is not a valid Integer; parsing " + getProcessedFile().getPath(), ex); 211 } 212 } 213 214 value = attributes.getValue(A_STYLE); 215 if (value != null) { 216 a.addAttribute(StyleConstants.Bold, 217 Boolean.valueOf(value.indexOf(V_BOLD) >= 0) 218 ); 219 a.addAttribute( 220 StyleConstants.Italic, 221 Boolean.valueOf(value.indexOf(V_ITALIC) >= 0) 222 ); 223 } 224 } 225 } catch (Exception ex) { 226 LOG.log(Level.WARNING, "Can't parse colorings file " + getProcessedFile().getPath(), ex); } 228 } 229 230 public InputSource resolveEntity(String pubid, String sysid) { 231 return new InputSource ( 232 new java.io.ByteArrayInputStream (new byte [0]) 233 ); 234 } 235 } 237 239 public static void deleteColorings( 240 MimePath mimePath, 241 String profile, 242 boolean colorings, boolean defaults ) { 245 String fileName = determineFileName(mimePath, colorings); 246 if (defaults) { 247 fileName = DEFAULTS_FOLDER + "/" + fileName; 248 } 249 250 Utils.deleteFileObject(mimePath, profile, fileName); 251 } 252 253 255 public static void saveColorings( 256 MimePath mimePath, 257 String profile, 258 boolean colorings, boolean defaults, Collection <AttributeSet > fontColors 261 ) { 262 String fileName = determineFileName(mimePath, colorings); 263 if (defaults) { 264 fileName = DEFAULTS_FOLDER + "/" + fileName; 265 } 266 267 FileObject fo = Utils.createFileObject(mimePath, profile, fileName); 268 saveColorings(fo, fontColors); 269 } 270 271 private static void saveColorings(FileObject fo, Collection <AttributeSet > colorings) { 272 Document doc = XMLUtil.createDocument(E_ROOT, null, PUBLIC_ID, SYSTEM_ID); 273 Node root = doc.getElementsByTagName(E_ROOT).item(0); 274 275 for(AttributeSet category : colorings) { 276 Element fontColor = doc.createElement(E_FONTCOLOR); 277 root.appendChild(fontColor); 278 fontColor.setAttribute(A_NAME, (String ) category.getAttribute(StyleConstants.NameAttribute)); 279 280 if (category.isDefined(StyleConstants.Foreground)) { 281 fontColor.setAttribute( 282 A_FOREGROUND, 283 Utils.colorToString((Color ) category.getAttribute(StyleConstants.Foreground)) 284 ); 285 } 286 if (category.isDefined(StyleConstants.Background)) { 287 fontColor.setAttribute( 288 A_BACKGROUND, 289 Utils.colorToString((Color ) category.getAttribute(StyleConstants.Background)) 290 ); 291 } 292 if (category.isDefined(StyleConstants.StrikeThrough)) { 293 fontColor.setAttribute( 294 A_STRIKETHROUGH, 295 Utils.colorToString((Color ) category.getAttribute(StyleConstants.StrikeThrough)) 296 ); 297 } 298 if (category.isDefined(EditorStyleConstants.WaveUnderlineColor)) { 299 fontColor.setAttribute( 300 A_WAVEUNDERLINE, 301 Utils.colorToString((Color ) category.getAttribute(EditorStyleConstants.WaveUnderlineColor)) 302 ); 303 } 304 if (category.isDefined(StyleConstants.Underline)) { 305 fontColor.setAttribute( 306 A_UNDERLINE, 307 Utils.colorToString((Color ) category.getAttribute(StyleConstants.Underline)) 308 ); 309 } 310 if (category.isDefined(EditorStyleConstants.Default)) { 311 fontColor.setAttribute( 312 A_DEFAULT, 313 (String ) category.getAttribute(EditorStyleConstants.Default) 314 ); 315 } 316 317 if ( category.isDefined(StyleConstants.FontFamily) || 318 category.isDefined(StyleConstants.FontSize) || 319 category.isDefined(StyleConstants.Bold) || 320 category.isDefined(StyleConstants.Italic) 321 ) { 322 Element font = doc.createElement(E_FONT); 323 fontColor.appendChild(font); 324 325 if (category.isDefined(StyleConstants.FontFamily)) { 326 font.setAttribute( 327 A_NAME, 328 (String ) category.getAttribute(StyleConstants.FontFamily) 329 ); 330 } 331 if (category.isDefined(StyleConstants.FontSize)) { 332 font.setAttribute( 333 A_SIZE, 334 ((Integer ) category.getAttribute(StyleConstants.FontSize)).toString() 335 ); 336 } 337 if (category.isDefined(StyleConstants.Bold) || 338 category.isDefined(StyleConstants.Italic) 339 ) { 340 Boolean bold = Boolean.FALSE, italic = Boolean.FALSE; 341 342 if (category.isDefined(StyleConstants.Bold)) { 343 bold = (Boolean ) category.getAttribute(StyleConstants.Bold); 344 } 345 if (category.isDefined(StyleConstants.Italic)) { 346 italic = (Boolean ) category.getAttribute(StyleConstants.Italic); 347 } 348 349 font.setAttribute(A_STYLE, bold.booleanValue() ? 350 (italic.booleanValue() ? V_BOLD_ITALIC : V_BOLD) : 351 (italic.booleanValue() ? V_ITALIC : V_PLAIN) 352 ); 353 } 354 } 355 } 356 357 XMLStorage.save(fo, doc); 358 } 359 360 private static String determineFileName(MimePath mimePath, boolean colorings) { 361 String fileName; 362 363 if (colorings) { 364 if (mimePath.size() == 0) { 365 fileName = ALL_LANGUAGES_FILE_NAME; 366 } else { 367 fileName = COLORING_FILE_NAME; 368 } 369 } else { 370 fileName = HIGHLIGHTING_FILE_NAME; 371 } 372 373 return fileName; 374 } 375 } 376 | Popular Tags |