Tuesday, July 5, 2011

Validate JAXB Object with XML Schema

We can validate JAXB Object with XML Schema(XSD). JAXB provide a class named Validator to validate against a schema. This class is available since Java 1.5


package javawiki.jaxb.validation;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlType(name = "Customer", propOrder = {
"name",
"phoneNumber",
})
public class Customer {

private String name;
private List phoneNumber;

public Customer() {
phoneNumber = new ArrayList();
}

public String getName() {
return name;
}

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

public List getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(List phoneNumber) {
this.phoneNumber = phoneNumber;
}
}

package javawiki.jaxb.validation;

public class PhoneNumber {

}


<?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="phoneNumber" maxOccurs="3" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>

 <xs:element name="phoneNumber">
  <xs:complexType />
 </xs:element>

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

</xs:schema>


package javawiki.jaxb.validation;

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

public class MyErrorHandler implements ErrorHandler {

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

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

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

}


package javawiki.jaxb.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 DemoSchemaValidation {

 public static void main(String[] args) {
  Customer customer = new Customer();
        customer.setName("Jane ");
        customer.getPhoneNumber().add(new PhoneNumber());
        customer.getPhoneNumber().add(new PhoneNumber());
        customer.getPhoneNumber().add(new PhoneNumber());
        
        try
        {
         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("src/javawiki/jaxb/validation/customer.xsd")); 
 
         Validator validator = schema.newValidator();
         validator.setErrorHandler(new MyErrorHandler());
         validator.validate(source);
        }catch(Exception ex)
        {
         ex.printStackTrace();
        }
 }

}

Output:
cvc-maxLength-valid: Value 'Jane Doe' with length = '8' is not facet-valid with respect to maxLength '5' for type 'stringMaxSize5'.
cvc-type.3.1.3: The value 'Jane Doe' of element 'name' is not valid.
cvc-complex-type.2.4.d: Invalid content was found starting with element 'phoneNumber'. No child element is expected at this point.

No comments:

Post a Comment