1 11 package org.eclipse.core.internal.resources; 12 13 import java.io.*; 14 import java.util.Properties ; 15 import java.util.Set ; 16 import org.eclipse.core.internal.utils.Messages; 17 import org.eclipse.core.resources.IResourceStatus; 18 import org.eclipse.core.resources.ResourcesPlugin; 19 import org.eclipse.core.runtime.*; 20 21 25 public class SafeFileTable { 26 protected IPath location; 27 protected Properties table; 28 29 public SafeFileTable(String pluginId) throws CoreException { 30 location = getWorkspace().getMetaArea().getSafeTableLocationFor(pluginId); 31 restore(); 32 } 33 34 public IPath[] getFiles() { 35 Set set = table.keySet(); 36 String [] keys = (String []) set.toArray(new String [set.size()]); 37 IPath[] files = new IPath[keys.length]; 38 for (int i = 0; i < keys.length; i++) 39 files[i] = new Path(keys[i]); 40 return files; 41 } 42 43 protected Workspace getWorkspace() { 44 return (Workspace) ResourcesPlugin.getWorkspace(); 45 } 46 47 public IPath lookup(IPath file) { 48 String result = table.getProperty(file.toOSString()); 49 return result == null ? null : new Path(result); 50 } 51 52 public void map(IPath file, IPath aLocation) { 53 if (aLocation == null) 54 table.remove(file); 55 else 56 table.setProperty(file.toOSString(), aLocation.toOSString()); 57 } 58 59 public void restore() throws CoreException { 60 java.io.File target = location.toFile(); 61 table = new Properties (); 62 if (!target.exists()) 63 return; 64 try { 65 FileInputStream input = new FileInputStream(target); 66 try { 67 table.load(input); 68 } finally { 69 input.close(); 70 } 71 } catch (IOException e) { 72 String message = Messages.resources_exSafeRead; 73 throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, message, e); 74 } 75 } 76 77 public void save() throws CoreException { 78 java.io.File target = location.toFile(); 79 try { 80 FileOutputStream output = new FileOutputStream(target); 81 try { 82 table.store(output, "safe table"); } finally { 84 output.close(); 85 } 86 } catch (IOException e) { 87 String message = Messages.resources_exSafeSave; 88 throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, message, e); 89 } 90 } 91 92 public void setLocation(IPath location) { 93 if (location != null) 94 this.location = location; 95 } 96 } 97 | Popular Tags |