KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileFilter JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import org.netbeans.modules.projectimport.LoggerFactory;
34 import org.netbeans.modules.projectimport.ProjectImporterException;
35
36 /**
37  * Parses given workspace and fills up it with found data.
38  *
39  * @author mkrauskopf
40  */

41 final class WorkspaceParser {
42     
43     /** Logger for this class. */
44     private static final Logger JavaDoc logger =
45             LoggerFactory.getDefault().createLogger(WorkspaceParser.class);
46     
47     private static final String JavaDoc VM_XML = "org.eclipse.jdt.launching.PREF_VM_XML"; // NOI18N
48
private static final String JavaDoc IGNORED_CP_ENTRY = "##<cp entry ignore>##"; // NOI18N
49

50     private static final String JavaDoc VARIABLE_PREFIX = "org.eclipse.jdt.core.classpathVariable."; // NOI18N
51
private static final int VARIABLE_PREFIX_LENGTH = VARIABLE_PREFIX.length();
52     
53     private static final String JavaDoc USER_LIBRARY_PREFIX = "org.eclipse.jdt.core.userLibrary."; // NOI18N
54
private static final int USER_LIBRARY_PREFIX_LENGTH = USER_LIBRARY_PREFIX.length();
55     
56     // private static final String CP_CONTAINER_PREFIX =
57
// "org.eclipse.jdt.core.classpathContainer.";
58
// private static final int CP_CONTAINER_PREFIX_LENGTH = CP_CONTAINER_PREFIX.length();
59
// private static final String CP_CONTAINER_SUFFIX =
60
// "|org.eclipse.jdt.launching.JRE_CONTAINER";
61
// private static final int CP_CONTAINER_SUFFIX_LENGTH = CP_CONTAINER_SUFFIX.length();
62

63     private final Workspace workspace;
64     
65     /** Creates a new instance of WorkspaceParser */
66     WorkspaceParser(Workspace workspace) {
67         this.workspace = workspace;
68     }
69     
70     /** Returns classpath content from project's .classpath file */
71     void parse() throws ProjectImporterException {
72         try {
73             parseLaunchingPreferences();
74             parseCorePreferences();
75             parseWorkspaceProjects();
76         } catch (IOException JavaDoc e) {
77             throw new ProjectImporterException(
78                     "Cannot load workspace properties", e); // NOI18N
79
}
80     }
81     
82     private void parseLaunchingPreferences() throws IOException JavaDoc, ProjectImporterException {
83         Properties JavaDoc launchProps = EclipseUtils.loadProperties(workspace.getLaunchingPrefsFile());
84         for (Iterator JavaDoc it = launchProps.entrySet().iterator(); it.hasNext(); ) {
85             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
86             String JavaDoc key = (String JavaDoc) entry.getKey();
87             String JavaDoc value = (String JavaDoc) entry.getValue();
88             if (key.equals(VM_XML)) {
89                 Map JavaDoc vmMap = PreferredVMParser.parse(value);
90                 workspace.setJREContainers(vmMap);
91             }
92         }
93     }
94     
95     private void parseCorePreferences() throws IOException JavaDoc, ProjectImporterException {
96         Properties JavaDoc coreProps = EclipseUtils.loadProperties(workspace.getCorePreferenceFile());
97         for (Iterator JavaDoc it = coreProps.entrySet().iterator(); it.hasNext(); ) {
98             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
99             String JavaDoc key = (String JavaDoc) entry.getKey();
100             String JavaDoc value = (String JavaDoc) entry.getValue();
101             if (key.startsWith(VARIABLE_PREFIX)) {
102                 Workspace.Variable var = new Workspace.Variable();
103                 var.setName(key.substring(VARIABLE_PREFIX_LENGTH));
104                 var.setLocation(value);
105                 workspace.addVariable(var);
106             } else if (key.startsWith(USER_LIBRARY_PREFIX) && !value.startsWith(IGNORED_CP_ENTRY)) { // #73542
107
String JavaDoc libName = key.substring(USER_LIBRARY_PREFIX_LENGTH);
108                 workspace.addUserLibrary(libName, UserLibraryParser.getJars(value));
109             } // else we don't use other properties in the meantime
110
}
111     }
112     
113     // private String parseJDKDir(ClassPath cp) {
114
// for (Iterator it = cp.getEntries().iterator(); it.hasNext(); ) {
115
// ClassPathEntry entry = (ClassPathEntry) it.next();
116
// if (entry.getRawPath().endsWith("rt.jar")) {
117
// return entry.getRawPath();
118
// }
119
// }
120
// return null;
121
// }
122

123     private void parseWorkspaceProjects() throws ProjectImporterException {
124         // directory filter
125
FileFilter JavaDoc dirFilter = new FileFilter JavaDoc() {
126             public boolean accept(File JavaDoc file) {
127                 return file.isDirectory();
128             }
129         };
130         
131         Set JavaDoc projectsDirs = new HashSet JavaDoc();
132         // let's find internal projects
133
File JavaDoc[] innerDirs = workspace.getDirectory().listFiles(dirFilter);
134         for (int i = 0; i < innerDirs.length; i++) {
135             File JavaDoc prjDir = innerDirs[i];
136             if (EclipseUtils.isRegularProject(prjDir)) {
137                 // we cannot load projects recursively until we have loaded
138
// information of all projects in the workspace
139
logger.finest("Found a regular Eclipse Project in: " // NOI18N
140
+ prjDir.getAbsolutePath());
141                 if (!projectsDirs.contains(prjDir.getName())) {
142                     addLightProject(projectsDirs, prjDir, true);
143                 } else {
144                     logger.warning("Trying to add the same project twice: " // NOI18N
145
+ prjDir.getAbsolutePath());
146                 }
147             } // else .metadata or something we don't care about yet
148
}
149         
150         // let's try to find external projects
151
File JavaDoc[] resourceDirs = workspace.getResourceProjectsDir().listFiles(dirFilter);
152         for (int i = 0; i < resourceDirs.length; i++) {
153             File JavaDoc resDir = resourceDirs[i];
154             File JavaDoc location = getLocation(resDir);
155             if (location != null) {
156                 if (EclipseUtils.isRegularProject(location)) {
157                     logger.finest("Found a regular Eclipse Project in: " // NOI18N
158
+ location.getAbsolutePath());
159                     if (!projectsDirs.contains(location.getName())) {
160                         addLightProject(projectsDirs, location, false);
161                     } else {
162                         logger.warning("Trying to add the same project twice: " // NOI18N
163
+ location.getAbsolutePath());
164                     }
165                 } else {
166                     logger.warning(location.getAbsolutePath() + " does not contain regular project"); // NOI18N
167
}
168             }
169         }
170         
171         // Project instances with base infos are loaded, let's load all the
172
// information we need (we have to do this here because project's
173
// classpath needs at least project's names and abs. paths during
174
// parsing
175
for (Iterator JavaDoc it = workspace.getProjects().iterator(); it.hasNext(); ) {
176             EclipseProject project = (EclipseProject) it.next();
177             project.setWorkspace(workspace);
178             ProjectFactory.getInstance().load(project);
179         }
180     }
181     
182     private void addLightProject(Set JavaDoc projectsDirs, File JavaDoc prjDir, boolean internal) {
183         EclipseProject project = EclipseProject.createProject(prjDir);
184         if (project != null) {
185             project.setName(prjDir.getName());
186             project.setInternal(internal);
187             workspace.addProject(project);
188             projectsDirs.add(prjDir.getName());
189         }
190     }
191     
192     /** Loads location of external project. */
193     private static File JavaDoc getLocation(final File JavaDoc prjDir) throws ProjectImporterException {
194         File JavaDoc locationFile = new File JavaDoc(prjDir, ".location"); // NOI18N
195
if (locationFile.isFile()) {
196             FileInputStream JavaDoc fis = null;
197             try {
198                 fis = new FileInputStream JavaDoc(locationFile);
199                 return getLocation(fis);
200             } catch (IOException JavaDoc e) {
201                 throw new ProjectImporterException("Error during reading " + // NOI18N
202
".location file", e); // NOI18N
203
} finally {
204                 if (fis != null) {
205                     try {
206                         fis.close();
207                     } catch (IOException JavaDoc e) {
208                         throw new ProjectImporterException(e);
209                     }
210                 }
211             }
212         }
213         return null;
214     }
215     
216     /**
217      * Loads location of external project. Package-private for unit tests only.
218      */

219     static File JavaDoc getLocation(final InputStream JavaDoc is) throws IOException JavaDoc {
220         // starts with 17 bytes.
221
long toSkip = 17;
222         while(toSkip != 0) {
223             toSkip -= is.skip(toSkip);
224         }
225         // follows byte describing path length
226
int pathLength = is.read();
227         // follows path itself
228
byte[] path = new byte[pathLength];
229         int read = is.read(path);
230         assert read == pathLength;
231         String JavaDoc pathS = new String JavaDoc(path, "ISO-8859-1"); // NOI18N
232
if (pathS.startsWith("URI//")) { // #89577 // NOI18N
233
pathS = pathS.substring(pathS.indexOf(':') + 1);
234         }
235         return new File JavaDoc(pathS);
236     }
237     
238 }
239
Popular Tags