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

Create Eclipse Editor Plugin for a sub set of an extension

我有一些特殊的 .xml 文件,我需要通过我自己的 eclipse 编辑器打开这些文件。所以我添加了一个自定义内容类型并与我的编辑器绑定。

这是我的描述符class。

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) 我是否也可以使用单独的文件图标,仅针对我的内容类型?

eclipse 编辑器是 file-base 而不是 content-based...您可以按照您的回答中的描述检查您的文件, 但文件类型由文件结尾决定。

这个概念在 eclipse 以及 windows 或类似的默认文件系统中使用...

也许您应该引入自定义文件类型,例如 myfile.tepxml

可以定义某些内容类型,它们是文件名和文件扩展名的组合,但它们是自定义定义的,作为您 eclipse 的 一部分设置不是你的编辑器的一部分(锁定是从eclipse预定义的)

describe 方法必须 return VALID 如果 XML 确实适合您的应用程序。

因此,如果 XML 对您的应用不正确,那么在您的代码中 XmlUtil.parseSuite 必须抛出异常。因此,例如,如果您的描述者获得了 Ant 构建脚本 XML,您必须拒绝它。

如果输入看起来像 XML 但不适合您的应用,您还应该 return 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 进行检查。