Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jaxb: Generate constant value for fixed-value attribute

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?

like image 812
Scorpio Avatar asked May 09 '16 07:05

Scorpio


1 Answers

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.

like image 121
DLight Avatar answered Sep 25 '22 09:09

DLight