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 8 import java.util.HashMap ; 9 import java.util.Map ; 10 11 public class ReferenceByIdUnmarshaller extends TreeUnmarshaller { 12 13 private Map values = new HashMap (); 14 private StringStack parentIdStack = new StringStack(16); 15 16 public ReferenceByIdUnmarshaller(Object root, HierarchicalStreamReader reader, 17 ConverterLookup converterLookup, ClassMapper classMapper, 18 String classAttributeIdentifier) { 19 super(root, reader, converterLookup, classMapper, classAttributeIdentifier); 20 } 21 22 public Object convertAnother(Object parent, Class type) { 23 if (parentIdStack.size() > 0) { values.put(parentIdStack.peek(), parent); 25 } 26 String reference = reader.getAttribute("reference"); 27 if (reference != null) { 28 return values.get(reference); 29 } else { 30 String currentId = reader.getAttribute("id"); 31 parentIdStack.push(currentId); 32 Object result = super.convertAnother(parent, type); 33 values.put(currentId, result); 34 parentIdStack.popSilently(); 35 return result; 36 } 37 } 38 39 } 40 | Popular Tags |