3

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 ?

Marc-Andre
  • 912
  • 1
  • 15
  • 34
  • Why is it important that the xsd / xml actual documents get converted to Java objects? – ControlAltDel Jan 15 '15 at 20:11
  • 1
    In fact the xml is used to exahcange data between servers and we use the Java object in the server to do the actual work. We are using an existing xsd from a standard data model, and what you see is a custom extention for that mnodel. – Marc-Andre Jan 15 '15 at 20:13
  • This may answer your question. If it does not, can you explain why not? http://pojo.sodhanalibrary.com/pojoFromXSD.html – ControlAltDel Jan 15 '15 at 20:39
  • @ControlAltDel Your pojo does not contain a variable that would be valid for all classes. I would need to have : `public class Person { public static String STATIC_VARIABLE`. That will enable me to do `Person.STATIC_VARIABLE` in my code. I could add how the class is generated at the moment and what I need it to look like if it helps – Marc-Andre Jan 15 '15 at 20:44

1 Answers1

1

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>
Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thank you for your answer. It was indeed what I wanted to achieve, and it solved my problem. FWIW, it works nicely with maven plugin too. – Marc-Andre Jan 16 '15 at 14:59