You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Brian Sayatovic/AMIG <BS...@amig.com> on 2003/08/05 21:58:09 UTC

Digester: setting a java.net.URL property

I have an XML file that contains this sort of element:

        <FtpUrl>ftp://user:pass@host:21/some/path</FtpUrl>

I also have a Config object that has a java.net.URL property with a 
getter/setter:

        public class MyConfig {
                // ...
                public URL getFtpUrl();
                public void setFtpUrl(URL url);
                // ...
        }

However, I can't decide how to tie these together with Digester.  I 
couldn't get a bean property setter to work, I suspect because there is no 
conversion between the String in the XML file and a java.net.URL.  Second, 
I thought I'd use an ObjectCreationFactory with a FactoryCreateRule to use 
the body text to construct a new URL and return it, then add a SetNextRule 
to set that URL into the config object.  But I can't seem to get at the 
body text via a ObjectCreationFactory (I get the attribtues passed to the 
factory,but not the body from what I could tell).

So, my next step is to create my own Rule and add it to the Digester.  But 
that seems a bit extreme.  Is there a less custom way of doing this?

Regards,
Brian.

Re: Digester: setting a java.net.URL property

Posted by Eric Galluzzo <eg...@einnovation.com>.
Brian Sayatovic/AMIG wrote:

>I have an XML file that contains this sort of element:
>
>        <FtpUrl>ftp://user:pass@host:21/some/path</FtpUrl>
>
>I also have a Config object that has a java.net.URL property with a 
>getter/setter:
>
>        public class MyConfig {
>                // ...
>                public URL getFtpUrl();
>                public void setFtpUrl(URL url);
>                // ...
>        }
>
>However, I can't decide how to tie these together with Digester.  I 
>couldn't get a bean property setter to work, I suspect because there is no 
>conversion between the String in the XML file and a java.net.URL.
>
That's right.  Try creating a new BeanUtils Converter, something like:

package whatever;

import java.net.MalformedURLException;
import org.apache.commons.beanutils.Converter;

public class URLConverter implements Converter
{
   public Object convert(Class type, Object value)
   {
      if ( value == null )
      {
         return null;
      }
      else if ( value instanceof URL )
      {
         return value;
      }
      else // assume it's a string
      {
         String strValue = (String) value;
         if ( strValue.equals( "" ) )
         {
            return null;
         }
         else
         {
            try
            {
               return new URL( strValue );
            }
            catch ( MalformedURLException mue )
            {
               return null; // FIXME: Should log the error somehow
            }
         }
      }
   }
}

Then register it with BeanUtils' ConvertUtils class, thus:

import org.apache.commons.beanutils.ConvertUtils;
import whatever.URLConverter;

...
// Near the beginning of your app...
ConvertUtils.register( new URLConverter(), URL.class );
...

Warning: The above may not quite compile, but you can probably figure it 
out from there.

    - Eric



Re: BeanUtils Properties

Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Tue, 5 Aug 2003, Brian McCallister wrote:

> Date: Tue, 5 Aug 2003 16:45:06 -0400
> From: Brian McCallister <mc...@forthillcompany.com>
> Reply-To: Jakarta Commons Users List <co...@jakarta.apache.org>
> To: Jakarta Commons Users List <co...@jakarta.apache.org>
> Subject: BeanUtils Properties
>
> Can anyone provide a quick answer on the methods BeanUtils uses to find
> properties? I am not particularly fluent in the JavaBeans specification
> (java.beans.* stuff).
>

The main introspection class is java.beans.Introspector.

> So, some specific questions...
>
> Can BeanUtils use a bean.get(String name) type accessor for a Bean?
>

If the bean in question is a DynaBean (that is, it implements the
org.apache.commons.beanutils.DynaBean interface), then the answer is yes.

> If so, how do I tell it to look for it? (copyProperties() / populate()
> behavior being of particular interest)
>

Inside BeanUtils (and PropertyUtils) there are instanceof checks for
DynaBean, so you don't have to do anything special.

> Does BeanUtils respect nested properties on both sides of a
> copyProperties() / populate() (ie, copy object graphs)
>

The copyProperties() method is shallow, but if the property type is an
object that is the root of a tree of objects, then the destination
property will end up pointing at the same tree.  The nested structures are
not cloned.

The populate() method accepts expressions ("foo.bar") in the keys of the
map, and calls the corresponding property setter on the destination bean,
which might navigate down into the tree hierarchy before setting.

>
> Much appreciated!
>
> -Brian

Craig

BeanUtils Properties

Posted by Brian McCallister <mc...@forthillcompany.com>.
Can anyone provide a quick answer on the methods BeanUtils uses to find 
properties? I am not particularly fluent in the JavaBeans specification 
(java.beans.* stuff).

So, some specific questions...

Can BeanUtils use a bean.get(String name) type accessor for a Bean?

If so, how do I tell it to look for it? (copyProperties() / populate() 
behavior being of particular interest)

Does BeanUtils respect nested properties on both sides of a 
copyProperties() / populate() (ie, copy object graphs)


Much appreciated!

-Brian