gpt4 book ai didi

java - 为扩展的子集创建 Eclipse 编辑器插件

转载 作者:行者123 更新时间:2023-12-01 21:49:24 24 4
gpt4 key购买 nike

我有特殊类型的 .xml 文件,需要通过我自己的 eclipse 编辑器打开。因此,我添加了自定义内容类型并与我的编辑器绑定(bind)。

这是我的描述器类。

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;
}

}

在控制台中,我可以看到已触发正确的状态,但项目资源管理器仍将所有 xml 文件视为我的类型,因此编辑器在尝试打开时会提示其他文件。

1)这里还有其他我需要重写的方法吗?

2) 我是否可以仅针对我的内容类型使用单独的文件图标?

最佳答案

如果 XML 对于您的应用程序来说确实正确,则 describe 方法必须仅返回 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 来进行检查。

关于java - 为扩展的子集创建 Eclipse 编辑器插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35424801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com