使用Custom Validator在eclipse插件中自定义xml验证?

时间:2012-06-26 17:33:02

标签: eclipse-plugin

我想使用自定义验证程序验证自定义内容类型文档(xml类型)。想要用xsd验证它,但只能在主文档的某些预处理之后。 不能使用普通的xml验证器,因为 - 1.)架构位置(xsd)&名称空间未在主文档文件中定义。 2.)和bcz的第一个原因&还有更多,想要在应用xsd验证之前对文档文件进行一些预处理。

所以我想使用xml验证器,但只能在我的文件预处理之后。

我的plugin.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

<extension
point="org.eclipse.core.runtime.contentTypes">

<content-type
id="com.xyz.ide.core.contentType.dummy"
base-type="org.eclipse.core.runtime.xml"
file-extensions="blabla"
/>

</extension>

<extension
point="org.eclipse.wst.sse.ui.sourcevalidation">
<validator
scope="total"
class="mc.CustomValidator"
id="com.xyz.myValidator">
<contentTypeIdentifier
id="com.xyz.ide.core.contentType.dummy">
<partitionType
id="org.eclipse.wst.xml.XML_DEFAULT">
</partitionType>
</contentTypeIdentifier>
</validator>
</extension>

</plugin>

CustomValidator.java

public class CustomValidator implements ISourceValidator, IValidator {
XMLValidator validator = new XMLValidator();
IDocument document;

public void validate(IValidationContext helper, IReporter reporter) {
String fileContent = this.document.get();
final InputStream is = new ByteArrayInputStream(fileContent.toLowerCase().getBytes());

// Whats the problem in this line???
XMLValidationReport report = validator.validate("/home/rchawla/xmlWorkspace/abc.xsd", is);

ValidationMessage[] messages = report.getValidationMessages();
for(ValidationMessage message:messages){
System.out.println(message.getMessage());
}
}

我可以在调试模式下运行插件时遇到validate方法,但是 该文档未通过xsd验证。 上述方法有什么问题, ValidationMessage [] messages = report.getValidationMessages();即使主文档文件中存在错误,也会给出零消息。

1 个答案:

答案 0 :(得分:0)

我在尝试使org.eclipse.wst.sse.ui.sourcevalidation扩展点工作时遇到了很多麻烦。我最终使用了另一个扩展点org.eclipse.wst.validation.validatorV2。两个验证器之间的唯一区别是,这个验证器仅在您保存文件时触发,而不是在您键入时触发。请参阅下面的示例:

<extension id="customValidator" name="Custom Validator" point="org.eclipse.wst.validation.validatorV2">
    <validator class="aaa.bbb.CustomValidator" markerId="customMarker" version="3">
        <include>
            <rules>
                <contentType id="customContentType" exactMatch="false"/>
            </rules>
        </include>
    </validator>
</extension>

验证器的实现应覆盖org.eclipse.wst.validation.AbstractValidator

相关问题