KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > MIMEOptionFolder


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.modules.editor.options;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import org.netbeans.editor.Settings;
32
33 import org.openide.cookies.InstanceCookie;
34 import org.openide.filesystems.FileChangeAdapter;
35 import org.openide.filesystems.FileEvent;
36 import org.openide.filesystems.FileLock;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileStateInvalidException;
39 import org.openide.filesystems.FileSystem;
40 import org.openide.loaders.DataFolder;
41 import org.openide.loaders.DataObject;
42 import org.openide.loaders.FolderInstance;
43 import org.openide.util.TaskListener;
44 import org.openide.util.Task;
45 import org.openide.xml.XMLUtil;
46 import org.w3c.dom.Document JavaDoc;
47 import org.openide.loaders.DataObjectNotFoundException;
48 import java.lang.ClassNotFoundException JavaDoc;
49 import org.netbeans.editor.BaseKit;
50 import org.openide.loaders.DataObjectExistsException;
51 import java.lang.reflect.Field JavaDoc;
52 import org.openide.filesystems.Repository;
53
54
55 /** MIME Options Folder representation.
56  * Folder maintains MIME specific settings.
57  * The folder contains XML settings files like fontscolors.xml,
58  * abbreviations.xml, macros.xml, properties.xml ...
59  * The folder also contains multi property subFolders like Popup, Macros, Abbreviations ...
60  *
61  * @author Martin Roskanin
62  * @since 08/2001
63  */

64
65 public class MIMEOptionFolder{
66     
67     private Map JavaDoc files = new HashMap JavaDoc(5);
68     private BaseOptions base;
69     private DataFolder folder;
70     private Map JavaDoc mpFolderMap = new Hashtable JavaDoc();
71     private Map JavaDoc subFolders = new Hashtable JavaDoc();
72     
73     /** Creates new MIMEOptionFolder */
74     public MIMEOptionFolder(DataFolder f, BaseOptions bean){
75         folder = f;
76         
77         /* commenting due to issue #65446
78         folder.getPrimaryFile().addFileChangeListener(new FileChangeAdapter(){
79             // update settings if new xml settings files appear
80             public void fileDataCreated(FileEvent fe) {
81                 loadAllFiles();
82             }
83
84             public void fileChanged(FileEvent fe) {
85                 loadAllFiles();
86             }
87             
88         });
89          */

90         
91         base = bean;
92         loadAllFiles();
93     }
94     
95     /** Creates instance of all founded and recognized XML files */
96     protected Object JavaDoc createInstance(InstanceCookie[] cookies)
97     throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
98         for (int i = 0; i < cookies.length; i++) {
99             if ( !(MIMEProcessor.class.isAssignableFrom(cookies[i].instanceClass() ))){
100                 continue;
101             }
102             
103             MIMEProcessor mp = (MIMEProcessor) cookies[i].instanceCreate();
104             if (!files.containsKey(mp.instanceClass())){
105                 synchronized(Settings.class){
106                     files.put(
107                     mp.instanceClass(),
108                     mp.createMIMEOptionFile(base, mp)
109                     );
110                 }
111             } else {
112                 MIMEOptionFile mof = (MIMEOptionFile) files.get(mp.instanceClass());
113                 if (mof != null){
114                     mof.reloadSettings();
115                 }
116             }
117         }
118         
119         return null;
120     }
121     
122     private void loadAllFiles(){
123         DataObject dob[] = folder.getChildren();
124         for (int i=0; i<dob.length; i++){
125             InstanceCookie ic = (InstanceCookie)dob[i].getCookie(InstanceCookie.class);
126             if (ic !=null){
127                 InstanceCookie instanceCookie[] = new InstanceCookie[]{ic};
128                 try{
129                     createInstance(instanceCookie);
130                 }catch(ClassNotFoundException JavaDoc cnfe){
131                     cnfe.printStackTrace();
132                 }catch(IOException JavaDoc ioex){
133                     ioex.printStackTrace();
134                 }
135             }
136         }
137     }
138     
139     /** Gets Multi Property Folder */
140     MultiPropertyFolder getMPFolder(String JavaDoc folderName, boolean forceCreation){
141         // check local map first
142
synchronized (Settings.class){
143             MultiPropertyFolder mpFolder = (MultiPropertyFolder) mpFolderMap.get(folderName);
144             if (mpFolder != null) return mpFolder;
145         }
146
147         FileObject fo = Repository.getDefault().getDefaultFileSystem().
148             findResource(folder.getPrimaryFile().getPath()+"/"+folderName); //NOI18N
149

150         if ( (fo==null) && forceCreation){
151             // let's create a DataFolder
152
try{
153                 DataFolder.create(folder,folderName);
154                 fo = Repository.getDefault().getDefaultFileSystem().
155                     findResource(folder.getPrimaryFile().getPath()+"/"+folderName); //NOI18N
156
}catch(IOException JavaDoc ioe){
157                 return null;
158             }
159         }
160
161         if (fo == null ) return null;
162
163         DataFolder df = DataFolder.findFolder(fo);
164         if (df!=null){
165             synchronized (Settings.class){
166                 MultiPropertyFolder mpFolder;
167                 if (!mpFolderMap.containsKey(folderName)){
168                     mpFolder = new MultiPropertyFolder(df, base);
169                     mpFolderMap.put(folderName, mpFolder);
170                 }else{
171                     mpFolder = (MultiPropertyFolder) mpFolderMap.get(folderName);
172                 }
173                 return mpFolder;
174             }
175         }
176         
177         return null;
178     }
179     
180     /** Gets Multi Property folder properties
181      * @param folderName the name of multi property subFolder */

