1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.StringTokenizer ; 16 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.Path; 19 import org.eclipse.pde.core.plugin.IPluginBase; 20 import org.eclipse.pde.core.plugin.IPluginElement; 21 import org.eclipse.pde.core.plugin.IPluginExtension; 22 import org.eclipse.pde.core.plugin.IPluginModelBase; 23 import org.eclipse.pde.core.plugin.IPluginObject; 24 import org.eclipse.pde.core.plugin.ISharedPluginModel; 25 import org.eclipse.pde.core.plugin.PluginRegistry; 26 import org.osgi.framework.Version; 27 28 public class SourceLocationManager implements ICoreConstants { 29 private SourceLocation[] fExtensionLocations = null; 30 31 class SearchResult { 32 SearchResult(SourceLocation loc, File file) { 33 this.loc = loc; 34 this.file = file; 35 } 36 SourceLocation loc; 37 File file; 38 } 39 40 public void reset() { 41 fExtensionLocations = null; 42 } 43 44 public SourceLocation[] getUserLocations() { 45 ArrayList userLocations = new ArrayList (); 46 String pref = PDECore.getDefault().getPluginPreferences().getString(P_SOURCE_LOCATIONS); 47 if (pref.length() > 0) 48 parseSavedSourceLocations(pref, userLocations); 49 return (SourceLocation[]) userLocations.toArray(new SourceLocation[userLocations.size()]); 50 } 51 52 public SourceLocation[] getExtensionLocations() { 53 if (fExtensionLocations == null) { 54 ArrayList list = new ArrayList (); 55 IPluginModelBase[] models = PluginRegistry.getExternalModels(); 56 for (int i = 0; i < models.length; i++) { 57 processExtensions(models[i], list); 58 } 59 fExtensionLocations = (SourceLocation[]) list.toArray(new SourceLocation[list.size()]); 60 } 61 return fExtensionLocations; 62 } 63 64 public void setExtensionLocations(SourceLocation[] locations) { 65 fExtensionLocations = locations; 66 } 67 68 public File findSourceFile(IPluginBase pluginBase, IPath sourcePath) { 69 IPath relativePath = getRelativePath(pluginBase, sourcePath); 70 SearchResult result = findSourceLocation(relativePath); 71 return result != null ? result.file : null; 72 } 73 74 public File findSourcePlugin(IPluginBase pluginBase) { 75 return findSourceFile(pluginBase, null); 76 } 77 78 public IPath findSourcePath(IPluginBase pluginBase, IPath sourcePath) { 79 IPath relativePath = getRelativePath(pluginBase, sourcePath); 80 SearchResult result = findSourceLocation(relativePath); 81 return result == null ? null : result.loc.getPath().append(relativePath); 82 } 83 84 private IPath getRelativePath(IPluginBase pluginBase, IPath sourcePath) { 85 try { 86 String pluginDir = pluginBase.getId(); 87 if (pluginDir == null) 88 return null; 89 String version = pluginBase.getVersion(); 90 if (version != null) { 91 Version vid = new Version(version); 92 pluginDir += "_" + vid.toString(); } 94 IPath path = new Path(pluginDir); 95 return sourcePath == null ? path : path.append(sourcePath); 96 } catch (IllegalArgumentException e) { 97 return null; 98 } 99 } 100 101 public SearchResult findSourceLocation(IPath relativePath) { 102 if (relativePath == null) 103 return null; 104 SearchResult result = findSearchResult(getUserLocations(), relativePath); 105 return (result != null) ? result : findSearchResult(getExtensionLocations(), relativePath); 106 } 107 108 private SearchResult findSearchResult(SourceLocation[] locations, IPath sourcePath) { 109 for (int i = 0; i < locations.length; i++) { 110 IPath fullPath = locations[i].getPath().append(sourcePath); 111 File file = fullPath.toFile(); 112 if (file.exists()) 113 return new SearchResult(locations[i], file); 114 } 115 return null; 116 } 117 118 private SourceLocation parseSourceLocation(String text) { 119 String path; 120 try { 121 text = text.trim(); 122 int commaIndex = text.lastIndexOf(','); 123 if (commaIndex == -1) 124 return new SourceLocation(new Path(text)); 125 126 int atLoc = text.indexOf('@'); 127 path = 128 (atLoc == -1) 129 ? text.substring(0, commaIndex) 130 : text.substring(atLoc + 1, commaIndex); 131 } catch (RuntimeException e) { 132 return null; 133 } 134 return new SourceLocation(new Path(path)); 135 } 136 137 private void parseSavedSourceLocations(String text, ArrayList entries) { 138 text = text.replace(File.pathSeparatorChar, ';'); 139 StringTokenizer stok = new StringTokenizer (text, ";"); while (stok.hasMoreTokens()) { 141 String token = stok.nextToken(); 142 SourceLocation location = parseSourceLocation(token); 143 if (location != null) 144 entries.add(location); 145 } 146 } 147 148 public static SourceLocation[] computeSourceLocations(IPluginModelBase[] models) { 149 ArrayList result = new ArrayList (); 150 for (int i = 0; i < models.length; i++) { 151 processExtensions(models[i], result); 152 } 153 return (SourceLocation[])result.toArray(new SourceLocation[result.size()]); 154 } 155 156 private static void processExtensions(IPluginModelBase model, ArrayList result) { 157 IPluginExtension[] extensions = model.getPluginBase().getExtensions(); 158 for (int j = 0; j < extensions.length; j++) { 159 IPluginExtension extension = extensions[j]; 160 if ((PDECore.PLUGIN_ID + ".source").equals(extension.getPoint())) { processExtension(extension, result); 162 } 163 } 164 } 165 166 private static void processExtension(IPluginExtension extension, ArrayList result) { 167 IPluginObject[] children = extension.getChildren(); 168 for (int j = 0; j < children.length; j++) { 169 if (children[j].getName().equals("location")) { IPluginElement element = (IPluginElement) children[j]; 171 String pathValue = element.getAttribute("path").getValue(); ISharedPluginModel model = extension.getModel(); 173 IPath path = new Path(model.getInstallLocation()).append(pathValue); 174 if (path.toFile().exists()) { 175 SourceLocation location = new SourceLocation(path); 176 location.setUserDefined(false); 177 if (!result.contains(location)) 178 result.add(location); 179 } 180 } 181 } 182 } 183 184 } 185 | Popular Tags |