繁体   English   中英

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

[英]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进行检查。

蚀编辑器是基于 文件的,而不是基于内容的 ...您可以按照答案中的说明检查文件,但是文件类型由文件结尾确定。

这个概念用于Eclipse以及默认文件系统(例如Windows或类似文件)中。

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

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM