[Commons Digester] Newbie (ish) help with odd structure

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[Commons Digester] Newbie (ish) help with odd structure

Ray-46
Hi there ... :-)

I wonder if someone could help me out with this problem.  I'm trying to
put together some Commons Digester XML rules, to fill the following Java
structure:

public class Request {

    private DataItem data = new DataItem();

    public DataItem getDataItem();

    public void setDataItem(DataItem dataItem);

}


public class DataItem {

    private DataString name = new DataString();
    private DataString postcode = new DataString();

    public DataString getName();
    public void setName(DataString name);
    public DataString getPostcode();
    public void setPostcode(DataString postcode);

}


public class DataString {

    private String value;

    public String getValue();
    public void setValue(String name);

 
}


Okay, I have the following XML data

<Request>
    <DataItem>
        <Name>Frederick</Name>
        <Postcode>xxx112223</Postcode>
    </DataItem>
</Request>

My problem is getting the Name and Postcode Elements into the DataString
objects, since they don't map exactly. I was trying something like

<call-method-rule pattern="Name" methodname="getName.setValue"/>

But I appear to be on the wrong track with this.

Nay help would be greatly appreciated. Thanks.





---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Commons Digester] Newbie (ish) help with odd structure

Simon Kitching
Hi Ray,

On Sat, 2005-05-14 at 10:14 +0100, Ray wrote:

> public class DataItem {
>
>     private DataString name = new DataString();
>     private DataString postcode = new DataString();
>
>     public DataString getName();
>     public void setName(DataString name);
>     public DataString getPostcode();
>     public void setPostcode(DataString postcode);
>
> }
>
>
> public class DataString {
>
>     private String value;
>
>     public String getValue();
>     public void setValue(String name);
>
>  
> }
>
>
> Okay, I have the following XML data
>
> <Request>
>     <DataItem>
>         <Name>Frederick</Name>
>         <Postcode>xxx112223</Postcode>
>     </DataItem>
> </Request>
>

I think you could solve this by registering a custom converter that can
convert String objects into DataString objects.

When Digester needs to call a method that takes a non-string parameter,
but it has just the string data extracted from the XML it uses the
beanutils ConvertUtils library to do the appropriate conversion. It
looks to me like you just need to tell ConvertUtils how to create a
DataString out of a String..

ConvertUtils.register(DataString.class, new Converter()) {
  public Object convert(Class Type, Object value) {
    if (value == null)
        return null;
    else
        return new DataString(value.toString());
  }
}

I presume a DataString has a constructor that takes a String. If not,
obviously creating an empty one and setting the value is also possible.

See here for details on beanutils:
  http://jakarta.apache.org/commons/beanutils

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Commons Digester] Newbie (ish) help with odd structure

Ray-46
Thanks Simon; that's exactly what I'm looking for. .. :-)

One question though; does this also for marshalling the object back into
XML, or do I need to provide a separate converter that takes a
DataString and return a String?

Or does it just use a toString call on the object?

Thanks again.


Simon Kitching wrote:

>Hi Ray,
>
>On Sat, 2005-05-14 at 10:14 +0100, Ray wrote:
>  
>
>>public class DataItem {
>>
>>    private DataString name = new DataString();
>>    private DataString postcode = new DataString();
>>
>>    public DataString getName();
>>    public void setName(DataString name);
>>    public DataString getPostcode();
>>    public void setPostcode(DataString postcode);
>>
>>}
>>
>>
>>public class DataString {
>>
>>    private String value;
>>
>>    public String getValue();
>>    public void setValue(String name);
>>
>>  
>>}
>>
>>
>>Okay, I have the following XML data
>>
>><Request>
>>    <DataItem>
>>        <Name>Frederick</Name>
>>        <Postcode>xxx112223</Postcode>
>>    </DataItem>
>></Request>
>>
>>    
>>
>
>I think you could solve this by registering a custom converter that can
>convert String objects into DataString objects.
>
>When Digester needs to call a method that takes a non-string parameter,
>but it has just the string data extracted from the XML it uses the
>beanutils ConvertUtils library to do the appropriate conversion. It
>looks to me like you just need to tell ConvertUtils how to create a
>DataString out of a String..
>
>ConvertUtils.register(DataString.class, new Converter()) {
>  public Object convert(Class Type, Object value) {
>    if (value == null)
> return null;
>    else
>        return new DataString(value.toString());
>  }
>}
>
>I presume a DataString has a constructor that takes a String. If not,
>obviously creating an empty one and setting the value is also possible.
>
>See here for details on beanutils:
>  http://jakarta.apache.org/commons/beanutils
>
>Regards,
>
>Simon
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>
>
>  
>




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Commons Digester] Newbie (ish) help with odd structure

Simon Kitching
On Sat, 2005-05-14 at 13:31 +0100, Ray wrote:
> Thanks Simon; that's exactly what I'm looking for. .. :-)
>
> One question though; does this also for marshalling the object back into
> XML, or do I need to provide a separate converter that takes a
> DataString and return a String?
>
> Or does it just use a toString call on the object?

Digester doesn't deal with object->XML conversions.  You might like to
look at Betwixt for that, though I don't know much about Betwixt.

ConvertUtils is really a one-way facility, String -> various types. It
doesn't deal with the reverse conversion.

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Commons Digester] Newbie (ish) help with odd structure

Ray-46
Ok.

Thanks very much for your help Simon.

Very much appreciated. ... :-)

Simon Kitching wrote:

>On Sat, 2005-05-14 at 13:31 +0100, Ray wrote:
>  
>
>>Thanks Simon; that's exactly what I'm looking for. .. :-)
>>
>>One question though; does this also for marshalling the object back into
>>XML, or do I need to provide a separate converter that takes a
>>DataString and return a String?
>>
>>Or does it just use a toString call on the object?
>>    
>>
>
>Digester doesn't deal with object->XML conversions.  You might like to
>look at Betwixt for that, though I don't know much about Betwixt.
>
>ConvertUtils is really a one-way facility, String -> various types. It
>doesn't deal with the reverse conversion.
>
>Regards,
>
>Simon
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>
>
>  
>




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]