Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JAXB support xsd:restriction?

<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.

like image 990
Narendra Pathai Avatar asked Dec 08 '12 07:12

Narendra Pathai


People also ask

What is restriction base in XSD?

Restriction element is used to define accepted values that an XML element can take.

What is XSD file in Java?

xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.

What is XSD and XJB?

The global schema binding declarations in bindings. xjb are the same as those in po. xsd for the Datatype Converter example. The only difference is that because the declarations in po. xsd are made inline, you need to embed them in <xs:appinfo> elements, which are in turn embedded in <xs:annotation> elements.

Is XJC a JAXB?

7.1. The JAXB-2 Maven plugin uses the JDK-supplied tool XJC, a JAXB Binding compiler tool that generates Java classes from XSD (XML Schema Definition). By default, this plugin locates XSD files in src/main/xsd.

What is the use of JAXB in Java?

JAXB provides for the creation of Java interfaces and classes which correspond to element and data types defined via XML Schema. These generated classes provide the ability to marshall Java instances of these classes to XML and unmarshall instances from XML.

What is Popo XSD in JAXB?

po.xsd is the XML schema that is used as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.

How do I add a JAXB class to an XSD schema candidate?

In order for a Java Class to be eligible for an XSD schema candidate, the class must be annotated with a @XmlType annotation. We'll reuse the Java class files from the previous example to configure the plugin: By default, JAXB recursively scans all the folders under src/main/java for annotated JAXB classes.

Can JAXB binding customizations be made to the XML Schema?

Unlike the examples in Basic JAXB Examples, which focus on the Java code in the respective Main.java class files, the examples here focus on customizations made to the XML schema before generating the schema-derived Java binding classes. Note: JAXB binding customizations currently must be made by hand.


3 Answers

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.

  • https://github.com/krasa/krasa-jaxb-tools
like image 80
bdoughan Avatar answered Oct 10 '22 21:10

bdoughan


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);
like image 5
Drunix Avatar answered Oct 10 '22 20:10

Drunix


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;

}
like image 5
vbence Avatar answered Oct 10 '22 21:10

vbence