1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.ArrayList ; 14 import java.util.Enumeration ; 15 import java.util.Map ; 16 17 import org.eclipse.core.resources.*; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.jdt.core.*; 20 import org.eclipse.jdt.core.compiler.CharOperation; 21 import org.eclipse.jdt.internal.core.util.MementoTokenizer; 22 import org.eclipse.jdt.internal.core.util.Messages; 23 import org.eclipse.jdt.internal.core.util.Util; 24 25 28 public class PackageFragmentRoot extends Openable implements IPackageFragmentRoot { 29 30 34 protected final static char ATTACHMENT_PROPERTY_DELIMITER= '*'; 35 38 public final static String NO_SOURCE_ATTACHMENT = ""; 40 44 protected Object resource; 45 46 50 protected PackageFragmentRoot(IResource resource, JavaProject project) { 51 super(project); 52 this.resource = resource; 53 } 54 55 58 public void attachSource(IPath sourcePath, IPath rootPath, IProgressMonitor monitor) throws JavaModelException { 59 try { 60 verifyAttachSource(sourcePath); 61 if (monitor != null) { 62 monitor.beginTask(Messages.element_attachingSource, 2); 63 } 64 SourceMapper oldMapper= getSourceMapper(); 65 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 66 boolean rootNeedsToBeClosed= false; 67 68 if (sourcePath == null) { 69 rootNeedsToBeClosed= true; 71 setSourceMapper(null); 72 81 } else { 82 91 92 IPath storedSourcePath= getSourceAttachmentPath(); 94 IPath storedRootPath= getSourceAttachmentRootPath(); 95 if (monitor != null) { 96 monitor.worked(1); 97 } 98 if (storedSourcePath != null) { 99 if (!(storedSourcePath.equals(sourcePath) && (rootPath != null && rootPath.equals(storedRootPath)) || storedRootPath == null)) { 100 rootNeedsToBeClosed= true; 101 } 102 } 103 Object target = JavaModel.getTarget(workspace.getRoot(), sourcePath, false); 105 if (target == null) { 106 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, sourcePath)); 107 } 108 SourceMapper mapper = createSourceMapper(sourcePath, rootPath); 109 if (rootPath == null && mapper.rootPath != null) { 110 rootPath = new Path(mapper.rootPath); 112 } 113 setSourceMapper(mapper); 114 } 115 if (sourcePath == null) { 116 Util.setSourceAttachmentProperty(getPath(), null); } else { 118 Util.setSourceAttachmentProperty( 120 getPath(), 121 sourcePath.toString() 122 + (rootPath == null ? "" : (ATTACHMENT_PROPERTY_DELIMITER + rootPath.toString()))); } 124 if (rootNeedsToBeClosed) { 125 if (oldMapper != null) { 126 oldMapper.close(); 127 } 128 BufferManager manager= BufferManager.getDefaultBufferManager(); 129 Enumeration openBuffers= manager.getOpenBuffers(); 130 while (openBuffers.hasMoreElements()) { 131 IBuffer buffer= (IBuffer) openBuffers.nextElement(); 132 IOpenable possibleMember= buffer.getOwner(); 133 if (isAncestorOf((IJavaElement) possibleMember)) { 134 buffer.close(); 135 } 136 } 137 if (monitor != null) { 138 monitor.worked(1); 139 } 140 } 141 } catch (JavaModelException e) { 142 Util.setSourceAttachmentProperty(getPath(), null); throw e; 144 } finally { 145 if (monitor != null) { 146 monitor.done(); 147 } 148 } 149 } 150 151 154 protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException { 155 156 IStatus status = validateOnClasspath(); 158 if (!status.isOK()) throw newJavaModelException(status); 159 if (!resourceExists()) throw newNotPresentException(); 160 161 ((PackageFragmentRootInfo) info).setRootKind(determineKind(underlyingResource)); 162 return computeChildren(info, newElements); 163 } 164 165 SourceMapper createSourceMapper(IPath sourcePath, IPath rootPath) { 166 SourceMapper mapper = new SourceMapper( 167 sourcePath, 168 rootPath == null ? null : rootPath.toOSString(), 169 getJavaProject().getOptions(true)); return mapper; 171 } 172 175 public void delete( 176 int updateResourceFlags, 177 int updateModelFlags, 178 IProgressMonitor monitor) 179 throws JavaModelException { 180 181 DeletePackageFragmentRootOperation op = new DeletePackageFragmentRootOperation(this, updateResourceFlags, updateModelFlags); 182 op.runOperation(monitor); 183 } 184 185 190 protected boolean computeChildren(OpenableElementInfo info, Map newElements) throws JavaModelException { 191 try { 194 IResource underlyingResource = getResource(); 197 if (underlyingResource.getType() == IResource.FOLDER || underlyingResource.getType() == IResource.PROJECT) { 198 ArrayList vChildren = new ArrayList (5); 199 IContainer rootFolder = (IContainer) underlyingResource; 200 char[][] inclusionPatterns = fullInclusionPatternChars(); 201 char[][] exclusionPatterns = fullExclusionPatternChars(); 202 computeFolderChildren(rootFolder, !Util.isExcluded(rootFolder, inclusionPatterns, exclusionPatterns), CharOperation.NO_STRINGS, vChildren, inclusionPatterns, exclusionPatterns); 203 IJavaElement[] children = new IJavaElement[vChildren.size()]; 204 vChildren.toArray(children); 205 info.setChildren(children); 206 } 207 } catch (JavaModelException e) { 208 info.setChildren(new IJavaElement[]{}); 210 throw e; 211 } 212 return true; 213 } 214 215 221 protected void computeFolderChildren(IContainer folder, boolean isIncluded, String [] pkgName, ArrayList vChildren, char[][] inclusionPatterns, char[][] exclusionPatterns) throws JavaModelException { 222 223 if (isIncluded) { 224 IPackageFragment pkg = getPackageFragment(pkgName); 225 vChildren.add(pkg); 226 } 227 try { 228 JavaProject javaProject = (JavaProject)getJavaProject(); 229 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 230 IResource[] members = folder.members(); 231 boolean hasIncluded = isIncluded; 232 int length = members.length; 233 if (length >0) { 234 String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true); 235 String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true); 236 for (int i = 0; i < length; i++) { 237 IResource member = members[i]; 238 String memberName = member.getName(); 239 240 switch(member.getType()) { 241 242 case IResource.FOLDER: 243 if (Util.isValidFolderNameForPackage(memberName, sourceLevel, complianceLevel)) { 246 if (javaProject.contains(member)) { 248 String [] newNames = Util.arrayConcat(pkgName, manager.intern(memberName)); 249 boolean isMemberIncluded = !Util.isExcluded(member, inclusionPatterns, exclusionPatterns); 250 computeFolderChildren((IFolder) member, isMemberIncluded, newNames, vChildren, inclusionPatterns, exclusionPatterns); 251 } 252 } 253 break; 254 case IResource.FILE: 255 if (!hasIncluded 257 && Util.isValidCompilationUnitName(memberName, sourceLevel, complianceLevel) 258 && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns)) { 259 hasIncluded = true; 260 IPackageFragment pkg = getPackageFragment(pkgName); 261 vChildren.add(pkg); 262 } 263 break; 264 } 265 } 266 } 267 } catch(IllegalArgumentException e){ 268 throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST); } catch (CoreException e) { 270 throw new JavaModelException(e); 271 } 272 } 273 274 277 public void copy( 278 IPath destination, 279 int updateResourceFlags, 280 int updateModelFlags, 281 IClasspathEntry sibling, 282 IProgressMonitor monitor) 283 throws JavaModelException { 284 285 CopyPackageFragmentRootOperation op = 286 new CopyPackageFragmentRootOperation(this, destination, updateResourceFlags, updateModelFlags, sibling); 287 op.runOperation(monitor); 288 } 289 290 293 protected Object createElementInfo() { 294 return new PackageFragmentRootInfo(); 295 } 296 297 300 public IPackageFragment createPackageFragment(String pkgName, boolean force, IProgressMonitor monitor) throws JavaModelException { 301 CreatePackageFragmentOperation op = new CreatePackageFragmentOperation(this, pkgName, force); 302 op.runOperation(monitor); 303 return getPackageFragment(op.pkgName); 304 } 305 306 313 protected int determineKind(IResource underlyingResource) throws JavaModelException { 314 IClasspathEntry entry = ((JavaProject)getJavaProject()).getClasspathEntryFor(underlyingResource.getFullPath()); 315 if (entry != null) { 316 return entry.getContentKind(); 317 } 318 return IPackageFragmentRoot.K_SOURCE; 319 } 320 321 327 public boolean equals(Object o) { 328 if (this == o) 329 return true; 330 if (!(o instanceof PackageFragmentRoot)) 331 return false; 332 PackageFragmentRoot other = (PackageFragmentRoot) o; 333 return this.resource.equals(other.resource) && 334 this.parent.equals(other.parent); 335 } 336 337 340 public boolean exists() { 341 return super.exists() && validateOnClasspath().isOK(); 342 } 343 344 private IClasspathEntry findSourceAttachmentRecommendation() { 345 try { 346 IPath rootPath = this.getPath(); 347 IClasspathEntry entry; 348 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); 349 350 JavaProject parentProject = (JavaProject) getJavaProject(); 352 try { 353 entry = parentProject.getClasspathEntryFor(rootPath); 354 if (entry != null){ 355 Object target = JavaModel.getTarget(workspaceRoot, entry.getSourceAttachmentPath(), true); 356 if (target instanceof IResource) { 357 if (target instanceof IFile) { 358 IFile file = (IFile) target; 359 if (org.eclipse.jdt.internal.compiler.util.Util.isArchiveFileName(file.getName())){ 360 return entry; 361 } 362 } else if (target instanceof IContainer) { 363 return entry; 364 } 365 } else if (target instanceof java.io.File ){ 366 java.io.File file = JavaModel.getFile(target); 367 if (file != null) { 368 if (org.eclipse.jdt.internal.compiler.util.Util.isArchiveFileName(file.getName())){ 369 return entry; 370 } 371 } else { 372 return entry; 374 } 375 } 376 } 377 } catch(JavaModelException e){ 378 } 380 381 IJavaModel model = getJavaModel(); 383 IJavaProject[] jProjects = model.getJavaProjects(); 384 for (int i = 0, max = jProjects.length; i < max; i++){ 385 JavaProject jProject = (JavaProject) jProjects[i]; 386 if (jProject == parentProject) continue; try { 388 entry = jProject.getClasspathEntryFor(rootPath); 389 if (entry != null){ 390 Object target = JavaModel.getTarget(workspaceRoot, entry.getSourceAttachmentPath(), true); 391 if (target instanceof IResource) { 392 if (target instanceof IFile){ 393 IFile file = (IFile) target; 394 if (org.eclipse.jdt.internal.compiler.util.Util.isArchiveFileName(file.getName())){ 395 return entry; 396 } 397 } else if (target instanceof IContainer) { 398 return entry; 399 } 400 } else if (target instanceof java.io.File ){ 401 java.io.File file = (java.io.File ) target; 402 if (file.isFile()) { 403 if (org.eclipse.jdt.internal.compiler.util.Util.isArchiveFileName(file.getName())){ 404 return entry; 405 } 406 } else { 407 return entry; 409 } 410 } 411 } 412 } catch(JavaModelException e){ 413 } 415 } 416 } catch(JavaModelException e){ 417 } 419 420 return null; 421 } 422 423 426 public char[][] fullExclusionPatternChars() { 427 try { 428 if (this.isOpen() && this.getKind() != IPackageFragmentRoot.K_SOURCE) return null; 429 ClasspathEntry entry = (ClasspathEntry)getRawClasspathEntry(); 430 if (entry == null) { 431 return null; 432 } else { 433 return entry.fullExclusionPatternChars(); 434 } 435 } catch (JavaModelException e) { 436 return null; 437 } 438 } 439 440 443 public char[][] fullInclusionPatternChars() { 444 try { 445 if (this.isOpen() && this.getKind() != IPackageFragmentRoot.K_SOURCE) return null; 446 ClasspathEntry entry = (ClasspathEntry)getRawClasspathEntry(); 447 if (entry == null) { 448 return null; 449 } else { 450 return entry.fullInclusionPatternChars(); 451 } 452 } catch (JavaModelException e) { 453 return null; 454 } 455 } 456 public String getElementName() { 457 if (this.resource instanceof IFolder) 458 return ((IFolder) this.resource).getName(); 459 return ""; } 461 464 public int getElementType() { 465 return PACKAGE_FRAGMENT_ROOT; 466 } 467 470 protected char getHandleMementoDelimiter() { 471 return JavaElement.JEM_PACKAGEFRAGMENTROOT; 472 } 473 476 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner owner) { 477 switch (token.charAt(0)) { 478 case JEM_PACKAGEFRAGMENT: 479 String pkgName; 480 if (memento.hasMoreTokens()) { 481 pkgName = memento.nextToken(); 482 char firstChar = pkgName.charAt(0); 483 if (firstChar == JEM_CLASSFILE || firstChar == JEM_COMPILATIONUNIT || firstChar == JEM_COUNT) { 484 token = pkgName; 485 pkgName = IPackageFragment.DEFAULT_PACKAGE_NAME; 486 } else { 487 token = null; 488 } 489 } else { 490 pkgName = IPackageFragment.DEFAULT_PACKAGE_NAME; 491 token = null; 492 } 493 JavaElement pkg = (JavaElement)getPackageFragment(pkgName); 494 if (token == null) { 495 return pkg.getHandleFromMemento(memento, owner); 496 } else { 497 return pkg.getHandleFromMemento(token, memento, owner); 498 } 499 } 500 return null; 501 } 502 505 protected void getHandleMemento(StringBuffer buff) { 506 IPath path; 507 IResource underlyingResource = getResource(); 508 if (underlyingResource != null) { 509 if (getResource().getProject().equals(getJavaProject().getProject())) { 511 path = underlyingResource.getProjectRelativePath(); 512 } else { 513 path = underlyingResource.getFullPath(); 514 } 515 } else { 516 path = getPath(); 518 } 519 ((JavaElement)getParent()).getHandleMemento(buff); 520 buff.append(getHandleMementoDelimiter()); 521 escapeMementoName(buff, path.toString()); 522 } 523 526 public int getKind() throws JavaModelException { 527 return ((PackageFragmentRootInfo)getElementInfo()).getRootKind(); 528 } 529 530 533 public Object [] getNonJavaResources() throws JavaModelException { 534 return ((PackageFragmentRootInfo) getElementInfo()).getNonJavaResources(getJavaProject(), getResource(), this); 535 } 536 537 540 public IPackageFragment getPackageFragment(String packageName) { 541 String [] pkgName = Util.getTrimmedSimpleNames(packageName); 543 return getPackageFragment(pkgName); 544 } 545 public PackageFragment getPackageFragment(String [] pkgName) { 546 return new PackageFragment(this, pkgName); 547 } 548 552 protected String getPackageName(IFolder folder) { 553 IPath myPath= getPath(); 554 IPath pkgPath= folder.getFullPath(); 555 int mySegmentCount= myPath.segmentCount(); 556 int pkgSegmentCount= pkgPath.segmentCount(); 557 StringBuffer pkgName = new StringBuffer (IPackageFragment.DEFAULT_PACKAGE_NAME); 558 for (int i= mySegmentCount; i < pkgSegmentCount; i++) { 559 if (i > mySegmentCount) { 560 pkgName.append('.'); 561 } 562 pkgName.append(pkgPath.segment(i)); 563 } 564 return pkgName.toString(); 565 } 566 567 570 public IPath getPath() { 571 return getResource().getFullPath(); 572 } 573 574 577 public IClasspathEntry getRawClasspathEntry() throws JavaModelException { 578 579 IClasspathEntry rawEntry = null; 580 JavaProject project = (JavaProject)this.getJavaProject(); 581 project.getResolvedClasspath(); Map rootPathToRawEntries = project.getPerProjectInfo().rootPathToRawEntries; 583 if (rootPathToRawEntries != null) { 584 rawEntry = (IClasspathEntry) rootPathToRawEntries.get(this.getPath()); 585 } 586 if (rawEntry == null) { 587 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_NOT_ON_CLASSPATH, this)); 588 } 589 return rawEntry; 590 } 591 592 595 public IResource getResource() { 596 return (IResource)this.resource; 597 } 598 599 602 public IPath getSourceAttachmentPath() throws JavaModelException { 603 if (getKind() != K_BINARY) return null; 604 605 IPath path = getPath(); 607 String serverPathString= Util.getSourceAttachmentProperty(path); 608 if (serverPathString != null) { 609 int index= serverPathString.lastIndexOf(ATTACHMENT_PROPERTY_DELIMITER); 610 if (index < 0) { 611 return new Path(serverPathString); 613 } else { 614 String serverSourcePathString= serverPathString.substring(0, index); 615 return new Path(serverSourcePathString); 616 } 617 } 618 619 IClasspathEntry entry = ((JavaProject) getParent()).getClasspathEntryFor(path); 621 IPath sourceAttachmentPath; 622 if (entry != null && (sourceAttachmentPath = entry.getSourceAttachmentPath()) != null) 623 return sourceAttachmentPath; 624 625 entry = findSourceAttachmentRecommendation(); 627 if (entry != null && (sourceAttachmentPath = entry.getSourceAttachmentPath()) != null) { 628 return sourceAttachmentPath; 629 } 630 631 return null; 632 } 633 634 638 public void setSourceMapper(SourceMapper mapper) throws JavaModelException { 639 ((PackageFragmentRootInfo) getElementInfo()).setSourceMapper(mapper); 640 } 641 642 643 644 647 public IPath getSourceAttachmentRootPath() throws JavaModelException { 648 if (getKind() != K_BINARY) return null; 649 650 IPath path = getPath(); 652 String serverPathString= Util.getSourceAttachmentProperty(path); 653 if (serverPathString != null) { 654 int index = serverPathString.lastIndexOf(ATTACHMENT_PROPERTY_DELIMITER); 655 if (index == -1) return null; 656 String serverRootPathString= IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH; 657 if (index != serverPathString.length() - 1) { 658 serverRootPathString= serverPathString.substring(index + 1); 659 } 660 return new Path(serverRootPathString); 661 } 662 663 IClasspathEntry entry = ((JavaProject) getParent()).getClasspathEntryFor(path); 665 IPath sourceAttachmentRootPath; 666 if (entry != null && (sourceAttachmentRootPath = entry.getSourceAttachmentRootPath()) != null) 667 return sourceAttachmentRootPath; 668 669 entry = findSourceAttachmentRecommendation(); 671 if (entry != null && (sourceAttachmentRootPath = entry.getSourceAttachmentRootPath()) != null) 672 return sourceAttachmentRootPath; 673 674 return null; 675 } 676 677 680 public SourceMapper getSourceMapper() { 681 SourceMapper mapper; 682 try { 683 PackageFragmentRootInfo rootInfo = (PackageFragmentRootInfo) getElementInfo(); 684 mapper = rootInfo.getSourceMapper(); 685 if (mapper == null) { 686 IPath sourcePath= getSourceAttachmentPath(); 688 IPath rootPath= getSourceAttachmentRootPath(); 689 if (sourcePath == null) 690 mapper = createSourceMapper(getPath(), rootPath); else 692 mapper = createSourceMapper(sourcePath, rootPath); 693 rootInfo.setSourceMapper(mapper); 694 } 695 } catch (JavaModelException e) { 696 mapper = null; 698 } 699 return mapper; 700 } 701 702 705 public IResource getUnderlyingResource() throws JavaModelException { 706 if (!exists()) throw newNotPresentException(); 707 return getResource(); 708 } 709 710 713 public boolean hasChildren() throws JavaModelException { 714 return true; 716 } 717 718 public int hashCode() { 719 return this.resource.hashCode(); 720 } 721 722 725 public boolean isArchive() { 726 return false; 727 } 728 729 732 public boolean isExternal() { 733 return false; 734 } 735 736 739 protected IStatus validateOnClasspath() { 740 741 IPath path = this.getPath(); 742 try { 743 JavaProject project = (JavaProject) getJavaProject(); 745 IClasspathEntry entry = project.getClasspathEntryFor(path); 746 if (entry != null) { 747 return Status.OK_STATUS; 748 } 749 } catch(JavaModelException e){ 750 return e.getJavaModelStatus(); 752 } 753 return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_NOT_ON_CLASSPATH, this); 754 } 755 758 public void move( 759 IPath destination, 760 int updateResourceFlags, 761 int updateModelFlags, 762 IClasspathEntry sibling, 763 IProgressMonitor monitor) 764 throws JavaModelException { 765 766 MovePackageFragmentRootOperation op = 767 new MovePackageFragmentRootOperation(this, destination, updateResourceFlags, updateModelFlags, sibling); 768 op.runOperation(monitor); 769 } 770 771 774 protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) { 775 buffer.append(this.tabString(tab)); 776 IPath path = getPath(); 777 if (getJavaProject().getElementName().equals(path.segment(0))) { 778 if (path.segmentCount() == 1) { 779 buffer.append("<project root>"); } else { 781 buffer.append(path.removeFirstSegments(1).makeRelative()); 782 } 783 } else { 784 if (isExternal()) { 785 buffer.append(path.toOSString()); 786 } else { 787 buffer.append(path); 788 } 789 } 790 if (info == null) { 791 buffer.append(" (not open)"); } 793 } 794 795 804 protected void verifyAttachSource(IPath sourcePath) throws JavaModelException { 805 if (!exists()) { 806 throw newNotPresentException(); 807 } else if (this.getKind() != K_BINARY) { 808 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, this)); 809 } else if (sourcePath != null && !sourcePath.isAbsolute()) { 810 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.RELATIVE_PATH, sourcePath)); 811 } 812 } 813 814 } 815 | Popular Tags |