<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.
Restriction element is used to define accepted values that an XML element can take.
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.
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.
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.
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.
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.
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.
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.
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.
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);
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With