mule:xml schema-validation-filter,其中未执行自定义errorHandler

时间:2014-10-22 18:02:45

标签: java xml xsd mule xml-validation

我在流上的处理器链中使用模式验证过滤器,我想设置一个自定义错误处理程序,以便我可以保存无效的XML行,但问题是我的 " schemaErrorHandler" 永远不会被执行。

注意:我的自定义资源解析程序(如下所示以类似方式配置)完全没有问题。

我已经尝试从消息过滤器中包装模式验证过滤器,并从中除去 throwOnUnaccepted =" true" atribbute但 schemaErrorHandler 永远不会被执行。

这是一个错误或我错过了什么?没有发现与此相关的JIRAs。

XML

    <processor-chain>
        <message-filter onUnaccepted="TextValidator">
            <mxml:is-xml-filter />
        </message-filter>
        <message-filter throwOnUnaccepted="true">
            <mxml:schema-validation-filter errorHandler-ref="schemaErrorHandler" resourceResolver-ref="schemaResourceResolver" schemaLocations="mySchema.xsd"/>
        </message-filter>
    </processor-chain>

    <spring:bean id="schemaResourceResolver" name="schemaResourceResolver" class="my.app.SchemaResourceResolver"/>
    <spring:bean id="schemaErrorHandler" name="schemaErrorHandler" class="my.app.SchemaErrorHandler"/>

SchemaErrorHandler.java

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class SchemaErrorHandler implements ErrorHandler {

private List<Object> allErrors = new ArrayList<>();

public List<Object> getAllErrors() {
    return allErrors;
}

public void setAllErrors(List<Object> allErrors) {
    this.allErrors = allErrors;
}

@Override
public void error(SAXParseException arg0) throws SAXException {
    System.out.println(arg0.getMessage());
    allErrors.add(arg0);

}

@Override
public void fatalError(SAXParseException arg0) throws SAXException {
    System.out.println(arg0.getMessage());
    allErrors.add(arg0);
}

@Override
public void warning(SAXParseException arg0) throws SAXException {
    System.out.println(arg0.getMessage());
    allErrors.add(arg0);
}

}

1 个答案:

答案 0 :(得分:0)

为了使流程工作,您需要使用不同的方法。附加示例xml

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

<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
    xmlns:http="http://www.mulesoft.org/schema/mule/http"     xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core     http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http     http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/xml     http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
<spring:beans>
    <spring:bean id="schemaErrorHandler" name="schemaErrorHandler"
        class="SchemaErrorHandler" />
</spring:beans>

<flow name="xml_schemaFlow1" doc:name="xml_schemaFlow1">
    <file:inbound-endpoint path="/home/anil/deleteme/xsd/file"
        responseTimeout="10000" doc:name="File" />



    <message-filter name="asdf" onUnaccepted="invalidRelationXmlFlow"
        doc:name="Message">
        <mulexml:schema-validation-filter
            schemaLocations="/home/anil/deleteme/xsd/xsd/abc.xsd"
            errorHandler-ref="schemaErrorHandler" returnResult="true"
            doc:name="Schema Validation">
        </mulexml:schema-validation-filter>
    </message-filter>



    <echo-component doc:name="Echo" />
</flow>

<flow name="invalidRelationXmlFlow" doc:name="invalidRelationXmlFlow">
    <logger level="INFO" doc:name="Logger" message="hello"/>

</flow>

这会在骡子中发出警告,但确实有效。精细。 如果文件验证失败,将调用invalidRelationXmlFlow。