Jaxb: generate constant value for fixed cost attribute

I am currently working on xsd which uses the following contruct:

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

Not a problem in itself, it is rather unpleasant for work, since the fixed cost of this definition increases between releases of the xsd specification, and we need to change the values ​​in a separate class of constants in order to keep them valid, although I have little interest in xsd. Xsd is supported elsewhere, so just changing it is not.

So I asked myself if there is a jaxb plugin or similarly turn fixed value attributes into ala constants

@XmlAttribute(name = "listVersionID")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected final String listVersionID = "1.0";

instead

@XmlAttribute(name = "listVersionID")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected String listVersionID;

which must be filled in manually.

Does anyone know about this?

+4
2

, jaxb, .

jaxb fixedAttributeAsConstantProperty. true, fixed java-.

2 :

1. :

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

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

:

@XmlRootElement(name = "example")
public class Example {
  @XmlAttribute
  public final static int SOMECONSTANT = 42;
}
+3

, - :

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

, @jmattheis .

+3

All Articles