用于获取Eclipse项目中内容类型的所有资源的API

时间:2010-02-05 05:34:24

标签: java eclipse eclipse-plugin

是否有任何API可以在Eclipse项目中获取特定内容类型的所有文件?

一种选择是访问所有资源并收集内容类型的文件。

我正在寻找将IProject和内容类型id作为参数并返回IPath或IFile或IResource对象的API。例如,获取项目中的所有Java文件。

提前致谢。

3 个答案:

答案 0 :(得分:3)

这就是我用来查找当前项目中所有c文件的内容:

    public static ArrayList<IResource> getAllCFilesInProject(){
    ArrayList<IResource> allCFiles = new ArrayList<IResource>();
    IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = FileParaviserUtils.getCurrentProject();

    IPath path = project.getLocation();

    recursiveFindCFiles(allCFiles,path,myWorkspaceRoot);
    return allCFiles;
}

private static void recursiveFindCFiles(ArrayList<IResource> allCFiles,IPath path, IWorkspaceRoot myWorkspaceRoot){
    IContainer  container =  myWorkspaceRoot.getContainerForLocation(path);

    try {
        IResource[] iResources;
        iResources = container.members();
        for (IResource iR : iResources){
            // for c files
            if ("c".equalsIgnoreCase(iR.getFileExtension()))
                allCFiles.add(iR);
            if (iR.getType() == IResource.FOLDER){
                IPath tempPath = iR.getLocation();
                recursiveFindCFiles(allCFiles,tempPath,myWorkspaceRoot);
            }
        }
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static IProject getCurrentProject(){
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window != null)
    {
        IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
        Object firstElement = selection.getFirstElement();
        if (firstElement instanceof IAdaptable)
        {
            IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
            return project;
        }
    }
    return null;
}

答案 1 :(得分:2)

不,没有。你的想法通常是如何做到的。

答案 2 :(得分:0)

release notes of eclipse3.1当时(2005年6月)确实提到了内容类型匹配的启发式更改。
它与bug 90218相关,是bug 82986的一部分(3.1中匹配的增强),引用bug 86862(“需要相关自定义对象查找的API”)

该API没有成功,但code is available可供您重复使用。

public Object[] findRelatedObjects(IContentType type, String fileName, IRelatedRegistry registry) {
  List allRelated = new ArrayList();
  // first add any objects directly related to the content type
  Object[] related = registry.getRelatedObjects(type);
  for (int i = 0; i < related.length; i++) {
    allRelated.add(related[i]);
  }
  // backward compatibility requested - add any objects related to the file name
  if (fileName != null) {
    related = registry.getRelatedObjects(fileName);
    for (int i = 0; i < related.length; i++) {
      if (!allRelated.contains(related[i])) {
        // we don't want to return duplicates
        allRelated.add(related[i]);
      }
    }
  }
  // now add any indirectly related objects, walking up the content type hierarchy 
  while ((type = type.getBaseType()) != null) {
    related = registry.getRelatedObjects(type);
    for (int i = 0; i < related.length; i++) {
      if (!allRelated.contains(related[i])) {
        // we don't want to return duplicates          
        allRelated.add(related[i]);
      }
    }
  }
  return allRelated.toArray();
}