Allow SpEL reserved words in type package names

Expand the kinds of tokens considered when parsing qualified type names.
This allows previously reserved words (for example 'mod') to be used as
part of a package name.

Issue: SPR-9862
This commit is contained in:
Phillip Webb
2012-10-09 21:18:51 -07:00
committed by Chris Beams
parent 7c399d795e
commit edce2e7bca
4 changed files with 103 additions and 22 deletions

View File

@@ -17,8 +17,10 @@
package org.springframework.expression.spel.standard;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.regex.Pattern;
import org.springframework.expression.ParseException;
import org.springframework.expression.ParserContext;
@@ -29,6 +31,7 @@ import org.springframework.expression.spel.SpelParseException;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.ast.*;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Hand written SpEL parser. Instances are reusable but are not thread safe.
@@ -38,6 +41,8 @@ import org.springframework.util.Assert;
*/
class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
private static final Pattern VALID_QUALIFIED_ID_PATTERN = Pattern.compile("[\\p{L}\\p{N}_$]+");
// The expression being parsed
private String expressionString;
@@ -567,14 +572,35 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
* TODO AndyC Could create complete identifiers (a.b.c) here rather than a sequence of them? (a, b, c)
*/
private SpelNodeImpl eatPossiblyQualifiedId() {
List<SpelNodeImpl> qualifiedIdPieces = new ArrayList<SpelNodeImpl>();
Token startnode = eatToken(TokenKind.IDENTIFIER);
qualifiedIdPieces.add(new Identifier(startnode.stringValue(),toPos(startnode)));
while (peekToken(TokenKind.DOT,true)) {
Token node = eatToken(TokenKind.IDENTIFIER);
qualifiedIdPieces.add(new Identifier(node.stringValue(),toPos(node)));
LinkedList<SpelNodeImpl> qualifiedIdPieces = new LinkedList<SpelNodeImpl>();
Token node = peekToken();
while (isValidQualifiedId(node)) {
nextToken();
if(node.kind != TokenKind.DOT) {
qualifiedIdPieces.add(new Identifier(node.stringValue(),toPos(node)));
}
node = peekToken();
}
return new QualifiedIdentifier(toPos(startnode.startpos,qualifiedIdPieces.get(qualifiedIdPieces.size()-1).getEndPosition()),qualifiedIdPieces.toArray(new SpelNodeImpl[qualifiedIdPieces.size()]));
if(qualifiedIdPieces.isEmpty()) {
if(node == null) {
raiseInternalException( expressionString.length(), SpelMessage.OOD);
}
raiseInternalException(node.startpos, SpelMessage.NOT_EXPECTED_TOKEN,
"qualified ID", node.getKind().toString().toLowerCase());
}
int pos = toPos(qualifiedIdPieces.getFirst().getStartPosition(), qualifiedIdPieces.getLast().getEndPosition());
return new QualifiedIdentifier(pos, qualifiedIdPieces.toArray(new SpelNodeImpl[qualifiedIdPieces.size()]));
}
private boolean isValidQualifiedId(Token node) {
if(node == null || node.kind == TokenKind.LITERAL_STRING) {
return false;
}
if(node.kind == TokenKind.DOT || node.kind == TokenKind.IDENTIFIER) {
return true;
}
String value = node.stringValue();
return StringUtils.hasLength(value) && VALID_QUALIFIED_ID_PATTERN.matcher(value).matches();
}
// This is complicated due to the support for dollars in identifiers. Dollars are normally separate tokens but