November 24, 2010

Validate a JAXB Object Model With an XML Schema

In this post I will demonstrate how to validate a JAXB object model against an XML schema.  This will be done using the javax.xml.validation APIs.

Background Information

In JAXB 1.0 there was the concept of a Validator. This Validator could be used to determine if when marshalled a graph of objects would produce valid XML. Since JAXB 1.0 had standard interfaces backed by vendor specific implementation classes it was easy for vendors to code generate the necessary validation logic. When JAXB 2.0 moved to annotated POJO classes the ability to have generated validation logic was lost and Validator was deprecated. In this post I'll demonstrate how to leverage the javax.xml.validation APIs to get the same sort of behaviour.

Java Model

The following object model will be used for this example.  Notice how the model classes do not contain any validation specific information.

package blog.validation;

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

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

    private String name;

    private List<PhoneNumber> phoneNumbers;

    public Customer() {
        phoneNumbers = new ArrayList<PhoneNumber>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement(name="phone-number")
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }

    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

}

package blog.validation;

public class PhoneNumber {

}

XML Schema

The following is our XML schema.  The following are the interesting constraints:
  • The customer's name cannot be longer than 5 characters.
  • The customer cannot have more than 2 phone numbers.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="customer">
        
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="stringMaxSize5" />
                <xs:element ref="phone-number" maxOccurs="2" />
             </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="phone-number">
        <xs:complexType />
    </xs:element>

    <xs:simpleType name="stringMaxSize5">
        <xs:restriction base="xs:string">
            <xs:maxLength value="5"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

Demo Code

In this example we will create an instance of Customer that would produce an XML document that does not conform to our XML schema.  The customers name will be too long, and it will contain more than two phone numbers.

A Validator will be created from the customer XML Schema.  This Validator can accept different types of XML inputs.  We will leverage JAXBSource (which implements javax.xml.transform.Source) to expose our JAXB object model as an XML input to the Validator.

package blog.validation;

import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.util.JAXBSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class Demo {

    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        customer.setName("Jane Doe");
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());

        JAXBContext jc = JAXBContext.newInstance(Customer.class);
        JAXBSource source = new JAXBSource(jc, customer);

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
        Schema schema = sf.newSchema(new File("customer.xsd")); 

        Validator validator = schema.newValidator();
        validator.setErrorHandler(new MyErrorHandler());
        validator.validate(source);
    }

}

An ErrorHandler provides a mechanism to capture the validation exceptions.  If you re-throw the exception then parsing stops, and if you swallow the exception parsing continues.  This provides a useful mechanism to ignore validation constraints.

package blog.validation;

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

public class MyErrorHandler implements ErrorHandler {

    public void warning(SAXParseException exception) throws SAXException {
        System.out.println("\nWARNING");
        exception.printStackTrace();
    }

    public void error(SAXParseException exception) throws SAXException {
        System.out.println("\nERROR");
        exception.printStackTrace();
    }

    public void fatalError(SAXParseException exception) throws SAXException {
        System.out.println("\nFATAL ERROR");
        exception.printStackTrace();
    }

}

Output

The following output is produced when EclipseLink MOXy is used as the JAXB implementation.

ERROR
org.xml.sax.SAXParseException: cvc-maxLength-valid: Value 'Jane Doe' with length = '8' is not facet-valid with respect to maxLength '5' for type 'stringWithMaxSize5'.
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:417)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3181)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3096)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:3006)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2149)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:817)
 at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.endElement(ValidatorHandlerImpl.java:563)
 at org.xml.sax.helpers.XMLFilterImpl.endElement(XMLFilterImpl.java:546)
 at org.eclipse.persistence.oxm.record.ContentHandlerRecord.endElement(ContentHandlerRecord.java:235)
 at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:315)
 at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:325)
 at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:934)
 at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:648)
 at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:608)
 at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:233)
 at javax.xml.bind.util.JAXBSource$1.parse(JAXBSource.java:222)
 at javax.xml.bind.util.JAXBSource$1.parse(JAXBSource.java:210)
 at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.validate(ValidatorHandlerImpl.java:697)
 at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:97)
 at javax.xml.validation.Validator.validate(Validator.java:127)
 at blog.validation.Demo.main(Demo.java:29)

