This discussion is archived
4 Replies Latest reply: Jan 13, 2012 6:02 AM by 910697 RSS

JAXB Inserting Underscores

910697 Newbie
Currently Being Moderated
Hi Everyone,

I am using jaxws to generate JAXB objects for an enumeration and am having an issue with it inserting extra underscores into the generated code and was wondering if anyone knew how to prevent this from happening. Specifically, I have an enum defined like this:
<simpleType name ="DataWidthEnum">
  <restriction base="string">
    <enumeration value="A8_BIT_DATA_BUS"/>
    <enumeration value="A16_BIT_DATA_BUS"/>
  </restriction>
</simpleType>
but my generated enum class has attributes like this:
  @XmlEnumValue("A8_BIT_DATA_BUS")
  A_8_BIT_DATA_BUS("A8_BIT_DATA_BUS"),
  @XmlEnumValue("A16_BIT_DATA_BUS")
  A_16_BIT_DATA_BUS("A16_BIT_DATA_BUS")
Is it possible to stop the underscore insertion happening between the A and 8 or A and 16?

Thanks in advance for any help!

Edited by: user3989028 on Jan 12, 2012 11:16 AM
  • 1. Re: JAXB Inserting Underscores
    jtahlborn Expert
    Currently Being Moderated
    Those values don't resemble each other in the slightest (e.g. "A8_BIT_DATA" and "A8_BIT_DATA_BUS"). are you sure you are looking at the right files?
  • 2. Re: JAXB Inserting Underscores
    910697 Newbie
    Currently Being Moderated
    Sorry I mistyped my XML in the original post. I have corrected it.
  • 3. Re: JAXB Inserting Underscores
    jtahlborn Expert
    Currently Being Moderated
    That's very weird, i've never seen jaxb insert extra '_' like that. that said, you can control the enum name using the jaxb extensions like so:
    <schema xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.0">
    
    <simpleType name ="DataWidthEnum">
    
              <annotation>
                <appinfo>
                    <jxb:typesafeEnumClass>
                        <jxb:typesafeEnumMember name="A8_BIT_DATA_BUS" value="A8_BIT_DATA_BUS" />
                        <jxb:typesafeEnumMember name="A16_BIT_DATA_BUS" value="A16_BIT_DATA_BUS" />
                    </jxb:typesafeEnumClass>
                </appinfo>
            </annotation>
    
      <restriction base="string">
        <enumeration value="A8_BIT_DATA_BUS"/>
        <enumeration value="A16_BIT_DATA_BUS"/>
      </restriction>
    </simpleType>
    
    </xs:schema>
  • 4. Re: JAXB Inserting Underscores
    910697 Newbie
    Currently Being Moderated
    Thanks jtahlborn that looks like what I was looking for.

    On another note, looking at the generated code more closely (like I probably should have done in the first place), I notice that the original name used in the schema is kept in the enum and accessible via a generated value() method (equivalent to the name() method if the extra underscore hadn't been inserted) and is reversible from a fromValue() method (equivalent to the valueOf() method if the extra underscore hadn't been inserted.) I also see that these methods are generated in other classes where the underscore insertion isn't done, but just as wrappers of the name() and valueOf() methods instead. As a result I can just use the value() fromValue methods everywhere in dealing with generated enums and my issue will be addressed.

    Thanks everyone for your input!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points