Hi Sriram,
sriram v wrote:
>
> Hi Jörg,
>
> Let explain my question a little better.
>
> The 'Address' class looks something like -
>
> class Address {
>
> private String street1;
>
> private String street2;
>
> private String city;
>
> private String state;
>
> private String zip;
>
> private ADDR_TYPE addressType; // this value is in the 'omitFields'
> list
> as we make an attribute of it later
>
> }
>
> where ADDR_TYPE is an emum defined somewhere else as -
>
> enum ADDR_TYPE {
> HOME,
> OFFICE
> }
>
> The AddressConverter looks like -
>
> public class AddressConverter extends JavaBeanConverter {
>
> public boolean canConvert(Class clazz) {
> return clazz.equals(Address.class);
> }
>
> public void marshal(Object value, HierarchicalStreamWriter writer,
> MarshallingContext context) {
> writer.addAttribute("type", ((Address)value).getAddressType());
> super.marshal(value, writer, context); // Calls 'JavaBeanConverter's
> marshal
> }
>
> public Object unmarshal(HierarchicalStreamReader reader,
> UnmarshallingContext context) {
> super.unmarshal(reader, context);
> }
>
> }
>
> Using this converter and alias for both the fields as 'address', I could
> get the xml which I said below.
OK, this makes sense now.
> Now since you rightly said -
>
>> Because PlaceInfo has no field named "address". Therefore the converter
>> for
>> the PlaceInfo instance does not know, what "address" means at all.
>
> during unmarshal, even though I have a qNameMap mapping 'address' to the
> 'Address.class', it makes a new instance but cannot relate the new
> instance to any member in the 'PlaceInfo' class.
Do you actually have namespaces in your XML? With an alias definition
XStream will normally map the element on its own to the appropriate field.
Nevertheless, in your case it will not work, since the second alias
definition will clobber the first one and XStream would try to set both
address instances to the same field - actually again not what you want.
> One solution to this would be a right a converter for the 'PlaceInfo'
> class and not just the 'Address' class.
>
> I was wondering if there is a way of specifying or changing the 'nodeName'
> that the HierarchicalStreamReader is seeing at the moment in the read
> process.
The node name is always in the responsibility of the converter that handles
the enclosing type i.e. in your case it is by default the
ReflectionConverter handling the PlaceInfo types. That one uses the field
names for the tags (or the aliases defined for the fields).
> For eg. when reader.getNodeName() equals 'address', can I refer to the
> mapper of the xstream configuration and give it a new name based on some
> condition. This way, in this case, I could say that look at the 'type'
> attribute of the opening tag and depending on that, rename the field to
> either 'homeAddress' or 'officeAddress'.
The only possibility for the ReflectionConverter requires two distinct
classes - again, not what's available in your use case.
> I will go ahead with making a converter for the 'PlaceInfo' class as that
> seems like the simplest solution.
Actually for this scenario it is the only one ;-)
Cheers,
Jörg
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email