1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.util;
17
18
19
20
21
22
23
24
25
26
27 public final class ObjectPathExpressionParser {
28
29
30
31
32
33 private static final ThreadLocal<ParseState> TL_EL_PARSE_STATE = new ThreadLocal<ParseState>();
34
35
36
37
38
39 public static interface PathEntry {
40
41
42
43
44
45
46
47
48
49 Object parse(String parentPath, Object node, String next);
50
51 }
52
53
54
55
56
57 private static final class ParseState {
58
59
60
61
62 private int nextScanIndex;
63
64
65
66
67 private int nextTokenIndex;
68
69
70
71
72 private String originalPath;
73
74
75
76
77 private int originalPathIndex;
78
79
80
81
82 private String parentPath;
83
84
85
86
87 private Object currentContinuation;
88
89
90
91
92 private boolean isActive() {
93 return currentContinuation != null;
94 }
95
96
97
98
99 private void reset() {
100 currentContinuation = null;
101 originalPath = null;
102 originalPathIndex = 0;
103 parentPath = null;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 public String prepareNextScan(String path) {
129 nextScanIndex = 0;
130
131 if (path.length() == 0) {
132 throw new IllegalArgumentException("Unexpected end of input " + parentPath);
133 }
134
135 int endOfCollectionReference = indexOfCloseBracket(path, 0);
136
137 if (endOfCollectionReference == -1) {
138 return path;
139 }
140
141
142 StringBuilder pathBuilder = new StringBuilder(path);
143 pathBuilder.deleteCharAt(endOfCollectionReference);
144 pathBuilder.deleteCharAt(0);
145
146
147 char firstChar = pathBuilder.charAt(0);
148 if ((firstChar == '\'' || firstChar == '\"') &&
149 path.charAt(endOfCollectionReference - 1) == firstChar) {
150
151 pathBuilder.deleteCharAt(endOfCollectionReference - 2);
152 pathBuilder.deleteCharAt(0);
153 }
154
155 int diff = path.length() - pathBuilder.length();
156
157
158 nextScanIndex += endOfCollectionReference + 1 - diff;
159
160
161 originalPathIndex += diff;
162
163 return pathBuilder.toString();
164 }
165
166
167
168
169
170
171 public void scan(String path) {
172 nextTokenIndex = -1;
173
174
175 for (int currentIndex = nextScanIndex; currentIndex < path.length(); currentIndex++) {
176 switch (path.charAt(currentIndex)) {
177 case ']':
178
179 throw new IllegalArgumentException("Unmatched ']': " + path);
180
181 case '[':
182 case '.':
183 if (nextTokenIndex == -1) {
184 nextTokenIndex = currentIndex;
185 }
186
187
188 originalPathIndex += nextTokenIndex;
189 return;
190 }
191 }
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 private String step(String path, PathEntry pathEntry) {
208
209 if (nextTokenIndex == -1) {
210
211 currentContinuation = pathEntry.parse(parentPath, currentContinuation, path);
212 parentPath = originalPath.substring(0, originalPathIndex);
213 return null;
214 }
215
216 char nextToken = path.charAt(nextTokenIndex);
217
218 switch (nextToken) {
219
220 case '[':
221
222 currentContinuation = pathEntry.parse(parentPath, currentContinuation,
223 path.substring(0, nextTokenIndex));
224 parentPath = originalPath.substring(0, originalPathIndex);
225 return path.substring(nextTokenIndex);
226
227 case '.':
228
229 currentContinuation = pathEntry.parse(parentPath, currentContinuation,
230 path.substring(0, nextTokenIndex));
231
232
233 parentPath = originalPath.substring(0, originalPathIndex++);
234
235 return path.substring(nextTokenIndex + 1);
236
237 default:
238 throw new IllegalArgumentException("Unexpected '" + nextToken + "' :" + path);
239 }
240 }
241
242 }
243
244
245
246
247
248
249
250
251
252
253
254 public static int indexOfCloseBracket(String path, int leftBracketIndex) {
255 if (path == null || path.length() <= leftBracketIndex || path.charAt(leftBracketIndex) != '[') {
256 return -1;
257 }
258
259 char inQuote = '\0';
260 int pathLen = path.length() - 1;
261 int bracketCount = 1;
262 int currentPos = leftBracketIndex;
263
264 do {
265 char currentChar = path.charAt(++currentPos);
266
267
268 if (inQuote == '\0' && (currentChar == '\'' || currentChar == '\"')) {
269 inQuote = currentChar;
270 } else if (inQuote == currentChar) {
271 inQuote = '\0';
272 }
273
274
275 if (inQuote != '\0') continue;
276
277
278 if (currentChar == '[') bracketCount++;
279 if (currentChar == ']') bracketCount--;
280 } while (currentPos < pathLen && bracketCount > 0);
281
282 if (bracketCount > 0) {
283 throw new IllegalArgumentException("Unmatched '[': " + path);
284 }
285
286 return currentPos;
287 }
288
289
290
291
292
293
294
295
296
297
298
299
300 public static boolean isPath(String propertyName) {
301 if (propertyName == null) {
302 return false;
303 }
304
305 int length = propertyName.length();
306 for (int i = 0; i < length; i++) {
307 char c = propertyName.charAt(i);
308 if (c != '_' && c != '$' && !Character.isLetterOrDigit(c)) {
309 return true;
310 }
311 }
312
313 return false;
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327 @SuppressWarnings("unchecked")
328 public static <T> T parsePathExpression(Object root, String path, final PathEntry pathEntry) {
329
330
331
332
333
334
335
336
337
338 ParseState parseState = (ParseState) TL_EL_PARSE_STATE.get();
339 boolean recycle;
340
341 if (parseState == null) {
342 TL_EL_PARSE_STATE.set(new ParseState());
343 parseState = TL_EL_PARSE_STATE.get();
344 recycle = true;
345 } else if (parseState.isActive()) {
346 ProcessLogger.ntrace("el-parse:", ":nested", 100);
347 parseState = new ParseState();
348 recycle = false;
349 } else {
350 recycle = true;
351 }
352
353 try {
354 parseState.originalPath = path;
355 parseState.originalPathIndex = 0;
356 parseState.parentPath = null;
357 parseState.currentContinuation = pathEntry.parse(null, root, null);
358 while (path != null) {
359 path = parseState.prepareNextScan(path);
360 parseState.scan(path);
361 path = parseState.step(path, pathEntry);
362 }
363 return (T) parseState.currentContinuation;
364 } finally {
365 assert !recycle || parseState == TL_EL_PARSE_STATE.get();
366 parseState.reset();
367 }
368 }
369
370
371
372
373 private ObjectPathExpressionParser() {}
374
375 }