KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > support > ConfigurableFlowAttributeMapper


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.support;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.springframework.binding.expression.Expression;
21 import org.springframework.binding.expression.ExpressionParser;
22 import org.springframework.binding.expression.SettableExpression;
23 import org.springframework.binding.mapping.AttributeMapper;
24 import org.springframework.binding.mapping.DefaultAttributeMapper;
25 import org.springframework.binding.mapping.Mapping;
26 import org.springframework.core.style.ToStringCreator;
27 import org.springframework.util.Assert;
28 import org.springframework.webflow.core.DefaultExpressionParserFactory;
29 import org.springframework.webflow.execution.ScopeType;
30
31 /**
32  * Generic flow attribute mapper implementation that allows mappings to be
33  * configured in a declarative fashion.
34  * <p>
35  * Two types of mappings may be configured, input mappings and output mappings:
36  * <ol>
37  * <li>Input mappings define the rules for mapping attributes in a parent flow
38  * to a spawning subflow.
39  * <li>Output mappings define the rules for mapping attributes returned from an
40  * ended subflow into the resuming parent.
41  * </ol>
42  * <p>
43  * The mappings defined using the configuration properties fully support bean
44  * property access. So an entry name in a mapping can either be "beanName" or
45  * "beanName.propName". Nested property values are also supported
46  * ("beanName.propName.nestedPropName"). When the <i>from</i> mapping string is
47  * enclosed in "${...}", it will be interpreted as an expression that will be
48  * evaluated against the flow execution request context.
49  *
50  * @see org.springframework.webflow.execution.RequestContext
51  *
52  * @author Erwin Vervaet
53  * @author Keith Donald
54  * @author Colin Sampaleanu
55  */

