1 12 13 package org.eclipse.ant.internal.ui.datatransfer; 14 15 import java.io.File ; 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.HashMap ; 19 import java.util.LinkedHashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.eclipse.ant.internal.ui.AntUIPlugin; 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IPath; 28 import org.eclipse.core.runtime.Path; 29 import org.eclipse.debug.core.ILaunchConfiguration; 30 import org.eclipse.jdt.core.IClasspathContainer; 31 import org.eclipse.jdt.core.IClasspathEntry; 32 import org.eclipse.jdt.core.IJavaProject; 33 import org.eclipse.jdt.core.IPackageFragmentRoot; 34 import org.eclipse.jdt.core.JavaCore; 35 import org.eclipse.jdt.core.JavaModelException; 36 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 37 import org.eclipse.jdt.launching.JavaRuntime; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 41 44 public class EclipseClasspath 45 { 46 protected List srcDirs = new ArrayList (); 47 protected List classDirs = new ArrayList (); 48 protected List inclusionLists = new ArrayList (); 49 protected List exclusionLists = new ArrayList (); 50 51 protected Map variable2valueMap = new LinkedHashMap (); 52 protected List rawClassPathEntries = new ArrayList (); 53 protected List rawClassPathEntriesAbsolute = new ArrayList (); 54 55 private IJavaProject project; 56 57 private static Map userLibraryCache = new HashMap (); 58 59 62 public EclipseClasspath(IJavaProject project) throws JavaModelException 63 { 64 init(project, project.getRawClasspath()); 65 } 66 67 74 public EclipseClasspath(IJavaProject project, ILaunchConfiguration conf, boolean bootstrap) 75 throws JavaModelException 76 { 77 IRuntimeClasspathEntry[] runtimeEntries; 79 try 80 { 81 runtimeEntries = JavaRuntime.computeUnresolvedRuntimeClasspath(conf); 83 } 84 catch (CoreException e) 85 { 86 throw new JavaModelException(e); 87 } 88 List classpathEntries = new ArrayList (runtimeEntries.length); 89 for (int i = 0; i < runtimeEntries.length; i++) 90 { 91 IRuntimeClasspathEntry entry = runtimeEntries[i]; 92 if ( bootstrap && (entry.getClasspathProperty() == IRuntimeClasspathEntry.BOOTSTRAP_CLASSES) || 93 ! bootstrap && (entry.getClasspathProperty() != IRuntimeClasspathEntry.BOOTSTRAP_CLASSES)) 94 { 95 if (entry.getClass().getName().equals("org.eclipse.jdt.internal.launching.VariableClasspathEntry")) { 99 IClasspathEntry e = convertVariableClasspathEntry(entry); 100 if (e != null) 101 { 102 classpathEntries.add(e); 103 } 104 } 105 else if (entry.getClass().getName().equals("org.eclipse.jdt.internal.launching.DefaultProjectClasspathEntry")) { 107 IClasspathEntry e = JavaCore.newProjectEntry(entry.getPath()); 108 classpathEntries.add(e); 109 } 110 else if (entry.getClasspathEntry() != null) 111 { 112 classpathEntries.add(entry.getClasspathEntry()); 113 } 114 } 115 } 116 IClasspathEntry[] entries = 117 (IClasspathEntry[]) classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]); 118 119 init(project, entries); 120 } 121 122 private void init(IJavaProject javaProject, IClasspathEntry entries[]) throws JavaModelException 123 { 124 this.project = javaProject; 125 handle(entries); 126 } 127 128 private void handle(IClasspathEntry[] entries) throws JavaModelException 129 { 130 for (int i = 0; i < entries.length; i++) 131 { 132 handleSources(entries[i]); 133 handleVariables(entries[i]); 134 handleJars(entries[i]); 135 handleLibraries(entries[i]); 136 handleProjects(entries[i]); 137 } 138 } 139 140 private void handleSources(IClasspathEntry entry) throws JavaModelException 141 { 142 String projectRoot = ExportUtil.getProjectRoot(project); 143 String defaultClassDir = project.getOutputLocation().toString(); 144 String defaultClassDirAbsolute = ExportUtil.resolve(project.getOutputLocation()); 145 146 if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE && 147 entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) 148 { 149 IPath srcDirPath = entry.getPath(); 151 IPath classDirPath = entry.getOutputLocation(); 152 String srcDir = handleLinkedResource(srcDirPath); 153 ExportUtil.removeProjectRoot((srcDirPath != null) ? srcDirPath.toString() : projectRoot, project.getProject()); 154 String classDir = ExportUtil.removeProjectRoot((classDirPath != null) ? classDirPath.toString() : defaultClassDir, project.getProject()); 155 srcDirs.add(srcDir); 156 classDirs.add(classDir); 157 String classDirAbsolute = (classDirPath != null) ? ExportUtil.resolve(classDirPath) : defaultClassDirAbsolute; 158 rawClassPathEntries.add(classDir); 159 rawClassPathEntriesAbsolute.add(classDirAbsolute); 160 IPath[] inclusions = entry.getInclusionPatterns(); 161 List inclusionList = new ArrayList (); 162 for (int j = 0; j < inclusions.length; j++) 163 { 164 if (inclusions[j] != null) 165 { 166 inclusionList.add(ExportUtil.removeProjectRoot(inclusions[j].toString(), project.getProject())); 167 } 168 } 169 inclusionLists.add(inclusionList); 170 IPath[] exclusions = entry.getExclusionPatterns(); 171 List exclusionList = new ArrayList (); 172 for (int j = 0; j < exclusions.length; j++) 173 { 174 if (exclusions[j] != null) 175 { 176 exclusionList.add(ExportUtil.removeProjectRoot(exclusions[j].toString(), project.getProject())); 177 } 178 } 179 exclusionLists.add(exclusionList); 180 } 181 } 182 183 190 private String handleLinkedResource(IPath srcDirPath) 191 { 192 String projectRoot = ExportUtil.getProjectRoot(project); 193 String srcDir = ExportUtil.removeProjectRoot((srcDirPath != null) ? srcDirPath.toString() : projectRoot, project.getProject()); 194 if (srcDirPath == null) 195 { 196 return srcDir; 197 } 198 IFile file; 199 try 200 { 201 file = ResourcesPlugin.getWorkspace().getRoot().getFile(srcDirPath); 202 } 203 catch (IllegalArgumentException e) 204 { 205 return srcDir; 206 } 207 if (file.isLinked()) 208 { 209 String pathVariable = file.getRawLocation().segment(0).toString(); 210 IPath pathVariableValue = file.getWorkspace().getPathVariableManager().getValue(pathVariable); 211 if (pathVariableValue != null) 212 { 213 String pathVariableExtension = file.getRawLocation().segment(1).toString(); 215 String relativePath = ExportUtil.getRelativePath(pathVariableValue.toString(), 216 projectRoot); 217 variable2valueMap.put(pathVariable + ".pathvariable", relativePath); variable2valueMap.put(srcDir + ".link", "${" + pathVariable + ".pathvariable}/" + pathVariableExtension); } 221 else 222 { 223 String relativePath = ExportUtil.getRelativePath(file.getLocation() + "", projectRoot); 225 variable2valueMap.put(srcDir + ".link", relativePath); } 227 srcDir = "${" + srcDir + ".link}"; } 229 return srcDir; 230 } 231 232 private void handleJars(IClasspathEntry entry) 233 { 234 if (entry.getContentKind() == IPackageFragmentRoot.K_BINARY && 235 entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) 236 { 237 String jarFile = entry.getPath().toString(); 238 StringBuffer jarFileBuffer = new StringBuffer (); 239 StringBuffer jarFileAbsoluteBuffer = new StringBuffer (); 240 String jarFileAbsolute = ExportUtil.resolve(entry.getPath()); 241 if (jarFileAbsolute == null) 242 { 243 jarFileAbsolute = jarFile; if (handleSubProjectClassesDirectory(jarFile, jarFileBuffer, jarFileAbsoluteBuffer)) 245 { 246 jarFile = jarFileBuffer.toString(); 247 jarFileAbsolute = jarFileAbsoluteBuffer.toString(); 248 } 249 } 250 String jarFileOld = jarFile; 251 jarFile = ExportUtil.removeProjectRoot(jarFile, project.getProject()); 252 if (jarFile.equals(jarFileOld)) 253 { 254 if (handleSubProjectClassesDirectory(jarFile, jarFileBuffer, jarFileAbsoluteBuffer)) 255 { 256 jarFile = jarFileBuffer.toString(); 257 jarFileAbsolute = jarFileAbsoluteBuffer.toString(); 258 } 259 } 260 rawClassPathEntries.add(jarFile); 261 rawClassPathEntriesAbsolute.add(jarFileAbsolute); 262 } 263 } 264 265 273 private boolean handleSubProjectClassesDirectory(String file, StringBuffer jarFile, StringBuffer jarFileAbsolute) 274 { 275 if (file != null && file.indexOf('/') == 0) 277 { 278 int i = file.indexOf('/', 1); 279 i = (i != -1) ? i : file.length(); 280 String subproject = file.substring(1, i); 281 IJavaProject javaproject = ExportUtil.getJavaProjectByName(subproject); 282 if (javaproject != null) 283 { 284 jarFile.setLength(0); 285 jarFileAbsolute.setLength(0); 286 String location = javaproject.getProject().getName() + ".location"; jarFileAbsolute.append(ExportUtil.replaceProjectRoot(file, javaproject.getProject(), ExportUtil.getProjectRoot(javaproject))); 288 jarFile.append(ExportUtil.replaceProjectRoot(file, javaproject.getProject(), "${" + location + "}")); String projectRoot= ExportUtil.getProjectRoot(project); 290 String relativePath = ExportUtil.getRelativePath(ExportUtil.getProjectRoot(javaproject), 291 projectRoot); 292 variable2valueMap.put(location, relativePath); 293 return true; 294 } 295 } 296 return false; 297 } 298 299 private void handleVariables(IClasspathEntry entry) 300 { 301 if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE && 302 entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) 303 { 304 String e = entry.getPath().toString(); 306 int index = e.indexOf('/'); 307 if (index == -1) 308 { 309 index = e.indexOf('\\'); 310 } 311 String variable = e; 312 String path = ""; if (index != -1) 314 { 315 variable = e.substring(0, index); 316 path = e.substring(index); 317 } 318 IPath value = JavaCore.getClasspathVariable(variable); 319 if (value != null) 320 { 321 String projectRoot = ExportUtil.getProjectRoot(project); 322 String relativePath = ExportUtil.getRelativePath(value.toString(), 323 projectRoot); 324 variable2valueMap.put(variable, relativePath); 325 } 326 else if (variable2valueMap.get(variable) == null) 327 { 328 variable2valueMap.put(variable, ""); } 331 rawClassPathEntriesAbsolute.add(value + path); 332 rawClassPathEntries.add("${" + variable + "}" + path); } 334 } 335 336 private void handleLibraries(IClasspathEntry entry) throws JavaModelException 337 { 338 if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE && 339 entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) 340 { 341 IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project); 343 if (container == null) { 344 return; 346 } 347 String jar = entry.getPath().toString(); 348 if (jar.startsWith(JavaRuntime.JRE_CONTAINER)) 349 { 350 } 358 else if (jar.startsWith(JavaCore.USER_LIBRARY_CONTAINER_ID)) 359 { 360 String libraryName = container.getDescription(); 362 String userlibraryRef = "${" + libraryName + ".userclasspath}"; if (container.getKind() == IClasspathContainer.K_SYSTEM) 364 { 365 userlibraryRef = "${" + libraryName + ".bootclasspath}"; } 367 userLibraryCache.put(userlibraryRef, container); 368 srcDirs.add(userlibraryRef); 369 classDirs.add(userlibraryRef); 370 rawClassPathEntries.add(userlibraryRef); 371 rawClassPathEntriesAbsolute.add(userlibraryRef); 372 inclusionLists.add(new ArrayList ()); 373 exclusionLists.add(new ArrayList ()); 374 } 375 else 376 { 377 String libraryName = container.getDescription(); 379 String pluginRef = "${" + libraryName + ".libraryclasspath}"; userLibraryCache.put(pluginRef, container); 381 srcDirs.add(pluginRef); 382 classDirs.add(pluginRef); 383 rawClassPathEntries.add(pluginRef); 384 rawClassPathEntriesAbsolute.add(pluginRef); 385 inclusionLists.add(new ArrayList ()); 386 exclusionLists.add(new ArrayList ()); 387 } 388 } 389 } 390 391 private void handleProjects(IClasspathEntry entry) 392 { 393 if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE && 394 entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) 395 { 396 String subProjectRoot = entry.getPath().toString(); 398 IJavaProject subProject = ExportUtil.getJavaProject(subProjectRoot); 399 if (subProject == null) 400 { 401 AntUIPlugin.log("project is not loaded in workspace: " + subProjectRoot, null); return; 404 } 405 String classpathRef = "${" + subProject.getProject().getName() + ".classpath}"; srcDirs.add(classpathRef); 408 classDirs.add(classpathRef); 409 rawClassPathEntries.add(classpathRef); 410 rawClassPathEntriesAbsolute.add(classpathRef); 411 inclusionLists.add(new ArrayList ()); 412 exclusionLists.add(new ArrayList ()); 413 } 414 } 415 416 419 public static String getClasspath(IJavaProject project) throws CoreException 420 { 421 List items = getClasspathList(project); 422 return ExportUtil.toString(items, File.pathSeparator); 423 } 424 425 428 public static List getClasspathList(IJavaProject project) throws CoreException 429 { 430 String [] classpath = JavaRuntime.computeDefaultRuntimeClassPath(project); 431 return Arrays.asList(classpath); 432 } 433 434 437 public static boolean isReference(String s) 438 { 439 return isProjectReference(s) || isUserLibraryReference(s) || 440 isUserSystemLibraryReference(s) || isLibraryReference(s); 441 } 443 444 447 public static boolean isProjectReference(String s) 448 { 449 return s.startsWith("${") && s.endsWith(".classpath}"); } 451 452 456 public static IJavaProject resolveProjectReference(String s) 457 { 458 String name = ExportUtil.removePrefixAndSuffix(s, "${", ".classpath}"); return ExportUtil.getJavaProjectByName(name); 460 } 461 462 465 public static boolean isUserLibraryReference(String s) 466 { 467 return s.startsWith("${") && s.endsWith(".userclasspath}"); } 469 470 474 public static boolean isUserSystemLibraryReference(String s) 475 { 476 return s.startsWith("${") && s.endsWith(".bootclasspath}"); } 478 479 484 public static boolean isLibraryReference(String s) 485 { 486 return s.startsWith("${") && s.endsWith(".libraryclasspath}"); } 488 489 499 public static IClasspathContainer resolveUserLibraryReference(String s) 500 { 501 return (IClasspathContainer) userLibraryCache.get(s); 502 } 503 504 508 public static boolean isLinkedResource(String s) 509 { 510 return s.startsWith("${") && s.endsWith(".link}"); } 512 513 518 public static String getLinkedResourceName(String s) 519 { 520 return ExportUtil.removePrefixAndSuffix(s, "${", ".link}"); } 522 523 526 public String resolveLinkedResource(String s) 527 { 528 String name = ExportUtil.removePrefixAndSuffix(s, "${", "}"); String value = (String ) variable2valueMap.get(name); 530 String suffix = ".pathvariable}"; int i = value.indexOf(suffix); 532 if (i != -1) 533 { 534 String pathVariable = value.substring(0, i + suffix.length() - 1); 536 pathVariable = ExportUtil.removePrefix(pathVariable, "${"); return (String ) variable2valueMap.get(pathVariable) + value.substring(i + suffix.length()); 538 } 539 return value; 540 } 541 542 547 private IClasspathEntry convertVariableClasspathEntry(IRuntimeClasspathEntry entry) 548 { 549 try 550 { 551 Document doc = ExportUtil.parseXmlString(entry.getMemento()); 552 Element element = (Element ) doc.getElementsByTagName("memento").item(0); String variableString = element.getAttribute("variableString"); ExportUtil.addVariable(variable2valueMap, variableString, ExportUtil.getProjectRoot(project)); 555 variableString = ExportUtil.removePrefix(variableString, "${"); int i = variableString.indexOf('}'); 558 if (i != -1) 559 { 560 variableString = variableString.substring(0, i) 561 + variableString.substring(i + 1); 562 } 563 IPath path = new Path(variableString); 564 return JavaCore.newVariableEntry(path, null, null); 565 } 566 catch (Exception e) 567 { 568 AntUIPlugin.log(e); 569 return null; 570 } 571 572 } 573 } 574 | Popular Tags |