KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jdt.core.IClasspathEntry;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.osgi.service.resolver.BundleDescription;
25 import org.eclipse.pde.core.build.IBuild;
26 import org.eclipse.pde.core.build.IBuildModel;
27 import org.eclipse.pde.core.plugin.IFragment;
28 import org.eclipse.pde.core.plugin.IFragmentModel;
29 import org.eclipse.pde.core.plugin.IPlugin;
30 import org.eclipse.pde.core.plugin.IPluginBase;
31 import org.eclipse.pde.core.plugin.IPluginLibrary;
32 import org.eclipse.pde.core.plugin.IPluginModelBase;
33 import org.eclipse.pde.core.plugin.PluginRegistry;
34 import org.eclipse.pde.core.plugin.TargetPlatform;
35 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
36 import org.eclipse.pde.internal.core.bundle.BundleFragment;
37 import org.eclipse.pde.internal.core.bundle.BundlePlugin;
38 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
39 import org.eclipse.pde.internal.core.plugin.Fragment;
40 import org.eclipse.pde.internal.core.plugin.Plugin;
41 import org.eclipse.pde.internal.core.plugin.PluginBase;
42
43 public class ClasspathUtilCore {
44     
45     public static void addLibraries(IPluginModelBase model, ArrayList JavaDoc result) throws CoreException {
46         if (new File JavaDoc(model.getInstallLocation()).isFile()) {
47             addJARdPlugin(model, result);
48         } else {
49             IPluginLibrary[] libraries = model.getPluginBase().getLibraries();
50             for (int i = 0; i < libraries.length; i++) {
51                 if (IPluginLibrary.RESOURCE.equals(libraries[i].getType()))
52                     continue;
53                 IClasspathEntry entry = createLibraryEntry(libraries[i]);
54                 if (entry != null && !result.contains(entry)) {
55                     result.add(entry);
56                 }
57             }
58         }
59     }
60     
61     private static void addJARdPlugin(IPluginModelBase model, ArrayList JavaDoc result)
62             throws CoreException {
63         
64         IPath sourcePath = getSourceAnnotation(model, "."); //$NON-NLS-1$
65
if (sourcePath == null)
66             sourcePath = new Path(model.getInstallLocation());
67         
68         IClasspathEntry entry = JavaCore.newLibraryEntry(
69                         new Path(model.getInstallLocation()),
70                         sourcePath,
71                         null,
72                         false);
73         if (entry != null && !result.contains(entry)) {
74             result.add(entry);
75         }
76     }
77
78     protected static IClasspathEntry createLibraryEntry(IPluginLibrary library) {
79         
80         IClasspathEntry entry = null;
81         try {
82             String JavaDoc name = library.getName();
83             String JavaDoc expandedName = expandLibraryName(name);
84
85             IPluginModelBase model = library.getPluginModel();
86             IPath path = getPath(model, expandedName);
87             if (path == null) {
88                 if (model.isFragmentModel() || !containsVariables(name))
89                     return null;
90                 model = resolveLibraryInFragments(library, expandedName);
91                 if (model == null)
92                     return null;
93                 path = getPath(model, expandedName);
94             }
95             
96             // classpath must not contain entries referencing external folders
97
if (model.getUnderlyingResource() == null && path.toFile().isDirectory())
98                 return null;
99             
100             entry = JavaCore.newLibraryEntry(
101                     path,
102                     getSourceAnnotation(model, expandedName),
103                     null,
104                     false);
105         } catch (CoreException e) {
106         }
107         return entry;
108     }
109     
110     public static boolean hasExtensibleAPI(IPluginModelBase model) {
111         IPluginBase pluginBase = model.getPluginBase();
112         if (pluginBase instanceof IPlugin)
113             return hasExtensibleAPI((IPlugin)pluginBase);
114         return false;
115     }
116     
117     public static boolean isPatchFragment(BundleDescription desc) {
118         IPluginModelBase model = PluginRegistry.findModel(desc);
119         return model instanceof IFragmentModel
120                     ? isPatchFragment(((IFragmentModel)model).getFragment()) : false;
121     }
122     
123     public static boolean isPatchFragment(IPluginModelBase model) {
124         IPluginBase pluginBase = model.getPluginBase();
125         if (pluginBase instanceof IFragment)
126             return isPatchFragment((IFragment)pluginBase);
127         return false;
128     }
129     
130     private static boolean hasExtensibleAPI(IPlugin plugin) {
131         if (plugin instanceof Plugin)
132             return ((Plugin) plugin).hasExtensibleAPI();
133         if (plugin instanceof BundlePlugin)
134             return ((BundlePlugin) plugin).hasExtensibleAPI();
135         return false;
136     }
137         
138     private static boolean isPatchFragment(IFragment fragment) {
139         if (fragment instanceof Fragment)
140             return ((Fragment)fragment).isPatch();
141         if (fragment instanceof BundleFragment)
142             return ((BundleFragment)fragment).isPatch();
143         return false;
144     }
145     
146     public static boolean hasBundleStructure(IPluginModelBase model) {
147         if (model.getUnderlyingResource() != null)
148             return model instanceof IBundlePluginModelBase;
149         
150         IPluginBase plugin = model.getPluginBase();
151         if (plugin instanceof PluginBase)
152             return ((PluginBase)plugin).hasBundleStructure();
153         return false;
154     }
155     
156     public static boolean containsVariables(String JavaDoc name) {
157         return name.indexOf("$os$") != -1 //$NON-NLS-1$
158
|| name.indexOf("$ws$") != -1 //$NON-NLS-1$
159
|| name.indexOf("$nl$") != -1 //$NON-NLS-1$
160
|| name.indexOf("$arch$") != -1; //$NON-NLS-1$
161
}
162
163     public static String JavaDoc expandLibraryName(String JavaDoc source) {
164         if (source == null || source.length() == 0)
165             return ""; //$NON-NLS-1$
166
if (source.indexOf("$ws$") != -1) //$NON-NLS-1$
167
source =
168                 source.replaceAll(
169                     "\\$ws\\$", //$NON-NLS-1$
170
"ws" + IPath.SEPARATOR + TargetPlatform.getWS()); //$NON-NLS-1$
171
if (source.indexOf("$os$") != -1) //$NON-NLS-1$
172
source =
173                 source.replaceAll(
174                     "\\$os\\$", //$NON-NLS-1$
175
"os" + IPath.SEPARATOR + TargetPlatform.getOS()); //$NON-NLS-1$
176
if (source.indexOf("$nl$") != -1) //$NON-NLS-1$
177
source =
178                 source.replaceAll(
179                         "\\$nl\\$", //$NON-NLS-1$
180
"nl" + IPath.SEPARATOR + TargetPlatform.getNL()); //$NON-NLS-1$
181
if (source.indexOf("$arch$") != -1) //$NON-NLS-1$
182
source =
183                 source.replaceAll(
184                         "\\$arch\\$", //$NON-NLS-1$
185
"arch" + IPath.SEPARATOR + TargetPlatform.getOSArch()); //$NON-NLS-1$
186
return source;
187     }
188
189     public static IPath getSourceAnnotation(IPluginModelBase model, String JavaDoc libraryName)
190         throws CoreException {
191         String JavaDoc zipName = getSourceZipName(libraryName);
192         IPath path = getPath(model, zipName);
193         if (path == null) {
194             SourceLocationManager manager = PDECore.getDefault().getSourceLocationManager();
195             path = manager.findSourcePath(model.getPluginBase(), new Path(zipName));
196         }
197         return path;
198     }
199     
200     public static String JavaDoc getSourceZipName(String JavaDoc libraryName) {
201         int dot = libraryName.lastIndexOf('.');
202         return (dot != -1) ? libraryName.substring(0, dot) + "src.zip" : libraryName; //$NON-NLS-1$
203
}
204
205     private static IPluginModelBase resolveLibraryInFragments(
206         IPluginLibrary library,
207         String JavaDoc libraryName) {
208         IFragmentModel[] fragments =
209             PDEManager.findFragmentsFor(library.getPluginModel());
210
211         for (int i = 0; i < fragments.length; i++) {
212             IPath path = getPath(fragments[i], libraryName);
213             if (path != null)
214                 return fragments[i];
215         }
216         return null;
217     }
218
219     private static IPath getPath(IPluginModelBase model, String JavaDoc libraryName) {
220         IResource resource = model.getUnderlyingResource();
221         if (resource != null) {
222             IResource jarFile = resource.getProject().findMember(libraryName);
223             if (jarFile != null)
224                 return jarFile.getFullPath();
225         } else {
226             File JavaDoc file = new File JavaDoc(model.getInstallLocation(), libraryName);
227             if (file.exists())
228                 return new Path(file.getAbsolutePath());
229         }
230         return null;
231     }
232     
233     public static IBuild getBuild(IPluginModelBase model)
234             throws CoreException {
235         IBuildModel buildModel = model.getBuildModel();
236         if (buildModel == null) {
237             IProject project = model.getUnderlyingResource().getProject();
238             IFile buildFile = project.getFile("build.properties"); //$NON-NLS-1$
239
if (buildFile.exists()) {
240                 buildModel = new WorkspaceBuildModel(buildFile);
241                 buildModel.load();
242             }
243         }
244         return (buildModel != null) ? buildModel.getBuild() : null;
245     }
246     
247     public static String JavaDoc getFilename(IPluginModelBase model) {
248         return new Path(model.getInstallLocation()).lastSegment();
249     }
250     
251 }
252
Popular Tags