public Object getHierarchicalPackageParent(final IScriptFolder child) {
   String name = child.getElementName();
   IProjectFragment parent = (IProjectFragment) child.getParent();
   int index = name.lastIndexOf(IScriptFolder.PACKAGE_DELIMITER);
   if (index != -1) {
     String realParentName = name.substring(0, index);
     IScriptFolder element = parent.getScriptFolder(realParentName);
     if (element != null && element.exists()) {
       try {
         if (fFoldPackages
             && ScriptExplorerContentProvider.isEmpty(element)
             && ScriptExplorerContentProvider.findSinglePackageChild(element, parent.getChildren())
                 != null) {
           return getHierarchicalPackageParent(element);
         }
       } catch (ModelException e) {
         // ignore
       }
       return element;
     } else if (element != null) { // bug 65240
       IResource resource = element.getResource();
       if (resource != null) {
         return resource;
       }
     }
   }
   if (parent.getResource() instanceof IProject) {
     return parent.getScriptProject();
   }
   return parent;
 }
  protected Object internalGetParent(final Object element) {
    if (!fIsFlatLayout && element instanceof IScriptFolder) {
      return getHierarchicalPackageParent((IScriptFolder) element);
    } else if (element instanceof IProjectFragment) {
      // since we insert logical package containers we have to fix
      // up the parent for package fragment roots so that they refer
      // to the container and containers refer to the project
      IProjectFragment root = (IProjectFragment) element;

      try {
        IBuildpathEntry entry = root.getRawBuildpathEntry();
        if (entry != null) {
          int entryKind = entry.getEntryKind();
          if (entryKind == IBuildpathEntry.BPE_CONTAINER) {
            return new BuildPathContainer(root.getScriptProject(), entry);
          } else if (fShowLibrariesNode && (entryKind == IBuildpathEntry.BPE_LIBRARY /*
																		 * ||
																		 * entryKind
																		 * ==
																		 * IBuildpathEntry
																		 * .
																		 * BPE_VARIABLE
																		 */)) {
            return new LibraryContainer(root.getScriptProject());
          }
        }
      } catch (ModelException e) {
        // fall through
      }
    } else if (element instanceof ProjectFragmentContainer) {
      return ((ProjectFragmentContainer) element).getScriptProject();
    }
    return super.internalGetParent(element);
  }
 /**
  * Possible failures:
  *
  * <ul>
  *   <li>NO_ELEMENTS_TO_PROCESS - the root supplied to the operation is <code>null</code>.
  *   <li>INVALID_NAME - the name provided to the operation is <code>null</code> or is not a valid
  *       script folder name.
  *   <li>READ_ONLY - the root provided to this operation is read only.
  *   <li>NAME_COLLISION - there is a pre-existing resource (file) with the same name as a folder
  *       in the script folder's hierarchy.
  *   <li>ELEMENT_NOT_PRESENT - the underlying resource for the root is missing
  * </ul>
  *
  * @see IScriptModelStatus
  * @see ScriptConventions
  */
 @Override
 public IModelStatus verify() {
   if (getParentElement() == null) {
     return new ModelStatus(IModelStatusConstants.NO_ELEMENTS_TO_PROCESS);
   }
   IPath packageName = this.pkgName == null ? null : this.pkgName.append("."); // $NON-NLS-1$
   String packageNameValue = null;
   if (packageName != null) {
     packageNameValue = packageName.toOSString();
   }
   if (this.pkgName == null
       || (this.pkgName.segmentCount() > 0
           && !Util.isValidFolderNameForPackage(
               (IContainer) getParentElement().getResource(), packageName.toString()))) {
     return new ModelStatus(IModelStatusConstants.INVALID_NAME, packageNameValue);
   }
   IProjectFragment root = (IProjectFragment) getParentElement();
   if (root.isReadOnly()) {
     return new ModelStatus(IModelStatusConstants.READ_ONLY, root);
   }
   IContainer parentFolder = (IContainer) root.getResource();
   int i;
   for (i = 0; i < this.pkgName.segmentCount(); i++) {
     IResource subFolder = parentFolder.findMember(this.pkgName.segment(i));
     if (subFolder != null) {
       if (subFolder.getType() != IResource.FOLDER) {
         return new ModelStatus(
             IModelStatusConstants.NAME_COLLISION,
             Messages.bind(Messages.status_nameCollision, subFolder.getFullPath().toString()));
       }
       parentFolder = (IContainer) subFolder;
     }
   }
   return ModelStatus.VERIFIED_OK;
 }
  /*
   * (non-Javadoc)
   *
   * @see IJavaSearchScope#encloses(IModelElement)
   */
  public boolean encloses(IModelElement element) {
    IDLTKLanguageToolkit elementToolkit = DLTKLanguageManager.getLanguageToolkit(element);
    if (!toolkit.getNatureId().equals(elementToolkit.getNatureId())) {
      return false;
    }

    if (this.elements != null) {
      for (int i = 0, length = this.elements.size(); i < length; i++) {
        IModelElement scopeElement = this.elements.get(i);
        IModelElement searchedElement = element;
        while (searchedElement != null) {
          if (searchedElement.equals(scopeElement)) return true;
          searchedElement = searchedElement.getParent();
        }
      }
      return false;
    }
    IProjectFragment root = (IProjectFragment) element.getAncestor(IModelElement.PROJECT_FRAGMENT);
    if (root != null && root.isArchive()) {
      // external or internal archive
      IPath rootPath = root.getPath();
      String rootPathToString = rootPath.toString();
      IPath relativePath = getPath(element, true /* relative path */);
      return indexOf(rootPathToString, relativePath.toString()) >= 0;
    }
    // resource in workspace
    String fullResourcePathString = getPath(element, false /* full path */).toString();
    return indexOf(fullResourcePathString) >= 0;
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.jdt.ui.StandardJavaElementContentProvider#getPackageFragmentRoots
   * (org.eclipse.jdt.core.IJavaProject)
   */
  protected Object[] getProjectFragments(final IScriptProject project) throws ModelException {
    if (!project.getProject().isOpen()) {
      return StandardModelElementContentProvider.NO_CHILDREN;
    }

    List<Object> result = new ArrayList<Object>();

    boolean addZIPContainer = false;

    IProjectFragment[] roots = project.getProjectFragments();
    for (int i = 0; i < roots.length; i++) {
      IProjectFragment root = roots[i];
      IBuildpathEntry classpathEntry = root.getRawBuildpathEntry();
      if (classpathEntry == null) {
        continue;
      }
      int entryKind = classpathEntry.getEntryKind();
      if (entryKind == IBuildpathEntry.BPE_CONTAINER) {
        // all ClassPathContainers are added later
      } else if (fShowLibrariesNode && (entryKind == IBuildpathEntry.BPE_LIBRARY /*
																 * || entryKind
																 * ==
																 * IBuildpathEntry
																 * .BPE_VARIABLE
																 */)) {
        addZIPContainer = true;
      } else {
        if (isProjectProjectFragment(root)) {
          // filter out package fragments that correspond to projects
          // and
          // replace them with the package fragments directly
          Object[] fragments = getProjectFragmentContent(root);
          for (int j = 0; j < fragments.length; j++) {
            result.add(fragments[j]);
          }
        } else {
          result.add(root);
        }
      }
    }

    if (addZIPContainer) {
      result.add(new LibraryContainer(project));
    }

    // separate loop to make sure all containers are on the classpath
    IBuildpathEntry[] rawBuidspath = project.getRawBuildpath();
    for (int i = 0; i < rawBuidspath.length; i++) {
      IBuildpathEntry classpathEntry = rawBuidspath[i];
      if (classpathEntry.getEntryKind() == IBuildpathEntry.BPE_CONTAINER) {
        result.add(new BuildPathContainer(project, classpathEntry));
      }
    }
    Object[] resources = project.getForeignResources();
    for (int i = 0; i < resources.length; i++) {
      result.add(resources[i]);
    }
    return result.toArray();
  }
 public List<ISourceModule> getExternalModules(int options) throws CoreException {
   validateFlags(options, DEFAULT);
   if (externalModules == null) {
     loadExternalPaths();
     final ExternalModuleCollector moduleCollector = new ExternalModuleCollector(monitor);
     for (IProjectFragment fragment : externalFragments) {
       if (!oldExternalFolders.contains(fragment.getPath())) {
         fragment.accept(moduleCollector);
       }
     }
     externalModules = unmodifiableList(moduleCollector.elements);
   }
   return externalModules;
 }
 private void loadExternalPaths() throws CoreException {
   if (externalPaths == null) {
     externalPaths = new ArrayList<IPath>();
     externalFragments = new ArrayList<IProjectFragment>();
     final IProjectFragment[] allFragments = getScriptProject().getAllProjectFragments();
     for (int i = 0; i < allFragments.length; i++) {
       final IProjectFragment fragment = allFragments[i];
       if (fragment.isExternal()) {
         final IPath path = fragment.getPath();
         externalPaths.add(path);
         externalFragments.add(fragment);
       }
     }
   }
 }
  public static boolean isLanguageModelElement(IModelElement element) {
    if (element != null) {
      final IProjectFragment fragment =
          (IProjectFragment) element.getAncestor(IModelElement.PROJECT_FRAGMENT);
      if (fragment != null && fragment.isExternal()) {
        final IPath path = fragment.getPath();

        // see getTargetLocation() below for description:
        if (path.segmentCount() > 2) {
          return "__language__".equals(path.segment(path.segmentCount() - 2));
        }
      }
    }
    return false;
  }
  private static boolean hasReadOnlyResourcesAndSubResources(IModelElement modelElement)
      throws CoreException {
    switch (modelElement.getElementType()) {
      case IModelElement.SOURCE_MODULE:
        IResource resource = ReorgUtils.getResource(modelElement);
        return (resource != null && Resources.isReadOnly(resource));
      case IModelElement.SCRIPT_FOLDER:
        IResource packResource = ReorgUtils.getResource(modelElement);
        if (packResource == null) return false;
        IScriptFolder pack = (IScriptFolder) modelElement;
        if (Resources.isReadOnly(packResource)) return true;
        Object[] nonScript = pack.getForeignResources();
        for (int i = 0; i < nonScript.length; i++) {
          Object object = nonScript[i];
          if (object instanceof IResource
              && hasReadOnlyResourcesAndSubResources((IResource) object)) return true;
        }
        return hasReadOnlyResourcesAndSubResources(pack.getChildren());
      case IModelElement.PROJECT_FRAGMENT:
        IProjectFragment root = (IProjectFragment) modelElement;
        if (root.isArchive()) return false;
        IResource pfrResource = ReorgUtils.getResource(modelElement);
        if (pfrResource == null) return false;
        if (Resources.isReadOnly(pfrResource)) return true;
        Object[] nonScript1 = root.getForeignResources();
        for (int i = 0; i < nonScript1.length; i++) {
          Object object = nonScript1[i];
          if (object instanceof IResource
              && hasReadOnlyResourcesAndSubResources((IResource) object)) return true;
        }
        return hasReadOnlyResourcesAndSubResources(root.getChildren());

      case IModelElement.FIELD:
        //			case IModelElement.IMPORT_CONTAINER:
        //			case IModelElement.IMPORT_DECLARATION:
        //			case IModelElement.INITIALIZER:
      case IModelElement.METHOD:
        //			case IModelElement.PACKAGE_DECLARATION:
      case IModelElement.TYPE:
        return false;
      default:
        Assert.isTrue(false); // not handled here
        return false;
    }
  }
  /** Adds all of the openables defined within this package fragment to the list. */
  private void injectAllOpenablesForPackageFragment(IScriptFolder packFrag, ArrayList openables) {

    try {
      IProjectFragment root = (IProjectFragment) packFrag.getParent();
      int kind = root.getKind();
      if (kind != 0) {
        boolean isSourcePackageFragment = (kind == IProjectFragment.K_SOURCE);
        if (isSourcePackageFragment) {
          ISourceModule[] cus = packFrag.getSourceModules();
          for (int i = 0; i < cus.length; i++) {
            openables.add(cus[i]);
          }
        }
      }
    } catch (ModelException e) {
      // ignore
    }
  }
Example #11
0
  protected IModelElement findInputForJavaElement(IModelElement je, boolean canChangeInputType) {
    if (je == null || !je.exists()) return null;

    if (isValidInput(je)) {

      // don't update if input must be project (i.e. project is used as
      // source folder)
      if (canChangeInputType)
        fLastInputWasProject = je.getElementType() == IModelElement.SCRIPT_PROJECT;
      return je;
    } else if (fLastInputWasProject) {
      IProjectFragment packageFragmentRoot =
          (IProjectFragment) je.getAncestor(IModelElement.PROJECT_FRAGMENT);
      if (!packageFragmentRoot.isExternal()) return je.getScriptProject();
    }

    return findInputForJavaElement(je.getParent(), canChangeInputType);
  }
 /**
  * Returns the hierarchical packages inside a given folder.
  *
  * @param folder The parent folder
  * @param result Collection where the resulting elements are added
  * @throws CoreException thrown when elements could not be accessed
  */
 private void getHierarchicalPackagesInFolder(
     final IFolder folder, final Collection<Object> result) throws CoreException {
   IResource[] resources = folder.members();
   for (int i = 0; i < resources.length; i++) {
     IResource resource = resources[i];
     if (resource instanceof IFolder) {
       IFolder curr = (IFolder) resource;
       IModelElement element = DLTKCore.create(curr);
       if (element instanceof IScriptFolder) {
         if (fFoldPackages) {
           IScriptFolder fragment = (IScriptFolder) element;
           IProjectFragment root = (IProjectFragment) fragment.getParent();
           element = ScriptExplorerContentProvider.getFolded(root.getChildren(), fragment);
         }
         result.add(element);
       }
     }
   }
 }
Example #13
0
    public void resourceChanged(IResourceChangeEvent event) {
      if (event.getBuildKind() == IncrementalProjectBuilder.CLEAN_BUILD) {
        Object source = event.getSource();
        try {
          if (source instanceof IProject) {
            IProject project = (IProject) source;
            ProjectIndexerManager.removeProject(project.getFullPath());
            ProjectIndexerManager.indexProject(project);

          } else if (source instanceof IWorkspace) {
            IWorkspace workspace = (IWorkspace) source;
            IProject[] projects = workspace.getRoot().getProjects();

            // remove from index:
            for (IProject project : projects) {
              if (!project.isAccessible()) {
                continue;
              }
              IScriptProject scriptProject = DLTKCore.create(project);
              if (scriptProject.isOpen()) {
                IProjectFragment[] projectFragments = scriptProject.getProjectFragments();
                for (IProjectFragment projectFragment : projectFragments) {
                  ProjectIndexerManager.removeProjectFragment(
                      scriptProject, projectFragment.getPath());
                }
                ProjectIndexerManager.removeProject(project.getFullPath());
              }
            }

            // add to index:
            for (IProject project : projects) {
              if (!project.isAccessible()) {
                continue;
              }
              ProjectIndexerManager.indexProject(project);
            }
          }
        } catch (CoreException e) {
          Logger.logException(e);
        }
      }
    }
 /** Adds all of the openables defined within this package fragment root to the list. */
 private void injectAllOpenablesForPackageFragmentRoot(
     IProjectFragment root, ArrayList openables) {
   try {
     IModelElement[] packFrags = root.getChildren();
     for (int i = 0; i < packFrags.length; i++) {
       IScriptFolder packFrag = (IScriptFolder) packFrags[i];
       injectAllOpenablesForPackageFragment(packFrag, openables);
     }
   } catch (ModelException e) {
     return;
   }
 }
 /**
  * Execute the operation - creates the new script folder and any side effect folders.
  *
  * @exception ModelException if the operation is unable to complete
  */
 @Override
 protected void executeOperation() throws ModelException {
   ModelElementDelta delta = null;
   IProjectFragment root = (IProjectFragment) getParentElement();
   beginTask(Messages.operation_createScriptFolderProgress, this.pkgName.segmentCount());
   IContainer parentFolder = (IContainer) root.getResource();
   IPath sideEffectPackageName = Path.EMPTY;
   ArrayList<IScriptFolder> results = new ArrayList<IScriptFolder>(this.pkgName.segmentCount());
   int i;
   for (i = 0; i < this.pkgName.segmentCount(); i++) {
     String subFolderName = this.pkgName.segment(i);
     sideEffectPackageName = sideEffectPackageName.append(subFolderName);
     IResource subFolder = parentFolder.findMember(subFolderName);
     if (subFolder == null) {
       createFolder(parentFolder, subFolderName, force);
       parentFolder = parentFolder.getFolder(new Path(subFolderName));
       IScriptFolder addedFrag = root.getScriptFolder(sideEffectPackageName);
       if (!Util.isExcluded(parentFolder, root)) {
         if (delta == null) {
           delta = newModelElementDelta();
         }
         delta.added(addedFrag);
       }
       results.add(addedFrag);
     } else {
       parentFolder = (IContainer) subFolder;
     }
     worked(1);
   }
   if (results.size() > 0) {
     this.resultElements = new IModelElement[results.size()];
     results.toArray(this.resultElements);
     if (delta != null) {
       addDelta(delta);
     }
   }
   done();
 }
  /**
   * Returns the hierarchical packages inside a given fragment or root.
   *
   * @param parent The parent package fragment root
   * @param fragment The package to get the children for or 'null' to get the children of the root.
   * @param result Collection where the resulting elements are added
   * @throws JavaModelException
   */
  private void getHierarchicalPackageChildren(
      final IProjectFragment parent, final IScriptFolder fragment, final Collection<Object> result)
      throws ModelException {
    IModelElement[] children = parent.getChildren();

    if (fragment == null || fragment.isRootFolder()) {
      List<IModelElement> newElements = new ArrayList<IModelElement>();
      for (int i = 0; i < children.length; ++i) {
        if (children[i] instanceof IScriptFolder) {
          IScriptFolder scriptFolder = (IScriptFolder) children[i];
          if (scriptFolder.isRootFolder()) {
            IModelElement[] members = scriptFolder.getChildren();
            for (int j = 0; j < members.length; ++j) {
              newElements.add(members[j]);
            }
            continue;
          }
        }
        newElements.add(children[i]);
      }
      children = newElements.toArray(new IModelElement[newElements.size()]);
    }

    String prefix =
        fragment != null
            ? fragment.getElementName() + IScriptFolder.PACKAGE_DELIMETER_STR
            : ""; //$NON-NLS-1$
    int prefixLen = prefix.length();
    for (int i = 0; i < children.length; i++) {
      if (children[i] instanceof IScriptFolder) {
        IScriptFolder curr = (IScriptFolder) children[i];
        String name = curr.getElementName();
        if (name.startsWith(prefix)
            && name.length() > prefixLen
            && name.indexOf(IScriptFolder.PACKAGE_DELIMITER, prefixLen) == -1) {
          if (fFoldPackages) {
            curr = ScriptExplorerContentProvider.getFolded(children, curr);
          }
          result.add(curr);
        } /*
           * else if (fragment == null && curr.isRootFolder()) {
           * result.add(curr); }
           */
      } else {
        result.add(children[i]);
      }
    }
  }
  /*
   * (non-Javadoc)
   *
   * @seeorg.eclipse.jdt.ui.StandardJavaElementContentProvider#
   * getPackageFragmentRootContent(org.eclipse.jdt.core.IPackageFragmentRoot)
   */
  protected Object[] getProjectFragmentContent(final IProjectFragment root) throws ModelException {
    if (fIsFlatLayout) {
      return super.getProjectFragmentContent(root);
    }

    // hierarchical package mode
    ArrayList<Object> result = new ArrayList<Object>();
    getHierarchicalPackageChildren(root, null, result);
    if (!isProjectProjectFragment(root)) {
      Object[] nonJavaResources = root.getForeignResources();
      for (int i = 0; i < nonJavaResources.length; i++) {
        result.add(nonJavaResources[i]);
      }
    }
    return result.toArray();
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.dltk.internal.ui.navigator.ScriptExplorerLabelProvider#getText
   * (java.lang.Object)
   *
   * Override the default text - do not display a full path for a folder
   */
  @Override
  public String getText(Object element) {

    if (element instanceof ExternalProjectFragment) {
      ExternalProjectFragment fragment = (ExternalProjectFragment) element;
      String name =
          LanguageModelInitializer.getPathName(
              EnvironmentPathUtils.getLocalPath(fragment.getPath()));
      if (name != null) {
        return name;
      }
      return fragment.toStringWithAncestors();
    }
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=295256
    if (element instanceof IProjectFragment) {
      IProjectFragment fragment = (IProjectFragment) element;
      return fragment.getElementName();
    }
    // end
    if (element instanceof IncludePath) {
      Object entry = ((IncludePath) element).getEntry();

      // An included PHP project
      if (entry instanceof IBuildpathEntry) {
        IBuildpathEntry iBuildpathEntry = (IBuildpathEntry) entry;
        if (iBuildpathEntry.getEntryKind() == IBuildpathEntry.BPE_PROJECT) {
          return iBuildpathEntry.getPath().lastSegment();
        }
        if (iBuildpathEntry.getEntryKind() == IBuildpathEntry.BPE_CONTAINER) {
          return getEntryDescription(element, iBuildpathEntry);
        } else {
          String result =
              LabelProviderUtil.getVariableName(
                  iBuildpathEntry.getPath(), iBuildpathEntry.getEntryKind());
          if (result == null) {
            IPath localPath = EnvironmentPathUtils.getLocalPath(iBuildpathEntry.getPath());
            return localPath.toOSString();
          }
          return result;
        }
      }
      if (entry instanceof ExternalProjectFragment) {
        return ((ExternalProjectFragment) entry).toStringWithAncestors();
      }

      if (entry instanceof IResource) {
        return (((IResource) entry).getFullPath().toString()).substring(1);
      }

      return null;
    }

    if (element != null) {
      for (ILabelProvider provider :
          TreeContentProviderRegistry.getInstance().getLabelProviders()) {
        String label = provider.getText(element);

        if (label != null) {
          return label;
        }
      }
    }

    return super.getText(element);
  }
Example #19
0
  /**
   * Add an element to the script search scope.
   *
   * @param element The element we want to add to current script search scope
   * @throws ModelException May happen if some Script Model info are not available
   */
  public void add(IModelElement element) throws ModelException {
    if (!natureFilter(element)) {
      return;
    }
    IPath containerPath = null;
    String containerPathToString = null;
    int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES;
    switch (element.getElementType()) {
      case IModelElement.SCRIPT_MODEL:
        // a workspace scope should be used
        break;
      case IModelElement.SCRIPT_PROJECT:
        add((ScriptProject) element, null, includeMask, new HashSet<IProject>(2), null);
        break;
      case IModelElement.PROJECT_FRAGMENT:
        IProjectFragment root = (IProjectFragment) element;
        String projectPath = null;
        if (!root.isExternal()) {
          IPath rootPath = root.getPath();
          containerPath =
              root.getKind() == IProjectFragment.K_SOURCE ? root.getParent().getPath() : rootPath;
          containerPathToString = containerPath.toString();
          IResource rootResource = root.getResource();
          projectPath = root.getScriptProject().getPath().toString();
          if (rootResource != null && rootResource.isAccessible()) {
            String relativePath =
                Util.relativePath(rootResource.getFullPath(), containerPath.segmentCount());
            add(projectPath, relativePath, containerPathToString, false /* not a package */, null);
          } else {
            add(
                projectPath,
                org.eclipse.dltk.compiler.util.Util.EMPTY_STRING,
                containerPathToString,
                false /* not a package */,
                null);
          }
        } else {
          projectPath = root.getScriptProject().getPath().toString();
          containerPath = root.getPath();
          containerPathToString = containerPath.toString();
          add(
              projectPath,
              org.eclipse.dltk.compiler.util.Util.EMPTY_STRING,
              containerPathToString,
              false /* not a package */,
              null);
        }
        break;
      case IModelElement.SCRIPT_FOLDER:
        root = (IProjectFragment) element.getParent();
        projectPath = root.getScriptProject().getPath().toString();
        if (root.isArchive()) {
          if (DLTKCore.DEBUG) {
            System.err.println("TODO: Check. Bug possible..."); // $NON-NLS-1$
          }
          String relativePath = ((ModelElement) element).getPath().toString() + '/';
          containerPath = root.getPath();
          containerPathToString = containerPath.toString();
          add(projectPath, relativePath, containerPathToString, true /* package */, null);
        } else {
          IResource resource = element.getResource();
          if (resource != null) {
            if (resource.isAccessible()) {
              containerPath =
                  root.getKind() == IProjectFragment.K_SOURCE
                      ? root.getParent().getPath()
                      : root.getPath();
            } else {
              // for working copies, get resource container full path
              containerPath = resource.getParent().getFullPath();
            }
            containerPathToString = containerPath.toString();
            String relativePath =
                Util.relativePath(resource.getFullPath(), containerPath.segmentCount());
            add(projectPath, relativePath, containerPathToString, true /* package */, null);
          }
        }
        break;
      default:
        // remember sub-cu (or sub-class file) script elements
        if (element instanceof IMember) {
          if (this.elements == null) {
            this.elements = new ArrayList<IModelElement>();
          }
          this.elements.add(element);
        }
        root = (IProjectFragment) element.getAncestor(IModelElement.PROJECT_FRAGMENT);
        projectPath = root.getScriptProject().getPath().toString();
        String relativePath;
        if (root.getKind() == IProjectFragment.K_SOURCE && !root.isExternal()) {
          containerPath = root.getParent().getPath();
          relativePath = Util.relativePath(getPath(element, false /* full path */), 1 /*
																				 * remove
																				 * project
																				 * segmet
																				 */);
        } else {
          containerPath = root.getPath();
          relativePath = getPath(element, true /* relative path */).toString();
        }
        containerPathToString = containerPath.toString();
        add(projectPath, relativePath, containerPathToString, false /*
																		 * not a
																		 * package
																		 */, null);
    }

    if (containerPath != null) addEnclosingProjectOrArchive(containerPath);
  }
Example #20
0
 /**
  * Returns the package fragment root corresponding to a given resource path.
  *
  * @param resourcePathString path of expected package fragment root.
  * @return the {@link IProjectFragment package fragment root} which path match the given one or
  *     <code>null</code> if none was found.
  */
 public IProjectFragment projectFragment(String resourcePathString) {
   int index = -1;
   int separatorIndex = resourcePathString.indexOf(FILE_ENTRY_SEPARATOR);
   boolean isZIPFile = separatorIndex != -1;
   boolean isSpecial = resourcePathString.startsWith(IBuildpathEntry.BUILDPATH_SPECIAL);
   if (isZIPFile) {
     // internal or external jar (case 3, 4, or 5)
     String zipPath = resourcePathString.substring(0, separatorIndex);
     String relativePath = resourcePathString.substring(separatorIndex + 1);
     index = indexOf(zipPath, relativePath);
   } else {
     // resource in workspace (case 1 or 2)
     index = indexOf(resourcePathString);
   }
   if (index >= 0) {
     int idx = projectIndexes[index];
     String projectPath = idx == -1 ? null : (String) this.projectPaths.get(idx);
     if (projectPath != null) {
       IScriptProject project =
           DLTKCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(projectPath));
       if (isZIPFile) {
         return project.getProjectFragment(this.containerPaths[index]);
       }
       if (isSpecial) {
         return project.getProjectFragment(this.containerPaths[index]);
       }
       Object target =
           Model.getTarget(
               ResourcesPlugin.getWorkspace().getRoot(),
               Path.fromPortableString(
                   this.containerPaths[index] + '/' + this.relativePaths[index]),
               false);
       if (target instanceof IProject) {
         return project.getProjectFragment((IProject) target);
       }
       if (target instanceof IResource) {
         IModelElement element = DLTKCore.create((IResource) target);
         return (IProjectFragment) element.getAncestor(IModelElement.PROJECT_FRAGMENT);
       }
       if (target instanceof IFileHandle) {
         try {
           IProjectFragment[] fragments = project.getProjectFragments();
           IFileHandle t = (IFileHandle) target;
           IPath absPath = t.getFullPath();
           for (int i = 0; i < fragments.length; ++i) {
             IProjectFragment f = fragments[i];
             if (f.isExternal()) {
               IPath pPath = f.getPath();
               if (pPath.isPrefixOf(absPath) && !Util.isExcluded(absPath, f, t.isDirectory())) {
                 return f;
               }
             }
           }
         } catch (ModelException e) {
           e.printStackTrace();
           return null;
         }
       }
     }
   }
   return null;
 }