代码之家  ›  专栏  ›  技术社区  ›  Adisesha

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

  •  4
  • Adisesha  · 技术社区  · 15 年前

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

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

    我正在研究的API将iproject和content-type-id作为参数,并返回ipath、ifile或iresource对象。例如,在项目中获取所有Java文件。

    事先谢谢。

    3 回复  |  直到 12 年前
        1
  •  2
  •   nitind    15 年前

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

        2
  •  3
  •   Kyriakos    12 年前

    这就是我在当前项目中查找所有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;
    }
    
        3
  •  0
  •   VonC    15 年前

    这个 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();
    }