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 ; 12 import java.util.Map ; 13 14 public class ReferenceByXPathUnmarshaller extends TreeUnmarshaller { 15 16 private Map values = new HashMap (); 17 private StringStack parentPathStack = new StringStack(16); 18 private PathTracker pathTracker = new PathTracker(); 19 private RelativePathCalculator relativePathCalculator = new RelativePathCalculator(); 20 21 public ReferenceByXPathUnmarshaller(Object root, HierarchicalStreamReader reader, 22 ConverterLookup converterLookup, ClassMapper classMapper, 23 String classAttributeIdentifier) { 24 super(root, reader, converterLookup, classMapper, classAttributeIdentifier); 25 this.reader = new PathTrackingReader(reader, pathTracker); 26 } 27 28 public Object convertAnother(Object parent, Class type) { 29 if (parentPathStack.size() > 0) { values.put(parentPathStack.peek(), parent); 31 } 32 String relativePathOfReference = reader.getAttribute("reference"); 33 String currentPath = pathTracker.getCurrentPath(); 34 if (relativePathOfReference != null) { 35 return values.get(relativePathCalculator.absolutePath(currentPath, relativePathOfReference)); 36 } else { 37 parentPathStack.push(currentPath); 38 Object result = super.convertAnother(parent, type); 39 values.put(currentPath, result); 40 parentPathStack.popSilently(); 41 return result; 42 } 43 } 44 45 } 46 | Popular Tags |