KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > convert > support > TextToExpression


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.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, "The expression parser is required");
46         this.expressionParser = expressionParser;
47     }
48
49     /**
50      * Returns the expression parser used by this converter.
51      */

52     public ExpressionParser getExpressionParser() {
53         return expressionParser;
54     }
55
56     public Class JavaDoc[] getSourceClasses() {
57         return new Class JavaDoc[] { String JavaDoc.class };
58     }
59
60     public Class JavaDoc[] getTargetClasses() {
61         return new Class JavaDoc[] { Expression.class, SettableExpression.class };
62     }
63
64     protected Object JavaDoc doConvert(Object JavaDoc source, Class JavaDoc targetClass, ConversionContext context) throws Exception JavaDoc {
65         String JavaDoc expressionString = (String JavaDoc)source;
66         if (getExpressionParser().isDelimitedExpression(expressionString)) {
67             return getExpressionParser().parseExpression((String JavaDoc)source);
68         }
69         else {
70             return new StaticExpression(expressionString);
71         }
72     }
73 }
Popular Tags