KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > JavaSourceLookupUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.launching;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
24 import org.eclipse.debug.core.sourcelookup.containers.DirectorySourceContainer;
25 import org.eclipse.debug.core.sourcelookup.containers.ExternalArchiveSourceContainer;
26 import org.eclipse.debug.core.sourcelookup.containers.FolderSourceContainer;
27 import org.eclipse.debug.core.sourcelookup.containers.ProjectSourceContainer;
28 import org.eclipse.jdt.core.IJavaModel;
29 import org.eclipse.jdt.core.IJavaProject;
30 import org.eclipse.jdt.core.IPackageFragmentRoot;
31 import org.eclipse.jdt.core.JavaCore;
32 import org.eclipse.jdt.core.JavaModelException;
33 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
34 import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer;
35 import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer;
36
37 /**
38  * Private source lookup utils. Translates runtime classpath entries
39  * to source containers.
40  *
41  * @since 3.0
42  */

43 public class JavaSourceLookupUtil {
44
45     /**
46      * Translates the given runtime classpath entries into associated source
47      * containers.
48      *
49      * @param entries entries to translate
50      * @return source containers corresponding to the given runtime classpath entries
51      */

52     public static ISourceContainer[] translate(IRuntimeClasspathEntry[] entries) {
53         List JavaDoc containers = new ArrayList JavaDoc(entries.length);
54         for (int i = 0; i < entries.length; i++) {
55             IRuntimeClasspathEntry entry = entries[i];
56             switch (entry.getType()) {
57                 case IRuntimeClasspathEntry.ARCHIVE:
58                     IPackageFragmentRoot root = getPackageFragmentRoot(entry);
59                     if (root == null) {
60                         String JavaDoc path = entry.getSourceAttachmentLocation();
61                         ISourceContainer container = null;
62                         if (path == null) {
63                             // use the archive itself
64
path = entry.getLocation();
65                         }
66                         if (path != null) {
67                             // check if file or folder
68
File JavaDoc file = new File JavaDoc(path);
69                             if (file.isDirectory()) {
70                                 IResource resource = entry.getResource();
71                                 if (resource instanceof IContainer) {
72                                     container = new FolderSourceContainer((IContainer)resource, false);
73                                 } else {
74                                     container = new DirectorySourceContainer(file, false);
75                                 }
76                             } else {
77                                 container = new ExternalArchiveSourceContainer(path, true);
78                             }
79                             if (!containers.contains(container)) {
80                                 containers.add(container);
81                             }
82                         }
83                     } else {
84                         ISourceContainer container = new PackageFragmentRootSourceContainer(root);
85                         if (!containers.contains(container)) {
86                             containers.add(container);
87                         }
88                     }
89                     break;
90                 case IRuntimeClasspathEntry.PROJECT:
91                     IResource resource = entry.getResource();
92                     if (resource != null && resource.getType() == IResource.PROJECT) {
93                         IJavaProject javaProject = JavaCore.create((IProject)resource);
94                         ISourceContainer container = null;
95                         if (javaProject.exists()) {
96                             container = new JavaProjectSourceContainer(javaProject);
97                         } else if (resource.exists()) {
98                             container = new ProjectSourceContainer((IProject)resource, false);
99                         }
100                         if (container != null && !containers.contains(container)) {
101                             containers.add(container);
102                         }
103                     }
104                     break;
105                 default:
106                     // no other classpath types are valid in a resolved path
107
break;
108             }
109         }
110         return (ISourceContainer[]) containers.toArray(new ISourceContainer[containers.size()]);
111     }
112     
113     /**
114      * Returns whether the source attachments of the given package fragment
115      * root and runtime classpath entry are equal.
116      * <p>
117      * NOTE: If the runtime classpath entry's source attachment is not specified,
118      * then it is considered equal. This way, the corresponding package fragment
119      * root is used for source lookup if it has a source attachment or not.
120      * </p>
121      *
122      * @param root package fragment root
123      * @param entry runtime classpath entry
124      * @return whether the source attachments of the given package fragment
125      * root and runtime classpath entry are equal
126      * @throws JavaModelException
127      */

128     private static boolean isSourceAttachmentEqual(IPackageFragmentRoot root, IRuntimeClasspathEntry entry) throws JavaModelException {
129         IPath entryPath = entry.getSourceAttachmentPath();
130         if (entryPath == null) {
131             return true;
132         }
133         IPath rootPath = root.getSourceAttachmentPath();
134         if (rootPath == null) {
135             // entry has a source attachment that the pkg root does not
136
return false;
137         }
138         return rootPath.equals(entryPath);
139         
140     }
141     
142     /**
143      * Determines if the given archive runtime classpath entry exists
144      * in the workspace as a package fragment root. Returns the associated
145      * package fragment root or <code>null</code> if none.
146      *
147      * @param entry archive runtime classpath entry
148      * @return package fragment root or <code>null</code>
149      */

150     private static IPackageFragmentRoot getPackageFragmentRoot(IRuntimeClasspathEntry entry) {
151         IResource resource = entry.getResource();
152         if (resource == null) {
153             // Check all package fragment roots for case of external archive.
154
// External jars are shared, so it does not matter which project it
155
// originates from
156
IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
157             try {
158                 IJavaProject[] jps = model.getJavaProjects();
159                 for (int i = 0; i < jps.length; i++) {
160                     IJavaProject jp = jps[i];
161                     IProject p = jp.getProject();
162                     if (p.isOpen()) {
163                         IPackageFragmentRoot[] allRoots = jp.getPackageFragmentRoots();
164                         for (int j = 0; j < allRoots.length; j++) {
165                             IPackageFragmentRoot root = allRoots[j];
166                             if (root.isExternal() && root.getPath().equals(new Path(entry.getLocation()))) {
167                                 if (isSourceAttachmentEqual(root, entry)) {
168                                     // use package fragment root
169
return root;
170                                 }
171                             }
172                         }
173                     }
174                 }
175             } catch (JavaModelException e) {
176                 LaunchingPlugin.log(e);
177             }
178         } else {
179             // check if the archive is a package fragment root
180
IProject project = resource.getProject();
181             IJavaProject jp = JavaCore.create(project);
182             try {
183                 if (project.isOpen() && jp.exists()) {
184                     IPackageFragmentRoot root = jp.getPackageFragmentRoot(resource);
185                     IPackageFragmentRoot[] allRoots = jp.getPackageFragmentRoots();
186                     for (int j = 0; j < allRoots.length; j++) {
187                         if (allRoots[j].equals(root)) {
188                             // ensure source attachment paths match
189
if (isSourceAttachmentEqual(root, entry)) {
190                                 // use package fragment root
191
return root;
192                             }
193                         }
194                     }
195
196                 }
197                 // check all other java projects to see if another project references
198
// the archive
199
IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
200                 IJavaProject[] jps = model.getJavaProjects();
201                 for (int i = 0; i < jps.length; i++) {
202                     IJavaProject jp1 = jps[i];
203                     IProject p = jp1.getProject();
204                     if (p.isOpen()) {
205                         IPackageFragmentRoot[] allRoots = jp1.getPackageFragmentRoots();
206                         for (int j = 0; j < allRoots.length; j++) {
207                             IPackageFragmentRoot root = allRoots[j];
208                             if (!root.isExternal() && root.getPath().equals(entry.getPath())) {
209                                 if (isSourceAttachmentEqual(root, entry)) {
210                                     // use package fragment root
211
return root;
212                                 }
213                             }
214                         }
215                     }
216                 }
217             } catch (JavaModelException e) {
218                 LaunchingPlugin.log(e);
219             }
220         }
221         return null;
222     }
223 }
224
Popular Tags