KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > method > TextToMethodSignature


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.method;
17
18 import org.springframework.binding.convert.ConversionContext;
19 import org.springframework.binding.convert.ConversionException;
20 import org.springframework.binding.convert.ConversionService;
21 import org.springframework.binding.convert.support.ConversionServiceAwareConverter;
22 import org.springframework.util.StringUtils;
23
24 /**
25  * Converter that takes an encoded string representation and produces a
26  * corresponding <code>MethodKey</code> object.
27  * <p>
28  * This converter supports the following encoded forms:
29  * <ul>
30  * <li> "methodName" - the name of the method to invoke, where the method is
31  * expected to have no arguments. </li>
32  * <li> "methodName(param1Type param1Name, paramNType paramNName)" - the name of
33  * the method to invoke, where the method is expected to have parameters
34  * delimited by a comma. In this example, the method has two parameters. The
35  * type is either the fully-qualified class of the argument OR a known type
36  * alias. The name is the logical name of the argument, which is used during
37  * data binding to retrieve the argument value.
38  * </ul>
39  *
40  * @see MethodSignature
41  *
42  * @author Keith Donald
43  */

44 public class TextToMethodSignature extends ConversionServiceAwareConverter {
45
46     /**
47      * Create a new converter that converts strings to MethodKey objects.
48      */

49     public TextToMethodSignature() {
50     }
51
52     /**
53      * Create a new converter that converts strings to MethodKey objects.
54      */

55     public TextToMethodSignature(ConversionService conversionService) {
56         super(conversionService);
57     }
58
59     public Class JavaDoc[] getSourceClasses() {
60         return new Class JavaDoc[] { String JavaDoc.class };
61     }
62
63     public Class JavaDoc[] getTargetClasses() {
64         return new Class JavaDoc[] { MethodSignature.class };
65     }
66
67     protected Object JavaDoc doConvert(Object JavaDoc source, Class JavaDoc targetClass, ConversionContext context) throws Exception JavaDoc {
68         String JavaDoc encodedMethodKey = (String JavaDoc)source;
69         encodedMethodKey = encodedMethodKey.trim();
70         int openParan = encodedMethodKey.indexOf('(');
71         if (openParan == -1) {
72             return new MethodSignature(encodedMethodKey);
73         }
74         else {
75             String JavaDoc methodName = encodedMethodKey.substring(0, openParan);
76             int closeParan = encodedMethodKey.lastIndexOf(')');
77             if (closeParan == -1) {
78                 throw new ConversionException(encodedMethodKey, MethodSignature.class, null,
79                         "Syntax error: No close parenthesis specified for method parameter list");
80             }
81             String JavaDoc delimParamList = encodedMethodKey.substring(openParan + 1, closeParan);
82             String JavaDoc[] paramArray = StringUtils.commaDelimitedListToStringArray(delimParamList);
83             Parameters params = new Parameters(paramArray.length);
84             for (int i = 0; i < paramArray.length; i++) {
85                 String JavaDoc param = paramArray[i].trim();
86                 String JavaDoc[] typeAndName = StringUtils.split(param, " ");
87                 if (typeAndName != null && typeAndName.length == 2) {
88                     Class JavaDoc type = (Class JavaDoc)converterFor(String JavaDoc.class, Class JavaDoc.class).execute(typeAndName[0]);
89                     params.add(new Parameter(type, parseExpression(typeAndName[1].trim())));
90                 }
91                 else {
92                     params.add(new Parameter(null, parseExpression(param)));
93                 }
94             }
95             return new MethodSignature(methodName, params);
96         }
97     }
98 }
Popular Tags