7

I'm currently working on a xsd which uses the following contruct:

<xs:attribute name="listVersionID" type="xs:normalizedString" use="required" fixed="1.0">

While not problematic per se, it is rather annoying to work with, since the fixed-value of this definition increases between releases of the xsd spec, and we need to modify the values in a seperate constants-class to keep them valid, although little if anything of interest in the xsd has changed. The xsd is maintained elsewhere, so just changing it is no option.

Thus I was asking myself wether there is a jaxb-plugin or similar to turn fixed-value attributes into 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 be populated manually.

Does anyone know of such?

Scorpio
  • 2,309
  • 1
  • 27
  • 45

2 Answers2

4

If you don't want to modify your schema, another option is to use an external binding file:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <jaxb:bindings schemaLocation="yourschema.xsd" node="/xs:schema">
    <jaxb:globalBindings fixedAttributeAsConstantProperty="true" />
  </jaxb:bindings>

</jaxb:bindings>

It is equivalent to what proposes @jmattheis in his answer.

DLight
  • 1,535
  • 16
  • 20
3

Yes it is possible through custom jaxb bindings, which can be added as file at the codegen.

In the jaxb bindings, there is the fixedAttributeAsConstantProperty-attribute. Setting this to true, instructs the code generator to generate attributes with the fixed attribute as java-constants.

There are 2 options for this:

1. Through global bindings: which then make all attributes with fixed values to 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: Which only defines the fixedAttributeAsConstantProperty property on a 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;
}
jmattheis
  • 10,494
  • 11
  • 46
  • 58