我如何使用Eclipse插件修改HTML源代码? [英] How can I modify HTML source using an Eclipse plugin?

查看:520
本文介绍了我如何使用Eclipse插件修改HTML源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个Eclipse插件来修改HTML源文件,例如。根据特定情况查找并更改 href src 。我对Eclipse相当陌生,但是我远远地搜索了一些可以实现这一点的东西,但是找不到任何类型的东西,我能找到的唯一API就是修改Java代码。



是否有任何API或可能是我可以修改的开放源代码Eclipse插件来完成此操作?

解决方案

如果您正在编写Eclipse插件,则可以使用Web Tools Platform(WTP)HTML插件中的API(下面的警告)。无头插件需要:


  • org.eclipse.wst.html.core

  • org.eclipse.wst.xml.core

  • org.eclipse.wst.sse.core

  • org.eclipse.text

  • org.eclipse.core.resources



它有很多依赖关系,但它们是相同的在WTP的HTML编辑器下运行的模型(除了JavaScript工具外,WTP提供的大多数编辑器)。

  import org .eclipse.wst.sse.core.internal.provisional.IModelManager; 
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
导入org.eclipse.jface.text.IDocument;
import org.w3c.dom.Element;
...
IModelManager modelManager = StructuredModelManager.getModelManager();
IDOMModel model = null;
尝试{
model =(IDOMModel)modelManager.getModelForEdit(anIFile);
// W3C类DOM操作
IDOMDocument doc = model.getDocument();
Element ele = doc.createElement(HTML40Namespace.ElementName.P);
doc.appendChild(ele);
// JFace IDocument兼容性
IDocument textDocument = model.getStructuredDocument();
textDocument .replace(0,textDocument .getLength(),< tag>一些文字< / tag>);
元素ele2 = doc.createElement(HTML40Namespace.ElementName.P);
doc.appendChild(ele2);
/ *您可以在这里使用两种或两种机制做更多。如果源自身是
*破碎,则DOM
*更改将立即反映在文本中,反之亦然,
*由DOM端付出最大努力。
* /
}
finally {
if(model!= null){
model.save();
model.releaseFromEdit();


$ / code>




  • 可以使用Eclipse的JFace Document API或非常接近W3C DOM API的方式修改文件内容。我们的模型完全可以作为文本文档进行修改,并且一些W3C API没有考虑到这一点。我们也有一些历史性的执行错误,例如XML声明在XML DOM中具有错误的节点类型。

  • 一些所需的类仍在内部临时包的旧版二进制兼容性。更改它们会破坏未知数量的下游插件。
  • 氖处理AngularJS样式的属性名称


从Eclipse启动以测试插件时,您可以只需从启动配置对话框中设置即可:




I want to write an Eclipse plugin to modify HTML source files, eg. find and change an href or an src depending on certain circumstances. I'm fairly new to Eclipse, but I searched far and wide for something that can accomplish this and couldn't find anything of the sort, the only APIs I could find were to modify Java code.

Is there any API or maybe an open source Eclipse plugin that I could modify to accomplish this?

解决方案

If you're writing an Eclipse plug-in, you can make use of APIs (caveats below) from the Web Tools Platform (WTP) HTML plug-ins. A headless plug-in will require:

  • org.eclipse.wst.html.core
  • org.eclipse.wst.xml.core
  • org.eclipse.wst.sse.core
  • org.eclipse.text
  • org.eclipse.core.resources

It's a lot of dependencies to take on, but these are the same models that run under WTP's HTML Editor (and most of the editors provided by WTP, aside from the JavaScript tools).

import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.jface.text.IDocument;
import org.w3c.dom.Element;
...
IModelManager modelManager = StructuredModelManager.getModelManager();
IDOMModel model = null;
try {
    model = (IDOMModel) modelManager.getModelForEdit(anIFile);
    // W3C-like DOM manipulation
    IDOMDocument doc = model.getDocument();
    Element ele = doc.createElement(HTML40Namespace.ElementName.P);
    doc.appendChild(ele);
    // JFace IDocument compatibility
    IDocument textDocument = model.getStructuredDocument();
    textDocument .replace(0, textDocument .getLength(), "<tag>some text</tag>");
    Element ele2 = doc.createElement(HTML40Namespace.ElementName.P);
    doc.appendChild(ele2);
    /* You can do more with either, or both, mechanisms here. DOM
     * changes are reflected in the text immediately and vice versa,
     * with a best effort by the DOM side if the source itself is
     * "broken".
     */
}
finally {
    if (model != null) {
        model.save();
        model.releaseFromEdit();
    }
}

  • Once loaded, you can modify the file contents using Eclipse's JFace Document APIs or something very close to the W3C DOM API. Our models are fully modifiable as text documents, and some of the W3C APIs weren't built with that in mind. There are also a few historic implementation mistakes on our part, e.g. the XML declaration has the wrong node type in the XML DOM.
  • Some required classes are still in internal or provisional packages for legacy binary compatibility. Changing them would break an unknown amount of downstream plug-ins.
  • Neon handles AngularJS-style attribute names better than prior releases, if that matters.
  • CSS and JavaScript sections and attribute values should automatically be handled and out of your way.
  • If you put org.eclipse.wst.sse.ui/actioncontributor/debugstatusfields=true (it's a trace option) into a file and use the file as the argument value to -debug when launching Eclipse, supported file types will have an extra field in the status bar that shows the selected text offset in the open editor. Double-clicking it will open some nerdy info about that selection. You can even set this up for your actual installation; aside from the numbers being shown, the only performance impact is when you double-click there.

When launching from Eclipse to test your plug-in, you can just set it from the launch config dialog:

这篇关于我如何使用Eclipse插件修改HTML源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