ERROR
org.xml.sax.SAXParseException: cvc-type.3.1.3: The value 'Jane Doe' of element 'name' is not valid.
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:417)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3181)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3097)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:3006)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2149)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:817)
 at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.endElement(ValidatorHandlerImpl.java:563)
 at org.xml.sax.helpers.XMLFilterImpl.endElement(XMLFilterImpl.java:546)
 at org.eclipse.persistence.oxm.record.ContentHandlerRecord.endElement(ContentHandlerRecord.java:235)
 at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:315)
 at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:325)
 at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:934)
 at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:648)
 at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:608)
 at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:233)
 at javax.xml.bind.util.JAXBSource$1.parse(JAXBSource.java:222)
 at javax.xml.bind.util.JAXBSource$1.parse(JAXBSource.java:210)
 at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.validate(ValidatorHandlerImpl.java:697)
 at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:97)
 at javax.xml.validation.Validator.validate(Validator.java:127)
 at blog.validation.Demo.main(Demo.java:29)

ERROR
org.xml.sax.SAXParseException: cvc-complex-type.2.4.d: Invalid content was found starting with element 'customer'. No child element '{phone-number}' is expected at this point.
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:417)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3181)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidComplexType(XMLSchemaValidator.java:3168)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3104)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:3006)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2149)
 at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:817)
 at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.endElement(ValidatorHandlerImpl.java:563)
 at org.xml.sax.helpers.XMLFilterImpl.endElement(XMLFilterImpl.java:546)
 at org.eclipse.persistence.oxm.record.ContentHandlerRecord.endElement(ContentHandlerRecord.java:235)
 at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:961)
 at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:648)
 at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:608)
 at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:233)
 at javax.xml.bind.util.JAXBSource$1.parse(JAXBSource.java:222)
 at javax.xml.bind.util.JAXBSource$1.parse(JAXBSource.java:210)
 at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.validate(ValidatorHandlerImpl.java:697)
 at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:97)
 at javax.xml.validation.Validator.validate(Validator.java:127)
 at blog.validation.Demo.main(Demo.java:29)

Summary

This example demonstrates the benefit of standards.  JAXB was not written specifically to work with javax.xml.validation APIs, but since it can be exposed as a javax.xml.transform.Source (using JAXBSource) it does.  In future posts I write more about schema validation, and other uses for JAXBSource.

