2010-02-05 46 views
4

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

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

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

在此先感谢。

回答

2

不,没有。你的想法一般是如何完成的。

+1

我意识到了。 IResourceVisitor和IResource.accept(IResourceVisitor)对我来说非常方便。 – Adi 2010-02-08 04:17:59

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(); 
} 
+0

感谢您的回答,但我的问题是有关特定内容类型的文件。也许我的问题不清楚,我用例子编辑了这个问题。 – Adi 2010-02-05 09:29:43

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; 
} 
相关问题