1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 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 ; 47 import org.openide.loaders.DataObjectNotFoundException; 48 import java.lang.ClassNotFoundException ; 49 import org.netbeans.editor.BaseKit; 50 import org.openide.loaders.DataObjectExistsException; 51 import java.lang.reflect.Field ; 52 import org.openide.filesystems.Repository; 53 54 55 64 65 public class MIMEOptionFolder{ 66 67 private Map files = new HashMap (5); 68 private BaseOptions base; 69 private DataFolder folder; 70 private Map mpFolderMap = new Hashtable (); 71 private Map subFolders = new Hashtable (); 72 73 74 public MIMEOptionFolder(DataFolder f, BaseOptions bean){ 75 folder = f; 76 77 90 91 base = bean; 92 loadAllFiles(); 93 } 94 95 96 protected Object createInstance(InstanceCookie[] cookies) 97 throws java.io.IOException , ClassNotFoundException { 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 cnfe){ 131 cnfe.printStackTrace(); 132 }catch(IOException ioex){ 133 ioex.printStackTrace(); 134 } 135 } 136 } 137 } 138 139 140 MultiPropertyFolder getMPFolder(String folderName, boolean forceCreation){ 141 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); 150 if ( (fo==null) && forceCreation){ 151 try{ 153 DataFolder.create(folder,folderName); 154 fo = Repository.getDefault().getDefaultFileSystem(). 155 findResource(folder.getPrimaryFile().getPath()+"/"+folderName); }catch(IOException 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 182 List getFolderProperties(String folderName){ 183 MultiPropertyFolder mpFolder = getMPFolder(folderName, false); 184 return (mpFolder!=null) ? mpFolder.getProperties() : new ArrayList (); 185 } 186 187 190 void setFolderProperties(String folderName, List props){ 191 MultiPropertyFolder mpFolder = getMPFolder(folderName, true); 192 if (mpFolder!=null){ 193 mpFolder.setProperties(props); 194 } 195 } 196 197 198 protected DataFolder getDataFolder(){ 199 return folder; 200 } 201 202 204 protected MIMEOptionFolder getFolder(String 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); 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 238 protected MIMEOptionFile getFile(Class file, boolean forceCreation){ 239 240 if (forceCreation && (!files.containsKey(file))){ 241 String publicID = null; 242 String systemID = null; 243 String tagRoot = null; 244 String 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 Object inst = null; 284 285 try{ 286 inst = file.newInstance(); 287 }catch(InstantiationException ie){ 288 return null; 289 }catch(IllegalAccessException iae){ 290 return null; 291 } 292 293 if (!(inst instanceof MIMEProcessor)) return null; 295 296 MIMEProcessor processorInst = (MIMEProcessor)inst; 297 298 publicID = processorInst.getPublicID(); 299 systemID = processorInst.getSystemID(); 300 301 Class mofClass = processorInst.getAsociatedMIMEOptionFile(); 302 303 try{ 304 Field tagRootField = mofClass.getDeclaredField("TAG_ROOT"); if (tagRootField != null){ 306 Object objFld = tagRootField.get(mofClass); 307 if ((objFld != null) && (objFld instanceof String )){ 308 tagRoot = (String ) objFld; 309 } 310 } 311 312 Field fnField = mofClass.getDeclaredField("FILENAME"); if (fnField != null){ 314 Object objFld = fnField.get(mofClass); 315 if ((objFld != null) && (objFld instanceof String )){ 316 fn = (String ) objFld; 317 } 318 } 319 }catch (Exception 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 335 private void createEmptyXMLFile(String fileName, String tagRoot, String publicID, String systemID){ 336 337 final String fn = fileName; 338 339 final Document 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 { 345 if( folder.getPrimaryFile().getFileObject(fn, "xml") != null) return; fileObj[0] = folder.getPrimaryFile().createData(fn, "xml"); FileLock lock = fileObj[0].lock(); 349 try { 350 OutputStream os = fileObj[0].getOutputStream(lock); 351 try { 352 XMLUtil.write(doc, os, "UTF-8"); 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 cnfe){ 374 cnfe.printStackTrace(); 375 }catch(IOException 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 ioe){ 387 ioe.printStackTrace(); 388 } 389 } 390 391 } 392 393 | Popular Tags |