无法使用JAXB生成枚举类

时间:2015-06-17 13:22:31

标签: java xml xsd jaxb

我在使用JAXB从XSD(以无法控制且无法更改的标准分发)中生成一些POJO时遇到了很多困难。该问题似乎与该XSD中定义某些类型的方式有关。下面我粘贴了一个名为TransactionCodeType的XSD中定义的简单类型,它是一个标记的并集,还有一个枚举值列表,也被定义为一个标记。

<xs:element name="TransactionContentDetails">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="TransactionIdentifier" type="xs:string" minOccurs="0"/>
            <xs:element name="TransactionCode" type="TransactionCodeType"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:simpleType name="TransactionCodeType">
    <xs:union memberTypes="xs:token">
        <xs:simpleType>
            <xs:restriction base="xs:token">
                xs:enumeration value="Design E-Filing"/>
                <xs:enumeration value="Design B2B-Filing" />
                <xs:enumeration value="Design Information" />
                <xs:enumeration value="Design Identifier List" />
                <xs:enumeration value="Design List" />
                <xs:enumeration value="Design Opposition E-Filing" />
                <xs:enumeration value="Design Opposition Information" />
                <xs:enumeration value="Design Opposition Identifier List" />
                <xs:enumeration value="Representative Appointment" />
                <xs:enumeration value="Amendment Registered Design" />
                <xs:enumeration value="Amendment Application" />
                <xs:enumeration value="Convention Application" />
                <xs:enumeration value="Additional Application" />
                <xs:enumeration value="Subsequent Application" />
                <xs:enumeration value="Divisional Application" />
                <xs:enumeration value="Assignment Application" />
                <xs:enumeration value="Assignment Registered Design" />
                <xs:enumeration value="Request Certified Copies" />
                <xs:enumeration value="Request Extension Time" />
                <xs:enumeration value="Request Hearing" />
                <xs:enumeration value="Opposition First Extension" />
                <xs:enumeration value="Opposition Various Renewal/Extension of Registration Restoration" />
                <xs:enumeration value="Withdrawal" />
                <xs:enumeration value="Withdraw Application" />
                <xs:enumeration value="Withdraw Design" />
                <xs:enumeration value="Response Examination Report" />
                <xs:enumeration value="Statement Support" />
                <xs:enumeration value="Withdraw Service Request" />
                <xs:enumeration value="Request Publication" />
                <xs:enumeration value="Request Registration" />
                <xs:enumeration value="Offer Surrender" />
                <xs:enumeration value="Request Exclusion" />
                <xs:enumeration value="Request Conversion" />
                <xs:enumeration value="Duplicate Certificate" />
                <xs:enumeration value="Request Supply Documents" />
                <xs:enumeration value="Request Delayed Registration" />
                <xs:enumeration value="Request Accelerated Examination" />
                <xs:enumeration value="Expiry Notice" />
                <xs:enumeration value="Refusal Withdrawal Notification" />
                <xs:enumeration value="Division International Registration Notification" />
                <xs:enumeration value="Refusal Notification" />
                <xs:enumeration value="Providing Certificate To Holder" />
                <xs:enumeration value="Transmittal Application To IB" />
                <xs:enumeration value="Irregularities" />
                <xs:enumeration value="Invalidation Notification" />
                <xs:enumeration value="Cancellation Representative Appointment" />
                <xs:enumeration value="Application Receipt" />
                <xs:enumeration value="Notice Document By Design Formality Examination" />
                <xs:enumeration value="Notice Document By Design Substantial Examination" />
                <xs:enumeration value="Notice Document By Design Registration" />
                <xs:enumeration value="Notice Document By Trial Examination - Invitation" />
                <xs:enumeration value="Notice Document By Trial Examination" />
                <xs:enumeration value="Notice Document By Trial Examination - Decision" />
                <xs:enumeration value="Memo By Design Formality Examination" />
                <xs:enumeration value="Memo By Design Substantial Examination" />
                <xs:enumeration value="Memo By Trial Examination" />
                <xs:enumeration value="Memo By Trial Notice" />
                <xs:enumeration value="Memo By Trial Examination - Correction" />
                <xs:enumeration value="Accept Design Application Document" />
                <xs:enumeration value="Accept Design Applicant Document" />
                <xs:enumeration value="Accept Design Attorney Document" />
                <xs:enumeration value="Accept Design Withdrawal And Abandonment Document" />
                <xs:enumeration value="Accept Design Response Document" />
                <xs:enumeration value="Accept Design Submission Document" />
                <xs:enumeration value="Accept Design Amendment Document" />
                <xs:enumeration value="Accept Design Request For Inspection" />
                <xs:enumeration value="Accept Demand For A Trial" />
                <xs:enumeration value="Accept Trial Intermediate Document" />
                <xs:enumeration value="Inspection Accept Design Application Document" />
                <xs:enumeration value="Inspection Accept Design Applicant Document" />
                <xs:enumeration value="Inspection Accept Design Attorney Document" />
                <xs:enumeration value="Inspection Accept Design Withdrawal And Abandonment Document" />
                <xs:enumeration value="Inspection Accept Design Response Document" />
                <xs:enumeration value="Inspection Accept Design Submission Document" />
                <xs:enumeration value="Inspection Accept Design Amendment Document" />
                <xs:enumeration value="Inspection Accept Design Request For Inspection" />
                <xs:enumeration value="Inspection Accept Demand For A Trial" />
                <xs:enumeration value="Inspection Accept Trial Intermediate Document" />
                <xs:enumeration value="Inspection Notice Document By Design Formality Examination" />
                <xs:enumeration value="Inspection Notice Document By Design Substantial Examination" />
                <xs:enumeration value="Inspection Notice Document By Design Registration" />
                <xs:enumeration value="Inspection Notice Document By Trial Examination - Invitation" />
                <xs:enumeration value="Inspection Notice Document By Trial Examination" />
                <xs:enumeration value="Inspection Notice Document By Trial Examination - Decision" />
                <xs:enumeration value="Inspection Memo By Design Formality Examination" />
                <xs:enumeration value="Inspection Memo By Design Substantial Examination" />
                <xs:enumeration value="Inspection Memo By Trial Examination" />
                <xs:enumeration value="Inspection Memo By Trial Notice" />
                <xs:enumeration value="Certified Copy Notice Document By Design Formality Examination" />
                <xs:enumeration value="Certified Copy Notice Document By Design Substantial Examination" />
                <xs:enumeration value="Certified Copy Notice Document By Design Registration" />
                <xs:enumeration value="Certified Copy Notice Document By Trial Examination - Invitation" />
                <xs:enumeration value="Certified Copy Notice Document By Trial Examination" />
                <xs:enumeration value="Certified Copy Notice Document By Trial Examination - Decision" />
                <xs:enumeration value="Accept Document List" />
                <xs:enumeration value="Notice Document List" />
                <xs:enumeration value="Application Information" />
                <xs:enumeration value="Balance Sheet Reference" />
                <xs:enumeration value="Division Number List" />
                <xs:enumeration value="Record Licence" />
                <xs:enumeration value="Record Mortgagee" />
                <xs:enumeration value="Terminate Licence" />
                <xs:enumeration value="Discharge Mortgagee" />
                <xs:enumeration value="Change of Name" />
            </xs:restriction>
        </xs:simpleType>
    </xs:union>
