| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.shindig.social.core.util.xstream; |
| 19 | |
|
| 20 | |
import com.google.common.collect.Maps; |
| 21 | |
|
| 22 | |
import com.thoughtworks.xstream.converters.MarshallingContext; |
| 23 | |
import com.thoughtworks.xstream.converters.UnmarshallingContext; |
| 24 | |
import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter; |
| 25 | |
import com.thoughtworks.xstream.io.HierarchicalStreamReader; |
| 26 | |
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; |
| 27 | |
import com.thoughtworks.xstream.mapper.Mapper; |
| 28 | |
|
| 29 | |
import org.apache.shindig.social.opensocial.spi.DataCollection; |
| 30 | |
|
| 31 | |
import java.util.HashMap; |
| 32 | |
import java.util.Map; |
| 33 | |
import java.util.Map.Entry; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public class DataCollectionConverter extends AbstractCollectionConverter { |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public DataCollectionConverter(Mapper mapper) { |
| 45 | 222 | super(mapper); |
| 46 | 222 | } |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
@Override |
| 54 | |
public boolean canConvert(Class clazz) { |
| 55 | 314 | boolean convert = (DataCollection.class.isAssignableFrom(clazz)); |
| 56 | 314 | return convert; |
| 57 | |
} |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
@Override |
| 67 | |
public void marshal(Object source, HierarchicalStreamWriter writer, |
| 68 | |
MarshallingContext context) { |
| 69 | |
|
| 70 | 9 | DataCollection collection = (DataCollection) source; |
| 71 | 9 | Map<String, Map<String, String>> internalMap = collection.getEntry(); |
| 72 | |
|
| 73 | |
if (false) { |
| 74 | |
Map<String, String> m = internalMap.values().iterator().next(); |
| 75 | |
for (Entry<String, String> e : m.entrySet()) { |
| 76 | |
writer.startNode("entry"); |
| 77 | |
writer.startNode("key"); |
| 78 | |
writer.setValue(e.getKey()); |
| 79 | |
writer.endNode(); |
| 80 | |
writer.startNode("value"); |
| 81 | |
writer.setValue(e.getValue()); |
| 82 | |
writer.endNode(); |
| 83 | |
writer.endNode(); |
| 84 | |
} |
| 85 | |
} else { |
| 86 | 9 | for (Entry<String, Map<String, String>> eo : internalMap.entrySet()) { |
| 87 | 11 | writer.startNode("entry"); |
| 88 | 11 | writer.startNode("key"); |
| 89 | 11 | writer.setValue(eo.getKey()); |
| 90 | 11 | writer.endNode(); |
| 91 | 11 | writer.startNode("value"); |
| 92 | 11 | for (Entry<String, String> ei : eo.getValue().entrySet()) { |
| 93 | 8 | writer.startNode("entry"); |
| 94 | 8 | writer.startNode("key"); |
| 95 | 8 | writer.setValue(ei.getKey()); |
| 96 | 8 | writer.endNode(); |
| 97 | 8 | writer.startNode("value"); |
| 98 | 8 | writer.setValue(ei.getValue()); |
| 99 | 8 | writer.endNode(); |
| 100 | 8 | writer.endNode(); |
| 101 | 8 | } |
| 102 | |
|
| 103 | 11 | writer.endNode(); |
| 104 | 11 | writer.endNode(); |
| 105 | 11 | } |
| 106 | |
} |
| 107 | 9 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
@SuppressWarnings("unchecked") |
| 116 | |
@Override |
| 117 | |
public Object unmarshal(HierarchicalStreamReader reader, |
| 118 | |
UnmarshallingContext context) { |
| 119 | 1 | reader.moveDown(); |
| 120 | 1 | Map<String, Object> m = Maps.newHashMap(); |
| 121 | 3 | while (reader.hasMoreChildren()) { |
| 122 | 2 | reader.moveDown(); |
| 123 | 2 | String ok = null; |
| 124 | 2 | Object ov = null; |
| 125 | 2 | while (reader.hasMoreChildren()) { |
| 126 | 0 | reader.moveDown(); |
| 127 | 0 | String elname = reader.getNodeName(); |
| 128 | 0 | if ("key".equals(elname)) { |
| 129 | 0 | ok = reader.getValue(); |
| 130 | 0 | } else if ("value".equals(elname)) { |
| 131 | 0 | ov = reader.getValue(); |
| 132 | 0 | if (reader.hasMoreChildren()) { |
| 133 | 0 | Map<String, String> innerMap = Maps.newHashMap(); |
| 134 | 0 | while (reader.hasMoreChildren()) { |
| 135 | 0 | reader.moveDown(); |
| 136 | 0 | String k = null; |
| 137 | 0 | String v = null; |
| 138 | 0 | while (reader.hasMoreChildren()) { |
| 139 | 0 | reader.moveDown(); |
| 140 | 0 | if ("key".equals(elname)) { |
| 141 | 0 | k = reader.getValue(); |
| 142 | 0 | } else if ("value".equals(elname)) { |
| 143 | 0 | v = reader.getValue(); |
| 144 | |
} |
| 145 | 0 | reader.moveUp(); |
| 146 | 0 | } |
| 147 | 0 | innerMap.put(k, v); |
| 148 | 0 | reader.moveUp(); |
| 149 | 0 | } |
| 150 | 0 | ov = innerMap; |
| 151 | |
} else { |
| 152 | |
} |
| 153 | |
} |
| 154 | 0 | reader.moveUp(); |
| 155 | 0 | } |
| 156 | 2 | reader.moveUp(); |
| 157 | 2 | m.put(ok, ov); |
| 158 | 2 | } |
| 159 | 1 | reader.moveUp(); |
| 160 | |
|
| 161 | 1 | boolean nonmap = false; |
| 162 | 1 | for (Entry<String, Object> e : m.entrySet()) { |
| 163 | 1 | if (e.getValue() instanceof String) { |
| 164 | 0 | nonmap = true; |
| 165 | |
} |
| 166 | 1 | } |
| 167 | 1 | Map<String, Map<String, String>> fm = Maps.newHashMap(); |
| 168 | 1 | if (nonmap) { |
| 169 | 0 | for (Entry<String, Object> e : m.entrySet()) { |
| 170 | 0 | if (e.getValue() instanceof Map) { |
| 171 | 0 | fm.put(e.getKey(), (Map<String, String>) e.getValue()); |
| 172 | 0 | } else { |
| 173 | |
|
| 174 | 0 | Map<String, String> mv = Maps.newHashMap(); |
| 175 | 0 | mv.put(e.getKey(), (String) e.getValue()); |
| 176 | 0 | fm.put(e.getKey(), mv); |
| 177 | |
} |
| 178 | 0 | } |
| 179 | |
|
| 180 | 0 | } else { |
| 181 | 1 | for (Entry<String, Object> e : m.entrySet()) { |
| 182 | 1 | fm.put(e.getKey(), (Map<String, String>) e.getValue()); |
| 183 | 1 | } |
| 184 | |
} |
| 185 | 1 | return new DataCollection(fm); |
| 186 | |
} |
| 187 | |
|
| 188 | |
} |