1 19 20 package org.netbeans.modules.projectimport.eclipse; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import java.util.logging.Logger ; 32 import org.netbeans.modules.projectimport.LoggerFactory; 33 import org.openide.filesystems.FileUtil; 34 35 40 public final class EclipseProject implements Comparable { 41 42 43 private static final Logger logger = 44 LoggerFactory.getDefault().createLogger(EclipseProject.class); 45 46 static final String PROJECT_FILE = ".project"; static final String CLASSPATH_FILE = ".classpath"; 49 private Workspace workspace; 50 51 private String name; 52 private boolean internal = true; 53 private boolean javaNature; 54 private ClassPath cp; 55 private Set links; 56 private Set otherNatures; 57 58 private final File projectDir; 59 private final File cpFile; 60 private final File prjFile; 61 private String jdkDirectory; 62 63 71 static EclipseProject createProject(File projectDir) { 72 if (!EclipseUtils.isRegularProject(projectDir)) { 73 logger.fine(projectDir + " doesn't contain regular Eclipse project."); return null; 75 } 76 return new EclipseProject(projectDir); 77 } 78 79 80 private EclipseProject(File projectDir) { 81 this.projectDir = projectDir; 82 this.cpFile = new File (projectDir, CLASSPATH_FILE); 83 this.prjFile = new File (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 105 public String getName() { 106 return name; 107 } 108 109 void setName(String 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 getDirectory() { 122 return projectDir; 123 } 124 125 130 File getProjectFile() { 131 return prjFile; 132 } 133 134 139 File 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 getOtherNatures() { 152 return otherNatures; 153 } 154 155 void addOtherNature(String nature) { 156 if (otherNatures == null) { 157 otherNatures = new HashSet (); 158 } 159 logger.fine("Project " + getName() + " has another nature: " + nature); 161 otherNatures.add(nature); 162 } 163 164 172 public String getJDKDirectory() { 173 if (jdkDirectory == null && workspace != null) { 174 logger.finest("Getting JDK directory for project " + this.getName()); jdkDirectory = workspace.getJDKDirectory(cp.getJREContainer()); 176 logger.finest("Resolved JDK directory: " + jdkDirectory); } 179 return jdkDirectory; 180 } 181 182 183 public Collection getSourceRoots() { 184 return cp.getSourceRoots(); 185 } 186 187 191 public Map getAllSourceRoots() { 192 Map rootsLabels = new HashMap (); 193 194 Collection srcRoots = cp.getSourceRoots(); 196 for (Iterator it = srcRoots.iterator(); it.hasNext(); ) { 197 ClassPathEntry cpe = (ClassPathEntry) it.next(); 198 File file = FileUtil.normalizeFile(new File (cpe.getAbsolutePath())); 199 rootsLabels.put(file, cpe.getRawPath()); 200 } 201 Collection extSrcRoots = cp.getExternalSourceRoots(); 203 for (Iterator it = extSrcRoots.iterator(); it.hasNext(); ) { 204 ClassPathEntry cpe = (ClassPathEntry) it.next(); 205 rootsLabels.put( 206 FileUtil.normalizeFile(new File (cpe.getAbsolutePath())), 207 cpe.getRawPath()); 208 } 209 210 return rootsLabels; 211 } 212 213 216 public Collection getAllLibrariesFiles() { 217 Collection files = new ArrayList (); 218 for (Iterator it = cp.getLibraries().iterator(); it.hasNext(); ) { 220 files.add(FileUtil.normalizeFile(new File (((ClassPathEntry)it.next()).getAbsolutePath()))); 221 222 } 223 for (Iterator it = cp.getExternalLibraries().iterator(); it.hasNext(); ) { 225 files.add(FileUtil.normalizeFile(new File (((ClassPathEntry)it.next()).getAbsolutePath()))); 226 } 227 for (Iterator it = getUserLibrariesJars().iterator(); it.hasNext(); ) { 229 files.add(FileUtil.normalizeFile(new File ((String ) it.next()))); 230 } 231 for (Iterator it = cp.getVariables().iterator(); it.hasNext(); ) { 233 ClassPathEntry entry = (ClassPathEntry)it.next(); 234 if (entry.getAbsolutePath() != null) { 236 files.add(FileUtil.normalizeFile(new File (entry.getAbsolutePath()))); 237 } 238 } 239 return files; 240 } 241 242 243 public Collection getExternalSourceRoots() { 244 return cp.getExternalSourceRoots(); 245 } 246 247 248 public Collection getLibraries() { 249 return cp.getLibraries(); 250 } 251 252 253 public Collection getExternalLibraries() { 254 return cp.getExternalLibraries(); 255 } 256 257 public Collection getUserLibrariesJars() { 258 Collection userLibrariesJars = new HashSet (); 259 if (workspace != null) { 260 for (Iterator it = cp.getUserLibraries().iterator(); it.hasNext(); ) { 261 userLibrariesJars.addAll( 262 workspace.getJarsForUserLibrary((String ) it.next())); 263 } 264 } 265 return userLibrariesJars; 266 } 267 268 269 public Collection getProjectsEntries() { 270 return cp.getProjects(); 271 } 272 273 private Set projectsWeDependOn; 274 275 278 public Set getProjects() { 279 if (workspace != null && projectsWeDependOn == null) { 280 projectsWeDependOn = new HashSet (); 281 for (Iterator 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 294 public Collection getVariables() { 295 return cp.getVariables(); 296 } 297 298 void addLink(ClassPath.Link link) { 299 if (links == null) { 300 links = new HashSet (); 301 } 302 links.add(link); 303 } 304 305 311 void setAbsolutePathForEntry(ClassPathEntry entry) { 312 entry.setAbsolutePath(null); 314 315 if (entry.getType() == ClassPathEntry.TYPE_CONTAINER) { 317 return; 320 } 321 322 if (entry.getType() == ClassPathEntry.TYPE_VARIABLE) { 324 String 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 if (entry.getType() == ClassPathEntry.TYPE_PROJECT) { 344 if (workspace != null) { 345 entry.setAbsolutePath(workspace.getProjectAbsolutePath( 346 entry.getRawPath().substring(1))); 347 } 348 return; 352 } 353 354 ClassPath.Link link = getLink(entry.getRawPath()); 356 if (link != null) { 357 logger.finest("Found link for entry \"" + entry + "\": " + link); if (FileUtil.normalizeFile(new File (link.getLocation())).exists()) { 359 entry.setType(ClassPathEntry.TYPE_LINK); 361 entry.setAbsolutePath(link.getLocation()); 362 } else { 363 logger.info("Not able to resolve absolute path for classpath" + " entry \"" + entry.getRawPath() + "\". This classpath" + " entry is external source which points to PATH VARIABLE" + " which points to final destination. This feature will be" + " supported in future version of Importer."); entry.setType(ClassPathEntry.TYPE_UNKNOWN); 369 } 370 return; 371 } 372 373 if (entry.isRawPathRelative()) { 375 entry.setAbsolutePath(projectDir.getAbsolutePath() + File.separator 377 + entry.getRawPath()); 378 } else { 379 entry.setAbsolutePath(entry.getRawPath()); 381 } 382 } 383 384 388 private Workspace.Variable getVariable(String rawPath) { 389 if (workspace == null) { 390 logger.fine("Workspace wasn't set for the project \"" + getName() + "\""); return null; 393 } 394 Set variables = workspace.getVariables(); 395 if (variables != null) { 396 for (Iterator 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); return null; 405 } 406 407 412 private Workspace.Variable getVariable(ClassPathEntry entry) { 413 return getVariable(entry.getRawPath()); 414 } 415 416 420 private ClassPath.Link getLink(String linkName) { 421 if (links != null) { 422 for (Iterator 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 toString() { 433 return "EclipseProject[" + getName() + ", " + getDirectory() + "]"; } 435 436 437 public boolean equals(Object 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 446 public int hashCode() { 447 int result = 17; 448 result = 37 * result + System.identityHashCode(name); 449 return result; 450 } 451 452 456 public int compareTo(Object o) { 457 String name1 = getName(); 458 String 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 |