3

is there a way to get comments from XSD to Java code using wsimport? For example, I have an XSD file

<!-- Enumerace /model/user/UserLevel.java -->
<xs:simpleType name="userLevel">
    <xs:restriction base="xs:string">
        <!-- basic user -->
        <xs:enumeration value="BASE"/>
        <!-- team leader -->
        <xs:enumeration value="TL"/>
        <!-- section leader -->
        <xs:enumeration value="SL"/>
    </xs:restriction>
</xs:simpleType>

and I want my generated java enum class to look something like this:

@XmlType(name = "userLevel")
@XmlEnum
public enum UserLevel {
    /**
     * basic user
     */
    BASE,
    /**
     * team leader
     */
    TL,
    /**
     * section leader
     */
    SL;
}

Is this even possible in contract first (eg. java code generated from XSD)?

Ondrej Skalicka
  • 2,839
  • 9
  • 28
  • 50

1 Answers1

1

Ok, I found a solution, this in XSD:

<xs:simpleType name="MyEnum">
    <xs:restriction base="xs:string">
        <xs:enumeration value="STANDARD">
            <xs:annotation>
                <xs:documentation>
                    This is a comment.
                </xs:documentation>
            </xs:annotation>
        </xs:enumeration>
    </xs:restriction>
</xs:simpleType>

produces a Java enum such as:

@XmlType(name = "MyEnum")
@XmlEnum
public enum MyEnum {


    /**
     * 
     *                         This is a comment.
     *                     
     * 
     */
    STANDARD,

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

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

}

The only problem is that xs:documentation does not ignore whitespace so there is a lot of blank space in the comments.

Ondrej Skalicka
  • 2,839
  • 9
  • 28
  • 50
  • 1
    Did you try adding the attribute `xml:space="default"` to your `` tag? Also, info from this question might help: http://stackoverflow.com/questions/1650249/how-to-make-generated-classes-contain-javadoc-from-xml-schema-documentation – Joachim Sauer Mar 18 '11 at 07:46
  • @JoachimSauer `xml:space="default"` does not work, wsimport simply ignores it. I've tried the `jxb:javadoc` mentioned in the other thread, but wsimport ended with errors ("[ERROR] JAXB version attribute must be present"). I'll look into it as soon as I have some time. Thanks for the link anyways – Ondrej Skalicka Mar 18 '11 at 10:04