views:

317

answers:

1

We are using JAXB to handle our wsdl validation. In the wsdl we have an enumeration like so:

<xs:simpleType name="myEnum">
    <xs:annotation>
        <xs:documentation>The enums which matter.</xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
        <xs:enumeration value="MYENUM_1">
            <xs:annotation>
                <xs:documentation>MYENUM_1 is the first enum.</xs:documentation>
            </xs:annotation>
        </xs:enumeration>
        <xs:enumeration value="MYENUM_2">
            <xs:annotation>
                <xs:documentation>MYENUM_2 is the second enum.</xs:documentation>
            </xs:annotation>
        </xs:enumeration>
    </xs:restriction>
</xs:simpleType>

If we pass in an invalid string as an enum for example, MYENUM_7, the value is set to null, instead of throwing an error like we would expect. Digging into the code, we find the following from RuntimeEnumLeafInfoImpl.java:

package com.sun.xml.bind.v2.model.impl;
//...
public T parse(CharSequence lexical) throws AccessorException, SAXException {
    // TODO: error handling

    B b = baseXducer.parse(lexical);
    if(b==null) {

        return null;
    }

    return parseMap.get(b);
}

It is clear to see that the parseMap is our list of enums, but if the key to the map, in this case the value of b, is not in the map it just returns null. We would like it to throw and exception of some sort if b is not in parseMap.

Short of fixing this and recompiling our own JAXB is there some other way to solve this?


EDIT: for clarification

We are using JAXB 2.1.9, and we want to unmarshall the data AND validate it. It is certainly possible I have overlooked something in the documentation about validation.

A: 

You are saying that you are using JAXB to validate data, but you are only describing an unexpected result after unmarshalling an XML document. In JAXB, validation and unmarshalling are two different and perhaps independent issues, since you can use JAXB in at least three different modes:

  • don't validate
  • validate, report errors, but continue unmarshalling
  • validate and break on errors

Are you sure that you've enabled schema validation on your unmarshaller and that you are evaluating potential errors? How to do that is rather different, depending on if you're using JAXB 1.0 or 2.0, but the documentation should tell you how.

jarnbjo
Thanks for the answer. First we are using JAXB 2.1.9 I updated my original post to reflect that. Second we want the third option you have listed. We have not seen that in the documentation, but this is all new to us, so even a link to that would help if you have it.
grieve
It is explained in the JavaDocs for the Unmarshaller class in section "Validation and Well-Formedness".
jarnbjo
Adding the link: https://jaxb.dev.java.net/nonav/jaxb20-pfd/api/javax/xml/bind/Unmarshaller.html
grieve