Question

I have an enumeration in XSD as follows:

<xsd:simpleType name="Status">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="ACTIVE"/>
        <xsd:enumeration value="INACTIVE"/>     
    </xsd:restriction>
</xsd:simpleType>

Using this schema and a JAXB binding file, I want to generate enumeration similar to this:

public enum Status {
    ACTIVE(1),
    INACTIVE(2);

    private final int statusId;

    Status(int statusId) {
        this.statusId = statusId;
    }

    public int getId() {
        return this.statusId
    }

    public static Status getStatusById(int id) {
        // iterate through all status and return it
    }
}

I am trying to find out the JAXB binding code to achieve the above Java enum. Thanks.

Was it helpful?

Solution

Here is what I could find after some research on the web: I don't think you can generate enums like I have one in the question. You write your enum the way you want and tell the xjc plugin to use that enum in the generated code.

You will first need a binding file that explains what to do with the enumeration in your XSD file: binding.xml

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:version="2.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <jaxb:bindings schemaLocation="Schema.xsd">
        <jaxb:bindings node="//xs:simpleType[@name='Status']">
            <jaxb:javaType name="com.yourcompany.project.enums.Status"
                parseMethod="com.yourcompany.project.util.ProjectUtils.parseStatus"
                printMethod="com.yourcompany.project.util.ProjectUtils.printStatus" />
        </jaxb:bindings>        
    </jaxb:bindings>

</jaxb:bindings>

Next write print and parse methods as required in the binding.xml: ProjectUtils.java

public class ProjectUtils {

    public static Status parseStatus(String statusStr) {
        return Status.valueOf(statusStr.trim().toUpperCase());              
    }

    public static String printStatus(Status status) {
        return status.name();
    }
}

Now you will need to reference the binding.xml file in your xjc tool. There are number of ways to do this but here I am using maven plugin cxf-xjc-plugin and you can have a profile which you can use to generate source files from your XSD.

<profiles>
    <profile>
        <id>xsdtojava</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-xjc-plugin</artifactId>
                    <version>2.3.0</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>xsdtojava</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <sourceRoot>${basedir}/src/main/java</sourceRoot>
                        <xsdOptions>
                            <xsdOption>
                                <extension>true</extension>
                                <xsd>${basedir}/src/main/resources/schema/Schema.xsd</xsd>
                                <bindingFile>${basedir}/src/main/resources/schema/binding.xml</bindingFile>
                                <extensionArgs>
                                    <arg>-Xdv</arg>
                                    <arg>-Xts</arg>
                                </extensionArgs>
                                <packagename>com.yourcompany.project.generated</packagename>
                            </xsdOption>
                        </xsdOptions>
                        <extensions>
                            <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.3.0</extension>
                            <extension>org.apache.cxf.xjcplugins:cxf-xjc-ts:2.3.0</extension>
                        </extensions>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top