KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > expression > support > AbstractExpressionParser


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.binding.expression.support;
17
18 import java.util.LinkedList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.springframework.binding.expression.Expression;
22 import org.springframework.binding.expression.ExpressionParser;
23 import org.springframework.binding.expression.ParserException;
24 import org.springframework.binding.expression.SettableExpression;
25 import org.springframework.util.StringUtils;
26
27 /**
28  * An expression parser that parses Ognl expressions.
29  * @author Keith Donald
30  */

31 public abstract class AbstractExpressionParser implements ExpressionParser {
32
33     /**
34      * The expression prefix.
35      */

36     private static final String JavaDoc DEFAULT_EXPRESSION_PREFIX = "${";
37
38     /**
39      * The expression suffix.
40      */

41     private static final String JavaDoc DEFAULT_EXPRESSION_SUFFIX = "}";
42
43     /**
44      * The marked expression delimter prefix.
45      */

46     private String JavaDoc expressionPrefix = DEFAULT_EXPRESSION_PREFIX;
47
48     /**
49      * The marked expression delimiter suffix.
50      */

51     private String JavaDoc expressionSuffix = DEFAULT_EXPRESSION_SUFFIX;
52
53     /**
54      * Returns the configured expression delimiter prefix.
55      */

56     public String JavaDoc getExpressionPrefix() {
57         return expressionPrefix;
58     }
59
60     /**
61      * Sets the expression delimiter prefix.
62      */

63     public void setExpressionPrefix(String JavaDoc expressionPrefix) {
64         this.expressionPrefix = expressionPrefix;
65     }
66
67     /**
68      * Returns the expression delimiter suffix.
69      */

70     public String JavaDoc getExpressionSuffix() {
71         return expressionSuffix;
72     }
73
74     /**
75      * Sets the expression delimiter suffix.
76      */

77     public void setExpressionSuffix(String JavaDoc expressionSuffix) {
78         this.expressionSuffix = expressionSuffix;
79     }
80
81     /**
82      * Check whether or not given criteria are expressed as an expression.
83      */

84     public boolean isDelimitedExpression(String JavaDoc expressionString) {
85         int prefixIndex = expressionString.indexOf(getExpressionPrefix());
86         if (prefixIndex == -1) {
87             return false;
88         }
89         int suffixIndex = expressionString.indexOf(getExpressionSuffix(), prefixIndex);
90         if (suffixIndex == -1) {
91             return false;
92         } else {
93             if (suffixIndex == prefixIndex + getExpressionPrefix().length()) {
94                 return false;
95             } else {
96                 return true;
97             }
98         }
99     }
100
101     public final Expression parseExpression(String JavaDoc expressionString) throws ParserException {
102         Expression[] expressions = parseExpressions(expressionString);
103         if (expressions.length == 1) {
104             return expressions[0];
105         } else {
106             return new CompositeStringExpression(expressions);
107         }
108     }
109
110     public abstract SettableExpression parseSettableExpression(String JavaDoc expressionString) throws ParserException,
111             UnsupportedOperationException JavaDoc;
112
113     /**
114      * Helper that parses given expression string using the configured parser.
115      * The expression string can contain any number of expressions all contained
116      * in "${...}" markers. For instance: "foo${expr0}bar${expr1}". The static
117      * pieces of text will also be returned as Expressions that just return that
118      * static piece of text. As a result, evaluating all returned expressions
119      * and concating the results produces the complete evaluated string.
120      * @param expressionString the expression string
121      * @return the parsed expressions
122      * @throws ParserException when the expressions cannot be parsed
123      */

124     private Expression[] parseExpressions(String JavaDoc expressionString) throws ParserException {
125         List JavaDoc expressions = new LinkedList JavaDoc();
126         if (StringUtils.hasText(expressionString)) {
127             int startIdx = 0;
128             while (startIdx < expressionString.length()) {
129                 int prefixIndex = expressionString.indexOf(getExpressionPrefix(), startIdx);
130                 if (prefixIndex >= startIdx) {
131                     // an expression was found
132
if (prefixIndex > startIdx) {
133                         expressions.add(new StaticExpression(expressionString.substring(startIdx, prefixIndex)));
134                         startIdx = prefixIndex;
135                     }
136                     int suffixIndex = expressionString.indexOf(getExpressionSuffix(), prefixIndex);
137                     if (suffixIndex == -1) {
138                         throw new ParserException(expressionString, null, "No ending suffix '" + getExpressionSuffix()
139                                 + "' for expression starting at character " + prefixIndex + ": "
140                                 + expressionString.substring(prefixIndex));
141                     } else if (suffixIndex == prefixIndex + getExpressionPrefix().length()) {
142                         throw new ParserException(expressionString, null, "No expression defined within delimiter '"
143                                 + getExpressionPrefix() + getExpressionSuffix() + "' at character " + prefixIndex);
144                     } else {
145                         String JavaDoc expr = expressionString.substring(prefixIndex + getExpressionPrefix().length(),
146                                 suffixIndex);
147                         expressions.add(doParseExpression(expr));
148                         startIdx = suffixIndex + 1;
149                     }
150                 } else {
151                     if (startIdx == 0) {
152                         // treat entire string as one expression
153
expressions.add(doParseExpression(expressionString));
154                     } else {
155                         // no more ${expressions} found in string
156
expressions.add(new StaticExpression(expressionString.substring(startIdx)));
157                     }
158                     startIdx = expressionString.length();
159                 }
160             }
161         } else {
162             expressions.add(new StaticExpression(expressionString));
163         }
164         return (Expression[]) expressions.toArray(new Expression[expressions.size()]);
165     }
166
167     /**
168      * Template for parsing a filtered expression string. Subclasses should
169      * override.
170      * @param expressionString the expression string
171      * @return the parsed expression
172      */

173     protected abstract Expression doParseExpression(String JavaDoc expressionString);
174
175 }
Popular Tags