Skip to content

Commit ae330c1

Browse files
author
Phillip Webb
committed
Create RelaxedPropertyResolver
Create RelaxedPropertyResolver class that can be used to get values from another PropertyResolver (probably an Environment) using the same relaxed rules as the RelaxedDataBinder. The commit extracts the relaxed naming rules from RelaxedDataBinder into a new RelaxedNames class. Issue: #55621278
1 parent c33f7e2 commit ae330c1

File tree

5 files changed

+556
-64
lines changed

5 files changed

+556
-64
lines changed

spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* case for example).
3838
*
3939
* @author Dave Syer
40+
* @see RelaxedNames
4041
*/
4142
public class RelaxedDataBinder extends DataBinder {
4243

@@ -224,76 +225,19 @@ private void extendMapIfNecessary(BeanWrapper wrapper, BeanPath path, int index)
224225

225226
private String getActualPropertyName(BeanWrapper target, String prefix, String name) {
226227
prefix = StringUtils.hasText(prefix) ? prefix + "." : "";
227-
for (Variation variation : Variation.values()) {
228-
for (Manipulation manipulation : Manipulation.values()) {
229-
// Apply all manipulations before attempting variations
230-
String candidate = variation.apply(manipulation.apply(name));
231-
try {
232-
if (target.getPropertyType(prefix + candidate) != null) {
233-
return candidate;
234-
}
235-
}
236-
catch (InvalidPropertyException ex) {
237-
// swallow and continue
228+
for (String candidate : new RelaxedNames(name)) {
229+
try {
230+
if (target.getPropertyType(prefix + candidate) != null) {
231+
return candidate;
238232
}
239233
}
234+
catch (InvalidPropertyException ex) {
235+
// swallow and continue
236+
}
240237
}
241238
return name;
242239
}
243240

244-
static enum Variation {
245-
NONE {
246-
@Override
247-
public String apply(String value) {
248-
return value;
249-
}
250-
},
251-
UPPERCASE {
252-
@Override
253-
public String apply(String value) {
254-
return value.toUpperCase();
255-
}
256-
},
257-
258-
LOWERCASE {
259-
@Override
260-
public String apply(String value) {
261-
return value.toLowerCase();
262-
}
263-
};
264-
265-
public abstract String apply(String value);
266-
}
267-
268-
static enum Manipulation {
269-
NONE {
270-
@Override
271-
public String apply(String value) {
272-
return value;
273-
}
274-
},
275-
UNDERSCORE {
276-
@Override
277-
public String apply(String value) {
278-
return value.replace("-", "_");
279-
}
280-
},
281-
282-
CAMELCASE {
283-
@Override
284-
public String apply(String value) {
285-
StringBuilder builder = new StringBuilder();
286-
for (String field : UNDERSCORE.apply(value).split("_")) {
287-
builder.append(builder.length() == 0 ? field : StringUtils
288-
.capitalize(field));
289-
}
290-
return builder.toString();
291-
}
292-
};
293-
294-
public abstract String apply(String value);
295-
}
296-
297241
static class MapHolder {
298242
private Map<String, Object> map;
299243

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright 2012-2013 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.boot.bind;
18+
19+
import java.util.Iterator;
20+
import java.util.NoSuchElementException;
21+
22+
import org.springframework.util.StringUtils;
23+
24+
/**
25+
* Generates relaxed name variations from a given source.
26+
*
27+
* @author Phillip Webb
28+
* @author Dave Syer
29+
* @see RelaxedDataBinder
30+
* @see RelaxedPropertyResolver
31+
*/
32+
public final class RelaxedNames implements Iterable<String> {
33+
34+
private final String name;
35+
36+
/**
37+
* Create a new {@link RelaxedNames} instance.
38+
*
39+
* @param name the source name. For the maximum number of variations specify the name
40+
* using dashed notation (e.g. {@literal my-property-name}
41+
*/
42+
public RelaxedNames(String name) {
43+
this.name = name;
44+
}
45+
46+
@Override
47+
public Iterator<String> iterator() {
48+
return new RelaxedNamesIterator();
49+
}
50+
51+
private class RelaxedNamesIterator implements Iterator<String> {
52+
53+
private int variation = 0;
54+
55+
private int manipulation = 0;
56+
57+
@Override
58+
public boolean hasNext() {
59+
return (this.variation < Variation.values().length);
60+
}
61+
62+
@Override
63+
public String next() {
64+
if (!hasNext()) {
65+
throw new NoSuchElementException();
66+
}
67+
String result = RelaxedNames.this.name;
68+
result = Manipulation.values()[this.manipulation].apply(result);
69+
result = Variation.values()[this.variation].apply(result);
70+
this.manipulation++;
71+
if (this.manipulation >= Manipulation.values().length) {
72+
this.variation++;
73+
this.manipulation = 0;
74+
}
75+
return result;
76+
}
77+
78+
@Override
79+
public void remove() {
80+
throw new UnsupportedOperationException();
81+
}
82+
83+
}
84+
85+
static enum Variation {
86+
NONE {
87+
@Override
88+
public String apply(String value) {
89+
return value;
90+
}
91+
},
92+
LOWERCASE {
93+
@Override
94+
public String apply(String value) {
95+
return value.toLowerCase();
96+
}
97+
},
98+
UPPERCASE {
99+
@Override
100+
public String apply(String value) {
101+
return value.toUpperCase();
102+
}
103+
};
104+
105+
public abstract String apply(String value);
106+
}
107+
108+
static enum Manipulation {
109+
NONE {
110+
@Override
111+
public String apply(String value) {
112+
return value;
113+
}
114+
},
115+
UNDERSCORE {
116+
@Override
117+
public String apply(String value) {
118+
return value.replace("-", "_");
119+
}
120+
},
121+
CAMELCASE {
122+
@Override
123+
public String apply(String value) {
124+
StringBuilder builder = new StringBuilder();
125+
for (String field : UNDERSCORE.apply(value).split("_")) {
126+
builder.append(builder.length() == 0 ? field : StringUtils
127+
.capitalize(field));
128+
}
129+
return builder.toString();
130+
}
131+
};
132+
133+
public abstract String apply(String value);
134+
}
135+
}

0 commit comments

Comments
 (0)