问题
I've a lot of difficulties generating some POJOs from an XSD (distributed as a standard that I don't have control and I cannot changed) using JAXB. The problem seems to be related to the way certain types are defined in that XSD. Below I've pasted a simple type defined in this XSD named TransactionCodeType which is a union of a token combined with an enumeration list of values, also defined as a token.
<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>
Using JAXB, the output generated (see below) is translate as the basic type String in Java. I'm using the latest version of maven-jaxb2-plugin from org.jvnet.jaxb2.maven2. At some point, this is what I expected. Therefore, I would like to be able to access from the Java code the list of possible values that are defined within the XSD. But how ? JAXB doesn't generated a enumeration class that defined all these values. I've searched on the web including under this site to find solutions; try some of the proposed solutions including the usage of bindings without any success.
@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;
}
}
So, I’m looking to see if you have any ideas on how I could resolve that problem, i.e., being able to generate a Java enumeration class containing all these values. As part of the solution, I don't want to have to redefined all these values within a binding file.
Thanks!
回答1:
It looks like unions are not well supported https://jaxb.java.net/tutorial/section_2_2_10-Defining-a-Type-Union.html
If you could modify the schema you could extract the enumerated anonymous simple type to a named type and then use in your code the Java enum generated for this new type....
回答2:
You can change your schema to add name then generate the pojo of Name "designEnum".
<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);
}
}
回答3:
xsd restrictions aren't an enum, if you use enum you will have problem with marshalling. You can add a custom behavior to jaxb code generator and add validation on setTransactionCode method.
Look at this article https://jaxb.java.net/guide/Adding_behaviors.html
来源:https://stackoverflow.com/questions/30892934/cannot-generated-enumeration-class-using-jaxb