1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.util.ArrayList;
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
18 import net.sourceforge.phpdt.core.IPackageFragment;
19 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
20 import net.sourceforge.phpdt.core.JavaCore;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.core.WorkingCopyOwner;
23 import net.sourceforge.phpdt.core.compiler.CharOperation;
24 import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
25 import net.sourceforge.phpdt.internal.core.util.Util;
27 import org.eclipse.core.resources.IContainer;
28 import org.eclipse.core.resources.IFolder;
29 import org.eclipse.core.resources.IResource;
30 import org.eclipse.core.resources.ResourcesPlugin;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IPath;
33 import org.eclipse.core.runtime.IProgressMonitor;
34 import org.eclipse.core.runtime.QualifiedName;
37 * @see IPackageFragmentRoot
39 public class PackageFragmentRoot extends Openable implements
40 IPackageFragmentRoot {
43 * The delimiter between the source path and root path in the attachment
46 protected final static char ATTACHMENT_PROPERTY_DELIMITER = '*';
49 * No source attachment property
51 protected final static String NO_SOURCE_ATTACHMENT = ""; //$NON-NLS-1$
54 * No source mapper singleton
56 // protected final static SourceMapper NO_SOURCE_MAPPER = new
59 * The resource associated with this root. (an IResource or a java.io.File
60 * (for external jar only))
62 protected Object resource;
65 * Constructs a package fragment root which is the root of the java package
66 * directory hierarchy.
68 protected PackageFragmentRoot(IResource resource, JavaProject project,
71 this.resource = resource;
77 protected boolean buildStructure(OpenableElementInfo info,
78 IProgressMonitor pm, Map newElements, IResource underlyingResource)
79 throws JavaModelException {
81 // check whether this pkg fragment root can be opened
82 if (!resourceExists()) { // || !isOnClasspath()) {
83 throw newNotPresentException();
86 ((PackageFragmentRootInfo) info)
87 .setRootKind(determineKind(underlyingResource));
88 return computeChildren(info, newElements);
92 * Returns the root's kind - K_SOURCE or K_BINARY, defaults to K_SOURCE if
93 * it is not on the classpath.
95 * @exception NotPresentException
96 * if the project and root do not exist.
98 protected int determineKind(IResource underlyingResource)
99 throws JavaModelException {
100 // IClasspathEntry[] entries=
101 // ((JavaProject)getJavaProject()).getExpandedClasspath(true);
102 // for (int i= 0; i < entries.length; i++) {
103 // IClasspathEntry entry= entries[i];
104 // if (entry.getPath().equals(underlyingResource.getFullPath())) {
105 // return entry.getContentKind();
108 return IPackageFragmentRoot.K_SOURCE;
112 * Compute the package fragment children of this package fragment root.
114 * @exception JavaModelException
115 * The resource associated with this package fragment root
118 protected boolean computeChildren(OpenableElementInfo info, Map newElements)
119 throws JavaModelException {
120 // Note the children are not opened (so not added to newElements) for a
121 // regular package fragment root
122 // Howver they are opened for a Jar package fragment root (see
123 // JarPackageFragmentRoot#computeChildren)
125 // the underlying resource may be a folder or a project (in the case
126 // that the project folder
127 // is actually the package fragment root)
128 IResource underlyingResource = getResource();
129 if (underlyingResource.getType() == IResource.FOLDER
130 || underlyingResource.getType() == IResource.PROJECT) {
131 ArrayList vChildren = new ArrayList(5);
132 IContainer rootFolder = (IContainer) underlyingResource;
133 // char[][] inclusionPatterns = fullInclusionPatternChars();
134 char[][] exclusionPatterns = fullExclusionPatternChars();
135 computeFolderChildren(rootFolder, !Util.isExcluded(rootFolder,
136 exclusionPatterns), "", vChildren, exclusionPatterns); //$NON-NLS-1$
138 IJavaElement[] children = new IJavaElement[vChildren.size()];
139 vChildren.toArray(children);
140 info.setChildren(children);
142 } catch (JavaModelException e) {
143 // problem resolving children; structure remains unknown
144 info.setChildren(new IJavaElement[] {});
151 * Starting at this folder, create package fragments and add the fragments
152 * that are not exclused to the collection of children.
154 * @exception JavaModelException
155 * The resource associated with this package fragment does
158 protected void computeFolderChildren(IContainer folder, boolean isIncluded,
159 String prefix, ArrayList vChildren, char[][] exclusionPatterns)
160 throws JavaModelException {
161 // , char[][] inclusionPatterns, char[][] exclusionPatterns) throws
162 // JavaModelException {
165 IPackageFragment pkg = getPackageFragment(prefix);
169 //JavaProject javaProject = (JavaProject) getJavaProject();
170 IResource[] members = folder.members();
171 boolean hasIncluded = isIncluded;
172 for (int i = 0, max = members.length; i < max; i++) {
173 IResource member = members[i];
174 String memberName = member.getName();
176 switch (member.getType()) {
178 case IResource.FOLDER:
179 if (Util.isValidFolderNameForPackage(memberName)) {
180 // boolean isMemberIncluded = !Util.isExcluded(member,
181 // exclusionPatterns);
182 // keep looking inside as long as included already, or
183 // may have child included due to inclusion patterns
184 // if (isMemberIncluded || inclusionPatterns != null) {
185 // // eliminate binary output only if nested inside
187 // if (javaProject.contains(member)) {
189 // if (prefix.length() == 0) {
190 // newPrefix = memberName;
192 // newPrefix = prefix + "." + memberName; //$NON-NLS-1$
194 // computeFolderChildren((IFolder) member,
195 // isMemberIncluded, newPrefix, vChildren,
196 // inclusionPatterns,
197 // exclusionPatterns);
203 // inclusion filter may only include files, in which case we
204 // still want to include the immediate parent package
207 && Util.isValidCompilationUnitName(memberName)
208 && !Util.isExcluded(member, exclusionPatterns)) {
210 IPackageFragment pkg = getPackageFragment(prefix);
216 } catch (IllegalArgumentException e) {
217 throw new JavaModelException(e,
218 IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST); // could
226 } catch (CoreException e) {
227 throw new JavaModelException(e);
232 * @see IPackageFragmentRoot
234 // public void attachSource(IPath sourcePath, IPath rootPath,
235 // IProgressMonitor monitor) throws JavaModelException {
237 // verifyAttachSource(sourcePath);
238 // if (monitor != null) {
239 // monitor.beginTask(ProjectPrefUtil.bind("element.attachingSource"), 2);
242 // SourceMapper oldMapper= getSourceMapper();
243 // IWorkspace workspace = ResourcesPlugin.getWorkspace();
244 // boolean rootNeedsToBeClosed= false;
246 // if (sourcePath == null) {
247 // //source being detached
248 // rootNeedsToBeClosed= true;
249 // setSourceMapper(null);
250 // /* Disable deltas (see 1GDTUSD)
251 // // fire a delta to notify the UI about the source detachement.
252 // JavaModelManager manager = (JavaModelManager)
253 // JavaModelManager.getJavaModelManager();
254 // JavaModel model = (JavaModel) getJavaModel();
255 // JavaElementDelta attachedSourceDelta = new JavaElementDelta(model);
256 // attachedSourceDelta .sourceDetached(this); // this would be a
257 // PackageFragmentRoot
258 // manager.registerResourceDelta(attachedSourceDelta );
259 // manager.fire(); // maybe you want to fire the change later. Let us know
264 // // fire a delta to notify the UI about the source attachement.
265 // JavaModelManager manager = (JavaModelManager)
266 // JavaModelManager.getJavaModelManager();
267 // JavaModel model = (JavaModel) getJavaModel();
268 // JavaElementDelta attachedSourceDelta = new JavaElementDelta(model);
269 // attachedSourceDelta .sourceAttached(this); // this would be a
270 // PackageFragmentRoot
271 // manager.registerResourceDelta(attachedSourceDelta );
272 // manager.fire(); // maybe you want to fire the change later. Let us know
276 // //check if different from the current attachment
277 // IPath storedSourcePath= getSourceAttachmentPath();
278 // IPath storedRootPath= getSourceAttachmentRootPath();
279 // if (monitor != null) {
280 // monitor.worked(1);
282 // if (storedSourcePath != null) {
283 // if (!(storedSourcePath.equals(sourcePath) && (rootPath != null &&
284 // rootPath.equals(storedRootPath)) || storedRootPath == null))
286 // rootNeedsToBeClosed= true;
289 // // check if source path is valid
290 // Object target = JavaModel.getTarget(workspace.getRoot(), sourcePath,
292 // if (target == null) {
293 // if (monitor != null) {
296 // throw new JavaModelException(new
297 // JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, sourcePath));
299 // SourceMapper mapper = createSourceMapper(sourcePath, rootPath);
300 // if (rootPath == null && mapper.rootPath != null) {
301 // // as a side effect of calling the SourceMapper constructor, the root
303 // rootPath = new Path(mapper.rootPath);
305 // setSourceMapper(mapper);
307 // if (sourcePath == null) {
308 // setSourceAttachmentProperty(null); //remove the property
310 // //set the property to the path of the mapped source
311 // setSourceAttachmentProperty(
312 // sourcePath.toString()
313 // + (rootPath == null ? "" : (ATTACHMENT_PROPERTY_DELIMITER +
314 // rootPath.toString()))); //$NON-NLS-1$
316 // if (rootNeedsToBeClosed) {
317 // if (oldMapper != null) {
318 // oldMapper.close();
320 // BufferManager manager= BufferManager.getDefaultBufferManager();
321 // Enumeration openBuffers= manager.getOpenBuffers();
322 // while (openBuffers.hasMoreElements()) {
323 // IBuffer buffer= (IBuffer) openBuffers.nextElement();
324 // IOpenable possibleMember= buffer.getOwner();
325 // if (isAncestorOf((IJavaElement) possibleMember)) {
329 // if (monitor != null) {
330 // monitor.worked(1);
333 // } catch (JavaModelException e) {
334 // setSourceAttachmentProperty(null); // loose info - will be recomputed
337 // if (monitor != null) {
342 // SourceMapper createSourceMapper(IPath sourcePath, IPath rootPath) {
343 // SourceMapper mapper = new SourceMapper(
345 // rootPath == null ? null : rootPath.toOSString(),
346 // this.isExternal() ? JavaCore.getOptions() :
347 // this.getJavaProject().getOptions(true)); // only project options if
353 * @see net.sourceforge.phpdt.core.IPackageFragmentRoot#delete
355 // public void delete(
356 // int updateResourceFlags,
357 // int updateModelFlags,
358 // IProgressMonitor monitor)
359 // throws JavaModelException {
361 // DeletePackageFragmentRootOperation op = new
362 // DeletePackageFragmentRootOperation(this, updateResourceFlags,
363 // updateModelFlags);
364 // runOperation(op, monitor);
367 * This root is being closed. If this root has an associated source
368 * attachment, close it too.
372 // protected void closing(Object info) throws JavaModelException { TODO
374 // ((PackageFragmentRootInfo) info).sourceMapper = null;
375 // super.closing(info);
378 * Compute the package fragment children of this package fragment root.
380 * @exception JavaModelException
381 * The resource associated with this package fragment root
384 // protected boolean computeChildren(OpenableElementInfo info) throws
385 // JavaModelException {
387 // // the underlying resource may be a folder or a project (in the case that
388 // the project folder
389 // // is actually the package fragment root)
390 // IResource resource = getResource();
391 // if (resource.getType() == IResource.FOLDER || resource.getType() ==
392 // IResource.PROJECT) {
393 // ArrayList vChildren = new ArrayList(5);
394 // char[][] exclusionPatterns = fullExclusionPatternChars();
395 // computeFolderChildren((IContainer) resource, "", vChildren,
396 // exclusionPatterns); //$NON-NLS-1$
397 // IJavaElement[] children = new IJavaElement[vChildren.size()];
398 // vChildren.toArray(children);
399 // info.setChildren(children);
401 // } catch (JavaModelException e) {
402 // //problem resolving children; structure remains unknown
403 // info.setChildren(new IJavaElement[]{});
409 * Starting at this folder, create package fragments and add the fragments
410 * that are not exclused to the collection of children.
412 * @exception JavaModelException
413 * The resource associated with this package fragment does
416 // protected void computeFolderChildren(IContainer folder, String prefix,
417 // ArrayList vChildren, char[][] exclusionPatterns) throws
418 // JavaModelException {
419 // IPackageFragment pkg = getPackageFragment(prefix);
420 // vChildren.add(pkg);
422 // JavaProject javaProject = (JavaProject)getJavaProject();
423 // IResource[] members = folder.members();
424 // for (int i = 0, max = members.length; i < max; i++) {
425 // IResource member = members[i];
426 // String memberName = member.getName();
427 // if (member.getType() == IResource.FOLDER
428 // && ProjectPrefUtil.isValidFolderNameForPackage(memberName)
429 // && !ProjectPrefUtil.isExcluded(member, exclusionPatterns)) {
431 // // eliminate binary output only if nested inside direct subfolders
432 // if (javaProject.contains(member)) {
434 // if (prefix.length() == 0) {
435 // newPrefix = memberName;
437 // newPrefix = prefix + "." + memberName; //$NON-NLS-1$
439 // computeFolderChildren((IFolder) member, newPrefix, vChildren,
440 // exclusionPatterns);
444 // } catch(IllegalArgumentException e){
445 // throw new JavaModelException(e,
446 // IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST); // could be thrown by
447 // ElementTree when path
449 // } catch (CoreException e) {
450 // throw new JavaModelException(e);
454 * Computes and returns the source attachment root path for the given source
455 * attachment path. Returns <code> null </code> if none could be found.
457 * @param sourceAttachmentPath the given absolute path to the source archive
458 * or folder @return the computed source attachment root path or <code> null
459 * </cde> if none could be found @throws JavaModelException
461 // public IPath computeSourceAttachmentRootPath(IPath sourceAttachmentPath)
462 // throws JavaModelException {
463 // IPath sourcePath = this.getSourceAttachmentPath();
464 // if (sourcePath == null) return null;
465 // SourceMapper mapper =
468 // null, // detect root path
469 // this.isExternal() ? JavaCore.getOptions() :
470 // this.getJavaProject().getOptions(true) // only project options if
474 // if (mapper.rootPath == null) return null;
475 // return new Path(mapper.rootPath);
478 * @see net.sourceforge.phpdt.core.IPackageFragmentRoot#copy
481 // IPath destination,
482 // int updateResourceFlags,
483 // int updateModelFlags,
484 // IClasspathEntry sibling,
485 // IProgressMonitor monitor)
486 // throws JavaModelException {
488 // CopyPackageFragmentRootOperation op =
489 // new CopyPackageFragmentRootOperation(this, destination,
490 // updateResourceFlags, updateModelFlags, sibling);
491 // runOperation(op, monitor);
494 * Returns a new element info for this element.
496 protected Object createElementInfo() {
497 return new PackageFragmentRootInfo();
501 * @see IPackageFragmentRoot
503 // public IPackageFragment createPackageFragment(String name, boolean force,
504 // IProgressMonitor monitor) throws JavaModelException {
505 // CreatePackageFragmentOperation op = new
506 // CreatePackageFragmentOperation(this, name, force);
507 // runOperation(op, monitor);
508 // return getPackageFragment(name);
511 * Returns the root's kind - K_SOURCE or K_BINARY, defaults to K_SOURCE if
512 * it is not on the classpath.
514 * @exception NotPresentException
515 * if the project and root do not exist.
517 // protected int determineKind(IResource underlyingResource) throws
518 // JavaModelException {
519 // IClasspathEntry[] entries=
520 // ((JavaProject)getJavaProject()).getExpandedClasspath(true);
521 // for (int i= 0; i < entries.length; i++) {
522 // IClasspathEntry entry= entries[i];
523 // if (entry.getPath().equals(underlyingResource.getFullPath())) {
524 // return entry.getContentKind();
527 // return IPackageFragmentRoot.K_SOURCE;
530 * Compares two objects for equality; for <code>PackageFragmentRoot</code>s,
531 * equality is having the same <code>JavaModel</code>, same resources,
532 * and occurrence count.
535 public boolean equals(Object o) {
538 if (!(o instanceof PackageFragmentRoot))
540 PackageFragmentRoot other = (PackageFragmentRoot) o;
541 return getJavaModel().equals(other.getJavaModel())
542 && this.resource.equals(other.resource)
543 && occurrenceCount == other.occurrenceCount;
549 // public boolean exists() {
550 // return super.exists()
551 // && isOnClasspath();
553 // public IClasspathEntry findSourceAttachmentRecommendation() {
555 // IPath rootPath = this.getPath();
556 // IClasspathEntry entry;
557 // IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
559 // // try on enclosing project first
560 // JavaProject parentProject = (JavaProject) getJavaProject();
562 // entry = parentProject.getClasspathEntryFor(rootPath);
563 // if (entry != null){
564 // Object target = JavaModel.getTarget(workspaceRoot,
565 // entry.getSourceAttachmentPath(), true);
566 // if (target instanceof IFile){
567 // IFile file = (IFile) target;
568 // if (ProjectPrefUtil.isArchiveFileName(file.getName())){
571 // } else if (target instanceof IFolder) {
574 // if (target instanceof java.io.File){
575 // java.io.File file = (java.io.File) target;
576 // if (file.isFile()) {
577 // if (ProjectPrefUtil.isArchiveFileName(file.getName())){
581 // // external directory
586 // } catch(JavaModelException e){
589 // // iterate over all projects
590 // IJavaModel model = getJavaModel();
591 // IJavaProject[] jProjects = model.getJavaProjects();
592 // for (int i = 0, max = jProjects.length; i < max; i++){
593 // JavaProject jProject = (JavaProject) jProjects[i];
594 // if (jProject == parentProject) continue; // already done
596 // entry = jProject.getClasspathEntryFor(rootPath);
597 // if (entry != null){
598 // Object target = JavaModel.getTarget(workspaceRoot,
599 // entry.getSourceAttachmentPath(), true);
600 // if (target instanceof IFile){
601 // IFile file = (IFile) target;
602 // if (ProjectPrefUtil.isArchiveFileName(file.getName())){
605 // } else if (target instanceof IFolder) {
608 // if (target instanceof java.io.File){
609 // java.io.File file = (java.io.File) target;
610 // if (file.isFile()) {
611 // if (ProjectPrefUtil.isArchiveFileName(file.getName())){
615 // // external directory
620 // } catch(JavaModelException e){
623 // } catch(JavaModelException e){
629 * Returns the exclusion patterns from the classpath entry associated with
632 char[][] fullExclusionPatternChars() {
636 // if (this.isOpen() && this.getKind() != IPackageFragmentRoot.K_SOURCE)
638 // ClasspathEntry entry = (ClasspathEntry)getRawClasspathEntry();
639 // if (entry == null) {
642 // return entry.fullExclusionPatternChars();
644 // } catch (JavaModelException e) {
652 protected boolean generateInfos(OpenableElementInfo info,
653 IProgressMonitor pm, Map newElements, IResource underlyingResource)
654 throws JavaModelException {
656 // ((PackageFragmentRootInfo)
657 // info).setRootKind(determineKind(underlyingResource));
658 // return computeChildren(info);
663 * @see JavaElement#getHandleMemento()
665 protected char getHandleMementoDelimiter() {
666 return JavaElement.JEM_PACKAGEFRAGMENTROOT;
672 public int getElementType() {
673 return PACKAGE_FRAGMENT_ROOT;
679 public IJavaElement getHandleFromMemento(String token,
680 MementoTokenizer memento, WorkingCopyOwner owner) {
681 switch (token.charAt(0)) {
683 return getHandleUpdatingCountFromMemento(memento, owner);
684 case JEM_PACKAGEFRAGMENT:
686 if (memento.hasMoreTokens()) {
687 pkgName = memento.nextToken();
688 char firstChar = pkgName.charAt(0);
689 // if (firstChar == JEM_CLASSFILE || firstChar ==
690 // JEM_COMPILATIONUNIT || firstChar == JEM_COUNT) {
691 if (firstChar == JEM_COMPILATIONUNIT || firstChar == JEM_COUNT) {
693 pkgName = IPackageFragment.DEFAULT_PACKAGE_NAME;
698 pkgName = IPackageFragment.DEFAULT_PACKAGE_NAME;
701 JavaElement pkg = (JavaElement) getPackageFragment(pkgName);
703 return pkg.getHandleFromMemento(memento, owner);
705 return pkg.getHandleFromMemento(token, memento, owner);
712 * @see JavaElement#getHandleMemento()
714 public String getHandleMemento() {
716 IResource underlyingResource = getResource();
717 if (underlyingResource != null) {
718 // internal jar or regular root
719 if (getResource().getProject()
720 .equals(getJavaProject().getProject())) {
721 path = underlyingResource.getProjectRelativePath();
723 path = underlyingResource.getFullPath();
729 StringBuffer buff = new StringBuffer(((JavaElement) getParent())
730 .getHandleMemento());
731 buff.append(getHandleMementoDelimiter());
732 escapeMementoName(buff, path.toString());
733 if (this.occurrenceCount > 1) {
734 buff.append(JEM_COUNT);
735 buff.append(this.occurrenceCount);
737 return buff.toString();
741 * @see IPackageFragmentRoot
743 public int getKind() throws JavaModelException {
744 return ((PackageFragmentRootInfo) getElementInfo()).getRootKind();
748 * Returns an array of non-java resources contained in the receiver.
750 // public Object[] getNonJavaResources() throws JavaModelException {
751 // return ((PackageFragmentRootInfo)
752 // getElementInfo()).getNonJavaResources(getJavaProject(), getResource(),
756 * @see IPackageFragmentRoot
758 public IPackageFragment getPackageFragment(String packageName) {
759 if (packageName.indexOf(' ') != -1) { // tolerate package names with
760 // spaces (e.g. 'x . y')
761 // (http://bugs.eclipse.org/bugs/show_bug.cgi?id=21957)
762 char[][] compoundName = Util.toCompoundChars(packageName);
763 StringBuffer buffer = new StringBuffer(packageName.length());
764 for (int i = 0, length = compoundName.length; i < length; i++) {
765 buffer.append(CharOperation.trim(compoundName[i]));
766 if (i != length - 1) {
770 packageName = buffer.toString();
772 return new PackageFragment(this, packageName);
776 * Returns the package name for the given folder (which is a decendent of
779 protected String getPackageName(IFolder folder) throws JavaModelException {
780 IPath myPath = getPath();
781 IPath pkgPath = folder.getFullPath();
782 int mySegmentCount = myPath.segmentCount();
783 int pkgSegmentCount = pkgPath.segmentCount();
784 StringBuffer name = new StringBuffer(
785 IPackageFragment.DEFAULT_PACKAGE_NAME);
786 for (int i = mySegmentCount; i < pkgSegmentCount; i++) {
787 if (i > mySegmentCount) {
790 name.append(pkgPath.segment(i));
792 return name.toString();
798 public IPath getPath() {
799 return getResource().getFullPath();
803 * @see IPackageFragmentRoot
805 // public IClasspathEntry getRawClasspathEntry() throws JavaModelException {
807 // IClasspathEntry rawEntry = null;
808 // IJavaProject project = this.getJavaProject();
809 // project.getResolvedClasspath(true); // force the reverse rawEntry cache
811 // JavaModelManager.PerProjectInfo perProjectInfo =
812 // JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project.getProject());
813 // if (perProjectInfo != null && perProjectInfo.resolvedPathToRawEntries !=
815 // rawEntry = (IClasspathEntry)
816 // perProjectInfo.resolvedPathToRawEntries.get(this.getPath());
823 public IResource getResource() {
824 return (IResource) this.resource;
828 * @see IPackageFragmentRoot
830 // public IPath getSourceAttachmentPath() throws JavaModelException {
831 // if (getKind() != K_BINARY) return null;
833 // String serverPathString= getSourceAttachmentProperty();
834 // if (serverPathString == null) {
837 // int index= serverPathString.lastIndexOf(ATTACHMENT_PROPERTY_DELIMITER);
839 // // no root path specified
840 // return new Path(serverPathString);
842 // String serverSourcePathString= serverPathString.substring(0, index);
843 // return new Path(serverSourcePathString);
847 * Returns the server property for this package fragment root's source
850 // protected String getSourceAttachmentProperty() throws JavaModelException
852 // String propertyString = null;
853 // QualifiedName qName= getSourceAttachmentPropertyName();
856 // ResourcesPlugin.getWorkspace().getRoot().getPersistentProperty(qName);
858 // // if no existing source attachment information, then lookup a
859 // recommendation from classpath entries
860 // if (propertyString == null) {
861 // IClasspathEntry recommendation = findSourceAttachmentRecommendation();
862 // if (recommendation != null) {
863 // IPath rootPath = recommendation.getSourceAttachmentRootPath();
865 // recommendation.getSourceAttachmentPath().toString()
866 // + ((rootPath == null)
867 // ? "" : //$NON-NLS-1$
868 // (ATTACHMENT_PROPERTY_DELIMITER + rootPath.toString()));
869 // setSourceAttachmentProperty(propertyString);
871 // // mark as being already looked up
872 // setSourceAttachmentProperty(NO_SOURCE_ATTACHMENT);
874 // } else if (NO_SOURCE_ATTACHMENT.equals(propertyString)) {
875 // // already looked up and no source attachment found
878 // return propertyString;
879 // } catch (CoreException ce) {
880 // throw new JavaModelException(ce);
884 * Returns the qualified name for the source attachment property of this
887 protected QualifiedName getSourceAttachmentPropertyName()
888 throws JavaModelException {
889 return new QualifiedName(JavaCore.PLUGIN_ID,
890 "sourceattachment: " + this.getPath().toOSString()); //$NON-NLS-1$
893 public void setSourceAttachmentProperty(String property) {
895 ResourcesPlugin.getWorkspace().getRoot().setPersistentProperty(
896 this.getSourceAttachmentPropertyName(), property);
897 } catch (CoreException ce) {
902 * For use by <code>AttachSourceOperation</code> only. Sets the source
903 * mapper associated with this root.
905 // public void setSourceMapper(SourceMapper mapper) throws
906 // JavaModelException {
907 // ((PackageFragmentRootInfo) getElementInfo()).setSourceMapper(mapper);
910 * @see IPackageFragmentRoot
912 // public IPath getSourceAttachmentRootPath() throws JavaModelException {
913 // if (getKind() != K_BINARY) return null;
915 // String serverPathString= getSourceAttachmentProperty();
916 // if (serverPathString == null) {
919 // int index = serverPathString.lastIndexOf(ATTACHMENT_PROPERTY_DELIMITER);
920 // if (index == -1) return null;
921 // String serverRootPathString=
922 // IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH;
923 // if (index != serverPathString.length() - 1) {
924 // serverRootPathString= serverPathString.substring(index + 1);
926 // return new Path(serverRootPathString);
931 // public SourceMapper getSourceMapper() {
932 // SourceMapper mapper;
934 // PackageFragmentRootInfo rootInfo = (PackageFragmentRootInfo)
936 // mapper = rootInfo.getSourceMapper();
937 // if (mapper == null) {
938 // // first call to this method
939 // IPath sourcePath= getSourceAttachmentPath();
940 // if (sourcePath != null) {
941 // IPath rootPath= getSourceAttachmentRootPath();
942 // mapper = this.createSourceMapper(sourcePath, rootPath);
943 // if (rootPath == null && mapper.rootPath != null) {
944 // // as a side effect of calling the SourceMapper constructor, the root
946 // rootPath = new Path(mapper.rootPath);
948 // //set the property to the path of the mapped source
949 // this.setSourceAttachmentProperty(
950 // sourcePath.toString()
951 // + ATTACHMENT_PROPERTY_DELIMITER
952 // + rootPath.toString());
954 // rootInfo.setSourceMapper(mapper);
956 // // remember that no source is attached
957 // rootInfo.setSourceMapper(NO_SOURCE_MAPPER);
960 // } else if (mapper == NO_SOURCE_MAPPER) {
961 // // a previous call to this method found out that no source was attached
964 // } catch (JavaModelException e) {
965 // // no source can be attached
973 public IResource getUnderlyingResource() throws JavaModelException {
975 throw newNotPresentException();
976 return getResource();
979 public int hashCode() {
980 return this.resource.hashCode();
984 * @see IPackageFragmentRoot
986 public boolean isArchive() {
991 * @see IPackageFragmentRoot
993 public boolean isExternal() {
998 * Returns whether this package fragment root is on the classpath of its
1001 // protected boolean isOnClasspath() {
1002 // if (this.getElementType() == IJavaElement.JAVA_PROJECT){
1006 // IPath path = this.getPath();
1008 // // check package fragment root on classpath of its project
1009 // IJavaProject project = this.getJavaProject();
1010 // IClasspathEntry[] classpath = project.getResolvedClasspath(true);
1011 // for (int i = 0, length = classpath.length; i < length; i++) {
1012 // IClasspathEntry entry = classpath[i];
1013 // if (entry.getPath().equals(path)) {
1017 // } catch(JavaModelException e){
1018 // // could not read classpath, then assume it is outside
1023 * @see net.sourceforge.phpdt.core.IPackageFragmentRoot#move
1025 // public void move(
1026 // IPath destination,
1027 // int updateResourceFlags,
1028 // int updateModelFlags,
1029 // IClasspathEntry sibling,
1030 // IProgressMonitor monitor)
1031 // throws JavaModelException {
1033 // MovePackageFragmentRootOperation op =
1034 // new MovePackageFragmentRootOperation(this, destination,
1035 // updateResourceFlags, updateModelFlags, sibling);
1036 // runOperation(op, monitor);
1040 // protected void openWhenClosed(IProgressMonitor pm) throws
1041 // JavaModelException {
1042 // if (!this.resourceExists()
1043 // || !this.isOnClasspath()) {
1044 // throw newNotPresentException();
1046 // super.openWhenClosed(pm);
1049 * Recomputes the children of this element, based on the current state of
1052 // public void refreshChildren() {
1054 // OpenableElementInfo info= (OpenableElementInfo)getElementInfo();
1055 // computeChildren(info);
1056 // } catch (JavaModelException e) {
1061 // * @see JavaElement#rootedAt(IJavaProject)
1063 // public IJavaElement rootedAt(IJavaProject project) {
1065 // new PackageFragmentRoot(
1071 * @private Debugging purposes
1073 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
1074 buffer.append(this.tabString(tab));
1075 if (getElementName().length() == 0) {
1076 buffer.append("[project root]"); //$NON-NLS-1$
1078 IPath path = getPath();
1079 if (getJavaProject().getElementName().equals(path.segment(0))) {
1080 buffer.append(path.removeFirstSegments(1).makeRelative());
1082 buffer.append(path);
1086 buffer.append(" (not open)"); //$NON-NLS-1$
1091 * Possible failures:
1093 * <li>ELEMENT_NOT_PRESENT - the root supplied to the operation does not
1095 * <li>INVALID_ELEMENT_TYPES - the root is not of kind K_BINARY
1096 * <li>RELATIVE_PATH - the path supplied to this operation must be an
1100 // protected void verifyAttachSource(IPath sourcePath) throws
1101 // JavaModelException {
1103 // throw newNotPresentException();
1104 // } else if (this.getKind() != K_BINARY) {
1105 // throw new JavaModelException(new
1106 // JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, this));
1107 // } else if (sourcePath != null && !sourcePath.isAbsolute()) {
1108 // throw new JavaModelException(new
1109 // JavaModelStatus(IJavaModelStatusConstants.RELATIVE_PATH, sourcePath));