JAXB - XJC - Mapping XML Encryption schema into Java classes

Viewed 49

I'm trying to map the XML Schema for XML Encryption into Java classes to be used with JAXB (https://www.w3.org/TR/xmlenc-core/xenc-schema.xsd), using XJC. I am a bit puzzled by the mapping of the EncryptionMethodType type. In the schema, it is defined as

  <complexType name='EncryptionMethodType' mixed='true'>
    <sequence>
      <element name='KeySize' minOccurs='0' type='xenc:KeySizeType'/>
      <element name='OAEPparams' minOccurs='0' type='base64Binary'/>
      <any namespace='##other' minOccurs='0' maxOccurs='unbounded'/>
    </sequence>
    <attribute name='Algorithm' type='anyURI' use='required'/>
  </complexType>

XJC generates the following type (comments removed for brevity):

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "EncryptionMethodType", propOrder = {
    "content"
})
public class EncryptionMethodType {

    @XmlElementRefs({
        @XmlElementRef(name = "KeySize", namespace = "http://www.w3.org/2001/04/xmlenc#", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "OAEPparams", namespace = "http://www.w3.org/2001/04/xmlenc#", type = JAXBElement.class, required = false)
    })
    @XmlMixed
    @XmlAnyElement(lax = true)
    protected List<Object> content;

    @XmlAttribute(name = "Algorithm", required = true)
    @XmlSchemaType(name = "anyURI")
    protected String algorithm;

    /**
     * Objects of the following type(s) are allowed in the list
     * {@link JAXBElement }{@code <}{@link BigInteger }{@code >}
     * {@link JAXBElement }{@code <}{@link byte[]}{@code >}
     * {@link String }
     * {@link Object }
     */
    public List<Object> getContent() {
        if (content == null) {
            content = new ArrayList<Object>();
        }
        return this.content;
    }

    public String getAlgorithm() {
        return algorithm;
    }

    public void setAlgorithm(String value) {
        this.algorithm = value;
    }
}

I am not a JAXB expert (and the documentation is sparse and mostly terrible), but since the KeySize and the OAEPparams elements have cardinality 0 or 1, I was expecting them to become separate properties in the Java code, leaving the List<Object> for the unbounded ##other elements.

Is there any parameter or custom bindings I can apply to get a simpler Java representation, i.e. like this one?

class EncryptionMethodType {
   public BigInteger KeySize;
   public byte[] OAEPparams;
   public List<Object> otherContent;
}

Thanks in advance!

0 Answers
Related