`

org.eclipse.core.resources.builders扩展点-转载

阅读更多
    org.eclipse.core.resources.builders用于提供一种操作,这种操作可以在IResource改变的时候自动去build,如同改变java文件,会自动进行build,显示错误一样,我们扩展这个builder,并且在自己的项目中使用。我们要做的就是实现build的过程,至于时机由eclipse控制
 
   
   <extension 
         id="builder的ID" 
         name="builder的NAME" 
         point="org.eclipse.core.resources.builders"> 
      <builder 
            hasNature="true"> 
         <run 
               class="builder的CLASS"> 
         </run> 
      </builder> 
   </extension> 
   <extension 
         id="nature的ID" 
         name="nature的NAME" 
         point="org.eclipse.core.resources.natures"> 
      <runtime> 
         <run 
               class="nature的CLASS"> 
         </run> 
      </runtime> 
      <builder 
            id="builder所在的插件ID+.+builder的ID"> 
      </builder> 
   </extension> 


<extension  
         id="builder的ID"  
         name="builder的NAME"  
         point="org.eclipse.core.resources.builders">  
      <builder  
            hasNature="true">  
         <run  
               class="builder的CLASS">  
         </run>  
      </builder>  
   </extension>  
   <extension  
         id="nature的ID"  
         name="nature的NAME"  
         point="org.eclipse.core.resources.natures">  
      <runtime>  
         <run  
               class="nature的CLASS">  
         </run>  
      </runtime>  
      <builder  
            id="builder所在的插件ID+.+builder的ID">  
      </builder>  
   </extension>  


Builder的例子代码:
package project_builder.builder;  
  
import java.util.Map;  
  
import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.parsers.SAXParser;  
import javax.xml.parsers.SAXParserFactory;  
  
import org.eclipse.core.resources.IFile;  
import org.eclipse.core.resources.IMarker;  
import org.eclipse.core.resources.IProject;  
import org.eclipse.core.resources.IResource;  
import org.eclipse.core.resources.IResourceDelta;  
import org.eclipse.core.resources.IResourceDeltaVisitor;  
import org.eclipse.core.resources.IResourceVisitor;  
import org.eclipse.core.resources.IncrementalProjectBuilder;  
import org.eclipse.core.runtime.CoreException;  
import org.eclipse.core.runtime.IProgressMonitor;  
import org.xml.sax.SAXException;  
import org.xml.sax.SAXParseException;  
import org.xml.sax.helpers.DefaultHandler;  
  
public class SampleBuilder extends IncrementalProjectBuilder {  
  
    class SampleDeltaVisitor implements IResourceDeltaVisitor {  
        /* 
         * (non-Javadoc) 
         *  
         * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta) 
         */  
        public boolean visit(IResourceDelta delta) throws CoreException {  
            IResource resource = delta.getResource();  
            switch (delta.getKind()) {  
            case IResourceDelta.ADDED:  
                // handle added resource  
                checkXML(resource);  
                break;  
            case IResourceDelta.REMOVED:  
                // handle removed resource  
                break;  
            case IResourceDelta.CHANGED:  
                // handle changed resource  
                checkXML(resource);  
                break;  
            }  
            //return true to continue visiting children.  
            return true;  
        }  
    }  
  
    class SampleResourceVisitor implements IResourceVisitor {  
        public boolean visit(IResource resource) {  
            checkXML(resource);  
            //return true to continue visiting children.  
            return true;  
        }  
    }  
  
    class XMLErrorHandler extends DefaultHandler {  
          
        private IFile file;  
  
        public XMLErrorHandler(IFile file) {  
            this.file = file;  
        }  
  
        private void addMarker(SAXParseException e, int severity) {  
            SampleBuilder.this.addMarker(file, e.getMessage(), e  
                    .getLineNumber(), severity);  
        }  
  
        public void error(SAXParseException exception) throws SAXException {  
            addMarker(exception, IMarker.SEVERITY_ERROR);  
        }  
  
        public void fatalError(SAXParseException exception) throws SAXException {  
            addMarker(exception, IMarker.SEVERITY_ERROR);  
        }  
  
        public void warning(SAXParseException exception) throws SAXException {  
            addMarker(exception, IMarker.SEVERITY_WARNING);  
        }  
    }  
  
    public static final String BUILDER_ID = "Project_Builder.sampleBuilder";  
  
    private static final String MARKER_TYPE = "Project_Builder.xmlProblem";  
  
    private SAXParserFactory parserFactory;  
  
    private void addMarker(IFile file, String message, int lineNumber,  
            int severity) {  
        try {  
            IMarker marker = file.createMarker(MARKER_TYPE);  
            marker.setAttribute(IMarker.MESSAGE, message);  
            marker.setAttribute(IMarker.SEVERITY, severity);  
            if (lineNumber == -1) {  
                lineNumber = 1;  
            }  
            marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);  
        } catch (CoreException e) {  
        }  
    }  
  
    /* 
     * (non-Javadoc) 
     *  
     * @see org.eclipse.core.internal.events.InternalBuilder#build(int, 
     *      java.util.Map, org.eclipse.core.runtime.IProgressMonitor) 
     */  
    protected IProject[] build(int kind, Map args, IProgressMonitor monitor)  
            throws CoreException {  
        if (kind == FULL_BUILD) {  
            fullBuild(monitor);  
        } else {  
            IResourceDelta delta = getDelta(getProject());  
            if (delta == null) {  
                fullBuild(monitor);  
            } else {  
                incrementalBuild(delta, monitor);  
            }  
        }  
        return null;  
    }  
  
    void checkXML(IResource resource) {  
        if (resource instanceof IFile && resource.getName().endsWith(".xml")) {  
            IFile file = (IFile) resource;  
            deleteMarkers(file);  
            XMLErrorHandler reporter = new XMLErrorHandler(file);  
            try {  
                getParser().parse(file.getContents(), reporter);  
            } catch (Exception e1) {  
            }  
        }  
    }  
  
    private void deleteMarkers(IFile file) {  
        try {  
            file.deleteMarkers(MARKER_TYPE, false, IResource.DEPTH_ZERO);  
        } catch (CoreException ce) {  
        }  
    }  
  
    protected void fullBuild(final IProgressMonitor monitor)  
            throws CoreException {  
        try {  
            getProject().accept(new SampleResourceVisitor());  
        } catch (CoreException e) {  
        }  
    }  
  
    private SAXParser getParser() throws ParserConfigurationException,  
            SAXException {  
        if (parserFactory == null) {  
            parserFactory = SAXParserFactory.newInstance();  
        }  
        return parserFactory.newSAXParser();  
    }  
  
    protected void incrementalBuild(IResourceDelta delta,  
            IProgressMonitor monitor) throws CoreException {  
        // the visitor does the work.  
        delta.accept(new SampleDeltaVisitor());  
    }  
}  


Nature的例子代码:
package project_builder.builder; 
 
import org.eclipse.core.resources.ICommand; 
import org.eclipse.core.resources.IProject; 
import org.eclipse.core.resources.IProjectDescription; 
import org.eclipse.core.resources.IProjectNature; 
import org.eclipse.core.runtime.CoreException; 
 
public class SampleNature implements IProjectNature { 
 
    /**
     * ID of this project nature
     */ 
    public static final String NATURE_ID = "Project_Builder.sampleNature"; 
 
    private IProject project; 
 
    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.core.resources.IProjectNature#configure()
     */ 
    public void configure() throws CoreException { 
        IProjectDescription desc = project.getDescription(); 
        ICommand[] commands = desc.getBuildSpec(); 
 
        for (int i = 0; i < commands.length; ++i) { 
            if (commands[i].getBuilderName().equals(SampleBuilder.BUILDER_ID)) { 
                return; 
            } 
        } 
 
        ICommand[] newCommands = new ICommand[commands.length + 1]; 
        System.arraycopy(commands, 0, newCommands, 0, commands.length); 
        ICommand command = desc.newCommand(); 
        command.setBuilderName(SampleBuilder.BUILDER_ID); 
        newCommands[newCommands.length - 1] = command; 
        desc.setBuildSpec(newCommands); 
        project.setDescription(desc, null); 
    } 
 
    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.core.resources.IProjectNature#deconfigure()
     */ 
    public void deconfigure() throws CoreException { 
        IProjectDescription description = getProject().getDescription(); 
        ICommand[] commands = description.getBuildSpec(); 
        for (int i = 0; i < commands.length; ++i) { 
            if (commands[i].getBuilderName().equals(SampleBuilder.BUILDER_ID)) { 
                ICommand[] newCommands = new ICommand[commands.length - 1]; 
                System.arraycopy(commands, 0, newCommands, 0, i); 
                System.arraycopy(commands, i + 1, newCommands, i, 
                        commands.length - i - 1); 
                description.setBuildSpec(newCommands); 
                return; 
            } 
        } 
    } 
 
    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.core.resources.IProjectNature#getProject()
     */ 
    public IProject getProject() { 
        return project; 
    } 
 
    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.core.resources.IProjectNature#setProject(org.eclipse.core.resources.IProject)
     */ 
    public void setProject(IProject project) { 
        this.project = project; 
    } 
 
} 
分享到:
评论

相关推荐

    Eclipse平台扩展点清单

    org.eclipse.core.resources.builders** 此扩展点用于定义构建器(Builder),它们负责对项目进行编译或其他处理工作。 **2. org.eclipse.core.resources.fileModificationValidator** 此扩展点用于定义文件修改...

    Eclipse平台SDK支持的扩展点

    8. org.eclipse.core.resources.builders:注册增量构建器,它们会在资源改变时自动构建项目,例如Java编译器。 9. org.eclipse.core.resources.fileModificationValidator:允许插件在文件保存或删除前进行验证,...

    junit的jar包

    org.junit.internal.builders.AllDefaultPossibilitiesBuilder.class org.junit.internal.builders.AnnotatedBuilder.class org.junit.internal.builders.IgnoredBuilder.class org.junit.internal.builders....

    PyPI 官网下载 | emmet-builders-0.8.0.tar.gz

    《PyPI官网下载:emmet-builders-0.8.0.tar.gz——探索Python库的构建与使用》 PyPI(Python Package Index)是Python开发者的重要资源库,它提供了丰富的Python库供全球开发者下载和使用。本文将深入探讨标题中的...

    Python库 | cortex-python-builders-1.2.1.tar.gz

    标题 "Python库 | cortex-python-builders-1.2.1.tar.gz" 提供的信息表明,这是一个与Python相关的库,具体来说是"Cortex Python Builders"的版本1.2.1,它被打包成了一个tar.gz文件。这个库可能是用于构建、打包...

    Angular-angular-builders.zip

    Angular-angular-builders.zip,angular cli@angular builders的一组自定义生成器,Angularjs于2016年发布,是Angularjs的重写版。它专注于良好的移动开发、模块化和改进的依赖注入。angular的设计目的是全面解决开发...

    kotlin-plugin-1.3.72-release-Studio3.6-1.zip

    其中,类型安全的构建器(Type-Safe Builders)进一步提升了代码的可读性和安全性,使得XML布局文件的编写更为简洁。智能转换(Smart Casts)功能在编译阶段就能检测出类型错误,减少了运行时异常的发生。此外,新...

    前端-后端java的Util类的工具类

    │ │ org.eclipse.jdt.core.prefs │ │ │ └─src │ │ logging.properties │ │ │ └─com │ └─mine │ │ BigMap.java │ │ LogPack.java │ │ │ └─logging │ ConsoleHandler.java │ Error...

    Hadoopeclipse-plugin编译方法[借鉴].pdf

    这些JAR文件可以从你的Eclipse安装目录的`plugins`子目录中找到,例如`org.eclipse.ui`, `org.eclipse.jdt`, `org.eclipse.core`, `org.eclipse.equinox`, `org.eclipse.debug`, `org.eclipse.osgi`, `org.eclipse....

    PyPI 官网下载 | aws_lambda_builders-0.8.0-py2-none-any.whl

    **PyPI 官网下载 | aws_lambda_builders-0.8.0-py2-none-any.whl** 在Python开发中,PyPI(Python Package Index)是官方的第三方Python软件包仓库,开发者可以在这里发布和分享自己的Python库。"aws_lambda_...

    Python库 | cortex_python_builders-1.2.0-py3-none-any.whl

    `cortex_python_builders-1.2.0-py3-none-any.whl` 是一个针对Python开发者的资源,它是一个预编译的Python库,用于简化和加速构建过程。这个库名为"Cortex Builders",版本为1.2.0,专为Python 3设计,支持所有平台...

    PyPI 官网下载 | cortex_python_builders-1.2.0-py3-none-any.whl

    资源来自pypi官网。 资源全名:cortex_python_builders-1.2.0-py3-none-any.whl

    PyPI 官网下载 | aws_lambda_builders-0.7.0-py3-none-any.whl

    资源来自pypi官网。 资源全名:aws_lambda_builders-0.7.0-py3-none-any.whl

    Python库 | aws_lambda_builders-0.7.0-py3-none-any.whl

    **Python库aws_lambda_builders-0.7.0-py3-none-any.whl详解** `aws_lambda_builders` 是一个Python库,专为构建和打包AWS Lambda函数而设计。Lambda是Amazon Web Services(AWS)提供的一个无服务器计算平台,允许...

    Python库 | aws_lambda_builders-1.11.0-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:aws_lambda_builders-1.11.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    JEECG3.4.3-MAVEN版本手把手入门-eclipse.docx

    JEECG3.4.3-MAVEN 版本手把手入门-eclipse 本文将详细介绍 JEECG3.4.3-MAVEN 版本的入门指南,使用 Eclipse 作为开发工具,通过 Maven 进行项目构建和管理。 一、Maven 环境搭建 Maven 是一个基于项目对象模型...

    JPF的Eclipse环境安装运行过程

    JPF 的 Eclipse 环境安装运行过程 JPF(Java Pathfinder)是一款基于 Java 的路径查找工具,用于软件测试和验证。为了在 Eclipse 环境中使用 JPF,需要安装和配置 JPF 的核心组件和 Eclipse 插件。下面将详细介绍 ...

    swt-3.8M5-win32-win32-x86

    5. **.project**:这是另一个Eclipse项目配置文件,包含了项目的属性设置,如构建配置、Natures(项目类型)、Builders(构建工具)等,帮助Eclipse识别和管理项目。 6. **src.zip**:这是一个包含SWT源代码的ZIP...

Global site tag (gtag.js) - Google Analytics