1 19 20 package org.netbeans.modules.projectimport.eclipse; 21 22 import java.io.File ; 23 import java.io.FileFilter ; 24 import java.io.FileInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 import java.util.Set ; 32 import java.util.logging.Logger ; 33 import org.netbeans.modules.projectimport.LoggerFactory; 34 import org.netbeans.modules.projectimport.ProjectImporterException; 35 36 41 final class WorkspaceParser { 42 43 44 private static final Logger logger = 45 LoggerFactory.getDefault().createLogger(WorkspaceParser.class); 46 47 private static final String VM_XML = "org.eclipse.jdt.launching.PREF_VM_XML"; private static final String IGNORED_CP_ENTRY = "##<cp entry ignore>##"; 50 private static final String VARIABLE_PREFIX = "org.eclipse.jdt.core.classpathVariable."; private static final int VARIABLE_PREFIX_LENGTH = VARIABLE_PREFIX.length(); 52 53 private static final String USER_LIBRARY_PREFIX = "org.eclipse.jdt.core.userLibrary."; private static final int USER_LIBRARY_PREFIX_LENGTH = USER_LIBRARY_PREFIX.length(); 55 56 63 private final Workspace workspace; 64 65 66 WorkspaceParser(Workspace workspace) { 67 this.workspace = workspace; 68 } 69 70 71 void parse() throws ProjectImporterException { 72 try { 73 parseLaunchingPreferences(); 74 parseCorePreferences(); 75 parseWorkspaceProjects(); 76 } catch (IOException e) { 77 throw new ProjectImporterException( 78 "Cannot load workspace properties", e); } 80 } 81 82 private void parseLaunchingPreferences() throws IOException , ProjectImporterException { 83 Properties launchProps = EclipseUtils.loadProperties(workspace.getLaunchingPrefsFile()); 84 for (Iterator it = launchProps.entrySet().iterator(); it.hasNext(); ) { 85 Map.Entry entry = (Map.Entry ) it.next(); 86 String key = (String ) entry.getKey(); 87 String value = (String ) entry.getValue(); 88 if (key.equals(VM_XML)) { 89 Map vmMap = PreferredVMParser.parse(value); 90 workspace.setJREContainers(vmMap); 91 } 92 } 93 } 94 95 private void parseCorePreferences() throws IOException , ProjectImporterException { 96 Properties coreProps = EclipseUtils.loadProperties(workspace.getCorePreferenceFile()); 97 for (Iterator it = coreProps.entrySet().iterator(); it.hasNext(); ) { 98 Map.Entry entry = (Map.Entry ) it.next(); 99 String key = (String ) entry.getKey(); 100 String value = (String ) 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)) { String libName = key.substring(USER_LIBRARY_PREFIX_LENGTH); 108 workspace.addUserLibrary(libName, UserLibraryParser.getJars(value)); 109 } } 111 } 112 113 123 private void parseWorkspaceProjects() throws ProjectImporterException { 124 FileFilter dirFilter = new FileFilter () { 126 public boolean accept(File file) { 127 return file.isDirectory(); 128 } 129 }; 130 131 Set projectsDirs = new HashSet (); 132 File [] innerDirs = workspace.getDirectory().listFiles(dirFilter); 134 for (int i = 0; i < innerDirs.length; i++) { 135 File prjDir = innerDirs[i]; 136 if (EclipseUtils.isRegularProject(prjDir)) { 137 logger.finest("Found a regular Eclipse Project in: " + 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: " + prjDir.getAbsolutePath()); 146 } 147 } } 149 150 File [] resourceDirs = workspace.getResourceProjectsDir().listFiles(dirFilter); 152 for (int i = 0; i < resourceDirs.length; i++) { 153 File resDir = resourceDirs[i]; 154 File location = getLocation(resDir); 155 if (location != null) { 156 if (EclipseUtils.isRegularProject(location)) { 157 logger.finest("Found a regular Eclipse Project in: " + 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: " + location.getAbsolutePath()); 164 } 165 } else { 166 logger.warning(location.getAbsolutePath() + " does not contain regular project"); } 168 } 169 } 170 171 for (Iterator 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 projectsDirs, File 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 193 private static File getLocation(final File prjDir) throws ProjectImporterException { 194 File locationFile = new File (prjDir, ".location"); if (locationFile.isFile()) { 196 FileInputStream fis = null; 197 try { 198 fis = new FileInputStream (locationFile); 199 return getLocation(fis); 200 } catch (IOException e) { 201 throw new ProjectImporterException("Error during reading " + ".location file", e); } finally { 204 if (fis != null) { 205 try { 206 fis.close(); 207 } catch (IOException e) { 208 throw new ProjectImporterException(e); 209 } 210 } 211 } 212 } 213 return null; 214 } 215 216 219 static File getLocation(final InputStream is) throws IOException { 220 long toSkip = 17; 222 while(toSkip != 0) { 223 toSkip -= is.skip(toSkip); 224 } 225 int pathLength = is.read(); 227 byte[] path = new byte[pathLength]; 229 int read = is.read(path); 230 assert read == pathLength; 231 String pathS = new String (path, "ISO-8859-1"); if (pathS.startsWith("URI//")) { pathS = pathS.substring(pathS.indexOf(':') + 1); 234 } 235 return new File (pathS); 236 } 237 238 } 239 | Popular Tags |