01: /*
02: * Copyright (C) 2003, 2004, 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007, 2008 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 26. September 2003 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.converters.collections;
13:
14: import com.thoughtworks.xstream.converters.MarshallingContext;
15: import com.thoughtworks.xstream.converters.UnmarshallingContext;
16: import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriterHelper;
17: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
18: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
19: import com.thoughtworks.xstream.mapper.Mapper;
20:
21: import java.util.HashMap;
22: import java.util.Hashtable;
23: import java.util.Iterator;
24: import java.util.Map;
25:
26: /**
27: * Converts a java.util.Map to XML, specifying an 'entry'
28: * element with 'key' and 'value' children.
29: * <p>Note: 'key' and 'value' is not the name of the generated tag. The
30: * children are serialized as normal elements and the implementation expects
31: * them in the order 'key'/'value'.</p>
32: * <p>Supports java.util.HashMap, java.util.Hashtable and
33: * java.util.LinkedHashMap.</p>
34: *
35: * @author Joe Walnes
36: */
37: public class MapConverter extends AbstractCollectionConverter {
38:
39: public MapConverter(Mapper mapper) {
40: super (mapper);
41: }
42:
43: public boolean canConvert(Class type) {
44: return type.equals(HashMap.class)
45: || type.equals(Hashtable.class)
46: || type.getName().equals("java.util.LinkedHashMap")
47: || type.getName().equals("sun.font.AttributeMap") // Used by java.awt.Font in JDK 6
48: ;
49: }
50:
51: public void marshal(Object source, HierarchicalStreamWriter writer,
52: MarshallingContext context) {
53: Map map = (Map) source;
54: for (Iterator iterator = map.entrySet().iterator(); iterator
55: .hasNext();) {
56: Map.Entry entry = (Map.Entry) iterator.next();
57: ExtendedHierarchicalStreamWriterHelper.startNode(writer,
58: mapper().serializedClass(Map.Entry.class),
59: Map.Entry.class);
60:
61: writeItem(entry.getKey(), context, writer);
62: writeItem(entry.getValue(), context, writer);
63:
64: writer.endNode();
65: }
66: }
67:
68: public Object unmarshal(HierarchicalStreamReader reader,
69: UnmarshallingContext context) {
70: Map map = (Map) createCollection(context.getRequiredType());
71: populateMap(reader, context, map);
72: return map;
73: }
74:
75: protected void populateMap(HierarchicalStreamReader reader,
76: UnmarshallingContext context, Map map) {
77: while (reader.hasMoreChildren()) {
78: reader.moveDown();
79:
80: reader.moveDown();
81: Object key = readItem(reader, context, map);
82: reader.moveUp();
83:
84: reader.moveDown();
85: Object value = readItem(reader, context, map);
86: reader.moveUp();
87:
88: map.put(key, value);
89:
90: reader.moveUp();
91: }
92: }
93:
94: }
|