1 17 18 package org.apache.geronimo.common.propertyeditor; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.IOException ; 22 import java.util.Iterator ; 23 import java.util.Properties ; 24 import java.util.Map ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 34 public class MapEditor 35 extends TextPropertyEditorSupport 36 { 37 private static final Log log = LogFactory.getLog(MapEditor.class); 38 42 public void setAsText(String text) { 43 try { 44 ByteArrayInputStream is = new ByteArrayInputStream (text == null? new byte[0]: text.getBytes()); 45 Properties p = new Properties (); 46 p.load(is); 47 48 setValue((Map )p); 49 } catch (IOException e) { 50 throw new PropertyEditorException(e); 51 } 52 } 53 54 public String getAsText() { 55 Map map = (Map ) getValue(); 56 if (!(map instanceof Properties )) { 57 Properties p = new Properties (); 58 if(map != null) { 59 if(!map.containsKey(null) && !map.containsValue(null)) { 60 p.putAll(map); 61 } else { 62 log.warn("Map contains null keys or values. Replacing null values with empty string."); 64 for(Iterator itr = map.entrySet().iterator(); itr.hasNext(); ) { 65 Map.Entry entry = (Map.Entry ) itr.next(); 66 Object key = entry.getKey(); 67 Object value = entry.getValue(); 68 if(key == null) { 69 key = ""; 70 } 71 if(value == null) { 72 value = ""; 73 } 74 p.put(key, value); 75 } 76 } 77 map = p; 78 } 79 } 80 PropertiesEditor pe = new PropertiesEditor(); 81 pe.setValue(map); 82 return pe.getAsText(); 83 } 84 } 85 | Popular Tags |