001/*
002 * Copyright 2002-2019 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.lang.Nullable;
022import org.springframework.util.StringUtils;
023
024/**
025 * Property editor that trims Strings.
026 *
027 * <p>Optionally allows transforming an empty string into a {@code null} value.
028 * Needs to be explicitly registered, e.g. for command binding.
029 *
030 * @author Juergen Hoeller
031 * @see org.springframework.validation.DataBinder#registerCustomEditor
032 */
033public class StringTrimmerEditor extends PropertyEditorSupport {
034
035        @Nullable
036        private final String charsToDelete;
037
038        private final boolean emptyAsNull;
039
040
041        /**
042         * Create a new StringTrimmerEditor.
043         * @param emptyAsNull {@code true} if an empty String is to be
044         * transformed into {@code null}
045         */
046        public StringTrimmerEditor(boolean emptyAsNull) {
047                this.charsToDelete = null;
048                this.emptyAsNull = emptyAsNull;
049        }
050
051        /**
052         * Create a new StringTrimmerEditor.
053         * @param charsToDelete a set of characters to delete, in addition to
054         * trimming an input String. Useful for deleting unwanted line breaks:
055         * e.g. "\r\n\f" will delete all new lines and line feeds in a String.
056         * @param emptyAsNull {@code true} if an empty String is to be
057         * transformed into {@code null}
058         */
059        public StringTrimmerEditor(String charsToDelete, boolean emptyAsNull) {
060                this.charsToDelete = charsToDelete;
061                this.emptyAsNull = emptyAsNull;
062        }
063
064
065        @Override
066        public void setAsText(@Nullable String text) {
067                if (text == null) {
068                        setValue(null);
069                }
070                else {
071                        String value = text.trim();
072                        if (this.charsToDelete != null) {
073                                value = StringUtils.deleteAny(value, this.charsToDelete);
074                        }
075                        if (this.emptyAsNull && value.isEmpty()) {
076                                setValue(null);
077                        }
078                        else {
079                                setValue(value);
080                        }
081                }
082        }
083
084        @Override
085        public String getAsText() {
086                Object value = getValue();
087                return (value != null ? value.toString() : "");
088        }
089
090}