KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > PropertyMatches


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;
18
19 import java.beans.PropertyDescriptor JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.springframework.util.ObjectUtils;
25 import org.springframework.util.StringUtils;
26
27 /**
28  * Helper class for calculating bean property matches, according to.
29  * Used by BeanWrapperImpl to suggest alternatives for an invalid property name.
30  *
31  * @author Alef Arendsen
32  * @author Arjen Poutsma
33  * @author Juergen Hoeller
34  * @since 2.0
35  * @see #forProperty(String, Class)
36  */

37 final class PropertyMatches {
38
39     //---------------------------------------------------------------------
40
// Static section
41
//---------------------------------------------------------------------
42

43     /** Default maximum property distance: 2 */
44     public static final int DEFAULT_MAX_DISTANCE = 2;
45
46
47     /**
48      * Create PropertyMatches for the given bean property.
49      * @param propertyName the name of the property to find possible matches for
50      * @param beanClass the bean class to search for matches
51      */

52     public static PropertyMatches forProperty(String JavaDoc propertyName, Class JavaDoc beanClass) {
53         return forProperty(propertyName, beanClass, DEFAULT_MAX_DISTANCE);
54     }
55
56     /**
57      * Create PropertyMatches for the given bean property.
58      * @param propertyName the name of the property to find possible matches for
59      * @param beanClass the bean class to search for matches
60      * @param maxDistance the maximum property distance allowed for matches
61      */

62     public static PropertyMatches forProperty(String JavaDoc propertyName, Class JavaDoc beanClass, int maxDistance) {
63         return new PropertyMatches(propertyName, beanClass, maxDistance);
64     }
65
66
67     //---------------------------------------------------------------------
68
// Instance section
69
//---------------------------------------------------------------------
70

71     private final String JavaDoc propertyName;
72
73     private String JavaDoc[] possibleMatches;
74
75
76     /**
77      * Create a new PropertyMatches instance for the given property.
78      */

79     private PropertyMatches(String JavaDoc propertyName, Class JavaDoc beanClass, int maxDistance) {
80         this.propertyName = propertyName;
81         this.possibleMatches = calculateMatches(BeanUtils.getPropertyDescriptors(beanClass), maxDistance);
82     }
83
84
85     /**
86      * Return the calculated possible matches.
87      */

88     public String JavaDoc[] getPossibleMatches() {
89         return possibleMatches;
90     }
91
92     /**
93      * Build an error message for the given invalid property name,
94      * indicating the possible property matches.
95      */

96     public String JavaDoc buildErrorMessage() {
97         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
98         buf.append("Bean property '");
99         buf.append(this.propertyName);
100         buf.append("' is not writable or has an invalid setter method. ");
101
102         if (ObjectUtils.isEmpty(this.possibleMatches)) {
103             buf.append("Does the parameter type of the setter match the return type of the getter?");
104         }
105         else {
106             buf.append("Did you mean ");
107             for (int i = 0; i < this.possibleMatches.length; i++) {
108                 buf.append('\'');
109                 buf.append(this.possibleMatches[i]);
110                 if (i < this.possibleMatches.length - 2) {
111                     buf.append("', ");
112                 }
113                 else if (i == this.possibleMatches.length - 2){
114                     buf.append("', or ");
115                 }
116             }
117             buf.append("'?");
118         }
119         return buf.toString();
120     }
121
122
123     /**
124      * Generate possible property alternatives for the given property and
125      * class. Internally uses the <code>getStringDistance</code> method, which
126      * in turn uses the Levenshtein algorithm to determine the distance between
127      * two Strings.
128      * @param propertyDescriptors the JavaBeans property descriptors to search
129      * @param maxDistance the maximum distance to accept
130      */

131     private String JavaDoc[] calculateMatches(PropertyDescriptor JavaDoc[] propertyDescriptors, int maxDistance) {
132         List JavaDoc candidates = new ArrayList JavaDoc();
133         for (int i = 0; i < propertyDescriptors.length; i++) {
134             if (propertyDescriptors[i].getWriteMethod() != null) {
135                 String JavaDoc possibleAlternative = propertyDescriptors[i].getName();
136                 if (calculateStringDistance(this.propertyName, possibleAlternative) <= maxDistance) {
137                     candidates.add(possibleAlternative);
138                 }
139             }
140         }
141         Collections.sort(candidates);
142         return StringUtils.toStringArray(candidates);
143     }
144
145     /**
146      * Calculate the distance between the given two Strings
147      * according to the Levenshtein algorithm.
148      * @param s1 the first String
149      * @param s2 the second String
150      * @return the distance value
151      */

152     private int calculateStringDistance(String JavaDoc s1, String JavaDoc s2) {
153         if (s1.length() == 0) {
154             return s2.length();
155         }
156         if (s2.length() == 0) {
157             return s1.length();
158         }
159         int d[][] = new int[s1.length() + 1][s2.length() + 1];
160
161         for (int i = 0; i <= s1.length(); i++) {
162             d[i][0] = i;
163         }
164         for (int j = 0; j <= s2.length(); j++) {
165             d[0][j] = j;
166         }
167
168         for (int i = 1; i <= s1.length(); i++) {
169             char s_i = s1.charAt(i - 1);
170             for (int j = 1; j <= s2.length(); j++) {
171                 int cost;
172                 char t_j = s2.charAt(j - 1);
173                 if (s_i == t_j) {
174                     cost = 0;
175                 } else {
176                     cost = 1;
177                 }
178                 d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1),
179                         d[i - 1][j - 1] + cost);
180             }
181         }
182
183         return d[s1.length()][s2.length()];
184     }
185
186 }
187
Popular Tags