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.binding.mapping;
17:
18: import java.util.Arrays;
19: import java.util.Iterator;
20: import java.util.LinkedList;
21: import java.util.List;
22:
23: import org.springframework.core.style.ToStringCreator;
24:
25: /**
26: * Generic attributes mapper implementation that allows mappings to be
27: * configured programatically.
28: *
29: * @author Erwin Vervaet
30: * @author Keith Donald
31: * @author Colin Sampaleanu
32: */
33: public class DefaultAttributeMapper implements AttributeMapper {
34:
35: /**
36: * The ordered list of mappings to apply.
37: */
38: private List mappings = new LinkedList();
39:
40: /**
41: * Add a mapping to this mapper.
42: * @param mapping the mapping to add (as an AttributeMapper)
43: * @return this, to support convenient call chaining
44: */
45: public DefaultAttributeMapper addMapping(AttributeMapper mapping) {
46: mappings.add(mapping);
47: return this ;
48: }
49:
50: /**
51: * Add a set of mappings.
52: * @param mappings the mappings
53: */
54: public void addMappings(AttributeMapper[] mappings) {
55: if (mappings == null) {
56: return;
57: }
58: this .mappings.addAll(Arrays.asList(mappings));
59: }
60:
61: /**
62: * Returns this mapper's list of mappings.
63: * @return the list of mappings
64: */
65: public AttributeMapper[] getMappings() {
66: return (AttributeMapper[]) mappings
67: .toArray(new AttributeMapper[mappings.size()]);
68: }
69:
70: public void map(Object source, Object target, MappingContext context) {
71: if (mappings != null) {
72: Iterator it = mappings.iterator();
73: while (it.hasNext()) {
74: AttributeMapper mapping = (AttributeMapper) it.next();
75: mapping.map(source, target, context);
76: }
77: }
78: }
79:
80: public String toString() {
81: return new ToStringCreator(this ).append("mappings", mappings)
82: .toString();
83: }
84: }
|