KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > SourceLocationManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
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 JavaDoc file) {
33             this.loc = loc;
34             this.file = file;
35         }
36         SourceLocation loc;
37         File JavaDoc file;
38     }
39     
40     public void reset() {
41         fExtensionLocations = null;
42     }
43
44     public SourceLocation[] getUserLocations() {
45         ArrayList JavaDoc userLocations = new ArrayList JavaDoc();
46         String JavaDoc 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 JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc pluginDir = pluginBase.getId();
87             if (pluginDir == null)
88                 return null;
89             String JavaDoc version = pluginBase.getVersion();
90             if (version != null) {
91                 Version vid = new Version(version);
92                 pluginDir += "_" + vid.toString(); //$NON-NLS-1$
93
}
94             IPath path = new Path(pluginDir);
95             return sourcePath == null ? path : path.append(sourcePath);
96         } catch (IllegalArgumentException JavaDoc 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 JavaDoc 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 JavaDoc text) {
119         String JavaDoc 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 JavaDoc e) {
132             return null;
133         }
134         return new SourceLocation(new Path(path));
135     }
136
137     private void parseSavedSourceLocations(String JavaDoc text, ArrayList JavaDoc entries) {
138         text = text.replace(File.pathSeparatorChar, ';');
139         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(text, ";"); //$NON-NLS-1$
140
while (stok.hasMoreTokens()) {
141             String JavaDoc 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 JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc 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())) { //$NON-NLS-1$
161
processExtension(extension, result);
162             }
163         }
164     }
165     
166     private static void processExtension(IPluginExtension extension, ArrayList JavaDoc result) {
167         IPluginObject[] children = extension.getChildren();
168         for (int j = 0; j < children.length; j++) {
169             if (children[j].getName().equals("location")) { //$NON-NLS-1$
170
IPluginElement element = (IPluginElement) children[j];
171                 String JavaDoc pathValue = element.getAttribute("path").getValue(); //$NON-NLS-1$b
172
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