KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > mapping > Mapping


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.binding.mapping;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.springframework.binding.convert.ConversionExecutor;
23 import org.springframework.binding.expression.Expression;
24 import org.springframework.binding.expression.SettableExpression;
25 import org.springframework.core.style.ToStringCreator;
26 import org.springframework.util.Assert;
27
28 /**
29  * A single mapping definition, encapulating the information neccessary to map
30  * the result of evaluating an expression on a source object to a property on a
31  * target object, optionally applying a type conversion during the mapping
32  * process.
33  * @author Keith Donald
34  */

35 public class Mapping implements AttributeMapper, Serializable JavaDoc {
36
37     protected static final Log logger = LogFactory.getLog(Mapping.class);
38
39     /**
40      * The source expression to evaluate against a source object to map from.
41      */

42     private final Expression sourceExpression;
43
44     /**
45      * The target expression to set on a target object to map to.
46      */

47     private final SettableExpression targetExpression;
48
49     /**
50      * A type converter to apply during the mapping process.
51      */

52     private final ConversionExecutor typeConverter;
53
54     /**
55      * Whether or not this is a required mapping; if true, the source expression
56      * must return a non-null value.
57      */

58     private boolean required;
59
60     /**
61      * Creates a new mapping.
62      * @param sourceExpression the source expression
63      * @param targetExpression the target expression
64      * @param typeConverter a type converter
65      */

66     public Mapping(Expression sourceExpression, SettableExpression targetExpression,
67             ConversionExecutor typeConverter) {
68         this(sourceExpression, targetExpression, typeConverter, false);
69     }
70
71     /**
72      * Creates a new mapping.
73      * @param sourceExpression the source expression
74      * @param targetExpression the target expression
75      * @param typeConverter a type converter
76      * @param required whether or not this mapping is required
77      */

78     protected Mapping(Expression sourceExpression, SettableExpression targetExpression,
79             ConversionExecutor typeConverter, boolean required) {
80         Assert.notNull(sourceExpression, "The source expression is required");
81         Assert.notNull(targetExpression, "The target expression is required");
82         this.sourceExpression = sourceExpression;
83         this.targetExpression = targetExpression;
84         this.typeConverter = typeConverter;
85         this.required = required;
86     }
87
88     /**
89      * Map the <code>sourceAttribute</code> in to the
90      * <code>targetAttribute</code> target map, performing type conversion if
91      * necessary.
92      * @param source The source data structure
93      * @param target The target data structure
94      */

95     public void map(Object JavaDoc source, Object JavaDoc target, MappingContext context) {
96         // get source value
97
Object JavaDoc sourceValue = sourceExpression.evaluate(source, null);
98         if (sourceValue == null) {
99             if (required) {
100                 throw new RequiredMappingException("This mapping is required; evaluation of expression '"
101                         + sourceExpression + "' against source of type [" + source.getClass()
102                         + "] must return a non-null value");
103             }
104             else {
105                 // source expression returned no value, simply abort mapping
106
return;
107             }
108         }
109         Object JavaDoc targetValue = sourceValue;
110         if (typeConverter != null) {
111             targetValue = typeConverter.execute(sourceValue);
112         }
113         // set target value
114
if (logger.isDebugEnabled()) {
115             logger.debug("Mapping '" + sourceExpression + "' value [" + sourceValue + "] to target property '"
116                     + targetExpression + "'; setting property value to [" + targetValue + "]");
117         }
118         targetExpression.evaluateToSet(target, targetValue, null);
119     }
120
121     public boolean equals(Object JavaDoc o) {
122         if (!(o instanceof Mapping)) {
123             return false;
124         }
125         Mapping other = (Mapping)o;
126         return sourceExpression.equals(other.sourceExpression)
127                 && targetExpression.equals(other.targetExpression);
128     }
129
130     public int hashCode() {
131         return sourceExpression.hashCode() + targetExpression.hashCode();
132     }
133
134     public String JavaDoc toString() {
135         return new ToStringCreator(this).append(sourceExpression + " -> " + targetExpression).toString();
136     }
137 }
Popular Tags