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.support;
17:
18: import java.io.Serializable;
19:
20: import org.springframework.binding.mapping.AttributeMapper;
21: import org.springframework.binding.mapping.MappingContext;
22: import org.springframework.webflow.core.collection.AttributeMap;
23: import org.springframework.webflow.core.collection.LocalAttributeMap;
24: import org.springframework.webflow.core.collection.MutableAttributeMap;
25: import org.springframework.webflow.engine.FlowAttributeMapper;
26: import org.springframework.webflow.execution.RequestContext;
27:
28: /**
29: * Convenient base class for attribute mapper implementations. Encapsulates
30: * common attribute mapper workflow. Contains no state. Subclasses must override
31: * the {@link #getInputMapper()} and {@link #getOutputMapper()} methods to
32: * return the input mapper and output mapper, respectively.
33: *
34: * @author Keith Donald
35: */
36: public abstract class AbstractFlowAttributeMapper implements
37: FlowAttributeMapper, Serializable {
38:
39: /**
40: * Returns the input mapper to use to map attributes of a parent flow
41: * {@link RequestContext} to a subflow input attribute {@link AttributeMap map}.
42: * @return the input mapper, or null if none
43: * @see #createFlowInput(RequestContext)
44: */
45: protected abstract AttributeMapper getInputMapper();
46:
47: /**
48: * Returns the output mapper to use to map attributes from a subflow output
49: * attribute map to the {@link RequestContext}.
50: * @return the output mapper, or null if none
51: * @see #mapFlowOutput(AttributeMap, RequestContext)
52: */
53: protected abstract AttributeMapper getOutputMapper();
54:
55: public MutableAttributeMap createFlowInput(RequestContext context) {
56: if (getInputMapper() != null) {
57: LocalAttributeMap input = new LocalAttributeMap();
58: // map from request context to input map
59: getInputMapper().map(context, input,
60: getMappingContext(context));
61: return input;
62: } else {
63: // an empty, but modifiable map
64: return new LocalAttributeMap();
65: }
66: }
67:
68: public void mapFlowOutput(AttributeMap subflowOutput,
69: RequestContext context) {
70: if (getOutputMapper() != null && subflowOutput != null) {
71: // map from subflow output map to request context
72: getOutputMapper().map(subflowOutput, context,
73: getMappingContext(context));
74: }
75: }
76:
77: /**
78: * Returns a map of contextual data available during mapping.
79: * This implementation just returns null.
80: */
81: protected MappingContext getMappingContext(RequestContext context) {
82: return null;
83: }
84: }
|