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