01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.convert.support;
17:
18: import org.springframework.binding.convert.ConversionContext;
19: import org.springframework.binding.expression.Expression;
20: import org.springframework.binding.expression.ExpressionParser;
21: import org.springframework.binding.expression.SettableExpression;
22: import org.springframework.binding.expression.support.StaticExpression;
23: import org.springframework.util.Assert;
24:
25: /**
26: * Converter that converts a String into an Expression object.
27: *
28: * @see org.springframework.binding.expression.Expression
29: * @see org.springframework.binding.expression.SettableExpression
30: *
31: * @author Erwin Vervaet
32: */
33: public class TextToExpression extends AbstractConverter {
34:
35: /**
36: * The expression string parser.
37: */
38: private ExpressionParser expressionParser;
39:
40: /**
41: * Creates a new string-to-expression converter.
42: * @param expressionParser the expression string parser
43: */
44: public TextToExpression(ExpressionParser expressionParser) {
45: Assert.notNull(expressionParser,
46: "The expression parser is required");
47: this .expressionParser = expressionParser;
48: }
49:
50: /**
51: * Returns the expression parser used by this converter.
52: */
53: public ExpressionParser getExpressionParser() {
54: return expressionParser;
55: }
56:
57: public Class[] getSourceClasses() {
58: return new Class[] { String.class };
59: }
60:
61: public Class[] getTargetClasses() {
62: return new Class[] { Expression.class, SettableExpression.class };
63: }
64:
65: protected Object doConvert(Object source, Class targetClass,
66: ConversionContext context) throws Exception {
67: String expressionString = (String) source;
68: if (getExpressionParser().isDelimitedExpression(
69: expressionString)) {
70: return getExpressionParser().parseExpression(
71: (String) source);
72: } else {
73: return new StaticExpression(expressionString);
74: }
75: }
76: }
|