Question re: setting a property after unmarshalling XML

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Question re: setting a property after unmarshalling XML

thomas fuller
Hi,

I use XStream to unmarshall XML into an instance of a data model. Each class in the data model extends from DefaultObject and the DefaultObject has a setPropertyChangeSupport method.

I'd like to configure XStream to set an instance of PropertyChangeSupport on every class in the data model that it creates.

Would you have any suggestions regarding how to approach this?

I have looked at implementing a converter however as I'm not converting anything I do wonder if this makes sense and thought I'd ask for a second opinion on the matter.

Thanks for your help,

Tom
Reply | Threaded
Open this post in threaded view
|

Re: Question re: setting a property after unmarshalling XML

Jörg Schaible-3
Hi Tom,

Thomas Fuller wrote:

> Hi,
>
> I use XStream to unmarshall XML into an instance of a data model. Each
> class in the data model extends from DefaultObject and the DefaultObject
> has a setPropertyChangeSupport method.
>
> I'd like to configure XStream to set an instance of PropertyChangeSupport
> on every class in the data model that it creates.
>
> Would you have any suggestions regarding how to approach this?
>
> I have looked at implementing a converter however as I'm not converting
> anything I do wonder if this makes sense and thought I'd ask for a second
> opinion on the matter.

I would use an own MarshallingStrategy, just derive from the one, you're
using by default:

==================== %< ==============
 class PropertyChangesSupportStrategy
     extends ReferenceByXPathMarshallingStrategy { // what ever you use
   public PropertyChangesSupportStrategy() {
     super(RELATIVE);
   }
   @Override
   protected TreeUnmarshaller createUnmarshallingContext(Object root,
HierarchicalStreamReader reader, ConverterLookup converterLookup, Mapper
mapper) {
     return new ReferenceByXPathUnmarshaller(root, reader, converterLookup,
mapper) {
       @Override
       protected Object convert(Object parent, Class type, Converter
converter) {
         Object result = super.convert(parent, type, converter);
         if (result instanceof DefaultObject) {
           DefaultObject do = DefaultObject.class.cast(result);
           do.setPropertyChangeSupport(...);
         }
         return result;
       }
     }
   }
 }
==================== %< ==============

Have a look at MultipleObjectsInOneStreamTest in XStream's acceptance tests,
it uses also a custom MarshallingStrategy for different purpose:
https://fisheye.codehaus.org/browse/xstream/trunk/xstream/src/test/com/thoughtworks/acceptance/MultipleObjectsInOneStreamTest.java

Cheers,
Jörg


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

    http://xircles.codehaus.org/manage_email