KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > core > ReferenceByXPathUnmarshaller


1 package com.thoughtworks.xstream.core;
2
3 import com.thoughtworks.xstream.alias.ClassMapper;
4 import com.thoughtworks.xstream.converters.ConverterLookup;
5 import com.thoughtworks.xstream.core.util.StringStack;
6 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
7 import com.thoughtworks.xstream.io.path.PathTracker;
8 import com.thoughtworks.xstream.io.path.PathTrackingReader;
9 import com.thoughtworks.xstream.io.path.RelativePathCalculator;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 public class ReferenceByXPathUnmarshaller extends TreeUnmarshaller {
15
16     private Map JavaDoc values = new HashMap JavaDoc();
17     private StringStack parentPathStack = new StringStack(16);
18     private PathTracker pathTracker = new PathTracker();
19     private RelativePathCalculator relativePathCalculator = new RelativePathCalculator();
20
21     public ReferenceByXPathUnmarshaller(Object JavaDoc root, HierarchicalStreamReader reader,
22                                         ConverterLookup converterLookup, ClassMapper classMapper,
23                                         String JavaDoc classAttributeIdentifier) {
24         super(root, reader, converterLookup, classMapper, classAttributeIdentifier);
25         this.reader = new PathTrackingReader(reader, pathTracker);
26     }
27
28     public Object JavaDoc convertAnother(Object JavaDoc parent, Class JavaDoc type) {
29         if (parentPathStack.size() > 0) { // handles circular references
30
values.put(parentPathStack.peek(), parent);
31         }
32         String JavaDoc relativePathOfReference = reader.getAttribute("reference");
33         String JavaDoc currentPath = pathTracker.getCurrentPath();
34         if (relativePathOfReference != null) {
35             return values.get(relativePathCalculator.absolutePath(currentPath, relativePathOfReference));
36         } else {
37             parentPathStack.push(currentPath);
38             Object JavaDoc result = super.convertAnother(parent, type);
39             values.put(currentPath, result);
40             parentPathStack.popSilently();
41             return result;
42         }
43     }
44
45 }
46
Popular Tags