I ran into a similar problem, and the best solution I found was to write my own XJC plugin. The XJC plugin has access to both the XML schema and the generated classes, so it seemed like the easiest way to get information about the constraint. My plugin added annotation to any field in the generated class, with restrictions applied to it in the schema. For example, an element such as
<xs:element name="text"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:maxLength value="20" /> </xs:restriction> </xs:simpleType> </xs:element>
will appear as
@XmlRestrictions(maxLength = 20) protected String text;
in the generated class file. You can then use reflection to extract constraint information from the field.
The documentation for creating the plugin is not too large, but I found this article useful. Take a look at the source code The default plugin to see how to deploy XJC to - a representation of the XML schema memory and the generated class structure. The JAXB , XSOM, and CodeModel APIs will also be useful.
source share