Hi,

I'm wondering, is it possible to marshall/unmarshall java.util.Locale
objects? I know they don't follow the JavaBean pattern in that they Follow
more of the Enum pattern (only providing getters and can only be constructed
with constructor params). With that being said, I tried the following code
and mappings and while the marshalling appears to write the data out, when
unmarshalling the object back in, I always get null. Almost like the Locale
object is never being created and added to the collection. I saw a similar
posting about the java.awt.Color object but never saw a solution either.

If I print out the Config object before marshalling, I see the Locale
objects in the list, after I unmarshal the object back My list is empty..

// Output
Before unmarshalling:AdminConfig{locales=[en_US, fr_FR]} After
unmarshalling: AdminConfig{locales=[]}


// Mapping file
<mapping>
    <class name="AdminConfig">
        <map-to xml="admin-config"/>

        <field name="locales" type="java.util.Locale"
collection="collection">
            <bind-xml name="locale" location="locales"/>
        </field>
    </class>

    <!-- Map a Locale object -->
    <class name="java.util.Locale" verify-constructable="false">
        <field name="language" set-method="%1" type="string">
            <bind-xml node="attribute"/>
        </field>
        <field name="country" set-method="%2" type="string">
            <bind-xml node="attribute"/>
        </field>
    </class>
</mapping>


My config object looks like this:

Public class AdminConfig {

        private List locales = new ArrayList();
        public void setLocales(List locales) {
                this.locales = locales;
        }
        public List getLocales() {
                return locales;
        }

        public String toString() {
        final StringBuffer buf = new StringBuffer();
        buf.append("AdminConfig");
        buf.append("{locales=").append(locales);
        buf.append('}');
        return buf.toString();
        }
}

Public class Test {

        public static void main(String[] args) {

                AdminConfig cfg = new AdminConfig();
                List locales = new ArrayList();
                locales.add(new Locale("en", "US"));
                locales.add(new Locale("fr", "FR"));

                cfg.setLocale(locales);
                ....
                // Marshall object 

                
        }
}
Resulting XML file...

<?xml version="1.0" encoding="UTF-8"?>
<admin-config>
    <locales>
        <locale name="en" value="US"/>
        <locale name="fr" value="FR"/>
    </locales>
</cas-admin>
 

- Tim Mull�
 
 

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-user

Reply via email to