</xs:simpleType>

使用JAXB,生成的输出(见下文)将转换为Java中的基本类型String。我使用的是org.jvnet.jaxb2.maven2中最新版本的maven-jaxb2-plugin。在某些时候,这是我的预期。因此,我希望能够从Java代码中访问XSD中定义的可能值列表。但是怎么样? JAXB没有生成定义所有这些值的枚举类。我在网上搜索过,包括在这个网站下搜索解决方案;尝试一些建议的解决方案,包括绑定的使用,但没有任何成功。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "transactionIdentifier",
    "transactionCode",
})
public static class TransactionContentDetails implements Serializable  {

    @XmlElement(name = "TransactionIdentifier")
    protected String transactionIdentifier;
    @XmlElement(name = "TransactionCode", required = true)
    protected String transactionCode;

    public String getTransactionIdentifier() {
        return transactionIdentifier;
    }

    public void setTransactionIdentifier(String value) {
        this.transactionIdentifier = value;
    }

    public String getTransactionCode() {
        return transactionCode;
    }

    public void setTransactionCode(String value) {
        this.transactionCode = value;
    }
}

所以,我想看看你是否对如何解决这个问题有任何想法,即能够生成包含所有这些值的Java枚举类。作为解决方案的一部分,我不想在绑定文件中重新定义所有这些值。

谢谢!

3 个答案:

答案 0 :(得分:1)

看来工会得不到很好的支持https://jaxb.java.net/tutorial/section_2_2_10-Defining-a-Type-Union.html

如果你可以修改模式,你可以将枚举的匿名简单类型提取到命名类型,然后在你的代码中使用为这种新类型生成的Java枚举....

答案 1 :(得分:0)

您可以更改架构以添加名称,然后生成名称“designEnum”的pojo。

  <xsd:simpleType name="designEnum">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Design1"/>
            <xsd:enumeration value="Design2"/>
        </xsd:restriction>
    </xsd:simpleType>

// Enum

import javax.xml.bind.annotation.*;
@XmlType(name = "designEnum")
@XmlEnum
public enum DesignEnum {

    Design1,
    Design1;

    public String value() {
        return name();
    }

    public static DesignEnum fromValue(String v) {
        return valueOf(v);
    }

}

答案 2 :(得分:0)

xsd限制不是枚举,如果使用枚举,则会遇到编组问题。您可以向jaxb代码生成器添加自定义行为,并在setTransactionCode方法上添加验证。

请看这篇文章https://jaxb.java.net/guide/Adding_behaviors.html