org.springframework.expression.common
Class TemplateAwareExpressionParser

java.lang.Object
  extended by org.springframework.expression.common.TemplateAwareExpressionParser
All Implemented Interfaces:
ExpressionParser
Direct Known Subclasses:
InternalSpelExpressionParser, SpelExpressionParser

public abstract class TemplateAwareExpressionParser
extends java.lang.Object
implements ExpressionParser

An expression parser that understands templates. It can be subclassed by expression parsers that do not offer first class support for templating.

Since:
3.0
Author:
Keith Donald, Juergen Hoeller, Andy Clement

Nested Class Summary
private static class TemplateAwareExpressionParser.Bracket
          This captures a type of bracket and the position in which it occurs in the expression.
 
Field Summary
private static ParserContext NON_TEMPLATE_PARSER_CONTEXT
          Default ParserContext instance for non-template expressions.
 
Constructor Summary
TemplateAwareExpressionParser()
           
 
Method Summary
private  Expression createLiteralExpression(ParserContext context, java.lang.String text)
           
protected abstract  Expression doParseExpression(java.lang.String expressionString, ParserContext context)
          Actually parse the expression string and return an Expression object.
private  boolean isSuffixHere(java.lang.String expressionString, int pos, java.lang.String suffix)
          Return true if the specified suffix can be found at the supplied position in the supplied expression string.
 Expression parseExpression(java.lang.String expressionString)
          Parse the expression string and return an Expression object you can use for repeated evaluation.
 Expression parseExpression(java.lang.String expressionString, ParserContext context)
          Parse the expression string and return an Expression object you can use for repeated evaluation.
private  Expression[] parseExpressions(java.lang.String expressionString, ParserContext context)
          Helper that parses given expression string using the configured parser.
private  Expression parseTemplate(java.lang.String expressionString, ParserContext context)
           
private  int skipToCorrectEndSuffix(java.lang.String prefix, java.lang.String suffix, java.lang.String expressionString, int afterPrefixIndex)
          Copes with nesting, for example '${...${...}}' where the correct end for the first ${ is the final }.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NON_TEMPLATE_PARSER_CONTEXT

private static final ParserContext NON_TEMPLATE_PARSER_CONTEXT
Default ParserContext instance for non-template expressions.

Constructor Detail

TemplateAwareExpressionParser

public TemplateAwareExpressionParser()
Method Detail

parseExpression

public Expression parseExpression(java.lang.String expressionString)
                           throws ParseException
Description copied from interface: ExpressionParser
Parse the expression string and return an Expression object you can use for repeated evaluation.

Some examples:

     3 + 4
     name.firstName
 

Specified by:
parseExpression in interface ExpressionParser
Parameters:
expressionString - the raw expression string to parse
Returns:
an evaluator for the parsed expression
Throws:
ParseException - an exception occurred during parsing

parseExpression

public Expression parseExpression(java.lang.String expressionString,
                                  ParserContext context)
                           throws ParseException
Description copied from interface: ExpressionParser
Parse the expression string and return an Expression object you can use for repeated evaluation.

Some examples:

     3 + 4
     name.firstName
 

Specified by:
parseExpression in interface ExpressionParser
Parameters:
expressionString - the raw expression string to parse
context - a context for influencing this expression parsing routine (optional)
Returns:
an evaluator for the parsed expression
Throws:
ParseException - an exception occurred during parsing

parseTemplate

private Expression parseTemplate(java.lang.String expressionString,
                                 ParserContext context)
                          throws ParseException
Throws:
ParseException

parseExpressions

private Expression[] parseExpressions(java.lang.String expressionString,
                                      ParserContext context)
                               throws ParseException
Helper that parses given expression string using the configured parser. The expression string can contain any number of expressions all contained in "${...}" markers. For instance: "foo${expr0}bar${expr1}". The static pieces of text will also be returned as Expressions that just return that static piece of text. As a result, evaluating all returned expressions and concatenating the results produces the complete evaluated string. Unwrapping is only done of the outermost delimiters found, so the string 'hello ${foo${abc}}' would break into the pieces 'hello ' and 'foo${abc}'. This means that expression languages that used ${..} as part of their functionality are supported without any problem. The parsing is aware of the structure of an embedded expression. It assumes that parentheses '(', square brackets '[' and curly brackets '}' must be in pairs within the expression unless they are within a string literal and a string literal starts and terminates with a single quote '.

Parameters:
expressionString - the expression string
Returns:
the parsed expressions
Throws:
ParseException - when the expressions cannot be parsed

createLiteralExpression

private Expression createLiteralExpression(ParserContext context,
                                           java.lang.String text)

isSuffixHere

private boolean isSuffixHere(java.lang.String expressionString,
                             int pos,
                             java.lang.String suffix)
Return true if the specified suffix can be found at the supplied position in the supplied expression string.

Parameters:
expressionString - the expression string which may contain the suffix
pos - the start position at which to check for the suffix
suffix - the suffix string
Returns:

skipToCorrectEndSuffix

private int skipToCorrectEndSuffix(java.lang.String prefix,
                                   java.lang.String suffix,
                                   java.lang.String expressionString,
                                   int afterPrefixIndex)
                            throws ParseException
Copes with nesting, for example '${...${...}}' where the correct end for the first ${ is the final }.

Parameters:
prefix - the prefix
suffix - the suffix
expressionString - the expression string
afterPrefixIndex - the most recently found prefix location for which the matching end suffix is being sought
Returns:
the position of the correct matching nextSuffix or -1 if none can be found
Throws:
ParseException

doParseExpression

protected abstract Expression doParseExpression(java.lang.String expressionString,
                                                ParserContext context)
                                         throws ParseException
Actually parse the expression string and return an Expression object.

Parameters:
expressionString - the raw expression string to parse
context - a context for influencing this expression parsing routine (optional)
Returns:
an evaluator for the parsed expression
Throws:
ParseException - an exception occurred during parsing