1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.*; 14 import java.util.*; 15 16 import org.eclipse.core.runtime.*; 17 18 public class SourceAttachmentManager { 19 private Hashtable entries; 20 private static final String KEY_PLATFORM_PATH = "platform-path"; 22 public static class SourceAttachmentEntry { 23 private IPath entryPath; 24 private IPath attachmentPath; 25 private IPath attachmentRootPath; 26 27 public SourceAttachmentEntry(IPath entryPath, IPath attachmentPath, IPath attachmentRootPath) { 28 this.entryPath = entryPath; 29 this.attachmentPath = attachmentPath; 30 this.attachmentRootPath = attachmentRootPath; 31 } 32 33 public IPath getEntryPath() { 34 return entryPath; 35 } 36 public IPath getAttachmentPath() { 37 return attachmentPath; 38 } 39 40 public IPath getAttachmentRootPath() { 41 return attachmentRootPath; 42 } 43 } 44 47 public SourceAttachmentManager() { 48 entries = new Hashtable(); 49 initialize(); 50 } 51 52 public boolean isEmpty() { 53 return entries.isEmpty(); 54 } 55 56 public SourceAttachmentEntry findEntry(IPath entryPath) { 57 return (SourceAttachmentEntry)entries.get(entryPath); 58 } 59 60 public void addEntry(IPath libraryPath, IPath attachmentPath, IPath attachmentRootPath) { 61 entries.put(libraryPath, new SourceAttachmentEntry(libraryPath, attachmentPath, attachmentRootPath)); 62 } 63 64 private String getFileName() { 65 IPath stateLocation = PDECore.getDefault().getStateLocation(); 66 IPath stateFile = stateLocation.append("sourceAttachements.properties"); return stateFile.toOSString(); 68 } 69 70 private void initialize() { 71 String fileName = getFileName(); 72 Properties properties = new Properties(); 73 try { 74 FileInputStream fis = new FileInputStream(fileName); 75 properties.load(fis); 76 parseProperties(properties); 77 fis.close(); 78 } 79 catch (IOException e) { 80 } 81 } 82 83 private void parseProperties(Properties properties) { 84 String platformPath = properties.getProperty(KEY_PLATFORM_PATH); 85 if (platformPath==null) return; 86 IPath oldPlatformPath = new Path(platformPath); 87 IPath currentPlatformPath = ExternalModelManager.getEclipseHome(); 88 if (oldPlatformPath.equals(currentPlatformPath)==false) return; 91 for (Enumeration keys = properties.keys(); keys.hasMoreElements();) { 92 String key = (String )keys.nextElement(); 93 if (key.startsWith("entry.")) parseEntryProperty(properties.getProperty(key)); 95 } 96 } 97 98 private void parseEntryProperty(String value) { 99 int semi = value.indexOf(';'); 100 101 String library = value.substring(0, semi); 102 String paths = value.substring(semi+1); 103 104 semi = paths.indexOf(";"); 106 String att, attRoot=null; 107 if (semi!= -1) { 108 att = paths.substring(0, semi); 109 attRoot = paths.substring(semi+1); 110 } 111 else 112 att = paths; 113 addEntry(new Path(library), new Path(att), attRoot!=null?new Path(attRoot):null); 114 } 115 116 public void save() { 117 String fileName = getFileName(); 118 Properties properties = new Properties(); 119 IPath platformPath = ExternalModelManager.getEclipseHome(); 120 properties.setProperty(KEY_PLATFORM_PATH, platformPath.toOSString()); 121 122 int i=0; 123 for (Enumeration keys=entries.keys(); keys.hasMoreElements();) { 124 IPath entryPath = (IPath)keys.nextElement(); 125 SourceAttachmentEntry entry = (SourceAttachmentEntry)entries.get(entryPath); 126 String library = entry.getEntryPath().toOSString(); 127 String value; 128 if (entry.getAttachmentRootPath()!=null) 129 value = library+";"+entry.getAttachmentPath().toOSString()+";"+entry.getAttachmentRootPath().toOSString(); else 131 value = library+";"+entry.getAttachmentPath().toOSString(); i++; 133 properties.setProperty("entry."+i, value); } 135 try { 136 FileOutputStream fos = new FileOutputStream(fileName); 137 properties.store(fos, "User-defined source attachments"); fos.flush(); 139 fos.close(); 140 } 141 catch (IOException e) { 142 } 143 } 144 } 145 | Popular Tags |