Hi Tim,

Can you point to me in the docs where it says Locale has built-in support. It might be the case with Castor-JDO, but not with Castor-XML.

Castor XML has no built-in support for it.

In order to get Locale to work, you'll need a mapping file and specify the country and language values as attributes (since there is no default constructor the values need to be mapped as attributes in order to be passed to the constructor). Something like the following should work:

<class name="java.util.Locale">
   <field name="language" type="string"
         get-method="getLanguage" set-method="%1">
      <bind-xml node="attribute"/>
   </field>
   <field name="country" type="string"
      set-method="%2" get-method="getCountry">
      <bind-xml node="attribute"/>
   </field>
</class>

--Keith


Tim Mulle' wrote:
Anyone have any ideas about this?

-----Original Message-----
From: Tim Mulle' [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 27, 2004 1:51 PM
To: [EMAIL PROTECTED]
Subject: Re: [castor-user] XML: Un/Marshalling java.util.Locale objects


Update:

I tried the same code but using java.awt.Color objects as well as my own,
and they seem to work meaning I get back the objects from the XML. For some
reason, nothing I do makes the java.util.Locale object come back. Could it
be a bug in the TypeInfo classes? I noticed in the docs that "locale" is a
built-in supported type.


-----Original Message-----
From: Tim Mulle' [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 27, 2004 11:23 AM
To: [EMAIL PROTECTED]
Subject: [castor-user] XML: Un/Marshalling java.util.Locale objects


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



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

Reply via email to