001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans.propertyeditors;
018
019import java.beans.PropertyEditorSupport;
020
021import org.springframework.util.StringUtils;
022
023/**
024 * Property editor that trims Strings.
025 *
026 * <p>Optionally allows transforming an empty string into a {@code null} value.
027 * Needs to be explicitly registered, e.g. for command binding.
028 *
029 * @author Juergen Hoeller
030 * @see org.springframework.validation.DataBinder#registerCustomEditor
031 */
032public class StringTrimmerEditor extends PropertyEditorSupport {
033
034        private final String charsToDelete;
035
036        private final boolean emptyAsNull;
037
038
039        /**
040         * Create a new StringTrimmerEditor.
041         * @param emptyAsNull {@code true} if an empty String is to be
042         * transformed into {@code null}
043         */
044        public StringTrimmerEditor(boolean emptyAsNull) {
045                this.charsToDelete = null;
046                this.emptyAsNull = emptyAsNull;
047        }
048
049        /**
050         * Create a new StringTrimmerEditor.
051         * @param charsToDelete a set of characters to delete, in addition to
052         * trimming an input String. Useful for deleting unwanted line breaks:
053         * e.g. "\r\n\f" will delete all new lines and line feeds in a String.
054         * @param emptyAsNull {@code true} if an empty String is to be
055         * transformed into {@code null}
056         */
057        public StringTrimmerEditor(String charsToDelete, boolean emptyAsNull) {
058                this.charsToDelete = charsToDelete;
059                this.emptyAsNull = emptyAsNull;
060        }
061
062
063        @Override
064        public void setAsText(String text) {
065                if (text == null) {
066                        setValue(null);
067                }
068                else {
069                        String value = text.trim();
070                        if (this.charsToDelete != null) {
071                                value = StringUtils.deleteAny(value, this.charsToDelete);
072                        }
073                        if (this.emptyAsNull && "".equals(value)) {
074                                setValue(null);
075                        }
076                        else {
077                                setValue(value);
078                        }
079                }
080        }
081
082        @Override
083        public String getAsText() {
084                Object value = getValue();
085                return (value != null ? value.toString() : "");
086        }
087
088}