KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005 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.util.ArrayList JavaDoc;
14 import java.util.HashSet 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.IAccessRule;
23 import org.eclipse.jdt.core.IClasspathAttribute;
24 import org.eclipse.jdt.core.IClasspathEntry;
25 import org.eclipse.jdt.core.IJavaModelStatus;
26 import org.eclipse.jdt.core.IJavaProject;
27 import org.eclipse.jdt.core.JavaConventions;
28 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.pde.core.build.IBuild;
30 import org.eclipse.pde.core.build.IBuildEntry;
31 import org.eclipse.pde.core.build.IBuildModel;
32 import org.eclipse.pde.core.plugin.IPluginLibrary;
33 import org.eclipse.pde.core.plugin.IPluginModelBase;
34 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
35 import org.eclipse.pde.internal.core.util.CoreUtility;
36 import org.eclipse.team.core.RepositoryProvider;
37
38 public class ClasspathComputer {
39     
40     public static void setClasspath(IProject project, IPluginModelBase model) throws CoreException {
41         IClasspathEntry[] entries = getClasspath(project, model, false);
42         JavaCore.create(project).setRawClasspath(entries, null);
43     }
44     
45     public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase model, boolean clear) throws CoreException {
46
47         ArrayList JavaDoc result = new ArrayList JavaDoc();
48                 
49         // add own libraries/source
50
addSourceAndLibraries(project, model, clear, result);
51     
52         // add JRE
53
result.add(ClasspathUtilCore.createJREEntry());
54
55         // add pde container
56
result.add(ClasspathUtilCore.createContainerEntry());
57
58         IClasspathEntry[] entries = (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
59         IJavaProject javaProject = JavaCore.create(project);
60         IJavaModelStatus validation =
61             JavaConventions.validateClasspath(
62                                 javaProject,
63                                 entries,
64                                 javaProject.getOutputLocation());
65         if (!validation.isOK()) {
66             PDECore.logErrorMessage(validation.getMessage());
67             throw new CoreException(validation);
68         }
69         return (IClasspathEntry[])result.toArray(new IClasspathEntry[result.size()]);
70     }
71
72     private static void addSourceAndLibraries(IProject project, IPluginModelBase model, boolean clear,
73             ArrayList JavaDoc result) throws CoreException {
74         
75         HashSet JavaDoc paths = new HashSet JavaDoc();
76
77         // keep existing source folders
78
if (!clear) {
79             IClasspathEntry[] entries = JavaCore.create(project).getRawClasspath();
80             for (int i = 0; i < entries.length; i++) {
81                 IClasspathEntry entry = entries[i];
82                 if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().segmentCount() > 1) {
83                     if (paths.add(entry.getPath()))
84                         result.add(entry);
85                 }
86             }
87         }
88
89         IBuild build = getBuild(project);
90         IClasspathAttribute[] attrs = getClasspathAttributes(project, model);
91         IPluginLibrary[] libraries = model.getPluginBase().getLibraries();
92         for (int i = 0; i < libraries.length; i++) {
93             IBuildEntry buildEntry = build == null ? null : build.getEntry("source." + libraries[i].getName()); //$NON-NLS-1$
94
if (buildEntry != null) {
95                 addSourceFolder(buildEntry, project, paths, result);
96             } else {
97                 if (libraries[i].getName().equals(".")) //$NON-NLS-1$
98
addJARdPlugin(project, getFilename(model), attrs, result);
99                 else
100                     addLibraryEntry(project, libraries[i], libraries[i].isExported(), attrs, result);
101             }
102         }
103         if (libraries.length == 0) {
104             if (build != null) {
105                 IBuildEntry buildEntry = build == null ? null : build.getEntry("source.."); //$NON-NLS-1$
106
if (buildEntry != null) {
107                     addSourceFolder(buildEntry, project, paths, result);
108                 }
109             } else if (ClasspathUtilCore.isBundle(model)) {
110                 addJARdPlugin(project, getFilename(model), attrs, result);
111             }
112         }
113     }
114     
115     private static IClasspathAttribute[] getClasspathAttributes(IProject project, IPluginModelBase model) {
116         IClasspathAttribute[] attributes = new IClasspathAttribute[0];
117         if (!RepositoryProvider.isShared(project)) {
118             JavadocLocationManager manager = PDECore.getDefault().getJavadocLocationManager();
119             String JavaDoc javadoc = manager.getJavadocLocation(model);
120             if (javadoc != null) {
121                 attributes = new IClasspathAttribute[]
122                    {JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadoc)};
123             }
124         }
125         return attributes;
126     }
127     
128     private static void addSourceFolder(IBuildEntry buildEntry, IProject project, HashSet JavaDoc paths, ArrayList JavaDoc result) throws CoreException {
129         String JavaDoc[] folders = buildEntry.getTokens();
130         for (int j = 0; j < folders.length; j++) {
131             String JavaDoc folder = folders[j];
132             IPath path = project.getFullPath().append(folder);
133             if (paths.add(path)) {
134                 if (project.findMember(folder) == null)
135                     CoreUtility.createFolder(project.getFolder(folder));
136                 result.add(JavaCore.newSourceEntry(path));
137             }
138         }
139     }
140     
141     public static String JavaDoc getFilename(IPluginModelBase model) {
142         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
143         String JavaDoc id = model.getPluginBase().getId();
144         if (id != null)
145             buffer.append(id);
146         buffer.append("_"); //$NON-NLS-1$
147
String JavaDoc version = model.getPluginBase().getVersion();
148         if (version != null)
149             buffer.append(version);
150         buffer.append(".jar"); //$NON-NLS-1$
151
return buffer.toString();
152     }
153
154     protected static IBuild getBuild(IProject project) throws CoreException {
155         IFile buildFile = project.getFile("build.properties"); //$NON-NLS-1$
156
IBuildModel buildModel = null;
157         if (buildFile.exists()) {
158             buildModel = new WorkspaceBuildModel(buildFile);
159             buildModel.load();
160         }
161         return (buildModel != null) ? buildModel.getBuild() : null;
162     }
163     
164     private static void addLibraryEntry(IProject project, IPluginLibrary library, boolean exported, IClasspathAttribute[] attrs, ArrayList JavaDoc result) {
165         String JavaDoc name = ClasspathUtilCore.expandLibraryName(library.getName());
166         IResource jarFile = project.findMember(name);
167         if (jarFile != null) {
168             IResource resource = project.findMember(getSourceZipName(name));
169             if (resource == null)
170                 resource = project.findMember(new Path(getSourceZipName(name)).lastSegment());
171             IPath srcAttachment = resource != null ? resource.getFullPath() : null;
172             IClasspathEntry entry = JavaCore.newLibraryEntry(jarFile.getFullPath(), srcAttachment, null, new IAccessRule[0], attrs, exported);
173             if (!result.contains(entry))
174                 result.add(entry);
175         }
176     }
177
178     private static void addJARdPlugin(IProject project, String JavaDoc filename, IClasspathAttribute[] attrs, ArrayList JavaDoc result) {
179         String JavaDoc name = ClasspathUtilCore.expandLibraryName(filename);
180         IResource jarFile = project.findMember(name);
181         if (jarFile != null) {
182             IResource resource = project.findMember(getSourceZipName(name));
183             IPath srcAttachment = resource != null ? resource.getFullPath() : jarFile.getFullPath();
184             IClasspathEntry entry =
185                 JavaCore.newLibraryEntry(jarFile.getFullPath(), srcAttachment, null, new IAccessRule[0], attrs, true);
186             if (!result.contains(entry))
187                 result.add(entry);
188         }
189     }
190
191     public static String JavaDoc getSourceZipName(String JavaDoc libraryName) {
192         int dot = libraryName.lastIndexOf('.');
193         return (dot != -1) ? libraryName.substring(0, dot) + "src.zip" : libraryName; //$NON-NLS-1$
194
}
195     
196 }
197
Popular Tags