001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014: import java.util.StringTokenizer;
015:
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IExtension;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.Path;
020: import org.eclipse.pde.core.plugin.IPluginBase;
021: import org.eclipse.pde.core.plugin.IPluginElement;
022: import org.eclipse.pde.core.plugin.IPluginExtension;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.core.plugin.IPluginObject;
025: import org.eclipse.pde.core.plugin.ISharedPluginModel;
026: import org.eclipse.pde.core.plugin.PluginRegistry;
027: import org.osgi.framework.Version;
028:
029: public class SourceLocationManager implements ICoreConstants {
030: private SourceLocation[] fExtensionLocations = null;
031:
032: class SearchResult {
033: SearchResult(SourceLocation loc, File file) {
034: this .loc = loc;
035: this .file = file;
036: }
037:
038: SourceLocation loc;
039: File file;
040: }
041:
042: public void reset() {
043: fExtensionLocations = null;
044: }
045:
046: public SourceLocation[] getUserLocations() {
047: ArrayList userLocations = new ArrayList();
048: String pref = PDECore.getDefault().getPluginPreferences()
049: .getString(P_SOURCE_LOCATIONS);
050: if (pref.length() > 0)
051: parseSavedSourceLocations(pref, userLocations);
052: return (SourceLocation[]) userLocations
053: .toArray(new SourceLocation[userLocations.size()]);
054: }
055:
056: public SourceLocation[] getExtensionLocations() {
057: if (fExtensionLocations == null) {
058: fExtensionLocations = processExtensions();
059: }
060: return fExtensionLocations;
061: }
062:
063: public void setExtensionLocations(SourceLocation[] locations) {
064: fExtensionLocations = locations;
065: }
066:
067: public File findSourceFile(IPluginBase pluginBase, IPath sourcePath) {
068: IPath relativePath = getRelativePath(pluginBase, sourcePath);
069: SearchResult result = findSourceLocation(relativePath);
070: return result != null ? result.file : null;
071: }
072:
073: public File findSourcePlugin(IPluginBase pluginBase) {
074: return findSourceFile(pluginBase, null);
075: }
076:
077: public IPath findSourcePath(IPluginBase pluginBase, IPath sourcePath) {
078: IPath relativePath = getRelativePath(pluginBase, sourcePath);
079: SearchResult result = findSourceLocation(relativePath);
080: return result == null ? null : result.loc.getPath().append(
081: relativePath);
082: }
083:
084: private IPath getRelativePath(IPluginBase pluginBase,
085: IPath sourcePath) {
086: try {
087: String pluginDir = pluginBase.getId();
088: if (pluginDir == null)
089: return null;
090: String version = pluginBase.getVersion();
091: if (version != null) {
092: Version vid = new Version(version);
093: pluginDir += "_" + vid.toString(); //$NON-NLS-1$
094: }
095: IPath path = new Path(pluginDir);
096: return sourcePath == null ? path : path.append(sourcePath);
097: } catch (IllegalArgumentException e) {
098: return null;
099: }
100: }
101:
102: public SearchResult findSourceLocation(IPath relativePath) {
103: if (relativePath == null)
104: return null;
105: SearchResult result = findSearchResult(getUserLocations(),
106: relativePath);
107: return (result != null) ? result : findSearchResult(
108: getExtensionLocations(), relativePath);
109: }
110:
111: private SearchResult findSearchResult(SourceLocation[] locations,
112: IPath sourcePath) {
113: for (int i = 0; i < locations.length; i++) {
114: IPath fullPath = locations[i].getPath().append(sourcePath);
115: File file = fullPath.toFile();
116: if (file.exists())
117: return new SearchResult(locations[i], file);
118: }
119: return null;
120: }
121:
122: private SourceLocation parseSourceLocation(String text) {
123: String path;
124: try {
125: text = text.trim();
126: int commaIndex = text.lastIndexOf(',');
127: if (commaIndex == -1)
128: return new SourceLocation(new Path(text));
129:
130: int atLoc = text.indexOf('@');
131: path = (atLoc == -1) ? text.substring(0, commaIndex) : text
132: .substring(atLoc + 1, commaIndex);
133: } catch (RuntimeException e) {
134: return null;
135: }
136: return new SourceLocation(new Path(path));
137: }
138:
139: private void parseSavedSourceLocations(String text,
140: ArrayList entries) {
141: text = text.replace(File.pathSeparatorChar, ';');
142: StringTokenizer stok = new StringTokenizer(text, ";"); //$NON-NLS-1$
143: while (stok.hasMoreTokens()) {
144: String token = stok.nextToken();
145: SourceLocation location = parseSourceLocation(token);
146: if (location != null)
147: entries.add(location);
148: }
149: }
150:
151: public static SourceLocation[] computeSourceLocations(
152: IPluginModelBase[] models) {
153: ArrayList result = new ArrayList();
154: for (int i = 0; i < models.length; i++) {
155: processExtensions(models[i], result);
156: }
157: return (SourceLocation[]) result
158: .toArray(new SourceLocation[result.size()]);
159: }
160:
161: private static void processExtensions(IPluginModelBase model,
162: ArrayList result) {
163: IPluginExtension[] extensions = model.getPluginBase()
164: .getExtensions();
165: for (int j = 0; j < extensions.length; j++) {
166: IPluginExtension extension = extensions[j];
167: if ((PDECore.PLUGIN_ID + ".source").equals(extension.getPoint())) { //$NON-NLS-1$
168: processExtension(extension, result);
169: }
170: }
171: }
172:
173: private static SourceLocation[] processExtensions() {
174: ArrayList result = new ArrayList();
175: IExtension[] extensions = PDECore.getDefault()
176: .getExtensionsRegistry().findExtensions(
177: PDECore.PLUGIN_ID + ".source"); //$NON-NLS-1$
178: for (int i = 0; i < extensions.length; i++) {
179: IConfigurationElement[] children = extensions[i]
180: .getConfigurationElements();
181: IPluginModelBase base = PluginRegistry
182: .findModel(extensions[i].getContributor().getName());
183: for (int j = 0; j < children.length; j++) {
184: if (children[j].getName().equals("location")) { //$NON-NLS-1$
185: String pathValue = children[j].getAttribute("path"); //$NON-NLS-1$
186: IPath path = new Path(base.getInstallLocation())
187: .append(pathValue);
188: if (path.toFile().exists()) {
189: SourceLocation location = new SourceLocation(
190: path);
191: location.setUserDefined(false);
192: if (!result.contains(location))
193: result.add(location);
194: }
195: }
196: }
197: }
198: return (SourceLocation[]) result
199: .toArray(new SourceLocation[result.size()]);
200: }
201:
202: private static void processExtension(IPluginExtension extension,
203: ArrayList result) {
204: IPluginObject[] children = extension.getChildren();
205: for (int j = 0; j < children.length; j++) {
206: if (children[j].getName().equals("location")) { //$NON-NLS-1$
207: IPluginElement element = (IPluginElement) children[j];
208: String pathValue = element
209: .getAttribute("path").getValue(); //$NON-NLS-1$b
210: ISharedPluginModel model = extension.getModel();
211: IPath path = new Path(model.getInstallLocation())
212: .append(pathValue);
213: if (path.toFile().exists()) {
214: SourceLocation location = new SourceLocation(path);
215: location.setUserDefined(false);
216: if (!result.contains(location))
217: result.add(location);
218: }
219: }
220: }
221: }
222:
223: }
|