KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > launching > StandardSourcePathProvider


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.launching;
12
13
14 import java.io.IOException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.jar.Attributes JavaDoc;
20 import java.util.jar.JarFile JavaDoc;
21 import java.util.jar.Manifest JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.debug.core.ILaunchConfiguration;
27 import org.eclipse.jdt.internal.launching.DefaultProjectClasspathEntry;
28 import org.eclipse.jdt.internal.launching.VariableClasspathEntry;
29
30 /**
31  * Default implementation of source lookup path computation and resolution.
32  * <p>
33  * This class may be subclassed.
34  * </p>
35  * @since 2.0
36  */

37 public class StandardSourcePathProvider extends StandardClasspathProvider {
38     
39     /* (non-Javadoc)
40      * @see org.eclipse.jdt.launching.IRuntimeClasspathProvider#computeUnresolvedClasspath(org.eclipse.debug.core.ILaunchConfiguration)
41      */

42     public IRuntimeClasspathEntry[] computeUnresolvedClasspath(ILaunchConfiguration configuration) throws CoreException {
43         boolean useDefault = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_SOURCE_PATH, true);
44         IRuntimeClasspathEntry[] entries = null;
45         if (useDefault) {
46             // the default source lookup path is the same as the classpath
47
entries = super.computeUnresolvedClasspath(configuration);
48         } else {
49             // recover persisted source path
50
entries = recoverRuntimePath(configuration, IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH);
51         }
52         return entries;
53
54     }
55
56     /* (non-Javadoc)
57      * @see org.eclipse.jdt.launching.IRuntimeClasspathProvider#resolveClasspath(org.eclipse.jdt.launching.IRuntimeClasspathEntry[], org.eclipse.debug.core.ILaunchConfiguration)
58      */

59     public IRuntimeClasspathEntry[] resolveClasspath(IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration) throws CoreException {
60         List JavaDoc all = new UniqueList(entries.length);
61         for (int i = 0; i < entries.length; i++) {
62             switch (entries[i].getType()) {
63                 case IRuntimeClasspathEntry.PROJECT:
64                     // a project resolves to itself for source lookup (rather than the class file output locations)
65
all.add(entries[i]);
66                     break;
67                 case IRuntimeClasspathEntry.OTHER:
68                     IRuntimeClasspathEntry2 entry = (IRuntimeClasspathEntry2)entries[i];
69                     String JavaDoc typeId = entry.getTypeId();
70                     IRuntimeClasspathEntry[] res = null;
71                     if (typeId.equals(DefaultProjectClasspathEntry.TYPE_ID)) {
72                         // add the resolved children of the project
73
IRuntimeClasspathEntry[] children = entry.getRuntimeClasspathEntries(configuration);
74                         res = JavaRuntime.resolveSourceLookupPath(children, configuration);
75                     } else if (typeId.equals(VariableClasspathEntry.TYPE_ID)) {
76                         // add the archive itself - we currently do not allow a source attachment
77
res = JavaRuntime.resolveRuntimeClasspathEntry(entry, configuration);
78                     } else {
79                         res = JavaRuntime.resolveRuntimeClasspathEntry(entry, configuration);
80                     }
81                     if (res != null) {
82                         for (int j = 0; j < res.length; j++) {
83                             all.add(res[j]);
84                             addManifestReferences(res[j], all);
85                         }
86                     }
87                     break;
88                 default:
89                     IRuntimeClasspathEntry[] resolved =JavaRuntime.resolveRuntimeClasspathEntry(entries[i], configuration);
90                     for (int j = 0; j < resolved.length; j++) {
91                         all.add(resolved[j]);
92                         addManifestReferences(resolved[j], all);
93                     }
94                     break;
95             }
96         }
97         return (IRuntimeClasspathEntry[])all.toArray(new IRuntimeClasspathEntry[all.size()]);
98     }
99
100     /**
101      * If the given entry is an archive, adds any archives referenced by the associated manifest.
102      *
103      * @param entry runtime classpath entry
104      * @param all list to add references to
105      */

106     protected void addManifestReferences(IRuntimeClasspathEntry entry, List JavaDoc all) {
107         if (entry.getType() == IRuntimeClasspathEntry.ARCHIVE) {
108             String JavaDoc location = entry.getLocation();
109             if (location != null) {
110                 JarFile JavaDoc jar = null;
111                 try {
112                     jar = new JarFile JavaDoc(location, false);
113                     Manifest JavaDoc manifest = jar.getManifest();
114                     if (manifest != null) {
115                         Attributes JavaDoc mainAttributes = manifest.getMainAttributes();
116                         if (mainAttributes != null) {
117                             String JavaDoc value = mainAttributes.getValue(Attributes.Name.CLASS_PATH);
118                             if (value != null) {
119                                 String JavaDoc[] entries = value.split("\\s+"); //$NON-NLS-1$
120
IPath base = new Path(location);
121                                 base = base.removeLastSegments(1);
122                                 for (int i = 0; i < entries.length; i++) {
123                                     IPath path = base.append(entries[i]);
124                                     if (path.toFile().exists()) {
125                                         IRuntimeClasspathEntry ref = JavaRuntime.newArchiveRuntimeClasspathEntry(path);
126                                         if (!all.contains(ref)) {
127                                             all.add(ref);
128                                         }
129                                     }
130                                 }
131                             }
132                         }
133                     }
134                 } catch (IOException JavaDoc e) {
135                 } finally {
136                     if (jar != null) {
137                         try {
138                             jar.close();
139                         } catch (IOException JavaDoc e) {
140                         }
141                     }
142                 }
143             }
144         }
145     }
146
147     /*
148      * An ArrayList that acts like a set -i.e. does not allow duplicate items.
149      * hack for bug 112774
150      */

151     class UniqueList extends ArrayList JavaDoc {
152         private static final long serialVersionUID = -7402160651027036270L;
153         HashSet JavaDoc set;
154         
155         public UniqueList(int length) {
156             super(length);
157             set = new HashSet JavaDoc(length);
158         }
159
160         public void add(int index, Object JavaDoc element) {
161             if (set.add(element))
162                 super.add(index, element);
163         }
164
165         public boolean add(Object JavaDoc o) {
166             if (set.add(o))
167                 return super.add(o);
168             return false;
169         }
170
171         public boolean addAll(Collection JavaDoc c) {
172             if (set.addAll(c))
173                 return super.addAll(c);
174             return false;
175         }
176
177         public boolean addAll(int index, Collection JavaDoc c) {
178             if (set.addAll(c))
179                 return super.addAll(index, c);
180             return false;
181         }
182
183         public void clear() {
184             set.clear();
185             super.clear();
186         }
187
188         public boolean contains(Object JavaDoc elem) {
189             return set.contains(elem);
190         }
191
192         public void ensureCapacity(int minCapacity) {
193             super.ensureCapacity(minCapacity);
194         }
195
196         public Object JavaDoc remove(int index) {
197             Object JavaDoc object = super.remove(index);
198             set.remove(object);
199             return object;
200         }
201
202         protected void removeRange(int fromIndex, int toIndex) {
203             for (int index = fromIndex; index<=toIndex; index++)
204                 remove(index);
205         }
206
207         public Object JavaDoc set(int index, Object JavaDoc element) {
208             set.remove(element);
209             if (set.add(element))
210                 return super.set(index, element);
211             return null; //should not happen.
212
}
213     }
214
215 }
216
Popular Tags