rcp(插件开发)org.eclipse.core.resources.builders扩展点-转载二进制兄弟的文章

不知这位仁兄现在在从事什么领域?

org.eclipse.core.resources.builders用于提供一种操作,这种操作可以在IResource改变的时候自动去build,如同改变java文件,会自动进行build,显示错误一样,我们扩展这个builder,并且在自己的项目中使用。我们要做的就是实现build的过程,至于时机由eclipse控制

  1. <extension 
  2.          id="builder的ID" 
  3.          name="builder的NAME" 
  4.          point="org.eclipse.core.resources.builders"> 
  5.       <builder 
  6.             hasNature="true"> 
  7.          <run 
  8.                class="builder的CLASS"> 
  9.          </run> 
  10.       </builder> 
  11.    </extension> 
  12.    <extension 
  13.          id="nature的ID" 
  14.          name="nature的NAME" 
  15.          point="org.eclipse.core.resources.natures"> 
  16.       <runtime> 
  17.          <run 
  18.                class="nature的CLASS"> 
  19.          </run> 
  20.       </runtime> 
  21.       <builder 
  22.             id="builder所在的插件ID+.+builder的ID"> 
  23.       </builder> 
  24.    </extension> 

Builder的例子代码:

  1. package project_builder.builder; 
  2.  
  3. import java.util.Map; 
  4.  
  5. import javax.xml.parsers.ParserConfigurationException; 
  6. import javax.xml.parsers.SAXParser; 
  7. import javax.xml.parsers.SAXParserFactory; 
  8.  
  9. import org.eclipse.core.resources.IFile; 
  10. import org.eclipse.core.resources.IMarker; 
  11. import org.eclipse.core.resources.IProject; 
  12. import org.eclipse.core.resources.IResource; 
  13. import org.eclipse.core.resources.IResourceDelta; 
  14. import org.eclipse.core.resources.IResourceDeltaVisitor; 
  15. import org.eclipse.core.resources.IResourceVisitor; 
  16. import org.eclipse.core.resources.IncrementalProjectBuilder; 
  17. import org.eclipse.core.runtime.CoreException; 
  18. import org.eclipse.core.runtime.IProgressMonitor; 
  19. import org.xml.sax.SAXException; 
  20. import org.xml.sax.SAXParseException; 
  21. import org.xml.sax.helpers.DefaultHandler; 
  22.  
  23. public class SampleBuilder extends IncrementalProjectBuilder { 
  24.  
  25.     class SampleDeltaVisitor implements IResourceDeltaVisitor { 
  26.         /*
  27.          * (non-Javadoc)
  28.          *
  29.          * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
  30.          */ 
  31.         public boolean visit(IResourceDelta delta) throws CoreException { 
  32.             IResource resource = delta.getResource(); 
  33.             switch (delta.getKind()) { 
  34.             case IResourceDelta.ADDED: 
  35.                 // handle added resource 
  36.                 checkXML(resource); 
  37.                 break
  38.             case IResourceDelta.REMOVED: 
  39.                 // handle removed resource 
  40.                 break
  41.             case IResourceDelta.CHANGED: 
  42.                 // handle changed resource 
  43.                 checkXML(resource); 
  44.                 break
  45.             } 
  46.             //return true to continue visiting children. 
  47.             return true
  48.         } 
  49.     } 
  50.  
  51.     class SampleResourceVisitor implements IResourceVisitor { 
  52.         public boolean visit(IResource resource) { 
  53.             checkXML(resource); 
  54.             //return true to continue visiting children. 
  55.             return true
  56.         } 
  57.     } 
  58.  
  59.     class XMLErrorHandler extends DefaultHandler { 
  60.          
  61.         private IFile file; 
  62.  
  63.         public XMLErrorHandler(IFile file) { 
  64.             this.file = file; 
  65.         } 
  66.  
  67.         private void addMarker(SAXParseException e, int severity) { 
  68.             SampleBuilder.this.addMarker(file, e.getMessage(), e 
  69.                     .getLineNumber(), severity); 
  70.         } 
  71.  
  72.         public void error(SAXParseException exception) throws SAXException { 
  73.             addMarker(exception, IMarker.SEVERITY_ERROR); 
  74.         } 
  75.  
  76.         public void fatalError(SAXParseException exception) throws SAXException { 
  77.             addMarker(exception, IMarker.SEVERITY_ERROR); 
  78.         } 
  79.  
  80.         public void warning(SAXParseException exception) throws SAXException { 
  81.             addMarker(exception, IMarker.SEVERITY_WARNING); 
  82.         } 
  83.     } 
  84.  
  85.     public static final String BUILDER_ID = "Project_Builder.sampleBuilder"
  86.  
  87.     private static final String MARKER_TYPE = "Project_Builder.xmlProblem"
  88.  
  89.     private SAXParserFactory parserFactory; 
  90.  
  91.     private void addMarker(IFile file, String message, int lineNumber, 
  92.             int severity) { 
  93.         try
  94.             IMarker marker = file.createMarker(MARKER_TYPE); 
  95.             marker.setAttribute(IMarker.MESSAGE, message); 
  96.             marker.setAttribute(IMarker.SEVERITY, severity); 
  97.             if (lineNumber == -1) { 
  98.                 lineNumber = 1
  99.             } 
  100.             marker.setAttribute(IMarker.LINE_NUMBER, lineNumber); 
  101.         } catch (CoreException e) { 
  102.         } 
  103.     } 
  104.  
  105.     /*
  106.      * (non-Javadoc)
  107.      *
  108.      * @see org.eclipse.core.internal.events.InternalBuilder#build(int,
  109.      *      java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
  110.      */ 
  111.     protected IProject[] build(int kind, Map args, IProgressMonitor monitor) 
  112.             throws CoreException { 
  113.         if (kind == FULL_BUILD) { 
  114.             fullBuild(monitor); 
  115.         } else
  116.             IResourceDelta delta = getDelta(getProject()); 
  117.             if (delta == null) { 
  118.                 fullBuild(monitor); 
  119.             } else
  120.                 incrementalBuild(delta, monitor); 
  121.             } 
  122.         } 
  123.         return null
  124.     } 
  125.  
  126.     void checkXML(IResource resource) { 
  127.         if (resource instanceof IFile && resource.getName().endsWith(".xml")) { 
  128.             IFile file = (IFile) resource; 
  129.             deleteMarkers(file); 
  130.             XMLErrorHandler reporter = new XMLErrorHandler(file); 
  131.             try
  132.                 getParser().parse(file.getContents(), reporter); 
  133.             } catch (Exception e1) { 
  134.             } 
  135.         } 
  136.     } 
  137.  
  138.     private void deleteMarkers(IFile file) { 
  139.         try
  140.             file.deleteMarkers(MARKER_TYPE, false, IResource.DEPTH_ZERO); 
  141.         } catch (CoreException ce) { 
  142.         } 
  143.     } 
  144.  
  145.     protected void fullBuild(final IProgressMonitor monitor) 
  146.             throws CoreException { 
  147.         try
  148.             getProject().accept(new SampleResourceVisitor()); 
  149.         } catch (CoreException e) { 
  150.         } 
  151.     } 
  152.  
  153.     private SAXParser getParser() throws ParserConfigurationException, 
  154.             SAXException { 
  155.         if (parserFactory == null) { 
  156.             parserFactory = SAXParserFactory.newInstance(); 
  157.         } 
  158.         return parserFactory.newSAXParser(); 
  159.     } 
  160.  
  161.     protected void incrementalBuild(IResourceDelta delta, 
  162.             IProgressMonitor monitor) throws CoreException { 
  163.         // the visitor does the work. 
  164.         delta.accept(new SampleDeltaVisitor()); 
  165.     } 

