KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > builder > TextToTargetStateResolver


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.webflow.engine.builder;
17
18 import org.springframework.binding.convert.ConversionContext;
19 import org.springframework.binding.convert.support.AbstractConverter;
20 import org.springframework.binding.expression.Expression;
21 import org.springframework.webflow.engine.TargetStateResolver;
22 import org.springframework.webflow.engine.support.DefaultTargetStateResolver;
23
24 /**
25  * Converter that takes an encoded string representation and produces a
26  * corresponding {@link TargetStateResolver} object.
27  * <p>
28  * This converter supports the following encoded forms:
29  * <ul>
30  * <li>"stateId" - will result in a TargetStateResolver that always resolves
31  * the same state. </li>
32  * <li>"${stateIdExpression} - will result in a TargetStateResolver that
33  * resolves the target state by evaluating an expression against the request
34  * context.</li>
35  * <li>"bean:&lt;id&gt;" - will result in usage of a custom TargetStateResolver
36  * bean implementation configured in an external context.</li>
37  * </ul>
38  *
39  * @author Keith Donald
40  * @author Erwin Vervaet
41  */

42 public class TextToTargetStateResolver extends AbstractConverter {
43
44     /**
45      * Prefix used when the user wants to use a custom TargetStateResolver
46      * implementation managed by a factory.
47      */

48     private static final String JavaDoc BEAN_PREFIX = "bean:";
49
50     /**
51      * Locator to use for loading custom TargetStateResolver beans.
52      */

53     private FlowServiceLocator flowServiceLocator;
54
55     /**
56      * Create a new converter that converts strings to transition target state
57      * resolver objects. The given conversion service will be used to do all
58      * necessary internal conversion (e.g. parsing expression strings).
59      */

60     public TextToTargetStateResolver(FlowServiceLocator flowServiceLocator) {
61         this.flowServiceLocator = flowServiceLocator;
62     }
63
64     public Class JavaDoc[] getSourceClasses() {
65         return new Class JavaDoc[] { String JavaDoc.class };
66     }
67
68     public Class JavaDoc[] getTargetClasses() {
69         return new Class JavaDoc[] { TargetStateResolver.class };
70     }
71
72     protected Object JavaDoc doConvert(Object JavaDoc source, Class JavaDoc targetClass, ConversionContext context) throws Exception JavaDoc {
73         String JavaDoc targetStateId = (String JavaDoc)source;
74         if (flowServiceLocator.getExpressionParser().isDelimitedExpression(targetStateId)) {
75             Expression expression = flowServiceLocator.getExpressionParser().parseExpression(targetStateId);
76             return new DefaultTargetStateResolver(expression);
77         }
78         else if (targetStateId.startsWith(BEAN_PREFIX)) {
79             return flowServiceLocator.getTargetStateResolver(targetStateId.substring(BEAN_PREFIX.length()));
80         }
81         else {
82             return new DefaultTargetStateResolver(targetStateId);
83         }
84     }
85 }
Popular Tags