Title: RE: [castor-user] Date in string Format

Erratum:

AFAIK, the _easiest_ way is to do as I said. The other one, you found it by yourself :-)

Following is a field handler we use to marshall/unmarshall our own type of URI objects (also works with collections), I did not write it myself, so will not be able to offer support nor explainations ;-)

public class URIFieldHandler extends GeneralizedFieldHandler {

        private boolean unmarshalling = false;

        /**
         * Used to "identify" this field handler
         */
        public java.lang.Class getFieldType(){
        return URI.class;
    }


        /**
         * Used during marshalling
         * @see org.exolab.castor.mapping.GeneralizedFieldHandler#convertUponGet(Object)
         */
        public Object convertUponGet(Object obj) {
                if (obj == null)
                        return null;
                try {
                        if (obj instanceof URI) {
                                URI uri = (URI)obj;
                                return uri.toString();
                        } else if (obj instanceof Enumeration) {
                                if (unmarshalling) {
                                        //convertUponGet called while unmarshalling :
                                        //return null so that convertUponSet is called again
                                        return null;
                                }
                                Enumeration enum = (Enumeration)obj;
                                Collection c = new ArrayList();
                                if (!enum.hasMoreElements())
                                        return null;
                                // return a collection of transformed elements
                                while (enum.hasMoreElements()) {
                                        Object next = ((Enumeration)obj).nextElement();
                                        if (next instanceof URI) {
                                                URI uri = (URI)next;
                                                c.add(uri.toString());
                                        }
                                }
                                return c;
                        }
                } catch (Exception e) {
                }
                return null;
        }

        /**
         * Used during unmarshalling
         * @see org.exolab.castor.mapping.GeneralizedFieldHandler#convertUponSet(Object)
         */
        public Object convertUponSet(Object obj) {
                if (obj == null)
                        return null;
                try {
                        // We are called only during unmarshalling
                        // set to true for convertUponGet calls...
                        unmarshalling = true;
                        if (obj instanceof String) {
                                return new URI((String)obj);
                        }
                        // if a collection, return first element transformed
                        else if (obj instanceof Collection) {
                                Iterator i = ((Collection)obj).iterator();
                                return new URI((String)i.next());
                        }
                } catch (Exception e) {
                }
                return null;
        }

}


-----Original Message-----
From: Claude Vedovini [mailto:[EMAIL PROTECTED]]
Sent: 5 August 2004 09:17
To: '[EMAIL PROTECTED]'
Subject: Re: [castor-user] Date in string Format


Hi,
Castor is marshalling/unmarshalling java.util.Date using the XML Schema representation for date-time which is ISO8601. AFAIK, if you wish to use your own format the only way is either to use a String in your bean or use another pair of getter/setter like getSaiDateAsString/setSailDateAsString and specify the get-method/set-method in your mapping file.

Regards,
Claude
> -----Original Message-----
> From: Mounagurusamy, Jayakumar (HAL)
> [mailto:[EMAIL PROTECTED]]
> Sent: 4 August 2004 21:14
> To: [EMAIL PROTECTED]
> Subject: [castor-user] Date in string Format
>
>
> Hello All,
>
> I am trying to marshal a java.util.Date object to of format
> "mmddccyy",
> by default castor does this  format
> <SailDate>2004-09-04T00:00:00.000-07:00</SailDate>. Is there
> any way for
> me to specify the string Date format that I want in the XML and vice
> versa
>
> Any help will be appreciated
>
> Thanks
> Jay
>
>

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

Reply via email to