Hi Michael,

Michael Mogley wrote:

> Ok, I modified the code to the following custom marshalling strategy.  I
> am able get the reference to the first object resolved, but not the
> second. I.e:
> 
> <typeA reference="../../../typeA[1]"/> works, but
> <typeA reference="../,./../typeA[2]"/> gives invalid reference.

Well, *this* reference *is* actually invalid. Do you recognize the ',' in 
the path?

> public class ReusingReferenceByXPathMarshallingStrategy implements
> MarshallingStrategy
> {
>   //~ Instance fields
> ----------------------------------------------------------
> 
>   private ReferenceByXPathMarshaller   marshaller;
>   private ReferenceByXPathUnmarshaller unmarshaller;
> 
>   //~ Methods
> ------------------------------------------------------------------
> 
>   @Override
>   public void marshal(HierarchicalStreamWriter writer, Object obj,
> ConverterLookup converterLookup, Mapper mapper, DataHolder dataHolder)
>   {
>     if (marshaller == null)
>     {
>       marshaller = new ReferenceByXPathMarshaller(writer, converterLookup,
> mapper, ReferenceByXPathMarshallingStrategy.RELATIVE);
>     }
> 
>     marshaller.start(obj, dataHolder);
>   }
> 
>   @Override
>   public Object unmarshal(Object root, HierarchicalStreamReader reader,
> DataHolder dataHolder, ConverterLookup converterLookup, Mapper mapper)
>   {
>     if (unmarshaller == null)
>     {
>       unmarshaller = new ReferenceByXPathUnmarshaller(root, reader,
> converterLookup, mapper);
>     }
> 
>     return unmarshaller.start(dataHolder);
>   }
> }

However, you're right, that the dereferencing fails here. The problem is, 
that in contrast to the IdMarshallingStrategy used originally in the 
example, the reference identifiers generated by the XPathMarshallingStrategy 
uses the absolute XPath. The path is tracked at unmarshalling time by 
wrapping moveDown/moveUp calls, but the mechanism fails, because the call of 
this method for the top level elements passes the wrapper.

Therefore you will have to use your own path tracker and a modified copy of 
the ReferenceByXPathUnmarshaller:

================= %< ==============
static class ReusingReferenceByXPathMarshallingStrategy implements 
MarshallingStrategy {
  private final static class ReusingReferenceByXPathUnmarshaller extends 
ReferenceByXPathUnmarshaller {
    private PathTracker pathTracker = new PathTracker();

    private ReusingReferenceByXPathUnmarshaller(Object root, 
HierarchicalStreamReader reader, ConverterLookup converterLookup, Mapper 
mapper) {
      super(root, reader, converterLookup, mapper);
      this.reader = new PathTrackingReader(reader, pathTracker);
    }

    private void initializeReuse(HierarchicalStreamReader reader) {
      pathTracker.popElement();
      pathTracker.pushElement(reader.getNodeName());
    }

    protected Object getReferenceKey(String reference) {
      final Path path = new Path(isNameEncoding ?
((AbstractReader)reader.underlyingReader()).decodeNode(reference) : 
reference);
      return reference.charAt(0) != '/' ? pathTracker.getPath().apply(path) 
: path;
    }

    protected Object getCurrentReferenceKey() {
      return pathTracker.getPath();
    }
  }

  private ReferenceByXPathMarshaller marshaller;
  private ReferenceByXPathUnmarshaller unmarshaller;

  public void marshal(HierarchicalStreamWriter writer, Object obj, 
ConverterLookup converterLookup, Mapper mapper, DataHolder dataHolder) {
    if (marshaller == null) {
      marshaller = new ReferenceByXPathMarshaller(writer, converterLookup, 
mapper, ReferenceByXPathMarshallingStrategy.RELATIVE | 
ReferenceByXPathMarshallingStrategy.SINGLE_NODE);
    }
    marshaller.start(obj, dataHolder);
  }

  public Object unmarshal(Object root, HierarchicalStreamReader reader, 
DataHolder dataHolder, ConverterLookup converterLookup, Mapper mapper) {
    if (unmarshaller == null) {
      unmarshaller = new ReusingReferenceByXPathUnmarshaller(root, reader, 
converterLookup, mapper);
    } else {
      
((ReusingReferenceByXPathUnmarshaller)unmarshaller).initializeReuse(reader);
    }
    return unmarshaller.start(dataHolder);
  }
}
================= %< ==============

Hope this helps now,
Jörg


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

    http://xircles.codehaus.org/manage_email


Reply via email to