Hi Evan,

Knapp, Evan wrote:

> Hi,
> 
> I have just started using custom converters with XStream and i was
> wondering if i could get a little help with and issue I am having.
> 
> Here's the scenario:
> 
> Say I have these java classes:
> 
> public abstract class Person {
>   private Map catchAllMap = new HashMap<String,String>();
> 
>   // Getter and Setter...
> }
> 
> @XStreamAlias("WorkingPerson")
> public class WorkingPerson extends Person {
>   @XStreamAlias("Age")
>   private int age;
> 
>   @XStreamAlias("Name")
>   private String name;
> 
>   // Setters and getters....
> }
> 
> 
> and say I have this XML format that I want to read in from a file and
> print out to another file:
> 
> <WorkingPerson>
>    <Age>27</Age>
>    <Name>John Smith</Name>
>    <Personality>Quiet</Personality>
>    <Career>Software Engineer</Career>
> </WorkingPerson>
> 
> *Notice that the Personality and Career elements do not show up in the
> class implementation.
> 
> What I want is to write a Generic style converter that will be able to
> handle this class in a special manner.  I want it use the base
> ReflectionConverter for normal cases but when it encounters an element
> that it doesn't recognize (such as "Career") to throw it into the
> "catchAllMap"
> like so: "catchAllMap.put(nodeName,nodeValue)".  I would write a smart
> custom converter that knows how to do this but I am looking to implement
> lets say various implementations of the Person class (maybe
> UnemployedPerson or TeenagePerson) and I would like to just have one
> converter that can handle any implementation of the Person class.
> 
> I had a few thoughts of how to do this.  I was thinking the best idea
> would be to have my custom generic converter extend the
> ReflectionConverter.
>  This way i can use the ReflectionConverter "super.marshal" for
>  marshalling
> (provided i write a custom converter for maps to be printed as I wish) and
> write an unmarshall that can use the ReflectionConverter "super.unmarshal"
> method as needed.  But I was unsure if this was even do-able or bad form?
>  Does anyone have any thoughts on how I can accomplish my goal?

What you need is a custom converter that is able to handle all Person types. 
However, I don't think you can really derive from the ReflectionConverter. 
There are not the appropriate entry points for your needs, e.g. you are not 
able to inject some kind of callback for unknown elements. You might simply 
copy the relevant code parts into your own converter. Said that, you might 
be interested in the code of "ToAttributedValueConverter" that is also 
reflection-based, but is much simpler structured. The complex code in 
(Abstract)ReflectionConverter is just because of all the support for 
aliases, attributes, implicit containers, and so on. If you your types do 
not use these features, you can simplify the code a lot.

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to