You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Ray <ra...@yahoo.co.uk> on 2005/05/14 11:14:40 UTC

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

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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


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

Posted by Ray <ra...@yahoo.co.uk>.
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: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


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

Posted by Simon Kitching <sk...@apache.org>.
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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


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

Posted by Ray <ra...@yahoo.co.uk>.
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: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


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

Posted by Simon Kitching <sk...@apache.org>.
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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org