KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > upgrade > ColoringStorage


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.upgrade;
21
22 import java.awt.Color JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.MissingResourceException JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35 import javax.swing.text.AttributeSet JavaDoc;
36 import javax.swing.text.SimpleAttributeSet JavaDoc;
37 import javax.swing.text.StyleConstants JavaDoc;
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 JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48
49
50 /**
51  * This class contains support static methods for loading / saving and
52  * translating coloring (fontsColors.xml) files. It calls XMLStorage utilities.
53  *
54  * @author Jan Jancura
55  */

56 class ColoringStorage {
57
58     
59     // load ....................................................................
60

61     static Map JavaDoc loadColorings (
62         InputStream JavaDoc is,
63         String JavaDoc name
64     ) {
65         return (Map JavaDoc) XMLStorage.load (is, name, new ColoringsReader ());
66     }
67
68     private static class ColoringsReader extends XMLStorage.Handler {
69         private Map JavaDoc<String JavaDoc, SimpleAttributeSet JavaDoc> colorings = new HashMap JavaDoc<String JavaDoc, SimpleAttributeSet JavaDoc> ();
70         private SimpleAttributeSet JavaDoc last;
71         
72         Object JavaDoc getResult () {
73             return colorings;
74         }
75         
76         public void startElement (
77             String JavaDoc uri,
78             String JavaDoc localName,
79             String JavaDoc name,
80             Attributes JavaDoc attributes
81         ) throws SAXException JavaDoc {
82             try {
83                 if (name.equals ("fontscolors")) {
84                 } else
85                 if (name.equals ("fontcolor")) {
86                     String JavaDoc n = (String JavaDoc) attributes.getValue ("syntaxName");
87                     if (n == null)
88                         n = (String JavaDoc) attributes.getValue ("name");
89                     if (n == null) {
90                         System.out.println("no syntaxName " + attributes);
91                         return;
92                     }
93                     SimpleAttributeSet JavaDoc a = new SimpleAttributeSet JavaDoc ();
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 JavaDoc) 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 JavaDoc 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 JavaDoc ex) {
160                 ErrorManager.getDefault ().notify (ex);
161             }
162         }
163         
164         public InputSource JavaDoc resolveEntity (String JavaDoc pubid, String JavaDoc sysid) {
165             return new InputSource JavaDoc (
166         new java.io.ByteArrayInputStream JavaDoc (new byte [0])
167         );
168         }
169     }
170
171     
172     // save colorings ..........................................................
173

174     static void saveColorings (FileObject fo, Collection JavaDoc colorings) {
175         final StringBuffer JavaDoc sb = XMLStorage.generateHeader ();
176         XMLStorage.generateFolderStart (sb, "fontscolors", null, "");
177         Iterator JavaDoc it = colorings.iterator ();
178         while (it.hasNext ()) {
179             AttributeSet JavaDoc category = (AttributeSet JavaDoc) it.next ();
180             Attribs attributes = new Attribs (true);
181             attributes.add (
182         "name",
183         (String JavaDoc) category.getAttribute (StyleConstants.NameAttribute)
184         );
185         if (category.isDefined (StyleConstants.Foreground))
186         attributes.add (
187             "foreColor",
188             XMLStorage.colorToString (
189             (Color JavaDoc) category.getAttribute (StyleConstants.Foreground)
190             )
191         );
192         if (category.isDefined (StyleConstants.Background))
193         attributes.add (
194             "bgColor",
195             XMLStorage.colorToString (
196             (Color JavaDoc) category.getAttribute (StyleConstants.Background)
197             )
198         );
199         if (category.isDefined (StyleConstants.StrikeThrough))
200         attributes.add (
201             "strikeThrough",
202             XMLStorage.colorToString (
203             (Color JavaDoc) category.getAttribute (StyleConstants.StrikeThrough)
204             )
205         );
206         if (category.isDefined ("waveUnderlined"))
207         attributes.add (
208             "waveUnderlined",
209             XMLStorage.colorToString (
210             (Color JavaDoc) category.getAttribute ("waveUnderlined")
211             )
212         );
213         if (category.isDefined (StyleConstants.Underline))
214         attributes.add (
215             "underline",
216             XMLStorage.colorToString (
217             (Color JavaDoc) category.getAttribute (StyleConstants.Underline)
218             )
219         );
220         if (category.isDefined ("default"))
221                 attributes.add (
222             "default",
223             (String JavaDoc) 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 JavaDoc) 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 JavaDoc bold = Boolean.FALSE, italic = Boolean.FALSE;
246                     if (category.isDefined (StyleConstants.Bold))
247                         bold = (Boolean JavaDoc) category.getAttribute (StyleConstants.Bold);
248                     if (category.isDefined (StyleConstants.Italic))
249                         italic = (Boolean JavaDoc) 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 JavaDoc (sb));
266     }
267     
268     /**
269      * Crates FileObject for given mimeTypes and profile.
270      */

271     private static String JavaDoc getFolderName (
272         String JavaDoc[] mimeTypes,
273         String JavaDoc profile
274     ) {
275         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
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     /**
286      * Crates FileObject for given mimeTypes and profile.
287      */

288     private static FileObject createFileObject (
289         FileObject root,
290         String JavaDoc[] mimeTypes,
291         String JavaDoc profile,
292         String JavaDoc 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 JavaDoc ex) {
308             ErrorManager.getDefault ().notify (ex);
309             return null;
310         }
311     }
312        
313     private static FileObject getFO (FileObject fo, String JavaDoc next) throws IOException JavaDoc {
314         FileObject fo1 = fo.getFileObject (next);
315         if (fo1 == null)
316             return fo.createFolder (next);
317         return fo1;
318     }
319 }
320
Popular Tags