182     List JavaDoc getFolderProperties(String JavaDoc folderName){
183         MultiPropertyFolder mpFolder = getMPFolder(folderName, false);
184         return (mpFolder!=null) ? mpFolder.getProperties() : new ArrayList JavaDoc();
185     }
186     
187     /** Sets Multi Property folder properties
188      * @param folderName the name of multi property subFolder
189      * @param props List of new properties values */

190     void setFolderProperties(String JavaDoc folderName, List JavaDoc props){
191         MultiPropertyFolder mpFolder = getMPFolder(folderName, true);
192         if (mpFolder!=null){
193             mpFolder.setProperties(props);
194         }
195     }
196
197     /** Gets the data folder of this MIMEOptionFolder */
198     protected DataFolder getDataFolder(){
199         return folder;
200     }
201     
202     /** Gets MIME Option subFolder from this folder
203      * @param subFolder the name of subFolder */

204     protected MIMEOptionFolder getFolder(String JavaDoc subFolder){
205
206         synchronized (Settings.class) {
207             if (subFolders.get(subFolder) != null){
208                 return (MIMEOptionFolder)subFolders.get(subFolder);
209             }
210
211             org.openide.filesystems.FileObject f = Repository.getDefault().getDefaultFileSystem().
212                 findResource(folder.getPrimaryFile().getPath()+"/"+subFolder); // NOI18N
213
if (f==null) return null;
214
215                 try {
216                     DataObject d = DataObject.find(f);
217                     DataFolder df = (DataFolder)d.getCookie(DataFolder.class);
218                     if (df != null) {
219                         MIMEOptionFolder mof = new MIMEOptionFolder(df, base);
220                         subFolders.put(subFolder, mof);
221                         return mof;
222                     }
223                 } catch (org.openide.loaders.DataObjectNotFoundException ex) {
224                     ex.printStackTrace();
225                 }
226
227
228
229             return null;
230         }
231     }
232     
233     
234     /** Gets MIME specific file located in this folder
235      * @param file Processor file class
236      * @param forceCreation if true, empty XML file will be created
237      * (used for saving the file ). False is used for loading file. */

