KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > StringTrimmerEditor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.propertyeditors;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20
21 import org.springframework.util.StringUtils;
22
23 /**
24  * Property editor that trims Strings.
25  *
26  * <p>Optionally allows transforming an empty string into a <code>null</code> value.
27  * Needs to be explictly registered, e.g. for command binding.
28  *
29  * @author Juergen Hoeller
30  * @see org.springframework.validation.DataBinder#registerCustomEditor
31  * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder
32  */

33 public class StringTrimmerEditor extends PropertyEditorSupport JavaDoc {
34
35     private final String JavaDoc charsToDelete;
36
37     private final boolean emptyAsNull;
38
39
40     /**
41      * Create a new StringTrimmerEditor instance.
42      * @param emptyAsNull <code>true</code> if an empty string is to be transformed into <code>null</code>
43      */

44     public StringTrimmerEditor(boolean emptyAsNull) {
45         this.charsToDelete = null;
46         this.emptyAsNull = emptyAsNull;
47     }
48
49     /**
50      * Create a new StringTrimmerEditor instance.
51      * @param charsToDelete a set of characters to delete, in addition to
52      * trimming an input String. Useful for deleting unwanted line breaks.
53      * E.g. "\r\n\f" will delete all new lines and line feeds in a String.
54      * @param emptyAsNull <code>true</code> if an empty string is to be transformed into <code>null</code>
55      */

56     public StringTrimmerEditor(String JavaDoc charsToDelete, boolean emptyAsNull) {
57         this.charsToDelete = charsToDelete;
58         this.emptyAsNull = emptyAsNull;
59     }
60
61
62     public void setAsText(String JavaDoc text) {
63         if (text == null) {
64             setValue(null);
65         }
66         else {
67             String JavaDoc 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 JavaDoc getAsText() {
81         Object JavaDoc value = getValue();
82         return (value != null ? value.toString() : "");
83     }
84
85 }
86
Popular Tags