<s> package org . eclipse . jdt . internal . core ; import java . util . ArrayList ; import java . util . Enumeration ; import java . util . Map ; import org . eclipse . core . resources . * ; import org . eclipse . core . runtime . * ; import org . eclipse . jdt . core . * ; import org . eclipse . jdt . core . compiler . CharOperation ; import org . eclipse . jdt . internal . core . util . MementoTokenizer ; import org . eclipse . jdt . internal . core . util . Messages ; import org . eclipse . jdt . internal . core . util . Util ; public class PackageFragmentRoot extends Openable implements IPackageFragmentRoot { protected final static char ATTACHMENT_PROPERTY_DELIMITER = '*' ; public final static String NO_SOURCE_ATTACHMENT = "" ; protected IResource resource ; protected PackageFragmentRoot ( IResource resource , JavaProject project ) { super ( project ) ; this . resource = resource ; } public void attachSource ( IPath sourcePath , IPath rootPath , IProgressMonitor monitor ) throws JavaModelException { try { verifyAttachSource ( sourcePath ) ; if ( monitor != null ) { monitor . beginTask ( Messages . element_attachingSource , 2 ) ; } SourceMapper oldMapper = getSourceMapper ( ) ; boolean rootNeedsToBeClosed = false ; if ( sourcePath == null ) { rootNeedsToBeClosed = true ; setSourceMapper ( null ) ; } else { IPath storedSourcePath = getSourceAttachmentPath ( ) ; IPath storedRootPath = getSourceAttachmentRootPath ( ) ; if ( monitor != null ) { monitor . worked ( 1 ) ; } if ( storedSourcePath != null ) { if ( ! ( storedSourcePath . equals ( sourcePath ) && ( rootPath != null && rootPath . equals ( storedRootPath ) ) || storedRootPath == null ) ) { rootNeedsToBeClosed = true ; } } Object target = JavaModel . getTarget ( sourcePath , false ) ; if ( target == null ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . INVALID_PATH , sourcePath ) ) ; } SourceMapper mapper = createSourceMapper ( sourcePath , rootPath ) ; if ( rootPath == null && mapper . rootPath != null ) { rootPath = new Path ( mapper . rootPath ) ; } setSourceMapper ( mapper ) ; } if ( sourcePath == null ) { Util . setSourceAttachmentProperty ( getPath ( ) , null ) ; } else { Util . setSourceAttachmentProperty ( getPath ( ) , sourcePath . toString ( ) + ( rootPath == null ? "" : ( ATTACHMENT_PROPERTY_DELIMITER + rootPath . toString ( ) ) ) ) ; } if ( rootNeedsToBeClosed ) { if ( oldMapper != null ) { oldMapper . close ( ) ; } BufferManager manager = BufferManager . getDefaultBufferManager ( ) ; Enumeration openBuffers = manager . getOpenBuffers ( ) ; while ( openBuffers . hasMoreElements ( ) ) { IBuffer buffer = ( IBuffer ) openBuffers . nextElement ( ) ; IOpenable possibleMember = buffer . getOwner ( ) ; if ( isAncestorOf ( ( IJavaElement ) possibleMember ) ) { buffer . close ( ) ; } } if ( monitor != null ) { monitor . worked ( 1 ) ; } } } catch ( JavaModelException e ) { Util . setSourceAttachmentProperty ( getPath ( ) , null ) ; throw e ; } finally { if ( monitor != null ) { monitor . done ( ) ; } } } protected boolean buildStructure ( OpenableElementInfo info , IProgressMonitor pm , Map newElements , IResource underlyingResource ) throws JavaModelException { ( ( PackageFragmentRootInfo ) info ) . setRootKind ( determineKind ( underlyingResource ) ) ; return computeChildren ( info , underlyingResource ) ; } SourceMapper createSourceMapper ( IPath sourcePath , IPath rootPath ) { SourceMapper mapper = new SourceMapper ( sourcePath , rootPath == null ? null : rootPath . toOSString ( ) , getJavaProject ( ) . getOptions ( true ) ) ; return mapper ; } public void delete ( int updateResourceFlags , int updateModelFlags , IProgressMonitor monitor ) throws JavaModelException { DeletePackageFragmentRootOperation op = new DeletePackageFragmentRootOperation ( this , updateResourceFlags , updateModelFlags ) ; op . runOperation ( monitor ) ; } protected boolean computeChildren ( OpenableElementInfo info , IResource underlyingResource ) throws JavaModelException { try { if ( underlyingResource . getType ( ) == IResource . FOLDER || underlyingResource . getType ( ) == IResource . PROJECT ) { ArrayList vChildren = new ArrayList ( 5 ) ; IContainer rootFolder = ( IContainer ) underlyingResource ; char [ ] [ ] inclusionPatterns = fullInclusionPatternChars ( ) ; char [ ] [ ] exclusionPatterns = fullExclusionPatternChars ( ) ; computeFolderChildren ( rootFolder , ! Util . isExcluded ( rootFolder , inclusionPatterns , exclusionPatterns ) , CharOperation . NO_STRINGS , vChildren , inclusionPatterns , exclusionPatterns ) ; IJavaElement [ ] children = new IJavaElement [ vChildren . size ( ) ] ; vChildren . toArray ( children ) ; info . setChildren ( children ) ; } } catch ( JavaModelException e ) { info . setChildren ( new IJavaElement [ ] { } ) ; throw e ; } return true ; } protected void computeFolderChildren ( IContainer folder , boolean isIncluded , String [ ] pkgName , ArrayList vChildren , char [ ] [ ] inclusionPatterns , char [ ] [ ] exclusionPatterns ) throws JavaModelException { if ( isIncluded ) { IPackageFragment pkg = getPackageFragment ( pkgName ) ; vChildren . add ( pkg ) ; } try { JavaProject javaProject = ( JavaProject ) getJavaProject ( ) ; JavaModelManager manager = JavaModelManager . getJavaModelManager ( ) ; IResource [ ] members = folder . members ( ) ; boolean hasIncluded = isIncluded ; int length = members . length ; if ( length > 0 ) { String sourceLevel = javaProject . getOption ( JavaCore . COMPILER_SOURCE , true ) ; String complianceLevel = javaProject . getOption ( JavaCore . COMPILER_COMPLIANCE , true ) ; for ( int i = 0 ; i < length ; i ++ ) { IResource member = members [ i ] ; String memberName = member . getName ( ) ; switch ( member . getType ( ) ) { case IResource . FOLDER : if ( Util . isValidFolderNameForPackage ( memberName , sourceLevel , complianceLevel ) ) { if ( javaProject . contains ( member ) ) { String [ ] newNames = Util . arrayConcat ( pkgName , manager . intern ( memberName ) ) ; boolean isMemberIncluded = ! Util . isExcluded ( member , inclusionPatterns , exclusionPatterns ) ; computeFolderChildren ( ( IFolder ) member , isMemberIncluded , newNames , vChildren , inclusionPatterns , exclusionPatterns ) ; } } break ; case IResource . FILE : if ( ! hasIncluded && Util . isValidCompilationUnitName ( memberName , sourceLevel , complianceLevel ) && ! Util . isExcluded ( member , inclusionPatterns , exclusionPatterns ) ) { hasIncluded = true ; IPackageFragment pkg = getPackageFragment ( pkgName ) ; vChildren . add ( pkg ) ; } break ; } } } } catch ( IllegalArgumentException e ) { throw new JavaModelException ( e , IJavaModelStatusConstants . ELEMENT_DOES_NOT_EXIST ) ; } catch ( CoreException e ) { throw new JavaModelException ( e ) ; } } public void copy ( IPath destination , int updateResourceFlags , int updateModelFlags , IClasspathEntry sibling , IProgressMonitor monitor ) throws JavaModelException { CopyPackageFragmentRootOperation op = new CopyPackageFragmentRootOperation ( this , destination , updateResourceFlags , updateModelFlags , sibling ) ; op . runOperation ( monitor ) ; } protected Object createElementInfo ( ) { return new PackageFragmentRootInfo ( ) ; } public IPackageFragment createPackageFragment ( String pkgName , boolean force , IProgressMonitor monitor ) throws JavaModelException { CreatePackageFragmentOperation op = new CreatePackageFragmentOperation ( this , pkgName , force ) ; op . runOperation ( monitor ) ; return getPackageFragment ( op . pkgName ) ; } protected int determineKind ( IResource underlyingResource ) throws JavaModelException { IClasspathEntry entry = ( ( JavaProject ) getJavaProject ( ) ) . getClasspathEntryFor ( underlyingResource . getFullPath ( ) ) ; if ( entry != null ) { return entry . getContentKind ( ) ; } return IPackageFragmentRoot . K_SOURCE ; } public boolean equals ( Object o ) { if ( this == o ) return true ; if ( ! ( o instanceof PackageFragmentRoot ) ) return false ; PackageFragmentRoot other = ( PackageFragmentRoot ) o ; return resource ( ) . equals ( other . resource ( ) ) && this . parent . equals ( other . parent ) ; } private IClasspathEntry findSourceAttachmentRecommendation ( ) { try { IPath rootPath = getPath ( ) ; IClasspathEntry entry ; JavaProject parentProject = ( JavaProject ) getJavaProject ( ) ; try { entry = parentProject . getClasspathEntryFor ( rootPath ) ; if ( entry != null ) { Object target = JavaModel . getTarget ( entry . getSourceAttachmentPath ( ) , true ) ; if ( target != null ) { return entry ; } } } catch ( JavaModelException e ) { } IJavaModel model = getJavaModel ( ) ; IJavaProject [ ] jProjects = model . getJavaProjects ( ) ; for ( int i = 0 , max = jProjects . length ; i < max ; i ++ ) { JavaProject jProject = ( JavaProject ) jProjects [ i ] ; if ( jProject == parentProject ) continue ; try { entry = jProject . getClasspathEntryFor ( rootPath ) ; if ( entry != null ) { Object target = JavaModel . getTarget ( entry . getSourceAttachmentPath ( ) , true ) ; if ( target != null ) { return entry ; } } } catch ( JavaModelException e ) { } } } catch ( JavaModelException e ) { } return null ; } public char [ ] [ ] fullExclusionPatternChars ( ) { try { if ( isOpen ( ) && getKind ( ) != IPackageFragmentRoot . K_SOURCE ) return null ; ClasspathEntry entry = ( ClasspathEntry ) getRawClasspathEntry ( ) ; if ( entry == null ) { return null ; } else { return entry . fullExclusionPatternChars ( ) ; } } catch ( JavaModelException e ) { return null ; } } public char [ ] [ ] fullInclusionPatternChars ( ) { try { if ( isOpen ( ) && getKind ( ) != IPackageFragmentRoot . K_SOURCE ) return null ; ClasspathEntry entry = ( ClasspathEntry ) getRawClasspathEntry ( ) ; if ( entry == null ) { return null ; } else { return entry . fullInclusionPatternChars ( ) ; } } catch ( JavaModelException e ) { return null ; } } public String getElementName ( ) { IResource res = resource ( ) ; if ( res instanceof IFolder ) return ( ( IFolder ) res ) . getName ( ) ; return "" ; } public int getElementType ( ) { return PACKAGE_FRAGMENT_ROOT ; } protected char getHandleMementoDelimiter ( ) { return JavaElement . JEM_PACKAGEFRAGMENTROOT ; } public IJavaElement getHandleFromMemento ( String token , MementoTokenizer memento , WorkingCopyOwner owner ) { switch ( token . charAt ( 0 ) ) { case JEM_PACKAGEFRAGMENT : String [ ] pkgName ; if ( memento . hasMoreTokens ( ) ) { token = memento . nextToken ( ) ; char firstChar = token . charAt ( 0 ) ; if ( firstChar == JEM_CLASSFILE || firstChar == JEM_COMPILATIONUNIT || firstChar == JEM_COUNT ) { pkgName = CharOperation . NO_STRINGS ; } else { pkgName = Util . splitOn ( '.' , token , 0 , token . length ( ) ) ; token = null ; } } else { pkgName = CharOperation . NO_STRINGS ; token = null ; } JavaElement pkg = getPackageFragment ( pkgName ) ; if ( token == null ) { return pkg . getHandleFromMemento ( memento , owner ) ; } else { return pkg . getHandleFromMemento ( token , memento , owner ) ; } } return null ; } protected void getHandleMemento ( StringBuffer buff ) { IPath path ; IResource underlyingResource = getResource ( ) ; if ( underlyingResource != null ) { if ( resource ( ) . getProject ( ) . equals ( getJavaProject ( ) . getProject ( ) ) ) { path = underlyingResource . getProjectRelativePath ( ) ; } else { path = underlyingResource . getFullPath ( ) ; } } else { path = getPath ( ) ; } ( ( JavaElement ) getParent ( ) ) . getHandleMemento ( buff ) ; buff . append ( getHandleMementoDelimiter ( ) ) ; escapeMementoName ( buff , path . toString ( ) ) ; } public int getKind ( ) throws JavaModelException { return ( ( PackageFragmentRootInfo ) getElementInfo ( ) ) . getRootKind ( ) ; } int internalKind ( ) throws JavaModelException { JavaModelManager manager = JavaModelManager . getJavaModelManager ( ) ; PackageFragmentRootInfo info = ( PackageFragmentRootInfo ) manager . peekAtInfo ( this ) ; if ( info == null ) { info = ( PackageFragmentRootInfo ) openWhenClosed ( createElementInfo ( ) , null ) ; } return info . getRootKind ( ) ; } public Object [ ] getNonJavaResources ( ) throws JavaModelException { return ( ( PackageFragmentRootInfo ) getElementInfo ( ) ) . getNonJavaResources ( getJavaProject ( ) , resource ( ) , this ) ; } public IPackageFragment getPackageFragment ( String packageName ) { String [ ] pkgName = Util . getTrimmedSimpleNames ( packageName ) ; return getPackageFragment ( pkgName ) ; } public PackageFragment getPackageFragment ( String [ ] pkgName ) { return new PackageFragment ( this , pkgName ) ; } protected String getPackageName ( IFolder folder ) { IPath myPath = getPath ( ) ; IPath pkgPath = folder . getFullPath ( ) ; int mySegmentCount = myPath . segmentCount ( ) ; int pkgSegmentCount = pkgPath . segmentCount ( ) ; StringBuffer pkgName = new StringBuffer ( IPackageFragment . DEFAULT_PACKAGE_NAME ) ; for ( int i = mySegmentCount ; i < pkgSegmentCount ; i ++ ) { if ( i > mySegmentCount ) { pkgName . append ( '.' ) ; } pkgName . append ( pkgPath . segment ( i ) ) ; } return pkgName . toString ( ) ; } public IPath getPath ( ) { return internalPath ( ) ; } public IPath internalPath ( ) { return resource ( ) . getFullPath ( ) ; } public IClasspathEntry getRawClasspathEntry ( ) throws JavaModelException { IClasspathEntry rawEntry = null ; JavaProject project = ( JavaProject ) getJavaProject ( ) ; project . getResolvedClasspath ( ) ; Map rootPathToRawEntries = project . getPerProjectInfo ( ) . rootPathToRawEntries ; if ( rootPathToRawEntries != null ) { rawEntry = ( IClasspathEntry ) rootPathToRawEntries . get ( getPath ( ) ) ; } if ( rawEntry == null ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . ELEMENT_NOT_ON_CLASSPATH , this ) ) ; } return rawEntry ; } public IClasspathEntry getResolvedClasspathEntry ( ) throws JavaModelException { IClasspathEntry resolvedEntry = null ; JavaProject project = ( JavaProject ) getJavaProject ( ) ; project . getResolvedClasspath ( ) ; Map rootPathToResolvedEntries = project . getPerProjectInfo ( ) . rootPathToResolvedEntries ; if ( rootPathToResolvedEntries != null ) { resolvedEntry = ( IClasspathEntry ) rootPathToResolvedEntries . get ( getPath ( ) ) ; } if ( resolvedEntry == null ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . ELEMENT_NOT_ON_CLASSPATH , this ) ) ; } return resolvedEntry ; } public IResource resource ( ) { if ( this . resource != null ) return this . resource ; return super . resource ( ) ; } public IResource resource ( PackageFragmentRoot root ) { return this . resource ; } public IPath getSourceAttachmentPath ( ) throws JavaModelException { if ( getKind ( ) != K_BINARY ) return null ; IPath path = getPath ( ) ; String serverPathString = Util . getSourceAttachmentProperty ( path ) ; if ( serverPathString != null ) { int index = serverPathString . lastIndexOf ( ATTACHMENT_PROPERTY_DELIMITER ) ; if ( index < 0 ) { return new Path ( serverPathString ) ; } else { String serverSourcePathString = serverPathString . substring ( 0 , index ) ; return new Path ( serverSourcePathString ) ; } } IClasspathEntry entry = ( ( JavaProject ) getParent ( ) ) . getClasspathEntryFor ( path ) ; IPath sourceAttachmentPath ; if ( entry != null && ( sourceAttachmentPath = entry . getSourceAttachmentPath ( ) ) != null ) return sourceAttachmentPath ; entry = findSourceAttachmentRecommendation ( ) ; if ( entry != null && ( sourceAttachmentPath = entry . getSourceAttachmentPath ( ) ) != null ) { return sourceAttachmentPath ; } return null ; } public void setSourceMapper ( SourceMapper mapper ) throws JavaModelException { ( ( PackageFragmentRootInfo ) getElementInfo ( ) ) . setSourceMapper ( mapper ) ; } public IPath getSourceAttachmentRootPath ( ) throws JavaModelException { if ( getKind ( ) != K_BINARY ) return null ; IPath path = getPath ( ) ; String serverPathString = Util . getSourceAttachmentProperty ( path ) ; if ( serverPathString != null ) { int index = serverPathString . lastIndexOf ( ATTACHMENT_PROPERTY_DELIMITER ) ; if ( index == - 1 ) return null ; String serverRootPathString = IPackageFragmentRoot . DEFAULT_PACKAGEROOT_PATH ; if ( index != serverPathString . length ( ) - 1 ) { serverRootPathString = serverPathString . substring ( index + 1 ) ; } return new Path ( serverRootPathString ) ; } IClasspathEntry entry = ( ( JavaProject ) getParent ( ) ) . getClasspathEntryFor ( path ) ; IPath sourceAttachmentRootPath ; if ( entry != null && ( sourceAttachmentRootPath = entry . getSourceAttachmentRootPath ( ) ) != null ) return sourceAttachmentRootPath ; entry = findSourceAttachmentRecommendation ( ) ; if ( entry != null && ( sourceAttachmentRootPath = entry . getSourceAttachmentRootPath ( ) ) != null ) return sourceAttachmentRootPath ; return null ; } public SourceMapper getSourceMapper ( ) { SourceMapper mapper ; try { PackageFragmentRootInfo rootInfo = ( PackageFragmentRootInfo ) getElementInfo ( ) ; mapper = rootInfo . getSourceMapper ( ) ; if ( mapper == null ) { IPath sourcePath = getSourceAttachmentPath ( ) ; IPath rootPath = getSourceAttachmentRootPath ( ) ; if ( sourcePath == null ) mapper = createSourceMapper ( getPath ( ) , rootPath ) ; else mapper = createSourceMapper ( sourcePath , rootPath ) ; rootInfo . setSourceMapper ( mapper ) ; } } catch ( JavaModelException e ) { mapper = null ; } return mapper ; } public IResource getUnderlyingResource ( ) throws JavaModelException { if ( ! exists ( ) ) throw newNotPresentException ( ) ; return resource ( ) ; } public boolean hasChildren ( ) throws JavaModelException { return true ; } public int hashCode ( ) { return resource ( ) . hashCode ( ) ; } public boolean isArchive ( ) { return false ; } public boolean isExternal ( ) { return false ; } protected IStatus validateOnClasspath ( ) { IPath path = getPath ( ) ; try { JavaProject project = ( JavaProject ) getJavaProject ( ) ; IClasspathEntry entry = project . getClasspathEntryFor ( path ) ; if ( entry != null ) { return Status . OK_STATUS ; } } catch ( JavaModelException e ) { return e . getJavaModelStatus ( ) ; } return new JavaModelStatus ( IJavaModelStatusConstants . ELEMENT_NOT_ON_CLASSPATH , this ) ; } public void move ( IPath destination , int updateResourceFlags , int updateModelFlags , IClasspathEntry sibling , IProgressMonitor monitor ) throws JavaModelException { MovePackageFragmentRootOperation op = new MovePackageFragmentRootOperation ( this , destination , updateResourceFlags , updateModelFlags , sibling ) ; op . runOperation ( monitor ) ; } protected void toStringInfo ( int tab , StringBuffer buffer , Object info , boolean showResolvedInfo ) { buffer . append ( tabString ( tab ) ) ; IPath path = getPath ( ) ; if ( isExternal ( ) ) { buffer . append ( path . toOSString ( ) ) ; } else if ( getJavaProject ( ) . getElementName ( ) . equals ( path . segment ( 0 ) ) ) { if ( path . segmentCount ( ) == 1 ) { buffer . append ( "" ) ; } else { buffer . append ( path . removeFirstSegments ( 1 ) . makeRelative ( ) ) ; } } else { buffer . append ( path ) ; } if ( info == null ) { buffer . append ( " (not open)" ) ; } } protected IStatus validateExistence ( IResource underlyingResource ) { IStatus status = validateOnClasspath ( ) ; if ( ! status . isOK ( ) ) return status ; if ( ! resourceExists ( underlyingResource ) ) return newDoesNotExistStatus ( ) ; return JavaModelStatus . VERIFIED_OK ; } protected void verifyAttachSource ( IPath sourcePath ) throws JavaModelException { if ( ! exists ( ) ) { throw newNotPresentException ( ) ; } else if ( getKind ( ) != K_BINARY ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . INVALID_ELEMENT_TYPES , this ) ) ; } else if ( sourcePath != null && ! sourcePath . isAbsolute ( ) ) { throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . RELATIVE_PATH , sourcePath ) ) ; } } } </s>