KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Editor for a {@link java.lang.Character}, to populate a property
25  * of type <code>Character</code> or <code>char</code> from a String value.
26  *
27  * <p>Note that the JDK does not contain a default
28  * {@link java.beans.PropertyEditor property editor} for <code>char</code>!
29  * {@link org.springframework.beans.BeanWrapperImpl} will register this
30  * editor by default.
31  *
32  * <p>Also supports conversion from a Unicode character sequence; e.g.
33  * <code>u0041</code> ('A').
34  *
35  * @author Juergen Hoeller
36  * @author Rob Harrop
37  * @author Rick Evans
38  * @since 1.2
39  * @see java.lang.Character
40  * @see org.springframework.beans.BeanWrapperImpl
41  */

42 public class CharacterEditor extends PropertyEditorSupport JavaDoc {
43
44     /**
45      * The prefix that identifies a string as being a Unicode character sequence.
46      */

47     private static final String JavaDoc UNICODE_PREFIX = "\\u";
48
49     /**
50      * The length of a Unicode character sequence.
51      */

52     private static final int UNICODE_LENGTH = 6;
53
54
55     private final boolean allowEmpty;
56
57
58     /**
59      * Create a new CharacterEditor instance.
60      * <p>The "allowEmpty" parameter controls whether an empty String is
61      * to be allowed in parsing, i.e. be interpreted as the <code>null</code>
62      * value when {@link #setAsText(String) text is being converted}. If
63      * <code>false</code>, an {@link IllegalArgumentException} will be thrown
64      * at that time.
65      * @param allowEmpty if empty strings are to be allowed
66      */

67     public CharacterEditor(boolean allowEmpty) {
68         this.allowEmpty = allowEmpty;
69     }
70
71
72     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
73         if (this.allowEmpty && !StringUtils.hasText(text)) {
74             // Treat empty String as null value.
75
setValue(null);
76         }
77         else if (text == null) {
78             throw new IllegalArgumentException JavaDoc("null String cannot be converted to char type");
79         }
80         else if (isUnicodeCharacterSequence(text)) {
81             setAsUnicode(text);
82         }
83         else if (text.length() != 1) {
84             throw new IllegalArgumentException JavaDoc("String [" + text + "] with length " +
85                     text.length() + " cannot be converted to char type");
86         }
87         else {
88             setValue(new Character JavaDoc(text.charAt(0)));
89         }
90     }
91
92     public String JavaDoc getAsText() {
93         Object JavaDoc value = getValue();
94         return (value != null ? value.toString() : "");
95     }
96
97
98     private void setAsUnicode(String JavaDoc text) {
99         int code = Integer.parseInt(text.substring(UNICODE_PREFIX.length()), 16);
100         setValue(new Character JavaDoc((char) code));
101     }
102
103
104     private static boolean isUnicodeCharacterSequence(String JavaDoc sequence) {
105         return sequence.startsWith(UNICODE_PREFIX)
106                 && sequence.length() == UNICODE_LENGTH;
107     }
108
109 }
110
Popular Tags