Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a static variable from an xsd

Tags:

java

jaxb

xsd

In a project I'm working on, we generate classes from multiple xsd. Some of my classes need a public static final String EXTENSION_CODE = "NAMEOFTHEEXTENSION";. The xsd of my class looks something like :

<xsd:complexType name="SomeExtension">
    <xsd:sequence>
        <xsd:element name="ID" type="xsd:string"  minOccurs="1" maxOccurs="1"/>
        <xsd:element name="element1" type="Class" minOccurs="0" maxOccurs="1" />
        <xsd:element name="element2" type="Class" minOccurs="0" maxOccurs="1" />
        <xsd:element name="element3" type="Class" minOccurs="0" maxOccurs="1" />
        <xsd:element name="element4" type="Class" minOccurs="0" maxOccurs="1" />
        <xsd:element name="otherID" type="xsd:string"  minOccurs="1" maxOccurs="1"/>
        <xsd:element name="element5" type="xsd:string"  minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
</xsd:complexType>

This will generate a class that will look like :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SomeExtension", propOrder = {
    "ID",
    "element1",
    "element2",
    "element3",
    "element4",
    "otherID",
    "element5"
})
public class SomeExtension
    implements Serializable, Cloneable, CopyTo
{

    private final static long serialVersionUID = 1L;
    @XmlElement(required = true)
    protected String iD;
    protected Element1 element1;
    protected Element2 element2;
    protected Element3 element3;
    protected Element4 element4;
    @XmlElement(required = true)
    protected String otherID;
    protected String element5;

What I need is :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SomeExtension", propOrder = {
    "ID",
    "element1",
    "element2",
    "element3",
    "element4",
    "otherID",
    "element5"
})
public class SomeExtension
    implements Serializable, Cloneable, CopyTo
{

    public static final String EXTENSION_CODE = "NAMEOFTHEEXTENSION";
    private final static long serialVersionUID = 1L;
    @XmlElement(required = true)
    protected String iD;
    protected Element1 element1;
    protected Element2 element2;
    protected Element3 element3;
    protected Element4 element4;
    @XmlElement(required = true)
    protected String otherID;
    protected String element5;

I searched on Google with the following keyword : java generate static variable from xsd, and didn't find something usefull. Is there a way I could do this or is my need of a public static String weird in the first place ?

like image 993
Marc-Andre Avatar asked Feb 27 '26 15:02

Marc-Andre


1 Answers

If I got you right, you want to add a static field to your schema-derived classes.

The simplest thing you can do is to use the code injector plugin:

Inserting code with XJC+xsd+jxb using the options " -Xinject-code -extension "

Something like:

<jxb:bindings schemaLocation="schema.xsd">
    <jxb:bindings node="/xs:schema/xs:complexType[@name='SomeExtension']">
        <ci:code>
            public static final String EXTENSION_CODE = "NAMEOFTHEEXTENSION";
        </ci:code>
    </jxb:bindings>
</jxb:bindings>
like image 126
lexicore Avatar answered Mar 02 '26 03:03

lexicore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!