KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import org.netbeans.modules.projectimport.LoggerFactory;
31 import org.openide.ErrorManager;
32
33 /**
34  * Provides access to an eclipse workspace.
35  *
36  * @author mkrauskopf
37  */

38 public final class Workspace {
39
40     /** Logger for this class. */
41     private static final Logger JavaDoc logger =
42             LoggerFactory.getDefault().createLogger(Workspace.class);
43     
44     /** Represents variable in Eclipse project's classpath. */
45     static class Variable {
46         private String JavaDoc name;
47         private String JavaDoc location;
48         
49         String JavaDoc getName() {
50             return name;
51         }
52         
53         void setName(String JavaDoc name) {
54             this.name = name;
55         }
56         
57         String JavaDoc getLocation() {
58             return location;
59         }
60         
61         void setLocation(String JavaDoc location) {
62             this.location = location;
63         }
64         
65         public String JavaDoc toString() {
66             return name + " = " + location;
67         }
68         
69         public boolean equals(Object JavaDoc obj) {
70             if (this == obj) return true;
71             if (!(obj instanceof Variable)) return false;
72             final Variable var = (Variable) obj;
73             if (name != null ? !name.equals(var.name) :var.name != null)
74                 return false;
75             if (location != null ? !location.equals(var.location) : var.location != null)
76                 return false;
77             return true;
78         }
79         
80         public int hashCode() {
81             int result = 17;
82             result = 37 * result + System.identityHashCode(name);
83             result = 37 * result + System.identityHashCode(location);
84             return result;
85         }
86     }
87     
88     private static final String JavaDoc RUNTIME_SETTINGS =
89             ".metadata/.plugins/org.eclipse.core.runtime/.settings/";
90     static final String JavaDoc CORE_PREFERENCE =
91             RUNTIME_SETTINGS + "org.eclipse.jdt.core.prefs";
92     static final String JavaDoc LAUNCHING_PREFERENCES =
93             RUNTIME_SETTINGS + "org.eclipse.jdt.launching.prefs";
94     
95     static final String JavaDoc RESOURCE_PROJECTS_DIR =
96             ".metadata/.plugins/org.eclipse.core.resources/.projects";
97     
98     static final String JavaDoc DEFAULT_JRE_CONTAINER =
99             "org.eclipse.jdt.launching.JRE_CONTAINER";
100     
101     private File JavaDoc corePrefFile;
102     private File JavaDoc launchingPrefsFile;
103     private File JavaDoc resourceProjectsDir;
104     private File JavaDoc workspaceDir;
105     
106     private Set JavaDoc variables;
107     private Set JavaDoc projects = new HashSet JavaDoc();
108     private Map JavaDoc jreContainers;
109     private Map JavaDoc userLibraries;
110     
111     /**
112      * Returns <code>Workspace</code> instance representing Eclipse Workspace
113      * found in the given <code>workspaceDir</code>. If a workspace is not found
114      * in the specified directory, <code>null</code> is returned.
115      *
116      * @return either a <code>Workspace</code> instance or null if a given
117      * <code>workspaceDir</code> doesn't contain valid Eclipse workspace.
118      */

119     static Workspace createWorkspace(File JavaDoc workspaceDir) {
120         if (!EclipseUtils.isRegularWorkSpace(workspaceDir)) {
121             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
122                     "There is not a regular workspace in " + workspaceDir);
123             return null;
124         }
125         Workspace workspace = new Workspace(workspaceDir);
126         return workspace;
127     }
128     
129     /** Sets up a workspace directory. */
130     private Workspace(File JavaDoc workspaceDir) {
131         this.workspaceDir = workspaceDir;
132         corePrefFile = new File JavaDoc(workspaceDir, CORE_PREFERENCE);
133         launchingPrefsFile = new File JavaDoc(workspaceDir, LAUNCHING_PREFERENCES);
134         resourceProjectsDir = new File JavaDoc(workspaceDir, RESOURCE_PROJECTS_DIR);
135     }
136     
137     File JavaDoc getDirectory() {
138         return workspaceDir;
139     }
140     
141     File JavaDoc getCorePreferenceFile() {
142         return corePrefFile;
143     }
144     
145     File JavaDoc getLaunchingPrefsFile() {
146         return launchingPrefsFile;
147     }
148     
149     File JavaDoc getResourceProjectsDir() {
150         return resourceProjectsDir;
151     }
152     
153     void addVariable(Variable var) {
154         if (variables == null) {
155             variables = new HashSet JavaDoc();
156         }
157         variables.add(var);
158     }
159     
160     Set JavaDoc getVariables() {
161         return variables;
162     }
163     
164     void setJREContainers(Map JavaDoc jreContainers) {
165         this.jreContainers = jreContainers;
166     }
167     
168     void addProject(EclipseProject project) {
169         projects.add(project);
170     }
171     
172     void addUserLibrary(String JavaDoc libName, Collection JavaDoc jars) {
173         if (userLibraries == null) {
174             userLibraries = new HashMap JavaDoc();
175         }
176         userLibraries.put(libName, jars);
177     }
178     
179     Collection JavaDoc getJarsForUserLibrary(String JavaDoc libRawPath) {
180         return (Collection JavaDoc) userLibraries.get(libRawPath);
181     }
182     
183     /**
184      * Tries to find an <code>EclipseProject</code> in the workspace and either
185      * returns its instance or null in the case it's not found.
186      */

187     EclipseProject getProjectByRawPath(String JavaDoc rawPath) {
188         EclipseProject project = null;
189         for (Iterator JavaDoc it = projects.iterator(); it.hasNext(); ) {
190             EclipseProject prj = (EclipseProject) it.next();
191             // rawpath = /name
192
if (prj.getName().equals(rawPath.substring(1))) {
193                 project = prj;
194             }
195         }
196         if (project == null) {
197             logger.info("Project with raw path \"" + rawPath + "\" cannot" + // NOI18N
198
" be found in project list: " + projects); // NOI18N
199
}
200         return project;
201     }
202     
203     public Set JavaDoc getProjects() {
204         return projects;
205     }
206     
207     String JavaDoc getProjectAbsolutePath(String JavaDoc projectName) {
208         for (Iterator JavaDoc it = projects.iterator(); it.hasNext(); ) {
209             EclipseProject project = ((EclipseProject) it.next());
210             if (project.getName().equals(projectName)) {
211                 return project.getDirectory().getAbsolutePath();
212             }
213         }
214         return null;
215     }
216     
217     /**
218      * Returns JDK used for compilation of project with the specified
219      * projectDirName.
220      */

221     String JavaDoc getJDKDirectory(String JavaDoc jreContainer) {
222         if (jreContainer != null) {
223             if (!DEFAULT_JRE_CONTAINER.equals(jreContainer)) {
224                 // JRE name seems to be after the last slash
225
jreContainer = jreContainer.substring(jreContainer.lastIndexOf('/') + 1);
226             }
227             for (Iterator JavaDoc it = jreContainers.entrySet().iterator(); it.hasNext(); ) {
228                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
229                 if (entry.getKey().equals(jreContainer)) {
230                     return (String JavaDoc) entry.getValue();
231                 }
232             }
233         }
234         return null;
235     }
236 }
237
Popular Tags