KickJava   Java API By Example, From Geeks To Geeks.

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


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.ConversionException;
20 import org.springframework.binding.convert.support.AbstractConverter;
21 import org.springframework.binding.expression.Expression;
22 import org.springframework.util.StringUtils;
23 import org.springframework.webflow.engine.TransitionCriteria;
24 import org.springframework.webflow.engine.WildcardTransitionCriteria;
25 import org.springframework.webflow.engine.support.BooleanExpressionTransitionCriteria;
26 import org.springframework.webflow.engine.support.EventIdTransitionCriteria;
27
28 /**
29  * Converter that takes an encoded string representation and produces a
30  * corresponding <code>TransitionCriteria</code> object.
31  * <p>
32  * This converter supports the following encoded forms:
33  * <ul>
34  * <li>"*" - will result in a TransitionCriteria object that matches on
35  * everything ({@link org.springframework.webflow.engine.WildcardTransitionCriteria})
36  * </li>
37  * <li>"eventId" - will result in a TransitionCriteria object that matches
38  * given event id ({@link org.springframework.webflow.engine.support.EventIdTransitionCriteria})
39  * </li>
40  * <li>"${...}" - will result in a TransitionCriteria object that evaluates
41  * given condition, expressed as an expression
42  * ({@link org.springframework.webflow.engine.support.BooleanExpressionTransitionCriteria})
43  * </li>
44  * <li>"bean:&lt;id&gt;" - will result in usage of a custom TransitionCriteria
45  * bean implementation.</li>
46  * </ul>
47  *
48  * @see org.springframework.webflow.engine.TransitionCriteria
49  *
50  * @author Keith Donald
51  * @author Erwin Vervaet
52  */

53 public class TextToTransitionCriteria extends AbstractConverter {
54
55     /**
56      * Prefix used when the user wants to use a custom TransitionCriteria
57      * implementation managed by a bean factory.
58      */

59     private static final String JavaDoc BEAN_PREFIX = "bean:";
60
61     /**
62      * Locator to use for loading custom TransitionCriteria beans.
63      */

64     private FlowServiceLocator flowServiceLocator;
65
66     /**
67      * Create a new converter that converts strings to transition criteria
68      * objects. Custom transition criteria will be looked up using given
69      * service locator.
70      */

71     public TextToTransitionCriteria(FlowServiceLocator flowServiceLocator) {
72         this.flowServiceLocator = flowServiceLocator;
73     }
74
75     public Class JavaDoc[] getSourceClasses() {
76         return new Class JavaDoc[] { String JavaDoc.class };
77     }
78
79     public Class JavaDoc[] getTargetClasses() {
80         return new Class JavaDoc[] { TransitionCriteria.class };
81     }
82
83     protected Object JavaDoc doConvert(Object JavaDoc source, Class JavaDoc targetClass, ConversionContext context) throws Exception JavaDoc {
84         String JavaDoc encodedCriteria = (String JavaDoc)source;
85         if (!StringUtils.hasText(encodedCriteria)
86                 || WildcardTransitionCriteria.WILDCARD_EVENT_ID.equals(encodedCriteria)) {
87             return WildcardTransitionCriteria.INSTANCE;
88         }
89         else if (flowServiceLocator.getExpressionParser().isDelimitedExpression(encodedCriteria)) {
90             Expression expression = flowServiceLocator.getExpressionParser().parseExpression(encodedCriteria);
91             return createBooleanExpressionTransitionCriteria(expression);
92         }
93         else if (encodedCriteria.startsWith(BEAN_PREFIX)) {
94             return flowServiceLocator.getTransitionCriteria(encodedCriteria.substring(BEAN_PREFIX.length()));
95         }
96         else {
97             return createEventIdTransitionCriteria(encodedCriteria);
98         }
99     }
100
101     /**
102      * Hook method subclasses can override to return a specialized eventId
103      * matching transition criteria implementation.
104      * @param eventId the event id to match
105      * @return the transition criteria object
106      * @throws ConversionException when something goes wrong
107      */

108     protected TransitionCriteria createEventIdTransitionCriteria(String JavaDoc eventId) throws ConversionException {
109         return new EventIdTransitionCriteria(eventId);
110     }
111
112     /**
113      * Hook method subclasses can override to return a specialized expression
114      * evaluating transition criteria implementation.
115      * @param expression the expression to evaluate
116      * @return the transition criteria object
117      * @throws ConversionException when something goes wrong
118      */

119     protected TransitionCriteria createBooleanExpressionTransitionCriteria(Expression expression)
120             throws ConversionException {
121         return new BooleanExpressionTransitionCriteria(expression);
122     }
123 }
Popular Tags