2

I need some help with my xsd and jaxb.

My xsd leads to an working class (MyWorkingType) and the 'ProblemType', which is not generated with an enum.

As I believe, that both forms are valid, why does this not work, where is the bug (jaxb or at the keyboard?)

Edit: I add my ant-script, because Xtian could create this via maven-plugin.

This is my 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>

This is my ant-script I use within 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>
Sammy
  • 1,178
  • 1
  • 14
  • 27
  • Are you asking _why_ creating an anonymous type doesn't create an enum or why _your_ schema definition create an enum field in ProductType? – Paul Samsotha Sep 12 '14 at 13:32
  • Sry, I am asking, why ProblemType is not created with an enum. – Sammy Sep 12 '14 at 13:53
  • Sadly, some comments here were deleted. They helped me with two links, please add again if possible! Thx – Sammy Sep 12 '14 at 14:20
  • 1
    [link1](http://stackoverflow.com/q/586224/2587435), [link2](https://jaxb.java.net/tutorial/section_2_3_1-Hints-on-Writing-XML-Schemas.html#Don%27t%20Use%20Anonymous%20Types) – Paul Samsotha Sep 12 '14 at 14:35

2 Answers2

2

Try specifying typesafeEnumBase. This is what I normally do when generating mappings:

<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>

However I don't think this is the cause.

Since my maven-jaxb2-plugin works and xjc via Ant does not, the question, what is the difference. Actually, strict, verbose and extension settings should make no difference for enums.

However it may be that maven-jaxb2-plugin uses a newer version of xjc than you're using with enum. Please try to update your xjc and see if it helps.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • I will keep both in mind (typesafeEnumBase and xjc-update). As I am using a pre-configured workspace and my time is running out, I will change the xsd not to use anonymous enums. – Sammy Sep 15 '14 at 07:21
  • OK. Using JAXB-2.2.7-Jar's didn't help :-( – Sammy Sep 15 '14 at 07:55
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>

I generated the class by this maven plugin

            <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);
    }

}

If you can change the structure .. this is the 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>
Xstian
  • 8,184
  • 10
  • 42
  • 72
  • OK. I use an ant-sxript in eclipse without having maven installed (not my decision). I will add this in my question, maybe there is something wrong. Thx so far! – Sammy Sep 12 '14 at 13:55
  • I added an attribute name at your inner complex type .. name="problemTypeEnum". Have you added? – Xstian Sep 12 '14 at 13:57
  • I tried that before and now again: [xjc] [ERROR] s4s-att-not-allowed: Attribute 'name' cannot appear in element 'simpleType'... Could you please try with strict on? – Sammy Sep 12 '14 at 14:02
  • Yes with strict=true does not generates the enum, but will generate if you use the second xsd in my answer. – Xstian Sep 12 '14 at 14:08
  • OK. Just to be sure. The reason, that your 2nd xsd works, maybe, that both (ProblemType and WorkingType) use the same working solution ;-) – Sammy Sep 12 '14 at 14:12
  • 1
    yes is the same solution. Maven allow a workaround, but the good solution is the second. :) – Xstian Sep 12 '14 at 14:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61119/discussion-between-xstian-and-sammy). – Xstian Sep 12 '14 at 14:32