2012-09-07 148 views
0

我在XSD枚舉如下:JAXB綁定文件:枚舉整數ID

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

使用這個模式和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 
    } 
} 

我想找出JAXB綁定代碼來實現上面的Java枚舉。謝謝。

回答

0

下面是我在網絡上的一些研究後可以找到的:我不認爲你可以生成像我在問題中有一個的枚舉。你按你想要的方式編寫你的枚舉,並告訴xjc插件在生成的代碼中使用該枚舉。

你首先需要解釋做什麼用你的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> 

下一頁寫打印和分析方法作爲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(); 
    } 
} 

現在,您將需要引用binding.xml文件在你的XJC工具。有很多方法可以做到這一點,但在這裏我使用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>