KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > eclipse > ClassPath


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.projectimport.eclipse;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import org.netbeans.modules.projectimport.LoggerFactory;
29
30 /**
31  * Represents classpath for an Eclipse project (.classpath file content)
32  *
33  * @author mkrauskopf
34  */

35 final class ClassPath {
36
37     /**
38      * Logger for this class
39      */

40     private static final Logger JavaDoc logger =
41             LoggerFactory.getDefault().createLogger(ClassPath.class);
42     
43     
44     /** Represents link in Eclipse project's classpath. */
45     static class Link {
46         public static final int TYPE_INVALID = 0;
47         public static final int TYPE_FILE = 1;
48         public static final int TYPE_FOLDER = 2;
49         
50         private String JavaDoc name;
51         private int type = TYPE_INVALID;
52         private String JavaDoc location;
53         
54         String JavaDoc getName() {
55             return name;
56         }
57         
58         void setName(String JavaDoc name) {
59             this.name = name;
60         }
61         
62         int getType() {
63             return type;
64         }
65         
66         void setType(int type) {
67             this.type = type;
68         }
69         
70         String JavaDoc getLocation() {
71             return location;
72         }
73         
74         void setLocation(String JavaDoc location) {
75             this.location = location;
76         }
77         
78         public String JavaDoc toString() {
79             return name + " = " + location + " (type: " + type + ")"; // NOI18N
80
}
81         
82         public boolean equals(Object JavaDoc obj) {
83             if (this == obj) return true;
84             if (!(obj instanceof Link)) return false;
85             final Link link = (Link) obj;
86             if (type != link.type) return false;
87             if (name != null ? !name.equals(link.name) : link.name != null)
88                 return false;
89             if (location != null ? !location.equals(link.location) : link.location != null)
90                 return false;
91             return true;
92         }
93         
94         public int hashCode() {
95             int result = 17;
96             result = 37 * result + type;
97             result = 37 * result + System.identityHashCode(name);
98             result = 37 * result + System.identityHashCode(location);
99             return result;
100         }
101     }
102     
103     private static final String JavaDoc USER_LIBRARY_PREFIX
104             = "org.eclipse.jdt.USER_LIBRARY/"; // NOI18N
105
private static final int USER_LIBRARY_PREFIX_LENGTH = USER_LIBRARY_PREFIX.length();
106     
107     private ClassPathEntry output;
108     private Collection JavaDoc/*<ClassPathEntry>*/ pathEntries = Collections.EMPTY_LIST;
109     
110     private String JavaDoc jreContainer;
111     private Collection JavaDoc/*<ClassPathEntry>*/ sourceRoots;
112     private Collection JavaDoc/*<ClassPathEntry>*/ externalSourceRoots;
113     private Collection JavaDoc/*<ClassPathEntry>*/ libraries;
114     private Collection JavaDoc/*<ClassPathEntry>*/ externalLibraries;
115     private Collection JavaDoc/*<ClassPathEntry>*/ projects;
116     private Collection JavaDoc/*<ClassPathEntry>*/ variables;
117     private Collection JavaDoc/*<ClassPathEntry>*/ userLibraries;
118     
119     /**
120      * Adds a given entry to entries list. If entry is output output member is
121      * set.
122      */

123     void addEntry(ClassPathEntry entry) {
124         if (entry != null){
125             if (entry.getType() == ClassPathEntry.TYPE_OUTPUT) {
126                 output = entry;
127             } else {
128                 addSource(entry);
129             }
130         }
131     }
132     
133     private void addSource(ClassPathEntry path) {
134         if (pathEntries == Collections.EMPTY_LIST) {
135             pathEntries = new ArrayList JavaDoc();
136         }
137         pathEntries.add(path);
138     }
139     
140     ClassPathEntry getOutput() {
141         return output;
142     }
143     
144     Collection JavaDoc/*<ClassPathEntry>*/ getEntries() {
145         return pathEntries;
146     }
147     
148     private Collection JavaDoc/*<ClassPathEntry>*/ getEntriesByType(ClassPathEntry.Type type) {
149         Collection JavaDoc/*<ClassPathEntry>*/ entries = new ArrayList JavaDoc();
150         for (Iterator JavaDoc it = pathEntries.iterator(); it.hasNext(); ) {
151             ClassPathEntry entry = (ClassPathEntry) it.next();
152             if (entry.getType() == type) {
153                 entries.add(entry);
154             }
155         }
156         return entries;
157     }
158     
159     /**
160      * Just provides more convenient access to source entries.
161      *
162      * @see #getEntries()
163      */

164     Collection JavaDoc/*<ClassPathEntry>*/ getSourceRoots() {
165         // lazy initialization
166
if (sourceRoots == null) {
167             sourceRoots = getEntriesByType(ClassPathEntry.TYPE_SOURCE);
168         }
169         return sourceRoots;
170     }
171     
172     /**
173      * Returns container classpath entry for JRE.
174      *
175      * @see #getEntries()
176      */

177     String JavaDoc getJREContainer() {
178         // lazy initialization
179
if (jreContainer == null) {
180             Collection JavaDoc col = getEntriesByType(ClassPathEntry.TYPE_CONTAINER);
181             for (Iterator JavaDoc it = col.iterator(); it.hasNext(); ) {
182                 ClassPathEntry cpe = (ClassPathEntry) it.next();
183                 if (cpe.getRawPath().startsWith(Workspace.DEFAULT_JRE_CONTAINER)) {
184                     jreContainer = cpe.getRawPath();
185                     logger.finest("jreContainer found: " + jreContainer); // NOI18N
186
break;
187                 }
188             }
189             if (jreContainer == null) {
190                 logger.fine("jreContainer wasn't found in classpath entries!"); // NOI18N
191
logger.fine("Classpath entries: " + this.getEntries()); // NOI18N
192
}
193         }
194         return jreContainer;
195     }
196     
197     /**
198      * Just provides more convenient access to external source entries.
199      *
200      * @see #getEntries()
201      */

202     Collection JavaDoc/*<ClassPathEntry>*/ getExternalSourceRoots() {
203         // lazy initialization
204
if (externalSourceRoots == null) {
205             externalSourceRoots = getEntriesByType(ClassPathEntry.TYPE_LINK);
206         }
207         return externalSourceRoots;
208     }
209     
210     /**
211      * Just provides more convenient access to library entries.
212      *
213      * @see #getEntries()
214      */

215     Collection JavaDoc/*<ClassPathEntry>*/ getLibraries() {
216         // lazy initialization
217
if (libraries == null) {
218             libraries = getEntriesByType(ClassPathEntry.TYPE_LIBRARY);
219         }
220         return libraries;
221     }
222     
223     /**
224      * Just provides more convenient access to external library entries.
225      *
226      * @see #getEntries()
227      */

228     Collection JavaDoc/*<ClassPathEntry>*/ getExternalLibraries() {
229         // lazy initialization
230
if (externalLibraries == null) {
231             externalLibraries = getEntriesByType(ClassPathEntry.TYPE_EXTERNAL_LIBRARY);
232         }
233         return externalLibraries;
234     }
235     
236     /**
237      * Returns collection of names of user defined libraries.
238      */

239     Collection JavaDoc/*<String>*/ getUserLibraries() {
240         // lazy initialization
241
if (userLibraries == null) {
242             Collection JavaDoc col = getEntriesByType(ClassPathEntry.TYPE_CONTAINER);
243             userLibraries = new HashSet JavaDoc();
244             for (Iterator JavaDoc it = col.iterator(); it.hasNext(); ) {
245                 ClassPathEntry cpe = (ClassPathEntry) it.next();
246                 String JavaDoc rawPath = cpe.getRawPath();
247                 if (rawPath.startsWith(USER_LIBRARY_PREFIX)) {
248                     userLibraries.add(rawPath.substring(USER_LIBRARY_PREFIX_LENGTH));
249                 }
250             }
251         }
252         return userLibraries;
253     }
254     
255     /**
256      * Just provides more convenient access to project entries.
257      *
258      * @see #getEntries()
259      */

260     Collection JavaDoc/*<ClassPathEntry>*/ getProjects() {
261         // lazy initialization
262
if (projects == null) {
263             projects = getEntriesByType(ClassPathEntry.TYPE_PROJECT);
264         }
265         return projects;
266     }
267     
268     /**
269      * Just provides more convenient access to project entries.
270      *
271      * @see #getEntries()
272      */

273     Collection JavaDoc/*<ClassPathEntry>*/ getVariables() {
274         // lazy initialization
275
if (variables == null) {
276             variables = getEntriesByType(ClassPathEntry.TYPE_VARIABLE);
277         }
278         return variables;
279     }
280 }
281
Popular Tags