JAXB不为受限元素创建枚举

时间:2014-09-12 13:06:11

标签: enums jaxb xsd restriction

我的xsd和jaxb需要一些帮助。

我的xsd导致工作类(MyWorkingType)和'ProblemType',它不是用枚举生成的。

我相信,这两种形式都有效,为什么这不起作用,哪里有bug(jaxb或键盘?)

编辑:我添加了我的ant脚本,因为Xtian可以通过maven-plugin创建它。

这是我的xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="ProblemType">
        <xs:sequence>
            <xs:element minOccurs="0" name="normalAttribute" nillable="true" type="xs:boolean" />
            <xs:element minOccurs="1" maxOccurs="1" name="enumAttribute">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="VALUE_1" />
                        <xs:enumeration value="VALUE_2" />
                        <xs:enumeration value="VALUE_3" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="workingEnum">
        <xs:restriction base="xs:string">
            <xs:enumeration value="VALUE_1" />
            <xs:enumeration value="VALUE_2" />
            <xs:enumeration value="VALUE_3" />
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="MyWorkingType">
        <xs:sequence>
            <xs:element minOccurs="0" name="normalAttribute" nillable="true" type="xs:boolean" />
            <xs:element minOccurs="1" maxOccurs="1" name="enumAttribute" type="workingEnum" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

这是我在eclipse中使用的蚂蚁脚本:

<?xml version="1.0" encoding="UTF-8"?>
<project name="generate" default="generate-jaxb" basedir=".">
    <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
        <classpath>
            <fileset dir="../lib" includes="*.jar" />
        </classpath>
    </taskdef>

    <target name="generate-jaxb">
        <xjc destdir="../../src-gen" package="test.gen">
            <arg value="-no-header" />
            <schema dir="xsd/" includes="*.xsd"/>
        </xjc>
    </target>
</project>

2 个答案:

答案 0 :(得分:2)

尝试指定typesafeEnumBase。这是我在生成映射时通常所做的事情:

<jaxb:bindings schemaLocation="mySchema.xsd" 
    node="/xs:schema">
    <jaxb:globalBindings
        fixedAttributeAsConstantProperty="false"
        typesafeEnumBase="xs:string"
        typesafeEnumMemberName="generateName" 
        generateIsSetMethod="true">
        <xjc:noValidator />
        <xjc:noValidatingUnmarshaller />
    </jaxb:globalBindings>

    <jaxb:schemaBindings>
        <jaxb:package name="my.package.name"/>
    </jaxb:schemaBindings>

</jaxb:bindings>

但我不认为这是原因。

由于我的maven-jaxb2-plugin有效,xjc通过Ant无效,问题是,有什么区别。 实际上,strictverboseextension设置对枚举没有任何区别。

然而,maven-jaxb2-plugin可能会使用较新版本的xjc,而不是使用枚举。请尝试更新xjc并查看是否有帮助。

答案 1 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="ProblemType">
        <xs:sequence>
            <xs:element minOccurs="0" name="normalAttribute" nillable="true" type="xs:boolean" />
            <xs:element minOccurs="1" maxOccurs="1" name="enumAttribute">
                <xs:simpleType name="problemTypeEnum">
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="VALUE_1" />
                        <xs:enumeration value="VALUE_2" />
                        <xs:enumeration value="VALUE_3" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="workingEnum">
        <xs:restriction base="xs:string">
            <xs:enumeration value="VALUE_1" />
            <xs:enumeration value="VALUE_2" />
            <xs:enumeration value="VALUE_3" />
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="MyWorkingType">
        <xs:sequence>
            <xs:element minOccurs="0" name="normalAttribute" nillable="true" type="xs:boolean" />
            <xs:element minOccurs="1" maxOccurs="1" name="enumAttribute" type="workingEnum" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

我通过这个maven插件生成了这个类

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.9.0</version>
                <executions>
                    <execution>
                        <id>commun-generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <generateDirectory>${basedir}/src/main/java/</generateDirectory>
                            <schemaDirectory>${basedir}/src/main/resources/schema/xsd</schemaDirectory>
                            <strict>false</strict>
                            <extension>true</extension>
                            <verbose>true</verbose>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

ProblemTypeEnum.java

@XmlType(name = "problemTypeEnum")
@XmlEnum
public enum ProblemTypeEnum {

    VALUE_1,
    VALUE_2,
    VALUE_3;

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

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

}

如果你可以改变结构..这是xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="ProblemType">
        <xs:sequence>
            <xs:element minOccurs="0" name="normalAttribute" nillable="true"
                type="xs:boolean" />
            <xs:element minOccurs="1" maxOccurs="1" name="enumAttribute"
                type="problemTypeEnum" />
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="problemTypeEnum">
        <xs:restriction base="xs:string">
            <xs:enumeration value="VALUE_1" />
            <xs:enumeration value="VALUE_2" />
            <xs:enumeration value="VALUE_3" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="workingEnum">
        <xs:restriction base="xs:string">
            <xs:enumeration value="VALUE_1" />
            <xs:enumeration value="VALUE_2" />
            <xs:enumeration value="VALUE_3" />
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="MyWorkingType">
        <xs:sequence>
            <xs:element minOccurs="0" name="normalAttribute" nillable="true"
                type="xs:boolean" />
            <xs:element minOccurs="1" maxOccurs="1" name="enumAttribute"
                type="workingEnum" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>