56 public class ConfigurableFlowAttributeMapper extends AbstractFlowAttributeMapper implements Serializable JavaDoc {
57
58     /**
59      * The expression parser that will parse input and output attribute
60      * expressions.
61      */

62     private ExpressionParser expressionParser = DefaultExpressionParserFactory.getExpressionParser();
63
64     /**
65      * The mapper that maps attributes into a spawning subflow.
66      */

67     private DefaultAttributeMapper inputMapper = new DefaultAttributeMapper();
68
69     /**
70      * The mapper that maps attributes returned by an ended subflow.
71      */

72     private DefaultAttributeMapper outputMapper = new DefaultAttributeMapper();
73
74     /**
75      * Set the expression parser responsible for parsing expression strings into
76      * evaluatable expression objects.
77      */

78     public void setExpressionParser(ExpressionParser expressionParser) {
79         Assert.notNull(expressionParser, "The expression parser is required");
80         this.expressionParser = expressionParser;
81     }
82
83     /**
84      * Adds a new input mapping. Use when you need full control over defining
85      * how a subflow input attribute mapping will be perfomed.
86      * @param inputMapping the input mapping
87      * @return this, to support call chaining
88      */

89     public ConfigurableFlowAttributeMapper addInputMapping(AttributeMapper inputMapping) {
90         inputMapper.addMapping(inputMapping);
91         return this;
92     }
93
94     /**
95      * Adds a collection of input mappings. Use when you need full control over
96      * defining how a subflow input attribute mapping will be perfomed.
97      * @param inputMappings the input mappings
98      */

99     public void addInputMappings(AttributeMapper[] inputMappings) {
100         inputMapper.addMappings(inputMappings);
101     }
102
103     /**
104      * Adds a new output mapping. Use when you need full control over defining
105      * how a subflow output attribute mapping will be perfomed.
106      * @param outputMapping the output mapping
107      * @return this, to support call chaining
108      */

109     public ConfigurableFlowAttributeMapper addOutputMapping(AttributeMapper outputMapping) {
110         outputMapper.addMapping(outputMapping);
111         return this;
112     }
113
114     /**
115      * Adds a collection of output mappings. Use when you need full control over
116      * defining how a subflow output attribute mapping will be perfomed.
117      * @param outputMappings the output mappings
118      */

119     public void addOutputMappings(AttributeMapper[] outputMappings) {
120         outputMapper.addMappings(outputMappings);
121     }
122
123     /**
124      * Adds an input mapping that maps a single attribute in parent <i>flow
125      * scope</i> into the subflow input map. For instance: "x" will result in
126      * the "x" attribute in parent flow scope being mapped into the subflow
127      * input map as "x".
128      * @param attributeName the attribute in flow scope to map into the subflow
129      * @return this, to support call chaining
130      */

131     public ConfigurableFlowAttributeMapper addInputAttribute(String JavaDoc attributeName) {
132         SettableExpression attribute = expressionParser.parseSettableExpression(attributeName);
133         Expression scope = new AttributeExpression(attribute, ScopeType.FLOW);
134         addInputMapping(new Mapping(scope, attribute, null));
135         return this;
136     }
137
138     /**
139      * Adds a collection of input mappings that map attributes in parent <i>flow
140      * scope</i> into the subflow input map. For instance: "x" will result in
141      * the "x" attribute in parent flow scope being mapped into the subflow
142      * input map as "x".
143      * @param attributeNames the attributes in flow scope to map into the
144      * subflow
145      */

146     public void addInputAttributes(String JavaDoc[] attributeNames) {
147         if (attributeNames == null) {
148             return;
149         }
150         for (int i = 0; i < attributeNames.length; i++) {
151             addInputAttribute(attributeNames[i]);
152         }
153     }
154
155     /**
156      * Adds an output mapping that maps a single subflow output attribute into
157      * the <i>flow scope</i> of the resuming parent flow. For instance: "y"
158      * will result in the "y" attribute of the subflow output map being mapped
159      * into the flowscope of the resuming parent flow as "y".
160      * @param attributeName the subflow output attribute to map into the parent
161      * flow scope
162      * @return this, to support call chaining
163      */

164     public ConfigurableFlowAttributeMapper addOutputAttribute(String JavaDoc attributeName) {
165         Expression attribute = expressionParser.parseExpression(attributeName);
166         SettableExpression scope = new AttributeExpression(attribute, ScopeType.FLOW);
167         addOutputMapping(new Mapping(attribute, scope, null));
168         return this;
169     }
170
171     /**
172      * Adds a collection of output mappings that map subflow output attributes
173      * into the scope of the resuming parent flow. For instance: "y" will result
174      * in the "y" attribute of the subflow output map being mapped into the
175      * flowscope of the resuming parent flow as "y".
176      * @param attributeNames the subflow output attributes to map into the
177      * parent flow
178      */

179     public void addOutputAttributes(String JavaDoc[] attributeNames) {
180         if (attributeNames == null) {
181             return;
182         }
183         for (int i = 0; i < attributeNames.length; i++) {
184             addOutputAttribute(attributeNames[i]);
185         }
186     }
187
188     /**
189      * Returns a typed-array of configured input mappings.
190      * @return the configured input mappings
191      */

192     public AttributeMapper[] getInputMappings() {
193         return inputMapper.getMappings();
194     }
195
196     /**
197      * Returns a typed-array of configured output mappings.
198      * @return the configured output mappings
199      */

200     public AttributeMapper[] getOutputMappings() {
201         return outputMapper.getMappings();
202     }
203
204     /**
205      * Returns the configured expression parser. Can be used by subclasses that
206      * build mappings.
207      */

208     protected ExpressionParser getExpressionParser() {
209         return expressionParser;
210     }
211
212     protected AttributeMapper getInputMapper() {
213         return inputMapper;
214     }
215
216     protected AttributeMapper getOutputMapper() {
217         return outputMapper;
218     }
219
220     public String JavaDoc toString() {
221         return new ToStringCreator(this).append("inputMapper", inputMapper).append("outputMapper", outputMapper)
222                 .toString();
223     }
224 }
Popular Tags