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.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:<id>" - 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 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(
61: FlowServiceLocator flowServiceLocator) {
62: this .flowServiceLocator = flowServiceLocator;
63: }
64:
65: public Class[] getSourceClasses() {
66: return new Class[] { String.class };
67: }
68:
69: public Class[] getTargetClasses() {
70: return new Class[] { TargetStateResolver.class };
71: }
72:
73: protected Object doConvert(Object source, Class targetClass,
74: ConversionContext context) throws Exception {
75: String targetStateId = (String) source;
76: if (flowServiceLocator.getExpressionParser()
77: .isDelimitedExpression(targetStateId)) {
78: Expression expression = flowServiceLocator
79: .getExpressionParser().parseExpression(
80: targetStateId);
81: return new DefaultTargetStateResolver(expression);
82: } else if (targetStateId.startsWith(BEAN_PREFIX)) {
83: return flowServiceLocator
84: .getTargetStateResolver(targetStateId
85: .substring(BEAN_PREFIX.length()));
86: } else {
87: return new DefaultTargetStateResolver(targetStateId);
88: }
89: }
90: }
|