19 comments:

  1. A great blog!

    Would it be great if JAXB Objects could be immutable!

    http://aniketshaligram.blogspot.com/2010/05/jaxb-immutable-objects.html

    I read this post but with no final fields how can a class really be immutable...

    ReplyDelete
  2. Thanks for your support, and the link to that post. Currently you could use an XmlAdapter to handle immutable objects (I plan to blog about this soon). I also have an enhancement request open for multi-arg constructors:

    - https://bugs.eclipse.org/328951

    ReplyDelete
  3. As promised, here is the link to my blog post about JAXB and immutable objects:

    - JAXB and Immutable Objects

    -Blaise

    ReplyDelete
  4. Thats a perfect solution for one who want to do validation outside of JAXB standard validation.
    my question is can't be get detailed information using it like we tend to do it in JAXB validation
    something like this

    public boolean handleEvent(ValidationEvent validationevent) {
    ValidationEventLocator locator = validationevent.getLocator();
    log.info("Validating XML file with the schema");
    if(validationevent.getSeverity()==ValidationEvent.FATAL_ERROR ){
    validationErrorFlag=true;
    log.fatal(
    "Line:Col[" + locator.getLineNumber()
    + ":" + locator.getColumnNumber()
    + "]:" + validationevent.getMessage());
    }

    ReplyDelete
  5. Hi Umesh,

    I prefer to think that this example demonstrates that JAXB cleanly supports the other Java SE XML standards (such as validation), as opposed to it demonstrating validation outside the JAXB standard.

    A ValidationEventLocation instance contains information that is very comparable to a SAXParseExeption.

    -Blaise

    ReplyDelete
  6. Hi Blaise,
    i was following your post to validate my XML document outside of Jaxb 2.x
    here is the piece of code i was using

    unmarshaller.setSchema(schema);
    SAXSource source = new SAXSource(new InputSource(xmlFileLocation));
    Validator validator = schema.newValidator();
    validator.setErrorHandler(new XMLErrorHandler());
    try {
    validator.validate(source);
    } catch (SAXException e) {

    and my XMLErrorHanlder class have following signature

    public class XMLErrorHandler implements ErrorHandler {
    public void error(SAXParseException exception) throws SAXException {
    xmlUnmarshaller.setValidationFlag(true);
    log.error(
    "Line:Col[" + exception.getLineNumber()
    + ":" + exception.getColumnNumber()
    + "]:" + exception.getMessage());


    exception.printStackTrace();

    }
    }

    }

    now its validating the XML with XSD but it only showing the first encountered error while i want to get print on console all errors and warning on console.
    i am not sure where i am doing wrong.
    also
    xmlUnmarshaller.setValidationFlag(true);
    has nothing relation with the validation process its an internal indication for validation error.

    ReplyDelete
  7. Hi Umesh,

    If you are validating the document outside of JAXB you will not need to set an instance of Schema on the Unmarshaller, or set the validation property on the Unmarshaller during the error event.

    The following link may help:

    - http://stackoverflow.com/questions/3893263/xml-with-xsd-java/3893640#3893640

    -Blaise

    ReplyDelete
  8. Thanks Blaise for the link
    unfortunately that question was asked my me and it was working but now when i started work on this suddenly it behaving differently it only existing after finding first validation error.

    ReplyDelete
  9. If you set an instance of the MyErrorHandler class from this blog, do you still see it fail on the first error?

    ReplyDelete
  10. Yes i did same what said in the blog
    here is the code
    SchemaFactory schemaFactory=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schema=schemaFactory.newSchema()

    SAXSource source = new SAXSource(new InputSource(xmlFileLocation));
    Validator validator = schema.newValidator();
    validator.setErrorHandler(new XMLErrorHandler());
    try {
    validator.validate(source);
    } catch (SAXException e) {

    }

    here is the XSD part






    and XML part is
    longDescription
    0
    categories

    error handler class is same as per the blog but its showing only the following error
    ERROR
    org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'typeCode'. One of '{stateID}' is expected.

    no more error

    ReplyDelete
  11. Thanks Blaise for the detailed reply.i posted my concern there:)

    ReplyDelete
  12. I have to work with DTDs and I use JAXB to generate classes from the DTDs. How can I validate my objects against the DTDs while using JAXB ?

    ReplyDelete
  13. The javax.xml.validation APIs do not support DTD validation (see Schema Language section).

    If your XML document references a DTD you can perform validation on an unmarshal by doing:

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(true);
    SAXParser sp = spf.newSAXParser();
    XMLReader reader = sp.getXMLReader();
    SAXSource source = new SAXSource(reader, inputSource);
    unmarshaller.unmarshal(source);

    -Blaise

    ReplyDelete
  14. Thank you ... but how do you carry out validation during marshalling ?

    ReplyDelete
  15. You can specify an XML schema on the Marshaller to validate during marshalling:

    - JAXB and Marshal/Unmarshal Schema Validation

    -Blaise

    ReplyDelete
  16. Sorry ... my question should have been "How do you carry out validation against the DTD during marshall operation ?"

    ReplyDelete
  17. Off hand I am not sure how to enforce validation against the DTD during a marshal operation. I would recommend posting this question on a forum like Stack Overflow.

    -Blaise

    ReplyDelete
  18. Hi Blaise,
    I followed your post and tried the same. I tried both ways using Validator as well as setting schema to UnMarshaller for the Validation.
    Both ways are exiting at first occurance of failure captured in Schema.

    I would like to capture ALL events to display to the user.

    FYI : I had tried using ValidationEventCollector as well but no luck. (That class suppose to hold all events in list and report at the end).

    Please advise.

    Thanks
    Rajesh

    ReplyDelete