当前位置: 代码迷 >> Eclipse >> org.eclipse.core.resources.builders扩张点-转载
  详细解决方案

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

热度:660   发布时间:2016-04-23 01:08:11.0
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;     }  } 
  相关解决方案