[英]JAXB binding file: enumeration with integer id
I have an enumeration in XSD as follows: 我在XSD中有一个枚举,如下所示:
<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: 使用此模式和一个JAXB绑定文件,我想生成类似于以下内容的枚举:
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. 我正在尝试找出JAXB绑定代码以实现上述Java枚举。 Thanks.
谢谢。
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.
您可以按自己的方式编写枚举,并告诉xjc插件在生成的代码中使用该枚举。
You will first need a binding file that explains what to do with the enumeration in your XSD file: binding.xml 您首先需要一个绑定文件,该文件说明如何处理XSD文件中的枚举: 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 接下来,根据需要在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. 现在,您将需要在xjc工具中引用binding.xml文件。 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. 有许多方法可以做到这一点,但是这里我使用的是maven插件
cxf-xjc-plugin
,您可以拥有一个配置文件,该配置文件可用于从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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.