如何为jaxb对象的xsd验证编写自定义错误处理程序

时间:2013-03-15 10:00:17

标签: xsd jaxb xsd-validation

我想使用xsd验证我的JAXB对象并希望返回正确的错误消息。但我能得到的错误信息是:

  

cvc-complex-type.2.4.a:找到无效的内容   元素'申请人 - dto'。预计会有一个'{applicant-id}'。

     

[cvc-pattern-valid:值'12345670'不是方面有效的   模式'^ \ d {3} - \ d {2} - \ d {4} $'用于类型'ssn'。,cvc-type.3.1.3:   元素'ssn'的值'12345670'无效。]

我想收到正确的错误消息。任何人都可以帮我解决问题。

我的xsd是

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="applicant" type="applicantDTO" />

  <xs:complexType name="applicantDTO" >
    <xs:sequence>
      <xs:element name="applicant-id" type="xs:long" minOccurs="1" />
      <xs:element name="first-name" type="name" minOccurs="0" />
      <xs:element name="middle-name" type="xs:string" minOccurs="0" />
      <xs:element name="last-name" type="name" minOccurs="0" />
      <xs:element name="ssn" type="ssn" minOccurs="1" />
      <xs:element name="date-of-birth" type="xs:string" minOccurs="0" />
    </xs:sequence>
  </xs:complexType>

  <xs:simpleType name="name">
    <xs:restriction base="xs:string">
        <xs:pattern value="[a-zA-Z ]{3,30}"></xs:pattern>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="ssn">
    <xs:restriction base="xs:string">
        <xs:pattern value="^\\d{3}-\\d{2}-\\d{4}$"></xs:pattern>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

我的JAXB对象是

  

ApplicantDTO

用XSD验证它我写下代码

@Test
    public void testValidateApplicantDTO() throws Exception {
        ApplicantDTO eDTO = getApplicantDTO();
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
        Schema schema = sf.newSchema(new File("src/test/resources/applicant.xsd")); 

        Validator validator =  schema.newValidator();
        CustomErrorHandler errorHandler = new CustomErrorHandler();
        validator.setErrorHandler(errorHandler);

        JAXBContext jc = JAXBContext.newInstance(ApplicantDTO.class);
        JAXBSource source = new JAXBSource(jc, eDTO);
        validator.validate(source);

        System.out.println(errorHandler.getErrors());
    }

我已编写自定义错误hadler

CustomErrorHandler

public class CustomErrorHandler extends DefaultHandler  {

    List<String> errors = new ArrayList<String>();

    @Override
    public void warning(SAXParseException exception) throws SAXException {
        System.out.println("CustomErrorHandler.warning()");
    }

    @Override
    public void error(SAXParseException exception) throws SAXException {
        System.out.println("CustomErrorHandler.error()");
        errors.add(exception.getLocalizedMessage());
    }

    @Override
    public void fatalError(SAXParseException exception) throws SAXException {
        System.out.println("CustomErrorHandler.fatalError()");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        System.out.println("CustomErrorHandler.startElement()");
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        System.out.println("CustomErrorHandler.endElement()");
    }

    /**
     * @return the errors
     */
    public List<String> getErrors() {
        return this.errors;
    }

    /**
     * @param errors the errors to set
     */
    public void setErrors(List<String> errors) {
        this.errors = errors;
    }
}

错误列表返回给我并显示错误消息:

0 个答案:

没有答案