4

I'm currently generating my first JAXB data binding. I have a schema which contains within it a xs:simpleType:

<xs:simpleType name="NINumberType">
    <xs:restriction base="xs:string">
        <xs:pattern value="[A-Z]{2}\d{6}[A-D]{0,1}"/>
    </xs:restriction>
</xs:simpleType>

In my binding.xjb I have this:

<jxb:bindings version="1.0"
     xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
     jxb:extensionBindingPrefixes="xjc">

     <jxb:bindings schemaLocation="mySchema.xsd" node="/xs:schema">
         <jxb:globalBindings mapSimpleTypeDef="true" />
         <jxb:schemaBindings>
             <jxb:package name="com.company.jaxb.mySchema"/>
         </jxb:schemaBindings>
     </jxb:bindings>

</jxb:bindings>

Then, through Eclipse (well RAD 7.5) I right click the schema, and choose Generate->Java. This produces data binding objects as expected, except that the NINumberType has no built in restrictions:

/**
 * <p>Java class for NINumberType simple type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;simpleType name="NINumberType">
 *   &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string">
 *     &lt;pattern value="[A-Z]{2}\d{6}[A-Z]{0,1}"/>
 *   &lt;/restriction>
 * &lt;/simpleType>
 * </pre>
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "NINumberType", propOrder = {"value"})
public class NINumberType {
    @XmlValue
    protected String value;

    public String getValue() {return value;}
    public void setValue(String value) {this.value = value;}
}

No mention is made of the regular expression restriction specified in my schema, except in the class level javadoc. JAXB appears to have the information it needs to generate the restriction code, but is not using it. Can anyone help me ensure that the restriction code is generated such that attempting to bind a poorly formatted NI number will fail?

Chris Knight
  • 24,333
  • 24
  • 88
  • 134

2 Answers2

9

JAXB will not generate those restrictions into the Java model. You can specify an instance of javax.xml.validation.Schema on the Marshaller/Unmarshaller if you want this constraint enforced during the conversion to/from XML.

For More Information


You may be able to find an XJC extension that leverages something like JSR-303 for validation constraints. The following link may help:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
2

You could also look to xjc plugin https://github.com/krasa/krasa-jaxb-tools

By documentation it support XJsr303Annotations and can generate:

  • @Valid annotation for all complex types, can be further restricted to generate only for types from defined schema: -XJsr303Annotations:targetNamespace=http://www.foo.com/bar
  • @NotNull annotation for objects that has a MinOccur value >= 1 or for attributes with required use
  • @Size for lists that have minOccurs > 1
  • @Size if there is a maxLength or minLength or length restriction
  • @DecimalMax for maxInclusive restriction
  • @DecimalMin for minInclusive restriction
  • @DecimalMax for maxExclusive restriction, enable new parameter (inclusive=false) with: -XJsr303Annotations:JSR_349=true
  • @DecimalMin for minExclusive restriction, enable new parameter (inclusive=false) with: -XJsr303Annotations:JSR_349=true
  • @Digits if there is a totalDigits or fractionDigits restriction.
  • @Pattern if there is a Pattern restriction

If you wonder how to use XJC plugins in your build environment (ant, maven, gradle) I may recomment look at examples of another plugin: immutable-xjc

So I hope it helps.

Related to Generation of XSD restrictions in a schema generated from Java JAXB annotated classes

Hubbitus
  • 5,161
  • 3
  • 41
  • 47