-
Notifications
You must be signed in to change notification settings - Fork 682
/
Copy pathDefaultValueExpressionParser.java
183 lines (142 loc) · 6.07 KB
/
DefaultValueExpressionParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.expression;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.spel.ExpressionDependencies;
import org.springframework.expression.Expression;
import org.springframework.expression.ParseException;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.util.Assert;
import org.springframework.util.SystemPropertyUtils;
/**
* Default {@link ValueExpressionParser} implementation. Instances are thread-safe.
*
* @author Mark Paluch
* @since 3.3
*/
class DefaultValueExpressionParser implements ValueExpressionParser {
public static final String PLACEHOLDER_PREFIX = SystemPropertyUtils.PLACEHOLDER_PREFIX;
public static final String EXPRESSION_PREFIX = ParserContext.TEMPLATE_EXPRESSION.getExpressionPrefix();
public static final char SUFFIX = '}';
public static final int PLACEHOLDER_PREFIX_LENGTH = PLACEHOLDER_PREFIX.length();
public static final char[] QUOTE_CHARS = { '\'', '"' };
public static final ValueExpressionParser DEFAULT = new DefaultValueExpressionParser(SpelExpressionParser::new);
private final ValueParserConfiguration configuration;
public DefaultValueExpressionParser(ValueParserConfiguration configuration) {
Assert.notNull(configuration, "ValueParserConfiguration must not be null");
this.configuration = configuration;
}
@Override
public ValueExpression parse(String expressionString) {
int placerholderIndex = expressionString.indexOf(PLACEHOLDER_PREFIX);
int expressionIndex = expressionString.indexOf(EXPRESSION_PREFIX);
if (placerholderIndex == -1 && expressionIndex == -1) {
return new LiteralValueExpression(expressionString);
}
if (placerholderIndex != -1 && expressionIndex == -1
&& findPlaceholderEndIndex(expressionString, placerholderIndex) != expressionString.length()) {
return createPlaceholder(expressionString);
}
if (placerholderIndex == -1
&& findPlaceholderEndIndex(expressionString, expressionIndex) != expressionString.length()) {
return createExpression(expressionString);
}
return parseComposite(expressionString, placerholderIndex, expressionIndex);
}
private CompositeValueExpression parseComposite(String expressionString, int placerholderIndex, int expressionIndex) {
List<ValueExpression> expressions = new ArrayList<>(PLACEHOLDER_PREFIX_LENGTH);
int startIndex = getStartIndex(placerholderIndex, expressionIndex);
if (startIndex != 0) {
expressions.add(new LiteralValueExpression(expressionString.substring(0, startIndex)));
}
while (startIndex != -1) {
int endIndex = findPlaceholderEndIndex(expressionString, startIndex);
if (endIndex == -1) {
throw new ParseException(expressionString, startIndex,
"No ending suffix '}' for expression starting at character %d: %s".formatted(startIndex,
expressionString.substring(startIndex)));
}
int afterClosingParenthesisIndex = endIndex + 1;
String part = expressionString.substring(startIndex, afterClosingParenthesisIndex);
if (part.startsWith(PLACEHOLDER_PREFIX)) {
expressions.add(createPlaceholder(part));
} else {
expressions.add(createExpression(part));
}
placerholderIndex = expressionString.indexOf(PLACEHOLDER_PREFIX, endIndex);
expressionIndex = expressionString.indexOf(EXPRESSION_PREFIX, endIndex);
startIndex = getStartIndex(placerholderIndex, expressionIndex);
if (startIndex == -1) {
// no next expression but we're capturing everything after the expression as literal.
expressions.add(new LiteralValueExpression(expressionString.substring(afterClosingParenthesisIndex)));
} else {
// capture literal after the expression ends and before the next starts.
expressions
.add(new LiteralValueExpression(expressionString.substring(afterClosingParenthesisIndex, startIndex)));
}
}
return new CompositeValueExpression(expressionString, expressions);
}
private static int getStartIndex(int placerholderIndex, int expressionIndex) {
return placerholderIndex != -1 && expressionIndex != -1 ? Math.min(placerholderIndex, expressionIndex)
: placerholderIndex != -1 ? placerholderIndex : expressionIndex;
}
private PlaceholderExpression createPlaceholder(String part) {
return new PlaceholderExpression(part);
}
private ExpressionExpression createExpression(String expression) {
Expression expr = configuration.getExpressionParser().parseExpression(expression,
ParserContext.TEMPLATE_EXPRESSION);
ExpressionDependencies dependencies = ExpressionDependencies.discover(expr);
return new ExpressionExpression(expr, dependencies);
}
private static int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
int index = startIndex + PLACEHOLDER_PREFIX_LENGTH;
char quotationChar = 0;
char nestingLevel = 0;
boolean skipEscape = false;
while (index < buf.length()) {
char c = buf.charAt(index);
if (!skipEscape && c == '\\') {
skipEscape = true;
} else if (skipEscape) {
skipEscape = false;
} else if (quotationChar == 0) {
for (char quoteChar : QUOTE_CHARS) {
if (quoteChar == c) {
quotationChar = c;
break;
}
}
} else if (quotationChar == c) {
quotationChar = 0;
}
if (!skipEscape && quotationChar == 0) {
if (nestingLevel != 0 && c == SUFFIX) {
nestingLevel--;
} else if (c == '{') {
nestingLevel++;
} else if (nestingLevel == 0 && c == SUFFIX) {
return index;
}
}
index++;
}
return -1;
}
}