1 package com.thoughtworks.xstream.converters.collections; 2 3 import com.thoughtworks.xstream.converters.Converter; 4 import com.thoughtworks.xstream.converters.MarshallingContext; 5 import com.thoughtworks.xstream.converters.UnmarshallingContext; 6 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 7 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 8 9 import java.util.Iterator ; 10 import java.util.Map ; 11 import java.util.Properties ; 12 13 23 public class PropertiesConverter implements Converter { 24 25 public boolean canConvert(Class type) { 26 return Properties .class.isAssignableFrom(type); 27 } 28 29 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 30 Properties properties = (Properties ) source; 31 for (Iterator iterator = properties.entrySet().iterator(); iterator.hasNext();) { 32 Map.Entry entry = (Map.Entry ) iterator.next(); 33 writer.startNode("property"); 34 writer.addAttribute("name", entry.getKey().toString()); 35 writer.addAttribute("value", entry.getValue().toString()); 36 writer.endNode(); 37 } 38 } 39 40 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 41 Properties properties = new Properties (); 42 while (reader.hasMoreChildren()) { 43 reader.moveDown(); 44 String name = reader.getAttribute("name"); 45 String value = reader.getAttribute("value"); 46 ; 47 properties.setProperty(name, value); 48 reader.moveUp(); 49 } 50 return properties; 51 } 52 } 53 | Popular Tags |