1 16 17 package org.springframework.beans.propertyeditors; 18 19 import java.beans.PropertyEditorSupport ; 20 21 import org.springframework.util.StringUtils; 22 23 33 public class StringTrimmerEditor extends PropertyEditorSupport { 34 35 private final String charsToDelete; 36 37 private final boolean emptyAsNull; 38 39 40 44 public StringTrimmerEditor(boolean emptyAsNull) { 45 this.charsToDelete = null; 46 this.emptyAsNull = emptyAsNull; 47 } 48 49 56 public StringTrimmerEditor(String charsToDelete, boolean emptyAsNull) { 57 this.charsToDelete = charsToDelete; 58 this.emptyAsNull = emptyAsNull; 59 } 60 61 62 public void setAsText(String text) { 63 if (text == null) { 64 setValue(null); 65 } 66 else { 67 String value = text.trim(); 68 if (this.charsToDelete != null) { 69 value = StringUtils.deleteAny(value, this.charsToDelete); 70 } 71 if (this.emptyAsNull && "".equals(value)) { 72 setValue(null); 73 } 74 else { 75 setValue(value); 76 } 77 } 78 } 79 80 public String getAsText() { 81 Object value = getValue(); 82 return (value != null ? value.toString() : ""); 83 } 84 85 } 86 | Popular Tags |