238     protected MIMEOptionFile getFile(Class JavaDoc file, boolean forceCreation){
239         
240         if (forceCreation && (!files.containsKey(file))){
241             String JavaDoc publicID = null;
242             String JavaDoc systemID = null;
243             String JavaDoc tagRoot = null;
244             String JavaDoc fn = null;
245             
246             if (FontsColorsMIMEProcessor.class.isAssignableFrom(file)){
247                 publicID = FontsColorsMIMEProcessor.PUBLIC_ID;
248                 systemID = FontsColorsMIMEProcessor.SYSTEM_ID;
249                 tagRoot = FontsColorsMIMEOptionFile.TAG_ROOT;
250                 fn = FontsColorsMIMEOptionFile.FILENAME;
251             }
252             
253             else if (AbbrevsMIMEProcessor.class.isAssignableFrom(file)){
254                 publicID = AbbrevsMIMEProcessor.PUBLIC_ID;
255                 systemID = AbbrevsMIMEProcessor.SYSTEM_ID;
256                 tagRoot = AbbrevsMIMEOptionFile.TAG_ROOT;
257                 fn = AbbrevsMIMEOptionFile.FILENAME;
258             }
259             
260             else if (MacrosMIMEProcessor.class.isAssignableFrom(file)){
261                 publicID = MacrosMIMEProcessor.PUBLIC_ID;
262                 systemID = MacrosMIMEProcessor.SYSTEM_ID;
263                 tagRoot = MacrosMIMEOptionFile.TAG_ROOT;
264                 fn = MacrosMIMEOptionFile.FILENAME;
265             }
266             
267             else if (KeyBindingsMIMEProcessor.class.isAssignableFrom(file)){
268                 publicID = KeyBindingsMIMEProcessor.PUBLIC_ID;
269                 systemID = KeyBindingsMIMEProcessor.SYSTEM_ID;
270                 tagRoot = KeyBindingsMIMEOptionFile.TAG_ROOT;
271                 fn = KeyBindingsMIMEOptionFile.FILENAME;
272             }
273             
274             else if (PropertiesMIMEProcessor.class.isAssignableFrom(file)){
275                 publicID = PropertiesMIMEProcessor.PUBLIC_ID;
276                 systemID = PropertiesMIMEProcessor.SYSTEM_ID;
277                 tagRoot = PropertiesMIMEOptionFile.TAG_ROOT;
278                 fn = PropertiesMIMEOptionFile.FILENAME;
279             }
280             
281             else{
282                 // providing possibility for other MIMEProcessor types
283
Object JavaDoc inst = null;
284
285                 try{
286                     inst = file.newInstance();
287                 }catch(InstantiationException JavaDoc ie){
288                     return null;
289                 }catch(IllegalAccessException JavaDoc iae){
290                     return null;
291                 }
292
293                 // Get rid of unknown processors
294
if (!(inst instanceof MIMEProcessor)) return null;
295
296                 MIMEProcessor processorInst = (MIMEProcessor)inst;
297
298                 publicID = processorInst.getPublicID();
299                 systemID = processorInst.getSystemID();
300
301                 Class JavaDoc mofClass = processorInst.getAsociatedMIMEOptionFile();
302
303                 try{
304                     Field JavaDoc tagRootField = mofClass.getDeclaredField("TAG_ROOT"); //NOI18N
305
if (tagRootField != null){
306                         Object JavaDoc objFld = tagRootField.get(mofClass);
307                         if ((objFld != null) && (objFld instanceof String JavaDoc)){
308                             tagRoot = (String JavaDoc) objFld;
309                         }
310                     }
311
312                     Field JavaDoc fnField = mofClass.getDeclaredField("FILENAME"); //NOI18N
313
if (fnField != null){
314                         Object JavaDoc objFld = fnField.get(mofClass);
315                         if ((objFld != null) && (objFld instanceof String JavaDoc)){
316                             fn = (String JavaDoc) objFld;
317                         }
318                     }
319                 }catch (Exception JavaDoc exc){
320                     return null;
321                 }
322                 
323             }
324             
325             if(publicID == null || systemID == null || tagRoot == null || fn == null) return null;
326             
327             synchronized (Settings.class){
328                 createEmptyXMLFile(fn, tagRoot, publicID, systemID);
329             }
330         }
331         return (MIMEOptionFile)files.get(file);
332     }
333     
334     /** Creates empty XML file with given name, root tag, publis ID and system ID */
335     private void createEmptyXMLFile(String JavaDoc fileName, String JavaDoc tagRoot, String JavaDoc publicID, String JavaDoc systemID){
336
337         final String JavaDoc fn = fileName;
338         
339         final Document JavaDoc doc = XMLUtil.createDocument(tagRoot, null, publicID, systemID);
340         
341         try{
342             final FileObject[] fileObj = new FileObject[1];
343             folder.getPrimaryFile().getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
344                 public void run() throws IOException JavaDoc {
345                     if( folder.getPrimaryFile().getFileObject(fn, "xml") != null) return; //NOI18N
346
// file doesn't exist, create it.
347
fileObj[0] = folder.getPrimaryFile().createData(fn, "xml"); // NOI18N
348
FileLock lock = fileObj[0].lock();
349                     try {
350                         OutputStream JavaDoc os = fileObj[0].getOutputStream(lock);
351                         try {
352                             XMLUtil.write(doc, os, "UTF-8"); // NOI18N
353
os.flush();
354                         } finally {
355                             os.close();
356                         }
357                     } finally {
358                         lock.releaseLock();
359                     }
360                 }
361             });
362             
363             if (fileObj[0]==null) return;
364             
365             try{
366                 DataObject dobj = DataObject.find(fileObj[0]);
367                 if (dobj!=null){
368                     InstanceCookie ic = (InstanceCookie)dobj.getCookie(InstanceCookie.class);
369                     if (ic !=null){
370                         InstanceCookie instanceCookie[] = new InstanceCookie[]{ic};
371                         try{
372                             createInstance(instanceCookie);
373                         }catch(ClassNotFoundException JavaDoc cnfe){
374                             cnfe.printStackTrace();
375                         }catch(IOException JavaDoc ioex){
376                             ioex.printStackTrace();
377                         }
378                     }
379                 }
380             }catch(DataObjectNotFoundException donf){
381                 donf.printStackTrace();
382             }
383             
384         }catch(FileStateInvalidException fsie){
385             fsie.printStackTrace();
386         }catch(IOException JavaDoc ioe){
387             ioe.printStackTrace();
388         }
389     }
390     
391 }
392
393
Popular Tags