Thank you for your replay :)

I've decided to make it easy (as I wanted to avoid create a converter for
the main object)...

StatusId is omited and I am displaying a new variable called
displayStatus...

In any case, i'll keep the "correct" implementation in mind in case i need
to improve it in the future.


On Thu, Sep 13, 2012 at 5:24 PM, Jörg Schaible
<[email protected]>wrote:

> Hi Elena,
>
> Elena Montojo Vanrell wrote:
>
> > Good morning,
> >
> > I have an object with many variables, three of them are an Integer and 2
> > dates. I am using xstream to create an output xml, but instead of display
> > the integer I want an String depending also on the dates, so in a
> "normal"
> > case i would create an static method (displayStatus(statusId, date1,
> > date2);)
> >
> > how should i do this? Do I need to create a new variable (String status)
> > and add @XStreamOmitField to statusId or there is a "cleaner" posibility?
> >
> > This is the code as I have it now, I would like to replace the underlined
> > code by the dates on fromDate and arcDate. is that possible?
> >
> > "
> > @XStreamConverter(
> > StatusConverter.class)
> >     private Integer statusId;
> >
> >     private Date    fromDate;
> >     private Date    arcDate;
> >
> > ....
> >
> > public class StatusConverter implements Converter {
> >     @Override
> >     public void marshal(Object o, HierarchicalStreamWriter
> > hierarchicalStreamWriter, MarshallingContext marshallingContext) {
> >         Integer statusId = (Integer) o;
> >
> >
> hierarchicalStreamWriter.setValue(PropertyFormatter.displayStatus(statusId,
> > *new Date(), new Date()*));
> >     }
> >
> >     @Override
> >     public Object unmarshal(HierarchicalStreamReader
> > hierarchicalStreamReader, UnmarshallingContext unmarshallingContext) {
> >         return hierarchicalStreamReader.getValue();
> >     }
> >
> >
> >     @Override
> >     public boolean canConvert(Class aClass) {
> >         return aClass.equals(Integer.class);
> >     }
> > }
> >
> >
> > "
> >
> > Thank you in advance.
>
> the problem in this approach is that your local converter has no access to
> the two date fields, it gets only the integer. You will need in any case a
> custom converter for the type that contains these fields.
>
> Here you have now two possibilities:
> 1/ Implement a custom converter as it is explained in the converter
> tutorial
> i.e. this converter must handle all fields of the type, not only these 3.
>
> 2/ Derive it from ReflectionConverter, overload canConvert (required to
> handle only your type) and overload marshal/unmarshal like
>
> ======== %< =======
> public void marshal(Object o, HierarchicalStreamWriter writer,
> MarshallingContext context) {
>     YourType instance = (YourType) o;
>     context.put("fromDate", instance.fromDate);
>     context.put("arcDate", instance.arcDate);
>
>     // call to super only allowed
>     // if you did nothing yet with the writer
>     super.marshal(o, writer, context);
> }
> public Object unmarshal(HierarchicalStreamReader reader,
> UnmarshallingContext context) {
>     // call to super only allowed
>     // if you did nothing yet with the reader
>     YourType instance = (YourType) super.unmarshal(reader, context);
>     instance.fromDate = (Date)context.get("fromDate");
>     instance.arcDate = (Date)context.get("arcDate");
>     return instance;
> }
> ======== %< =======
>
> and your StatusConverter should use the contest to get the dates form the
> context at marshalling time and set the dates into the context when
> unmarshalling.
>
> You will have to declare the two date fields either as transient or add an
> annotation to omit them.
>
>
> Your choice, hope it helps,
> Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>

Reply via email to