Keith,

Should the FieldHandlerFactory method also work for the Marshaller?  When
I test it, the only method that gets called on my FieldHandlerFactory is
getSupportedTypes.  Currently my getSupportedTypes is just:

    public Class[] getSupportedTypes() {
        System.out.println( "getSupportedTypes()" );
        return new Class[] { GregorianCalendar.class } ;
    }

However, when I marshall the document, no other methods are called and I
still get output such as:

"password-last-changed-ts="java.util.GregorianCalendar[time=10753234......."

Any ideas?  Will this be incorporated by the next release?  I really like
the FieldHandlerFactory technique much better than specifying a handler
for each field in your mapping..

Jeremy


On Wed, 28 Jan 2004 13:01:40 -0600, "Keith Visco" <[EMAIL PROTECTED]>
said:
> 
> Hi Jeremy,
> 
> I recall the discussion, though the problem obviously still exists if
> you are running into it. You might want to file a bug on that
> (http://bugzilla.exolab.org).
> 
> There is an "undocumented" workaround for anyone using the CVS version,
> it's the ability to set a GeneralizedFieldHandler for a specific type
> and not only for a specific field:
> 
> 1. Implement org.exolab.castor.mapping.FieldHandlerFactory
> 
>    This factory should return your GeneralizedFieldHandler for
>    the InetAddress class.
> 
> 2. Remove the GeneralizedFieldHandler reference from the mapping file.
>    Leave the <field> mapping, just remove the handler="myHandler".
> 
> 3. Set the FieldHandlerFactory in the introspector as such:
> 
>    import org.exolab.castor.xml.ClassDescriptorResolver;
>    import org.exolab.castor.xml.Introspector;
>    import org.exolab.castor.xml.util.ClassDescriptorResolverImpl;
> 
> 
>    ClassDescriptorResolverImpl cdr = new ClassDescriptorResolverImpl();
>    cdr.getIntrospector().addFieldHandlerFactory(myFactory);
> 
> 4. Now use this ClassDescriptorResolver in the Unmarshaller and
> Unmarshal:
> 
> 
>    Unmarshaller unm = new Unmarshaller(...);
>    unm.setResolver(cdr);
>    Object root = unm.unmarshal(reader);
> 
> Hope that helps,
> 
> --Keith
> 
> 
>   
> Jeremy Haile wrote:
> > 
> > I'm new to the list, so I hope I'm not rehashing a question that has been
> > discussed recently.  I looked in the mail archives and only found posts
> > up to March of last year discussing using GeneralizedFieldHandler with
> > Collections.  It seems that the strange behavior when using a
> > GeneralizedFieldHandler with collections is still present.
> > 
> > My needs are similar to those of the old "URL" post.  In short, I am
> > trying to marshall and unmarshall a Set of java.net.InetAddress objects.
> > I want to marshall them into a java.lang.String via the
> > InetAddress.getHostName() method.  My generalized field handler receives
> > an enumeration of InetAddress objects in convertUponGet() while
> > marshalling.  I simply convert this to a set of java.lang.String objects
> > and return it.  This works as expected.
> > 
> > However, it is odd that when unmarshalling, convertUponSet() gets called
> > for each element in the collection instead of with an enumeration - fine,
> > I can deal with that.  But why does convertUponGet() get called while I
> > am unmarshalling.  Has this not been fixed in 0.9.5.2?  How should I be
> > handling this situation?
> > 
> > Relevant code is below.  Note that my code fails during unmarshalling
> > when convertUponGet() is called.  If I comment out my convertUponGet() my
> > code functions (I could probably develop a workaround), but I figured
> > that Castor should have a defined, working way of dealing with this
> > situation.
> > 
> > Thanks!
> > 
> > Running in Redhat Linux, JDK 1.4.2_03, Castor 0.9.5.2
> > 
> > -==MAPPING==-
> > 
> >     <field name="ipAddresses"
> >            type="java.lang.String"
> >            handler="com.company.module.support.InetAddressFieldHandler"
> >            get-method="getIpAddresses"
> >            set-method="setIpAddresses"
> >            collection="set">
> >       <bind-xml name="ip-address"
> >                 node="element" />
> >     </field>
> > 
> > -==FIELD HANDLER==-
> >     /**
> >      * Used for marshalling of a collection of InetAddresses by
> >      converting them
> >      * into a set of strings that represents their hostnames.
> >      * <p>
> >      * This method uses the [EMAIL PROTECTED] InetAddress#getHostAddress} method to
> >      * convert the InetAddress object into a string form.
> >      *
> >      * @param o the InetAddress object to convert
> >      *
> >      * @return a String encoding of the InetAddress object
> >      */
> >     public Object convertUponGet(Object o) {
> > 
> >         if( o == null )
> >             return null;
> > 
> >         // Create new set to hold ip addresses that are marshalled
> >         Set addressSet = new HashSet();
> > 
> >         // Loop through each value given by castor and convert to a
> >         string.
> >         // The string will be stored in the new set and returned to
> >         castor
> >         // to be marshalled in the XML file.
> >         Enumeration enum = (Enumeration) o;
> >         while ( enum.hasMoreElements() ) {
> >             InetAddress address = (InetAddress)enum.nextElement();
> > 
> >             // Add address host name to the address collection
> >             addressSet.add( address.getHostName() );
> >         }
> > 
> >         return addressSet;
> >     }
> > 
> >     /**
> >      * Unmarshalls a set of String IP Addresses into a set of InetAddress
> >      * objects.
> >      * <p>
> >      * The IP address is expected to be formatted in a way accepted by
> >      * [EMAIL PROTECTED] InetAddress#getByName(String)}.
> >      *
> >      *
> >      * @param o the String encoding of a InetAddress object
> >      *
> >      * @return the newly create java.util.InetAddress object
> >      */
> >     public Object convertUponSet(Object o) {
> > 
> >         if( o == null )
> >             return null;
> > 
> >         String hostName = (String) o;
> >         InetAddress inetAddress = null;
> > 
> >         try {
> > 
> >             // convert string into an inet address object
> >             inetAddress = InetAddress.getByName( hostName );
> > 
> >         } catch (UnknownHostException e) {
> > 
> >             // Castor doesn't allow us to throw exceptions in this class,
> >             so
> >             // a runtime exception is thrown as this is a fatal error...
> >             //todo Create a more elegant error reporting mechanism for
> >             field support
> >             throw new RuntimeException( "Host name is not a legal value:
> >             '" + hostName + "'" );
> >         }
> > 
> >         return inetAddress;
> >     }
> > 
> > --
> >   Jeremy Haile
> >   [EMAIL PROTECTED]
> >
> 
> ----------------------------------------------------------- 
> If you wish to unsubscribe from this mailing, send mail to
> [EMAIL PROTECTED] with a subject of:
>         unsubscribe castor-dev
> 
-- 
  Jeremy Haile
  [EMAIL PROTECTED]

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

Reply via email to