The problem:

Based on the following documentation provided by Oracle about the use of the java.util.Locale: [[Internationalization: Understanding Locale in the Java Platform]](http://www.oracle.com/us/technologies/java/locale-140624.html) , I have the following question related to JAXB and the Locale.

I have an XML file that looks like this:

```
<?xml version="1.0" encoding="utf-8"?>
<dataschema>
  <delimited>
    <locale language="en" country="US" variant="SiliconValley" />
  </delimited>
</dataschema>

```

Which is based on the following XML schema:

```
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="dataschema">
        <xs:complexType>
            <xs:choice>
                <xs:element minOccurs="1" maxOccurs="1" name="delimited" type="DelimitedSchemaType"/>
                <xs:element minOccurs="1" maxOccurs="1" name="fixedwidth" type="FixedWidthSchemaType"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="DelimitedSchemaType">
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="locale" type="LocaleType"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="FixedWidthSchemaType">
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="locale" type="LocaleType"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="LocaleType">
        <xs:attribute name="language" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="[a-z]{2,3}"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="country" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="[A-Z]{2}"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="variant" use="optional">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="[A-Z]{2}"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:schema>

```

Now the problem is that I get the following generated classes for the LocaleType xml complexType which does not seem to reflect the actual java.util.Locale datatype within the generated DelimitedDataSchema class. I would have expected this to be of type java.util.Locale and NOT of type org.mylib.schema.LocaleType?

The generated classes by JAXB 2.x are:

**Dataschema.java:**

```
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "delimited",
    "fixedwidth"
})
@XmlRootElement(name = "dataschema")
public class Dataschema {

    protected DelimitedDataSchema delimited;
    protected FixedWidthDataSchema fixedwidth;

    public DelimitedDataSchema getDelimited() {
        return delimited;
    }

    public void setDelimited(DelimitedDataSchema value) {
        this.delimited = value;
    }

    public FixedWidthDataSchema getFixedwidth() {
        return fixedwidth;
    }

    public void setFixedwidth(FixedWidthDataSchema value) {
        this.fixedwidth = value;
    }
}

```

**DelimitedDataSchema.java:**

```
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DelimitedSchemaType", propOrder = {
    "localeType"
})
public class DelimitedDataSchema {

    @XmlElement(required = true)
    protected LocaleType locale;

    public LocaleType getLocale() {
        return locale;
    }

    public void setLocale(LocaleType value) {
        this.locale = value;
    }
}

```

**LocaleType:**

```
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LocaleType")
public class LocaleType {

    @XmlAttribute(name = "language", required = true)
    protected String language;
    @XmlAttribute(name = "country", required = true)
    protected String country;
    @XmlAttribute(name = "variant")
    protected String variant;

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String value) {
        this.language = value;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String value) {
        this.country = value;
    }

    public String getVariant() {
        return variant;
    }

    public void setVariant(String value) {
        this.variant = value;
    }
}

```

I bravely followed the instructions in the following blog posts from Blaise Doughan about JAXB XmlAdapters: [JAXB and Package Level XmlAdapters](http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html) and also [XmlAdapter - JAXB's Secret Weapon](http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html)

So I created an XmlAdapter myself, hoping that the generated class (DelimitedDataSchema) would contain the java.util.Locale return datatype in the getter and the java.util.Locale parameter datatype in the setter. Which I mistakenly assumed. 

**LocaleXmlAdapter.java:**

```
public class LocaleXmlAdapter extends XmlAdapter<org.mylib.schema.LocaleType, java.util.Locale> {
    @Override
    public java.util.Locale unmarshal(org.mylib.schema.LocaleType pSchemaLocale) throws Exception {
        if (pSchemaLocale == null) {
            throw new NullPointerException("LocaleXmlAdapter.unmarshal(...) received a NULL literal.");
        }

        java.util.Locale mLocale = null;
        String mLanguage = pSchemaLocale.getLanguage().toLowerCase();
        String mCountry = pSchemaLocale.getCountry().toUpperCase();
        String mVariant = pSchemaLocale.getVariant();

        if (mVariant == null) {
            mLocale = new java.util.Locale(mLanguage, mCountry);
        } else {
            mLocale = new java.util.Locale(mLanguage, mCountry, mVariant);
        }
        return mLocale;
    }

    @Override
    public org.mylib.schema.LocaleType marshal(java.util.Locale pJavaLocale) throws Exception {
        if (pJavaLocale == null) {
            throw new NullPointerException("LocaleXmlAdapter.marshal(...) received a NULL literal.");
        }

        org.mylib.schema.LocaleType mLocale = new org.mylib.schema.LocaleType();
        mLocale.setLanguage(pJavaLocale.getLanguage().toLowerCase());
        mLocale.setCountry(pJavaLocale.getCountry().toUpperCase());
        String mVariant = pJavaLocale.getVariant();
        if (mVariant != null) {
            mLocale.setVariant(mVariant);
        }

        return mLocale;
    }
}

```

To let the JAXB library know that it must use the LocaleXmlAdapter, I provided the library with an external binding file, in which the LocaleXmlAdapter is defined for the Locale class.

External JAXB binding file:

```
<?xml version="1.0" encoding="utf-8"?>
<jaxb:bindings jaxb:version="2.1" xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               schemaLocation="dataschema.xsd" node="/xs:schema">

    <jaxb:schemaBindings>
        <jaxb:package name="org.mylib.schema">
            <jaxb:javadoc>
                Package level documentation for generated package org.mylib.schema.
            </jaxb:javadoc>
        </jaxb:package>
    </jaxb:schemaBindings>

    <jaxb:bindings node="//xs:complexType[@name='LocaleType']">
        <jaxb:class name="LocaleType"/>
        <jaxb:property>
            <jaxb:baseType name="org.mylib.schema.LocaleXmlAdapter"/>
        </jaxb:property>
    </jaxb:bindings>

    <jaxb:bindings node="//xs:complexType[@name='DelimitedSchemaType']">
        <jaxb:class name="DelimitedDataSchema"/>
    </jaxb:bindings>

    <jaxb:bindings node="//xs:complexType[@name='FixedWidthSchemaType']">
        <jaxb:class name="FixedWidthDataSchema"/>
    </jaxb:bindings>
</jaxb:bindings>

```

Now the weird part, which I obviously don't get, is that I would have expected that the JAXB library would translate the org.mylib.schema.LocaleType type into the java.util.Locale type for the DelimitedDataSchema class, so you would see the following method signatures in the DelimitedDataSchema class:

* public java.util.Locale getLocale() {}
* public void setLocale(java.util.Locale value) {}

What I want to accomplish is that the java.util.Locale datatype is used instead of the org.mylib.schema.LocaleType datatype. How else do I get the translation done between the user code and the JAXB generated code? I can't call the LocaleXmlAdapter class myself to translate the locale type for me, that must be done by the JAXB library, but I do want to call: getLocale() and in return get a java.util.Locale datatype.

What am I doing 'wrong'?

**Update:**

So far I figured out that the <**jaxb**:baseType /> should NOT be used. Instead the <**xjc**:javaType > should be used within the binding file as a child element of <**jaxb**:baseType>.
I also falsely assumed that the <**jaxb**:baseType> had to be defined under the LocaleType node, which is NOT true. It must be defined under the element node of the DelimitedSchemaType node and FixedWidthSchemaType node. Like this:

```
...
<jaxb:bindings node="//xs:complexType[@name='DelimitedSchemaType']">
    <jaxb:property>
        <jaxb:baseType>
            <xjc:javaType name="org.mylib.schema.LocaleType" adapter="org.mylib.schema.LocaleXmlAdapter"/>
        </jaxb:baseType>
    </jaxb:property>
</jaxb:bindings>
...

```

This should be correct, but somehow the XJC compiler produced compile errors.
The following error occurs:

```
[ERROR] Error while parsing schema(s).Location [ file:/C:/IdeaProjects/JaxbMarshalling/src/main/resources/dataschema.xjb{25,113}].
com.sun.istack.SAXParseException2; systemId: file:/C:/IdeaProjects/JaxbMarshalling/src/main/resources/dataschema.xjb; lineNumber: 25; columnNumber: 113; compiler was unable to honor this conversion customization. It is attached to a wrong place, or its inconsistent with other bindings.
    at com.sun.tools.xjc.ErrorReceiver.error(ErrorReceiver.java:86)
    etc.

```

It keeps nagging about **"compiler was unable to honor this conversion customization. It is attached to a wrong place, or its inconsistent with other bindings."**, while there is no mistake within the bindings file to be found.

I have improved my bindings file, but still something isn't 'right'. I can't pinpoint the exact location where it goes 'wrong'.

BTW: I am using the following tools:

* Oracle Java JDK 64-bit, version 1.8.0\_112-b15
* xjc, version 2.2.8-b130911.1802 (shipped with above mentioned JDK)
* maven3, version 3.3.9
* IntelliJ IDEA 2016.3, version 163.7743.44
* maven-jaxb2-plugin, version 0.13.1

Because I am struggling with this for a few days now, I have started a bounty. The person that **really** solves the problem using the external bindings file and correct annotations gets my bounty points.