KickJava   Java API By Example, From Geeks To Geeks.

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


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