Nature的例子代码:

  1. package project_builder.builder; 
  2.  
  3. import org.eclipse.core.resources.ICommand; 
  4. import org.eclipse.core.resources.IProject; 
  5. import org.eclipse.core.resources.IProjectDescription; 
  6. import org.eclipse.core.resources.IProjectNature; 
  7. import org.eclipse.core.runtime.CoreException; 
  8.  
  9. public class SampleNature implements IProjectNature { 
  10.  
  11.     /**
  12.      * ID of this project nature
  13.      */ 
  14.     public static final String NATURE_ID = "Project_Builder.sampleNature"
  15.  
  16.     private IProject project; 
  17.  
  18.     /*
  19.      * (non-Javadoc)
  20.      *
  21.      * @see org.eclipse.core.resources.IProjectNature#configure()
  22.      */ 
  23.     public void configure() throws CoreException { 
  24.         IProjectDescription desc = project.getDescription(); 
  25.         ICommand[] commands = desc.getBuildSpec(); 
  26.  
  27.         for (int i = 0; i < commands.length; ++i) { 
  28.             if (commands[i].getBuilderName().equals(SampleBuilder.BUILDER_ID)) { 
  29.                 return
  30.             } 
  31.         } 
  32.  
  33.         ICommand[] newCommands = new ICommand[commands.length + 1]; 
  34.         System.arraycopy(commands, 0, newCommands, 0, commands.length); 
  35.         ICommand command = desc.newCommand(); 
  36.         command.setBuilderName(SampleBuilder.BUILDER_ID); 
  37.         newCommands[newCommands.length - 1] = command; 
  38.         desc.setBuildSpec(newCommands); 
  39.         project.setDescription(desc, null); 
  40.     } 
  41.  
  42.     /*
  43.      * (non-Javadoc)
  44.      *
  45.      * @see org.eclipse.core.resources.IProjectNature#deconfigure()
  46.      */ 
  47.     public void deconfigure() throws CoreException { 
  48.         IProjectDescription description = getProject().getDescription(); 
  49.         ICommand[] commands = description.getBuildSpec(); 
  50.         for (int i = 0; i < commands.length; ++i) { 
  51.             if (commands[i].getBuilderName().equals(SampleBuilder.BUILDER_ID)) { 
  52.                 ICommand[] newCommands = new ICommand[commands.length - 1]; 
  53.                 System.arraycopy(commands, 0, newCommands, 0, i); 
  54.                 System.arraycopy(commands, i + 1, newCommands, i, 
  55.                         commands.length - i - 1); 
  56.                 description.setBuildSpec(newCommands); 
  57.                 return
  58.             } 
  59.         } 
  60.     } 
  61.  
  62.     /*
  63.      * (non-Javadoc)
  64.      *
  65.      * @see org.eclipse.core.resources.IProjectNature#getProject()
  66.      */ 
  67.     public IProject getProject() { 
  68.         return project; 
  69.     } 
  70.  
  71.     /*
  72.      * (non-Javadoc)
  73.      *
  74.      * @see org.eclipse.core.resources.IProjectNature#setProject(org.eclipse.core.resources.IProject)
  75.      */ 
  76.     public void setProject(IProject project) { 
  77.         this.project = project; 
  78.     } 
  79.  

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值