1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils.expression;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class DefaultResolver implements Resolver {
58
59 private static final char NESTED = '.';
60 private static final char MAPPED_START = '(';
61 private static final char MAPPED_END = ')';
62 private static final char INDEXED_START = '[';
63 private static final char INDEXED_END = ']';
64
65
66
67
68 public DefaultResolver() {
69 }
70
71
72
73
74
75
76
77
78
79 public int getIndex(final String expression) {
80 if (expression == null || expression.length() == 0) {
81 return -1;
82 }
83 for (int i = 0; i < expression.length(); i++) {
84 final char c = expression.charAt(i);
85 if (c == NESTED || c == MAPPED_START) {
86 return -1;
87 } else if (c == INDEXED_START) {
88 final int end = expression.indexOf(INDEXED_END, i);
89 if (end < 0) {
90 throw new IllegalArgumentException("Missing End Delimiter");
91 }
92 final String value = expression.substring(i + 1, end);
93 if (value.length() == 0) {
94 throw new IllegalArgumentException("No Index Value");
95 }
96 int index = 0;
97 try {
98 index = Integer.parseInt(value, 10);
99 } catch (final Exception e) {
100 throw new IllegalArgumentException("Invalid index value '"
101 + value + "'");
102 }
103 return index;
104 }
105 }
106 return -1;
107 }
108
109
110
111
112
113
114
115
116 public String getKey(final String expression) {
117 if (expression == null || expression.length() == 0) {
118 return null;
119 }
120 for (int i = 0; i < expression.length(); i++) {
121 final char c = expression.charAt(i);
122 if (c == NESTED || c == INDEXED_START) {
123 return null;
124 } else if (c == MAPPED_START) {
125 final int end = expression.indexOf(MAPPED_END, i);
126 if (end < 0) {
127 throw new IllegalArgumentException("Missing End Delimiter");
128 }
129 return expression.substring(i + 1, end);
130 }
131 }
132 return null;
133 }
134
135
136
137
138
139
140
141 public String getProperty(final String expression) {
142 if (expression == null || expression.length() == 0) {
143 return expression;
144 }
145 for (int i = 0; i < expression.length(); i++) {
146 final char c = expression.charAt(i);
147 if (c == NESTED) {
148 return expression.substring(0, i);
149 } else if (c == MAPPED_START || c == INDEXED_START) {
150 return expression.substring(0, i);
151 }
152 }
153 return expression;
154 }
155
156
157
158
159
160
161
162
163 public boolean hasNested(final String expression) {
164 if (expression == null || expression.length() == 0) {
165 return false;
166 } else {
167 return (remove(expression) != null);
168 }
169 }
170
171
172
173
174
175
176
177
178 public boolean isIndexed(final String expression) {
179 if (expression == null || expression.length() == 0) {
180 return false;
181 }
182 for (int i = 0; i < expression.length(); i++) {
183 final char c = expression.charAt(i);
184 if (c == NESTED || c == MAPPED_START) {
185 return false;
186 } else if (c == INDEXED_START) {
187 return true;
188 }
189 }
190 return false;
191 }
192
193
194
195
196
197
198
199
200 public boolean isMapped(final String expression) {
201 if (expression == null || expression.length() == 0) {
202 return false;
203 }
204 for (int i = 0; i < expression.length(); i++) {
205 final char c = expression.charAt(i);
206 if (c == NESTED || c == INDEXED_START) {
207 return false;
208 } else if (c == MAPPED_START) {
209 return true;
210 }
211 }
212 return false;
213 }
214
215
216
217
218
219
220
221
222 public String next(final String expression) {
223 if (expression == null || expression.length() == 0) {
224 return null;
225 }
226 boolean indexed = false;
227 boolean mapped = false;
228 for (int i = 0; i < expression.length(); i++) {
229 final char c = expression.charAt(i);
230 if (indexed) {
231 if (c == INDEXED_END) {
232 return expression.substring(0, i + 1);
233 }
234 } else if (mapped) {
235 if (c == MAPPED_END) {
236 return expression.substring(0, i + 1);
237 }
238 } else {
239 if (c == NESTED) {
240 return expression.substring(0, i);
241 } else if (c == MAPPED_START) {
242 mapped = true;
243 } else if (c == INDEXED_START) {
244 indexed = true;
245 }
246 }
247 }
248 return expression;
249 }
250
251
252
253
254
255
256
257
258
259 public String remove(final String expression) {
260 if (expression == null || expression.length() == 0) {
261 return null;
262 }
263 final String property = next(expression);
264 if (expression.length() == property.length()) {
265 return null;
266 }
267 int start = property.length();
268 if (expression.charAt(start) == NESTED) {
269 start++;
270 }
271 return expression.substring(start);
272 }
273 }