java - Jaxb: Generate constant value for fixed-value attribute -
i'm working on xsd uses following contruct:
<xs:attribute name="listversionid" type="xs:normalizedstring" use="required" fixed="1.0">
while not problematic per se, rather annoying work with, since fixed-value of definition increases between releases of xsd spec, , need modify values in seperate constants-class keep them valid, although little if of interest in xsd has changed. xsd maintained elsewhere, changing no option.
thus asking myself wether there jaxb-plugin or similar turn fixed-value attributes constants ala
@xmlattribute(name = "listversionid") @xmljavatypeadapter(normalizedstringadapter.class) @xmlschematype(name = "normalizedstring") protected final string listversionid = "1.0";
instead of just
@xmlattribute(name = "listversionid") @xmljavatypeadapter(normalizedstringadapter.class) @xmlschematype(name = "normalizedstring") protected string listversionid;
which must populated manually.
does know of such?
yes possible through custom jaxb bindings, can added file @ codegen.
in jaxb bindings, there fixedattributeasconstantproperty
-attribute. setting true, instructs code generator generate attributes fixed
attribute java-constants.
there 2 options this:
1. through global bindings: make attributes fixed values constants
<schema targetnamespace="http://stackoverflow.com/example" xmlns="http://www.w3.org/2001/xmlschema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"> <annotation> <appinfo> <jaxb:globalbindings fixedattributeasconstantproperty="true" /> </appinfo> </annotation> ... </schema>
2. through local mappings: defines fixedattributeasconstantproperty
property on specific attribute.
<schema targetnamespace="http://stackoverflow.com/example" xmlns="http://www.w3.org/2001/xmlschema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"> <complextype name="example"> <attribute name="someconstant" type="xsd:int" fixed="42"> <annotation> <appinfo> <jaxb:property fixedattributeasconstantproperty="true" /> </appinfo> </annotation> </attribute> </complextype> ... </schema>
both examples should result in:
@xmlrootelement(name = "example") public class example { @xmlattribute public final static int someconstant = 42; }
Comments
Post a Comment