- 浏览: 29035 次
- 性别:
- 来自: 广州
-
文章分类
最新评论
org.eclipse.core.resources.builders用于提供一种操作,这种操作可以在IResource改变的时候自动去build,如同改变java文件,会自动进行build,显示错误一样,我们扩展这个builder,并且在自己的项目中使用。我们要做的就是实现build的过程,至于时机由eclipse控制
Builder的例子代码:
Nature的例子代码:
<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; } }
发表评论
-
GEF简介与事件机制
2014-08-07 09:56 3640一、GEF简介 GEF(Graphica ... -
GEF入门参考
2014-08-07 09:37 641一些网站 http://www.ibm.com/develo ... -
SWT-GEF事件转义相关类
2014-08-07 09:35 882在Draw2d中,Lightweight是gef图形系统的”s ... -
GEF中Editor创建的时序图
2014-08-07 09:33 637以eclipse网站上例子A Shape Diagram Ed ... -
org.eclipse.ui.decorators 使用(转)
2013-10-12 14:24 667org.eclipse.ui.decorators这个扩展点可 ... -
org.eclipse.ui.menus扩展点
2013-06-06 11:17 1908eclipse插件开发中 ... -
Eclipse开发中的 NoClassDefFoundError
2013-03-26 16:18 1240在eclipse插件开发过程中,需要引入很多的第三方j ... -
Eclipse事件与通讯
2013-01-18 15:58 989在开发Eclipse插件开发rcp过程中需要用到很多的 ... -
使用代码生成插件工程,脱离eclipse本身的新建工程向导(3)
2012-11-22 10:58 1196在第二步中需要有个PluginClassCodeGen ... -
使用代码生成插件工程,脱离eclipse本身的新建工程向导(2)
2012-11-22 10:52 3764在第一步完成后,我从pde的向导代码入手看eclips ... -
使用代码生成插件工程,脱离eclipse本身的新建工程向导(1)
2012-11-22 10:43 1797第一步,新建一个普通的Java工程。在网上,我找到了网 ... -
从handler中获取全局状态
2012-09-27 15:16 1411当运用eclipse扩展点handler来使用comm ... -
过滤第三方插件扩展点
2012-09-26 09:57 1109在开发rcp过程中,不可避免的需要加入第三方插件,与此 ... -
插件开发基础类(转载)
2012-09-25 16:36 7951..IWorkbench: workbench是e ... -
获取需要平台的信息
2012-09-25 16:16 786开发rcp程序的时候,需要获取许多平台和编辑器的相关信 ...
相关推荐
此扩展点与 `org.eclipse.core.contenttype.contentTypes` 类似,主要用于定义和管理内容类型。 **5. org.eclipse.core.runtime.preferences** 此扩展点用于定义全局的偏好设置(Preferences),使得插件可以在 ...
1. org.eclipse.ant.core.antTasks:此扩展点允许开发者注册自定义的Ant任务,这些任务可以与插件中的类关联,从而增强Ant在Eclipse中的功能。例如,你可以创建一个新的Ant任务来执行特定的构建过程或自动化任务。 ...
│ │ org.eclipse.jdt.core.prefs │ │ │ └─src │ │ logging.properties │ │ │ └─com │ └─mine │ │ BigMap.java │ │ LogPack.java │ │ │ └─logging │ ConsoleHandler.java │ Error...
创建和操作资源通常需要依赖org.eclipse.core.resources插件,可以创建本地资源、工程、目录和文件,也可以处理链接资源。 Marker是Eclipse资源管理中的一个重要组件,用于标记资源上的问题或错误。开发者可以通过...
可以删除`.metadata\.plugins`目录下的`org.eclipse.core.resources`子目录,但记得先备份重要信息。 总结来说,MyEclipse的优化是一个系统性的工作,涉及内存分配、启动配置、代码提示、项目构建、资源管理和版本...
这些文件通常位于 `workspace/.metadata/.plugins/org.eclipse.core.resources/.snap` 目录下。 - 如果是内存问题,可以尝试增加 Eclipse 的启动参数,比如 `-Xms` 和 `-Xmx` 来指定初始和最大堆内存大小。 - 检查...