1 16 17 package org.springframework.beans.propertyeditors; 18 19 import java.beans.PropertyEditorSupport ; 20 import java.io.ByteArrayInputStream ; 21 import java.io.IOException ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 26 40 public class PropertiesEditor extends PropertyEditorSupport { 41 42 46 private final static String COMMENT_MARKERS = "#!"; 47 48 49 54 public void setAsText(String text) throws IllegalArgumentException { 55 Properties props = new Properties (); 56 if (text != null) { 57 try { 58 props.load(new ByteArrayInputStream (text.getBytes("ISO-8859-1"))); 60 dropComments(props); 61 } 62 catch (IOException ex) { 63 throw new IllegalArgumentException ( 65 "Failed to parse [" + text + "] into Properties: " + ex.getMessage()); 66 } 67 } 68 setValue(props); 69 } 70 71 74 public void setValue(Object value) { 75 if (!(value instanceof Properties ) && value instanceof Map ) { 76 Properties props = new Properties (); 77 props.putAll((Map ) value); 78 super.setValue(props); 79 } 80 else { 81 super.setValue(value); 82 } 83 } 84 85 90 private void dropComments(Properties props) { 91 Iterator keys = props.keySet().iterator(); 92 while (keys.hasNext()) { 93 String key = (String ) keys.next(); 94 if (key.length() > 0 && COMMENT_MARKERS.indexOf(key.charAt(0)) != -1) { 96 keys.remove(); 97 } 98 } 99 } 100 101 } 102 | Popular Tags |