[英]Create Eclipse Editor Plugin for a sub set of an extension
我有一种特殊的.xml文件,需要通过自己的Eclipse编辑器打开。 因此,我添加了自定义内容类型并与我的编辑器绑定。
这是我的Describer类。
public class TEPFileContentDescriber implements IContentDescriber {
@Override
public int describe(InputStream contents, IContentDescription description)
throws IOException {
try{
XmlUtil.parseSuite(contents);
System.out.println("VALID");
return IContentDescriber.VALID;
}
catch(Exception e){
System.out.println("INVALID");
return IContentDescriber.INVALID;
}
}
@Override
public QualifiedName[] getSupportedOptions() {
// TODO Auto-generated method stub
return null;
}
}
在控制台中,我可以看到已触发正确的状态,但是Project Explorer仍将所有xml文件视为我的类型,因此编辑器在尝试打开时会抱怨其他文件。
1)我还有其他需要覆盖的方法吗?
2)是否可以仅针对我的内容类型使用单独的文件图标?
describe
方法必须仅在XML对您的应用程序正确时才返回VALID
。
因此,如果XML对您的应用程序不正确,则在您的代码中XmlUtil.parseSuite
必须引发异常。 因此,例如,如果为您的描述者提供了一个Ant构建脚本XML,那么您必须拒绝它。
如果输入看起来像XML但不适用于您的应用程序,则还应该返回INDETERMINATE
。
您可以扩展XMLContentDescriber
来为您完成一些工作。
例如,这是Ant插件内容描述程序:
public final class AntBuildfileContentDescriber extends XMLContentDescriber {
private int checkCriteria(InputSource contents) throws IOException {
AntHandler antHandler = new AntHandler();
try {
if (!antHandler.parseContents(contents)) {
return INDETERMINATE;
}
}
catch (SAXException e) {
// we may be handed any kind of contents... it is normal we fail to parse
return INDETERMINATE;
}
catch (ParserConfigurationException e) {
// some bad thing happened - force this describer to be disabled
String message = "Internal Error: XML parser configuration error during content description for Ant buildfiles"; //$NON-NLS-1$
throw new RuntimeException(message);
}
// Check to see if we matched our criteria.
if (antHandler.hasRootProjectElement()) {
if (antHandler.hasProjectDefaultAttribute() || antHandler.hasTargetElement() || antHandler.hasAntElement()) {
// project and default attribute or project and target element(s)
// or project and top level ant element(s) (classpath, import, macrodef, path, property, taskdef, typedef)
return VALID;
}
// only a top level project element...maybe an Ant buildfile
return INDETERMINATE;
}
return INDETERMINATE;
}
@Override
public int describe(InputStream contents, IContentDescription description) throws IOException {
// call the basic XML describer to do basic recognition
if (super.describe(contents, description) == INVALID) {
return INVALID;
}
// super.describe will have consumed some chars, need to rewind
contents.reset();
// Check to see if we matched our criteria.
return checkCriteria(new InputSource(contents));
}
public int describe(Reader contents, IContentDescription description) throws IOException {
// call the basic XML describer to do basic recognition
if (super.describe(contents, description) == INVALID) {
return INVALID;
}
// super.describe will have consumed some chars, need to rewind
contents.reset();
// Check to see if we matched our criteria.
return checkCriteria(new InputSource(contents));
}
}
您可以以此为基础,并更改checkCriteria
进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.