package iwa.xml;
import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.AbstractMap; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry;
import org.junit.Test;
import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.converters.UnmarshallingContext; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
public class MapConverterTest {
@Test public void marshal() throws Exception { Map<String,Object> map = new HashMap<String,Object>(); map.put("name","chris"); map.put("island","faranga"); Map<String,Object> map2 = new HashMap<String,Object>(); map2.put("test", "테스트"); map2.put("code", "1"); map.put("tests", map2); Map<String,Object> map3 = new HashMap<String,Object>(); map3.put("national", "korea"); map3.put("dev", "kkk"); map2.put("devList", map3);
XStream x = new XStream(); x.alias("root", Map.class); x.registerConverter(new MapEntryConverter()); String xml = x.toXML(map); System.out.println(xml); } @Test public void unmarshal() throws Exception { String xml = "<root><tests><test>테스트</test><code>1</code><devList><dev>kkk</dev><national>korea</national></devList></tests><name>chris</name><island>faranga</island></root>"; XStream x = new XStream(); x.autodetectAnnotations(true); x.alias("root", HashMap.class); x.registerConverter(new MapEntryConverter()); InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8")); HashMap<String, Object> o = (HashMap<String, Object>)(x.fromXML(is)); System.out.println(o); } public static class MapEntryConverter implements Converter { public boolean canConvert(Class clazz) { return AbstractMap.class.isAssignableFrom(clazz); }
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) { AbstractMap map = (AbstractMap) value; for (Object obj : map.entrySet()) { Entry entry = (Entry) obj; Object entryValue = entry.getValue(); if(entryValue instanceof Map) { writer.startNode(entry.getKey().toString()); marshal(entry.getValue(), writer, context); writer.endNode(); continue; } writer.startNode(entry.getKey().toString()); writer.setValue(entryValue.toString()); writer.endNode(); } } public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { Map<String, Object> map = new HashMap<String, Object>();
while(reader.hasMoreChildren()) { reader.moveDown(); if(reader.hasMoreChildren()) { Map<String, Object> childMap = new HashMap<String, Object>(); map.put(reader.getNodeName(), childMap); unmarshalHierarchical(reader, context, childMap); reader.moveUp(); continue; } map.put(reader.getNodeName(), reader.getValue()); reader.moveUp(); } return map; } private void unmarshalHierarchical(HierarchicalStreamReader reader, UnmarshallingContext context, Map<String, Object> map) { while(reader.hasMoreChildren()) { reader.moveDown(); if(reader.hasMoreChildren()) { Map<String, Object> childMap = new HashMap<String, Object>(); map.put(reader.getNodeName(), childMap); unmarshalHierarchical(reader, context, childMap); reader.moveUp(); continue; } map.put(reader.getNodeName(), reader.getValue()); reader.moveUp(); } } } }
|