21
<xs:element name="age">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="120"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

So I want it to get converted to Java code like this:

public void setAge(int age){
    if(age < 0 || age > 120){
         //throw some exception
    }
     //setting the age as it is a valid value
}

Is it possible in JAXB?

Had seen some WebService Client stub generator doing this maybe axis2 webservice but not sure.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120

3 Answers3

21

The JAXB (JSR-222) specification does not cover generating fail fast logic into the domain model. A common practice now is to express validation rules in the form of annotations (or XML) and run validation on them. Bean Validation (JSR-303) standardizes this and is available in any Java EE 6 implementation.

XJC Extensions

I have not tried the following extension myself but it appears as though it will generate Bean Validation (JSR-303) annotations onto the domain model representation validation rules from the XML schema. As XJC is very extensible there may be other plug-ins available as well.

MeTTeO
  • 2,088
  • 16
  • 19
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    Thanks and there is one more thing. There is a [http://java.net/jira/browse/JAXB-917] JIRA for JAXB Facets, this is not included in JSR for JAXB. Right? Is this some standard implementation of some other standard? – Narendra Pathai Dec 12 '12 at 12:09
  • 2
    @NarendraPathai - This is the first I've heard of JAXB Facets. This is currently not part of the JSR and is being proposed as an enhancement to the JAXB reference implementation. I have posted a comment on the JIRA issue asking about it's compatibility with Bean Validation (JSR-303). A JSR-303 compatible approach would be something that could be supported in a future version of the JAXB specification. – bdoughan Dec 12 '12 at 14:13
  • 1
    Thanks Blaise. I follow your blogs and would love to hear about some implementation of this in JAXB. – Narendra Pathai Dec 12 '12 at 18:22
  • @BlaiseDoughan - What's the status of this? – Everlight Feb 29 '16 at 18:36
5

The suggested way to perform this validation in JAXB is switching on schema validation on the marshaller resp. unmarshaller:

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
Schema schema = schemaFactory.newSchema(...);

ValidationEventHandler valHandler = new ValidationEventHandler() {
  public boolean handleEvent(ValidationEvent event) {
      ...
  }
};

marshaller.setSchema(schema);
marshaller.setEventHandler(valHandler);
Drunix
  • 3,313
  • 8
  • 28
  • 50
  • I know but if the schema contains xsd:restriction then the generated code does not reflect those constraints – Narendra Pathai Dec 10 '12 at 09:18
  • 1
    Ok, in JAXB standard and RI there are no such mechanisms. I guess they are not used since not all schema constraints can be validated immediately (for example "minOccurs=2"). – Drunix Dec 10 '12 at 09:50
  • but as I have mentioned in the question have seen some axis tools generating classes which utilize xs:restriction. – Narendra Pathai Dec 10 '12 at 09:59
  • There used to be the verification plugin, but that is inactive for a long time. Maybe krasa-jaxb-tools comes close to what you are looking for. It generates validation annotations. I have to admit that I have not used it for real projects. – Drunix Dec 11 '12 at 07:41
5

You can try JAXB-Facets. Quick snippet:

class MyClass {

    @MinOccurs(1) @MaxOccurs(10)
    @Facets(minInclusive=-100, maxInclusive=100)
    public List<Integer> value;

    @Facets(pattern="[a-z][a-z0-9]{0,4}")
    public String name;

}
Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78
vbence
  • 20,084
  • 9
  • 69
  • 118