KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import org.netbeans.modules.projectimport.LoggerFactory;
33 import org.openide.filesystems.FileUtil;
34
35 /**
36  * Represents Eclipse project structure.
37  *
38  * @author mkrauskopf
39  */

40 public final class EclipseProject implements Comparable JavaDoc {
41
42     /** Logger for this class. */
43     private static final Logger JavaDoc logger =
44             LoggerFactory.getDefault().createLogger(EclipseProject.class);
45     
46     static final String JavaDoc PROJECT_FILE = ".project"; // NOI18N
47
static final String JavaDoc CLASSPATH_FILE = ".classpath"; // NOI18N
48

49     private Workspace workspace;
50     
51     private String JavaDoc name;
52     private boolean internal = true;
53     private boolean javaNature;
54     private ClassPath cp;
55     private Set JavaDoc links;
56     private Set JavaDoc otherNatures;
57     
58     private final File JavaDoc projectDir;
59     private final File JavaDoc cpFile;
60     private final File JavaDoc prjFile;
61     private String JavaDoc jdkDirectory;
62     
63     /**
64      * Returns <code>EclipseProject</code> instance representing Eclipse project
65      * found in the given <code>projectDir</code>. If a project is not found in
66      * the specified directory, <code>null</code> is returned.
67      *
68      * @return either a <code>EclipseProject</code> instance or null if a given
69      * <code>projectDir</code> doesn't contain valid Eclipse project.
70      */

71     static EclipseProject createProject(File JavaDoc projectDir) {
72         if (!EclipseUtils.isRegularProject(projectDir)) {
73             logger.fine(projectDir + " doesn't contain regular Eclipse project."); // NOI18N
74
return null;
75         }
76         return new EclipseProject(projectDir);
77     }
78     
79     /** Sets up a project directory. */
80     private EclipseProject(File JavaDoc projectDir) {
81         this.projectDir = projectDir;
82         this.cpFile = new File JavaDoc(projectDir, CLASSPATH_FILE);
83         this.prjFile = new File JavaDoc(projectDir, PROJECT_FILE);
84     }
85     
86     void setWorkspace(Workspace workspace) {
87         this.workspace = workspace;
88     }
89     
90     public Workspace getWorkspace() {
91         return workspace;
92     }
93     
94     void setClassPath(ClassPath cp) {
95         this.cp = cp;
96     }
97     
98     ClassPath getClassPath() {
99         return cp;
100     }
101     
102     /**
103      * Returns project's name.
104      */

105     public String JavaDoc getName() {
106         return name;
107     }
108     
109     void setName(String JavaDoc name) {
110         this.name = name;
111     }
112     
113     void setInternal(boolean internal) {
114         this.internal = internal;
115     }
116     
117     public boolean isInternal() {
118         return internal;
119     }
120     
121     public File JavaDoc getDirectory() {
122         return projectDir;
123     }
124     
125     /**
126      * Returns metadata file containing information about this projects. I.e.
127      * normally <em>.project</em> file withing the project's directory. See
128      * {@link #PROJECT_FILE}.
129      */

130     File JavaDoc getProjectFile() {
131         return prjFile;
132     }
133     
134     /**
135      * Returns metadata file containing information about this projects. I.e.
136      * normally <em>.classpath</em> file withing the project's directory. See
137      * {@link #CLASSPATH_FILE}.
138      */

139     File JavaDoc getClassPathFile() {
140         return cpFile;
141     }
142     
143     public boolean hasJavaNature() {
144         return javaNature;
145     }
146     
147     void setJavaNature(boolean javaNature) {
148         this.javaNature = javaNature;
149     }
150     
151     public Set JavaDoc getOtherNatures() {
152         return otherNatures;
153     }
154     
155     void addOtherNature(String JavaDoc nature) {
156         if (otherNatures == null) {
157             otherNatures = new HashSet JavaDoc();
158         }
159         logger.fine("Project " + getName() + " has another nature: " + // NOI18N
160
nature);
161         otherNatures.add(nature);
162     }
163     
164     /**
165      * Returns JDK directory for platform this project uses. Can be null in a
166      * case when a JDK was set for an eclipse project in Eclipse then the
167      * directory with JDK was deleted from filesystem and then a project is
168      * imported to NetBeans.
169      *
170      * @return JDK directory for the project
171      */

172     public String JavaDoc getJDKDirectory() {
173         if (jdkDirectory == null && workspace != null) {
174             logger.finest("Getting JDK directory for project " + this.getName()); // NOI18N
175
jdkDirectory = workspace.getJDKDirectory(cp.getJREContainer());
176             logger.finest("Resolved JDK directory: " + jdkDirectory); // NOI18N
177
// jdkDirectory = workspace.getJDKDirectory(projectDir.getName());
178
}
179         return jdkDirectory;
180     }
181     
182     /** Convenient delegate to <code>ClassPath</code> */
183     public Collection JavaDoc getSourceRoots() {
184         return cp.getSourceRoots();
185     }
186     
187     /**
188      * Returns map of file-label entries representing Eclipse project's source
189      * roots.
190      */

191     public Map JavaDoc/*<File, String>*/ getAllSourceRoots() {
192         Map JavaDoc rootsLabels = new HashMap JavaDoc();
193         
194         // internal sources
195
Collection JavaDoc srcRoots = cp.getSourceRoots();
196         for (Iterator JavaDoc it = srcRoots.iterator(); it.hasNext(); ) {
197             ClassPathEntry cpe = (ClassPathEntry) it.next();
198             File JavaDoc file = FileUtil.normalizeFile(new File JavaDoc(cpe.getAbsolutePath()));
199             rootsLabels.put(file, cpe.getRawPath());
200         }
201         // external sources
202
Collection JavaDoc extSrcRoots = cp.getExternalSourceRoots();
203         for (Iterator JavaDoc it = extSrcRoots.iterator(); it.hasNext(); ) {
204             ClassPathEntry cpe = (ClassPathEntry) it.next();
205             rootsLabels.put(
206                     FileUtil.normalizeFile(new File JavaDoc(cpe.getAbsolutePath())),
207                     cpe.getRawPath());
208         }
209         
210         return rootsLabels;
211     }
212     
213     /**
214      * Returns all libraries on the project classpath.
215      */

216     public Collection JavaDoc/*<File>*/ getAllLibrariesFiles() {
217         Collection JavaDoc files = new ArrayList JavaDoc();
218         // internal libraries
219
for (Iterator JavaDoc it = cp.getLibraries().iterator(); it.hasNext(); ) {
220             files.add(FileUtil.normalizeFile(new File JavaDoc(((ClassPathEntry)it.next()).getAbsolutePath())));
221             
222         }
223         // external libraries
224
for (Iterator JavaDoc it = cp.getExternalLibraries().iterator(); it.hasNext(); ) {
225             files.add(FileUtil.normalizeFile(new File JavaDoc(((ClassPathEntry)it.next()).getAbsolutePath())));
226         }
227         // jars in user libraries
228
for (Iterator JavaDoc it = getUserLibrariesJars().iterator(); it.hasNext(); ) {
229             files.add(FileUtil.normalizeFile(new File JavaDoc((String JavaDoc) it.next())));
230         }
231         // variables
232
for (Iterator JavaDoc it = cp.getVariables().iterator(); it.hasNext(); ) {
233             ClassPathEntry entry = (ClassPathEntry)it.next();
234             // in case a variable wasn't resolved
235
if (entry.getAbsolutePath() != null) {
236                 files.add(FileUtil.normalizeFile(new File JavaDoc(entry.getAbsolutePath())));
237             }
238         }
239         return files;
240     }
241     
242     /** Convenient delegate to <code>ClassPath</code> */
243     public Collection JavaDoc getExternalSourceRoots() {
244         return cp.getExternalSourceRoots();
245     }
246     
247     /** Convenient delegate to <code>ClassPath</code> */
248     public Collection JavaDoc getLibraries() {
249         return cp.getLibraries();
250     }
251     
252     /** Convenient delegate to <code>ClassPath</code> */
253     public Collection JavaDoc getExternalLibraries() {
254         return cp.getExternalLibraries();
255     }
256     
257     public Collection JavaDoc getUserLibrariesJars() {
258         Collection JavaDoc userLibrariesJars = new HashSet JavaDoc();
259         if (workspace != null) {
260             for (Iterator JavaDoc it = cp.getUserLibraries().iterator(); it.hasNext(); ) {
261                 userLibrariesJars.addAll(
262                         workspace.getJarsForUserLibrary((String JavaDoc) it.next()));
263             }
264         }
265         return userLibrariesJars;
266     }
267     
268     /** Convenient delegate to <code>ClassPath</code> */
269     public Collection JavaDoc getProjectsEntries() {
270         return cp.getProjects();
271     }
272     
273     private Set JavaDoc projectsWeDependOn;
274     
275     /**
276      * Returns collection of <code>EclipseProject</code> this project requires.
277      */

278     public Set JavaDoc getProjects() {
279         if (workspace != null && projectsWeDependOn == null) {
280             projectsWeDependOn = new HashSet JavaDoc();
281             for (Iterator JavaDoc it = cp.getProjects().iterator(); it.hasNext(); ) {
282                 ClassPathEntry cp = (ClassPathEntry) it.next();
283                 EclipseProject prj = workspace.getProjectByRawPath(cp.getRawPath());
284                 if (prj != null) {
285                     projectsWeDependOn.add(prj);
286                 }
287             }
288         }
289         return projectsWeDependOn == null ?
290             Collections.EMPTY_SET : projectsWeDependOn;
291     }
292     
293     /** Convenient delegate to <code>ClassPath</code> */
294     public Collection JavaDoc getVariables() {
295         return cp.getVariables();
296     }
297     
298     void addLink(ClassPath.Link link) {
299         if (links == null) {
300             links = new HashSet JavaDoc();
301         }
302         links.add(link);
303     }
304     
305     /**
306      * Inteligently sets absolute path for a given entry with recongnizing of
307      * links, projects, variables, relative and absolute entries.
308      * If it is not possible (e.g. workspace Varible is not found) sets abs.
309      * path to null.
310      */

311     void setAbsolutePathForEntry(ClassPathEntry entry) {
312         // set abs. path default (null)
313
entry.setAbsolutePath(null);
314         
315         // try to resolve entry as a CONTAINER
316
if (entry.getType() == ClassPathEntry.TYPE_CONTAINER) {
317             // we don't support CONTAINERs so we don't care about them here
318
// (we support JRE/JDK containers but those are solved elsewhere)
319
return;
320         }
321         
322         // try to resolve entry as a VARIABLE
323
if (entry.getType() == ClassPathEntry.TYPE_VARIABLE) {
324             String JavaDoc rawPath = entry.getRawPath();
325             int slashIndex = rawPath.indexOf('/');
326             if (slashIndex != -1) {
327                 Workspace.Variable parent = getVariable(
328                         rawPath.substring(0, slashIndex));
329                 if (parent != null) {
330                     entry.setAbsolutePath(parent.getLocation() +
331                             rawPath.substring(slashIndex));
332                 }
333             } else {
334                 Workspace.Variable var = getVariable(entry);
335                 if (var != null) {
336                     entry.setAbsolutePath(var.getLocation());
337                 }
338             }
339             return;
340         }
341         
342         // try to resolve entry as a PROJECT
343
if (entry.getType() == ClassPathEntry.TYPE_PROJECT) {
344             if (workspace != null) {
345                 entry.setAbsolutePath(workspace.getProjectAbsolutePath(
346                         entry.getRawPath().substring(1)));
347             }
348             // else {
349
// ErrorManager.getDefault().log(ErrorManager.WARNING, "workspace == null");
350
// }
351
return;
352         }
353         
354         // try to resolve entry as a LINK
355
ClassPath.Link link = getLink(entry.getRawPath());
356         if (link != null) {
357             logger.finest("Found link for entry \"" + entry + "\": " + link); // NOI18N
358
if (FileUtil.normalizeFile(new File JavaDoc(link.getLocation())).exists()) {
359                 // change type from source to source link
360
entry.setType(ClassPathEntry.TYPE_LINK);
361                 entry.setAbsolutePath(link.getLocation());
362             } else {
363                 logger.info("Not able to resolve absolute path for classpath" + // NOI18N
364
" entry \"" + entry.getRawPath() + "\". This classpath" + // NOI18N
365
" entry is external source which points to PATH VARIABLE" + // NOI18N
366
" which points to final destination. This feature will be" + // NOI18N
367
" supported in future version of Importer."); // NOI18N
368
entry.setType(ClassPathEntry.TYPE_UNKNOWN);
369             }
370             return;
371         }
372         
373         // not VARIABLE, not PROJECT, not LINK -> either source root or library
374
if (entry.isRawPathRelative()) {
375             // internal src or lib
376
entry.setAbsolutePath(projectDir.getAbsolutePath() + File.separator
377                     + entry.getRawPath());
378         } else {
379             // external src or lib
380
entry.setAbsolutePath(entry.getRawPath());
381         }
382     }
383     
384     /**
385      * Find variable for the given variable rawPath. Note that this method
386      * returns <code>null</code> if workspace wasn't set for the project.
387      */

388     private Workspace.Variable getVariable(String JavaDoc rawPath) {
389         if (workspace == null) {
390             // workspace wasn't set for this project
391
logger.fine("Workspace wasn't set for the project \"" + getName() + "\""); // NOI18N
392
return null;
393         }
394         Set JavaDoc variables = workspace.getVariables();
395         if (variables != null) {
396             for (Iterator JavaDoc it = workspace.getVariables().iterator(); it.hasNext(); ) {
397                 Workspace.Variable variable = (Workspace.Variable) it.next();
398                 if (variable.getName().equals(rawPath)) {
399                     return variable;
400                 }
401             }
402         }
403         logger.info("Cannot resolve variable for raw path: " + rawPath); // NOI18N
404
return null;
405     }
406     
407     /**
408      * Recongises if a given entry represents variable. If yes returns variable
409      * it represents otherwise null. Note that this method returns null if
410      * workspace wasn't set for this project.
411      */

412     private Workspace.Variable getVariable(ClassPathEntry entry) {
413         return getVariable(entry.getRawPath());
414     }
415     
416     /**
417      * Recongises if a given entry represents link. If yes returns link it
418      * represents otherwise null.
419      */

420     private ClassPath.Link getLink(String JavaDoc linkName) {
421         if (links != null) {
422             for (Iterator JavaDoc it = links.iterator(); it.hasNext(); ) {
423                 ClassPath.Link link = (ClassPath.Link) it.next();
424                 if (link.getName().equals(linkName)) {
425                     return link;
426                 }
427             }
428         }
429         return null;
430     }
431     
432     public String JavaDoc toString() {
433         return "EclipseProject[" + getName() + ", " + getDirectory() + "]"; // NOI18N
434
}
435     
436     /* name is enough for now */
437     public boolean equals(Object JavaDoc obj) {
438         if (this == obj) return true;
439         if (!(obj instanceof EclipseProject)) return false;
440         final EclipseProject ePrj = (EclipseProject) obj;
441         if (!name.equals(ePrj.name)) return false;
442         return true;
443     }
444     
445     /* name is enough for now */
446     public int hashCode() {
447         int result = 17;
448         result = 37 * result + System.identityHashCode(name);
449         return result;
450     }
451     
452     /**
453      * Compares projects based on theirs <code>name</code>s. Projects which has
454      * null-name will be last.
455      */

456     public int compareTo(Object JavaDoc o) {
457         String JavaDoc name1 = getName();
458         String JavaDoc name2 = null;
459         if (o instanceof EclipseProject) {
460             name2 = ((EclipseProject) o).getName();
461         }
462         if (name2 == null) {
463             return (name1 == null ? 0 : -1);
464         }
465         return (name1 == null ? 1 : name1.compareToIgnoreCase(name2));
466     }
467 }
468
Popular Tags