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?
Evan Knapp
Email Disclaimer:
http://www.redprairie.com/emaildisclaimer/