简体   繁体   English

扩展XML验证/自动完成

[英]Extending XML validation / auto-completion

I have several types of data-sources, that I would like to use for additional XML validation and providing auto-completion (using Eclipse if possible). 我有几种类型的数据源,我想将其用于其他XML验证并提供自动完成功能(如果可能,请使用Eclipse)。

This source could be some other XML (from another or the same file): 此源可能是其他一些XML(来自另一个文件或相同文件):

<type name="TypeA"/>
<type name="TypeB"/>

or a Java-class 或Java类

public List<String> getValues() {
    return Arrays.asList("Val1", "Val2", "Val3");
}

These values are then referenced in other XML-files: 这些值然后在其他XML文件中引用:

<x type="TypeA" value="Val2" />
<x type="TypeB" value="Val3" />

Now I would like to improve editing this file by 现在,我想通过以下方式改进对该文件的编辑

  • Validating the XML-File (underline wrong types/values, if possible display a red x in Package Expl.) 验证 XML文件(在错误的类型/值下划线,如果可能,请在Package Expl中显示红色x。)
  • Providing code-completion (suggest TypeA and TypeB when typing type=" ) 提供代码 TypeA (键入type="时,建议使用TypeATypeB

I'll certainly have to write some code, but what is the best way start? 我当然必须编写一些代码,但是最好的开始方式是什么?

  • Can the standard XML-Editor be extended? 可以扩展标准的XML-Editor吗?
  • Are there any plugins that can help? 是否有任何可以帮助您的插件? (Maybe Rinzo XML Editor ?) (也许是Rinzo XML编辑器 ?)
  • Any other options that I did not think of? 我没有想到的其他选择?

I'm pretty sure eclipse already does both these things but they are part of one of the extended packages. 我很确定eclipse已经完成了这两项工作,但是它们是扩展软件包之一的一部分。 Try downloading eclipse for Java EE developers. 尝试为Java EE开发人员下载eclipse。 I'm fairly sure validation and completetion are part of the Web Tools Platform. 我相当确定,验证和完成是Web工具平台的一部分。

Check Here For Validating XML 检查此处以验证XML

You can write XSD schemas for your XML files, then Eclipse can validate them. 您可以为XML文件编写XSD模式,然后Eclipse可以对其进行验证。

There are, for sure, frameworks who generate XSD schemas from your Java classes. 当然,有一些框架可以从您的Java类生成XSD模式。

Check the answers here: utility to generate xsd from java class 在此处检查答案: 从Java类生成xsd的实用程序

If you decide extending Rinzo, it seems documentation has bee updated on how to extend the same features you'll like to customize :) 如果您决定扩展Rinzo,似乎有关如何扩展您想自定义的相同功能的文档已更新:)

http://editorxml.sourceforge.net/extendingRinzo.html http://editorxml.sourceforge.net/extendingRinzo.html

Peter, 彼得,

I answer you in a new post since I didn't have enough space in a comment. 由于我的评论空间不足,因此我在新帖子中回答了您。

If you want to extend Rinzo according to your example I guess you'll need to create a plugin contributing the extension points declared in the site's documentation. 如果您想根据您的示例扩展Rinzo,我想您需要创建一个插件,以贡献站点文档中声明的扩展点。

For the content assistant implementation I guess a rough implementation based on your examples could be as follow: 对于内容助手实现,我想根据您的示例进行的粗略实现可能如下:

public class CustomSourceAssistProcessor implements IXMLContentAssistProcessor {
@Override
public void addAttributeValuesProposals(XMLNode currentNode, String attributeName, String prefix,
        ITextViewer viewer, int offset, Collection<ICompletionProposal> results) {
    if("x".equals(currentNode.getTagName()) && "type".equals(attributeName)) {
        for (String possibleValue : this.getPossibleValuesFromXML()) {
            results.add(new CompletionProposal(possibleValue, offset, prefix.length(), 0, null, "Proposal Description...", null, null));
        }
    }
    if("x".equals(currentNode.getTagName()) && "value".equals(attributeName)) {
        for (String possibleValue : this.getPossibleValuesFromJavaClass()) {
            results.add(new CompletionProposal(possibleValue, offset, prefix.length(), 0, null, "Proposal Description...", null, null));
        }
    }
}

} }

That's as far as interacting with Rinzo's API, and your particular logic to gather values either from an external XML file or java-class should be implemented in the methods getPossibleValuesFromXML() and getPossibleValuesFromJavaClass() 可以与Rinzo的API进行交互,并且您的特殊逻辑(用于从外部XML文件或Java类收集值)应该在方法getPossibleValuesFromXML()getPossibleValuesFromJavaClass()中实现

On the other hand in order to add your custom validator I guess the rough implementation of your extension point, also based on your example, should be similar to this one: 另一方面,为了添加自定义验证器,我猜也是基于您的示例,扩展点的粗略实现应与此类似:

public class CustomSourceXMLValidator implements XmlValidator {
@Override
public void validate(RinzoXMLEditor editor) {
    editor.getModel().getTree().accept(new HierarchicalVisitor() {
        @Override
        public boolean visitStart(XMLNode node) {
            if(node.isTag() && "x".equals(node.getTagName())) {
                for (Entry<String, XMLAttribute> entry : node.getAttributes().entrySet()) {
                    if("type".equals(entry.getKey())) {
                        this.validateValueFromXML(entry.getValue().getValue());
                    }
                    if("value".equals(entry.getKey())) {
                        this.valdateValueFromJavaClass(entry.getValue().getValue());
                    }
                }
            }
            return true;
        }
        private void valdateValueFromJavaClass(XMLAttribute xmlAttribute) {
            if(!this.getPossibleValuesFromXML().contains(xmlAttribute.getValue())) {
                this.createMarker(editor, xmlAttribute);
            }
        }
        private void validateValueFromXML(XMLAttribute xmlAttribute) {
            if(!this.getPossibleValuesFromJavaClass().contains(xmlAttribute.getValue())) {
                this.createMarker(editor, xmlAttribute);
            }
        }
    });
}

} }

And once again it's up to you the implementation of the methods getPossibleValuesFromXML() and getPossibleValuesFromJavaClass() . 再次由您决定方法getPossibleValuesFromXML()getPossibleValuesFromJavaClass()的实现 You can also see the source code of ClassNamesValidatorVisitor as an example. 您还可以看到ClassNamesValidatorVisitor的源代码作为示例。

Keep on rockin' in the free world! 在自由世界中继续前进! :) :)

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

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