From 3cdb942cbe7aaedb89d5f87d311ed10544440293 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Thu, 12 Nov 2009 03:53:59 +0000 Subject: [PATCH] Deferring object mapper until Spring 3.1 when it will be used by other projects --- .../org/springframework/mapping/Mapper.java | 35 - .../mapping/MappingException.java | 99 -- .../mapping/MappingFailure.java | 52 - .../mapping/support/BeanMappableType.java | 59 - .../ConverterMappingTargetFactory.java | 41 - .../support/DefaultMappableTypeFactory.java | 27 - .../support/DefaultMappingTargetFactory.java | 47 - .../mapping/support/FieldToFieldMapping.java | 87 -- .../support/FieldToMultiFieldMapping.java | 77 -- .../mapping/support/FlexibleFieldMapping.java | 82 -- .../mapping/support/MapMappableType.java | 67 -- .../mapping/support/MappableType.java | 52 - .../mapping/support/MappableTypeFactory.java | 48 - .../mapping/support/MapperBuilder.java | 231 ---- .../mapping/support/MapperFactory.java | 58 - .../mapping/support/MappingContextHolder.java | 82 -- .../support/MappingConversionService.java | 29 - .../mapping/support/MappingConverter.java | 79 -- .../mapping/support/MappingTargetFactory.java | 46 - .../support/MultiFieldToFieldMapping.java | 89 -- .../mapping/support/SpelMapper.java | 222 ---- .../mapping/support/SpelMapperBuilder.java | 135 --- .../mapping/support/SpelMapping.java | 34 - .../mapping/support/SpelMappingContext.java | 91 -- .../mapping/support/MappingTests.java | 1003 ----------------- 25 files changed, 2872 deletions(-) delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/Mapper.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/MappingException.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/MappingFailure.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/BeanMappableType.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/ConverterMappingTargetFactory.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/DefaultMappableTypeFactory.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/DefaultMappingTargetFactory.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/FieldToFieldMapping.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/FieldToMultiFieldMapping.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/FlexibleFieldMapping.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/MapMappableType.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/MappableType.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/MappableTypeFactory.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/MapperBuilder.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/MapperFactory.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/MappingContextHolder.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/MappingConversionService.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/MappingConverter.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/MappingTargetFactory.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/MultiFieldToFieldMapping.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapper.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapperBuilder.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapping.java delete mode 100644 org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMappingContext.java delete mode 100644 org.springframework.context/src/test/java/org/springframework/mapping/support/MappingTests.java diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/Mapper.java b/org.springframework.context/src/main/java/org/springframework/mapping/Mapper.java deleted file mode 100644 index 7893a40b7b7..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/Mapper.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping; - -/** - * Maps between a source and target. - * @author Keith Donald - * @param the source type mapped from - * @param the target type mapped to - */ -public interface Mapper { - - /** - * Map the source to the target. - * @param source the source to map from - * @param target the target to map to - * @return the mapped target object - * @throws MappingException if the mapping process failed - */ - T map(S source, T target); - -} diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/MappingException.java b/org.springframework.context/src/main/java/org/springframework/mapping/MappingException.java deleted file mode 100644 index cf5e87aa560..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/MappingException.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping; - -import java.io.PrintStream; -import java.io.PrintWriter; -import java.util.Iterator; -import java.util.List; - -/** - * Thrown in a map operation fails. - * @see Mapper#map(Object, Object) - * @author Keith Donald - */ -public final class MappingException extends RuntimeException { - - private List mappingFailures; - - public MappingException(List mappingFailures) { - super((String) null); - this.mappingFailures = mappingFailures; - } - - public int getMappingFailureCount() { - return this.mappingFailures.size(); - } - - public List getMappingFailures() { - return this.mappingFailures; - } - - @Override - public String getMessage() { - StringBuilder sb = new StringBuilder(getMappingFailureCount() + " mapping failure(s) occurred:"); - int i = 1; - for (Iterator it = this.mappingFailures.iterator(); it.hasNext(); i++) { - MappingFailure failure = it.next(); - sb.append(" #").append(i + ") ").append(failure.getMessage()); - if (it.hasNext()) { - sb.append(","); - } - } - return sb.toString(); - } - - @Override - public void printStackTrace(PrintStream ps) { - super.printStackTrace(ps); - synchronized (ps) { - ps.println(); - ps.println("Mapping Failure Traces:"); - int i = 1; - for (Iterator it = this.mappingFailures.iterator(); it.hasNext(); i++) { - MappingFailure failure = it.next(); - ps.println("- MappingFailure #" + i + ":"); - Throwable t = failure.getCause(); - if (t != null) { - t.printStackTrace(ps); - } else { - ps.println("null"); - } - } - } - } - - @Override - public void printStackTrace(PrintWriter pw) { - super.printStackTrace(pw); - synchronized (pw) { - pw.println(); - pw.println("Mapping Failure Traces:"); - int i = 1; - for (Iterator it = this.mappingFailures.iterator(); it.hasNext(); i++) { - MappingFailure failure = it.next(); - pw.println("- MappingFailure #" + i + ":"); - Throwable t = failure.getCause(); - if (t != null) { - t.printStackTrace(pw); - } else { - pw.println("null"); - } - } - } - } - -} diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/MappingFailure.java b/org.springframework.context/src/main/java/org/springframework/mapping/MappingFailure.java deleted file mode 100644 index d78916d0746..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/MappingFailure.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping; - -/** - * Indicates an individual mapping failed. - * @author Keith Donald - */ -public final class MappingFailure { - - private final Throwable cause; - - /** - * Create a new mapping failure caused by the exception. - * @param cause the failure cause - */ - public MappingFailure(Throwable cause) { - this.cause = cause; - } - - /** - * The failure message. - */ - public String getMessage() { - return getCause().getMessage(); - } - - /** - * The cause of this failure. - */ - public Throwable getCause() { - return cause; - } - - public String toString() { - return "[MappingFailure cause = " + cause + "]"; - } - -} diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/BeanMappableType.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/BeanMappableType.java deleted file mode 100644 index 57009c664ce..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/BeanMappableType.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import java.beans.PropertyDescriptor; -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Set; - -import org.springframework.beans.BeanUtils; -import org.springframework.core.convert.ConversionService; -import org.springframework.expression.EvaluationContext; -import org.springframework.expression.spel.support.StandardEvaluationContext; -import org.springframework.expression.spel.support.StandardTypeConverter; - -final class BeanMappableType implements MappableType { - - public boolean isInstance(Object object) { - return true; - } - - public EvaluationContext getEvaluationContext(Object object, ConversionService conversionService) { - StandardEvaluationContext context = new StandardEvaluationContext(object); - context.setTypeConverter(new StandardTypeConverter(conversionService)); - return context; - } - - public Set getFieldNames(Object object) { - Set fields = new LinkedHashSet(); - PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(object.getClass()); - for (PropertyDescriptor descriptor : descriptors) { - String propertyName = descriptor.getName(); - if (propertyName.equals("class")) { - continue; - } - fields.add(descriptor.getName()); - } - return fields; - } - - public Map getNestedFields(String fieldName, Object object) { - return null; - } - - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/ConverterMappingTargetFactory.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/ConverterMappingTargetFactory.java deleted file mode 100644 index 11a369f6ee6..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/ConverterMappingTargetFactory.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.converter.Converter; - -/** - * Creates a mapping target by calling a converter. - * @author Keith Donald - */ -final class ConverterMappingTargetFactory implements MappingTargetFactory { - - private Converter converter; - - public ConverterMappingTargetFactory(Converter converter) { - this.converter = converter; - } - - public boolean supports(TypeDescriptor targetType) { - return true; - } - - public Object createTarget(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { - return this.converter.convert(source); - } - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/DefaultMappableTypeFactory.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/DefaultMappableTypeFactory.java deleted file mode 100644 index 6a2b08573db..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/DefaultMappableTypeFactory.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -/** - * Default mappable type factory that registers Map and Bean mappable types. - * @author Keith Donald - */ -final class DefaultMappableTypeFactory extends MappableTypeFactory { - public DefaultMappableTypeFactory() { - add(new MapMappableType()); - add(new BeanMappableType()); - } -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/DefaultMappingTargetFactory.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/DefaultMappingTargetFactory.java deleted file mode 100644 index 95fb6bdd304..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/DefaultMappingTargetFactory.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import org.springframework.beans.BeanUtils; -import org.springframework.core.convert.TypeDescriptor; -import org.springframework.util.ClassUtils; - -/** - * Creates a mapping target by calling its default constructor. - * @author Keith Donald - * @see BeanUtils#instantiate(Class) - */ -final class DefaultMappingTargetFactory implements MappingTargetFactory { - - private static final DefaultMappingTargetFactory INSTANCE = new DefaultMappingTargetFactory(); - - private DefaultMappingTargetFactory() { - - } - - public boolean supports(TypeDescriptor targetType) { - return ClassUtils.hasConstructor(targetType.getType(), null); - } - - public Object createTarget(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { - return BeanUtils.instantiate(targetType.getType()); - } - - public static MappingTargetFactory getInstance() { - return INSTANCE; - } - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/FieldToFieldMapping.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/FieldToFieldMapping.java deleted file mode 100644 index f4c481376f9..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/FieldToFieldMapping.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.expression.Expression; - -/** - * A mapping between a source field and a target field. - * @author Keith Donald - */ -final class FieldToFieldMapping implements SpelMapping { - - private final Expression sourceField; - - private final Expression targetField; - - @SuppressWarnings("unchecked") - private final Converter converter; - - private final Expression condition; - - public FieldToFieldMapping(Expression sourceField, Expression targetField, Converter converter, Expression condition) { - this.sourceField = sourceField; - this.targetField = targetField; - this.converter = converter; - this.condition = condition; - } - - public String getSourceField() { - return this.sourceField.getExpressionString(); - } - - public String getTargetField() { - return this.targetField.getExpressionString(); - } - - public boolean mapsField(String field) { - return getSourceField().equals(field); - } - - @SuppressWarnings("unchecked") - public void map(SpelMappingContext context) { - if (!context.conditionHolds(this.condition)) { - return; - } - try { - Object value = context.getSourceFieldValue(this.sourceField); - if (this.converter != null) { - value = this.converter.convert(value); - } - context.setTargetFieldValue(this.targetField, value); - } catch (Exception e) { - context.addMappingFailure(e); - } - } - - public int hashCode() { - return getSourceField().hashCode() + getTargetField().hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof FieldToFieldMapping)) { - return false; - } - FieldToFieldMapping m = (FieldToFieldMapping) o; - return getSourceField().equals(m.getSourceField()) && getTargetField().equals(m.getTargetField()); - } - - public String toString() { - return "[FieldToFieldMapping<" + getSourceField() + " -> " + getTargetField() + ">]"; - } - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/FieldToMultiFieldMapping.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/FieldToMultiFieldMapping.java deleted file mode 100644 index 522d9363b11..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/FieldToMultiFieldMapping.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import org.springframework.expression.Expression; -import org.springframework.mapping.Mapper; - -/** - * A mapping between a source field and several target fields. - * @author Keith Donald - */ -final class FieldToMultiFieldMapping implements SpelMapping { - - private final Expression sourceField; - - @SuppressWarnings("unchecked") - private final Mapper targetFieldMapper; - - private final Expression condition; - - public FieldToMultiFieldMapping(Expression sourceField, Mapper targetFieldMapper, Expression condition) { - this.sourceField = sourceField; - this.targetFieldMapper = targetFieldMapper; - this.condition = condition; - } - - public String getSourceField() { - return this.sourceField.getExpressionString(); - } - - public boolean mapsField(String field) { - return getSourceField().equals(field); - } - - @SuppressWarnings("unchecked") - public void map(SpelMappingContext context) { - if (!context.conditionHolds(this.condition)) { - return; - } - try { - Object value = context.getSourceFieldValue(this.sourceField); - this.targetFieldMapper.map(value, context.getTarget()); - } catch (Exception e) { - context.addMappingFailure(e); - } - } - - public int hashCode() { - return getSourceField().hashCode() + this.targetFieldMapper.hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof FieldToMultiFieldMapping)) { - return false; - } - FieldToMultiFieldMapping m = (FieldToMultiFieldMapping) o; - return getSourceField().equals(m.getSourceField()) && this.targetFieldMapper.equals(m.targetFieldMapper); - } - - public String toString() { - return "[FieldToFieldMapping<" + getSourceField() + " -> " + this.targetFieldMapper + ">]"; - } - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/FlexibleFieldMapping.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/FlexibleFieldMapping.java deleted file mode 100644 index 3bbd4b8478c..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/FlexibleFieldMapping.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import org.springframework.core.style.StylerUtils; -import org.springframework.expression.Expression; -import org.springframework.mapping.Mapper; - -/** - * A mapping between several source fields and one or more target fields. - * @author Keith Donald - */ -final class FlexibleFieldMapping implements SpelMapping { - - private String[] fields; - - @SuppressWarnings("unchecked") - private final Mapper flexibleFieldMapper; - - private Expression condition; - - public FlexibleFieldMapping(String[] fields, Mapper flexibleFieldMapper, Expression condition) { - this.fields = fields; - this.flexibleFieldMapper = flexibleFieldMapper; - this.condition = condition; - } - - public String[] getSourceFields() { - return fields; - } - - public boolean mapsField(String field) { - for (String f : this.fields) { - if (f.equals(field)) { - return true; - } - } - return false; - } - - @SuppressWarnings("unchecked") - public void map(SpelMappingContext context) { - if (!context.conditionHolds(this.condition)) { - return; - } - try { - this.flexibleFieldMapper.map(context.getSource(), context.getTarget()); - } catch (Exception e) { - context.addMappingFailure(e); - } - } - - public int hashCode() { - return this.flexibleFieldMapper.hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof FlexibleFieldMapping)) { - return false; - } - FlexibleFieldMapping m = (FlexibleFieldMapping) o; - return this.flexibleFieldMapper.equals(m.flexibleFieldMapper); - } - - public String toString() { - return "[FlexibleFieldMapping<" + StylerUtils.style(this.fields) + " -> " + this.flexibleFieldMapper + ">]"; - } - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/MapMappableType.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/MapMappableType.java deleted file mode 100644 index 88833bdf46b..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/MapMappableType.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Set; - -import org.springframework.context.expression.MapAccessor; -import org.springframework.core.convert.ConversionService; -import org.springframework.expression.EvaluationContext; -import org.springframework.expression.spel.support.StandardEvaluationContext; -import org.springframework.expression.spel.support.StandardTypeConverter; - -final class MapMappableType implements MappableType> { - - public boolean isInstance(Object object) { - return object instanceof Map; - } - - public EvaluationContext getEvaluationContext(Map object, - ConversionService conversionService) { - StandardEvaluationContext context = new StandardEvaluationContext(object); - context.setTypeConverter(new StandardTypeConverter(conversionService)); - context.addPropertyAccessor(new MapAccessor()); - return context; - } - - public Set getFieldNames(Map object) { - Set fieldNames = new LinkedHashSet(object.size(), 1); - for (Object key : object.keySet()) { - if (key != null && key instanceof String) { - fieldNames.add((String) key); - } - } - return fieldNames; - } - - public Map getNestedFields(String fieldName, Map object) { - Map fields = new LinkedHashMap(object.size(), 1); - for (Map.Entry entry : object.entrySet()) { - Object key = entry.getKey(); - if (key != null && key instanceof String) { - String name = (String) key; - if (name.startsWith(fieldName + ".")) { - fields.put(name.substring(fieldName.length() + 1), entry.getValue()); - } - } - } - return fields; - } - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappableType.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/MappableType.java deleted file mode 100644 index ee970bad5f0..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappableType.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import java.util.Map; -import java.util.Set; - -import org.springframework.core.convert.ConversionService; -import org.springframework.expression.EvaluationContext; - -/** - * Encapsulates mapping context for a type of object. - * @param the object type - * @author Keith Donald - */ -interface MappableType { - - /** - * Is this object to map an instanceof this mappable type? - * @param object the object to test - */ - boolean isInstance(Object object); - - /** - * A evaluation context for accessing the object. - */ - EvaluationContext getEvaluationContext(T object, ConversionService conversionService); - - /** - * The names of the fields on the object that are eligible for mapping, including any nested fields. - */ - Set getFieldNames(T object); - - /** - * A map of the nested fields on the object that are nested relative to fieldName. - */ - Map getNestedFields(String fieldName, T object); - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappableTypeFactory.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/MappableTypeFactory.java deleted file mode 100644 index 5ce3eaafdc5..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappableTypeFactory.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import java.util.LinkedHashSet; -import java.util.Set; - -/** - * Generic MappableTypeFactory that has no mappable types registered by default. - * Call {@link #add(MappableType)} to register. - * @author Keith Donald - */ -class MappableTypeFactory { - - private Set> mappableTypes = new LinkedHashSet>(); - - /** - * Add a MappableType to this factory. - * @param mappableType the mappable type - */ - public void add(MappableType mappableType) { - this.mappableTypes.add(mappableType); - } - - @SuppressWarnings("unchecked") - public MappableType getMappableType(T object) { - for (MappableType type : mappableTypes) { - if (type.isInstance(object)) { - return type; - } - } - throw new IllegalArgumentException("Object of type [" + object.getClass().getName() - + "] is not mappable - no suitable MappableType exists"); - } -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/MapperBuilder.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/MapperBuilder.java deleted file mode 100644 index 40f33def6d0..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/MapperBuilder.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import java.util.Map; - -import org.joda.time.DateTime; -import org.springframework.core.convert.converter.Converter; -import org.springframework.mapping.Mapper; - -/** - * A fluent interface for configuring a {@link Mapper} between a source type and a target type. - * To use, call one or more of the builder methods on this class, then {@link #getMapper()} to obtain the Mapper instance. - * @author Keith Donald - * @param the source type to map from - * @param the target type to map to - * @see #setAutoMappingEnabled(boolean) - * @see #addMapping(String) - * @see #addMapping(String, Converter) - * @see #addMapping(String, Mapper) - * @see #addMapping(String, String) - * @see #addMapping(String, String, Converter) - * @see #addMapping(Mapper) - * @see #addConverter(Converter) - * @see #getMapper() - */ -public interface MapperBuilder { - - /** - * Sets whether "auto mapping" is enabled. - * When enabled, source and target fields with the same name will automatically be mapped unless an explicit mapping override has been registered. - * Set to false to require explicit registration of all source-to-target mapping rules. - * Default is enabled (true). - * @param autoMappingEnabled auto mapping status - */ - MapperBuilder setAutoMappingEnabled(boolean autoMappingEnabled); - - /** - * Register a mapping between a source field and a target field. - * The source and target field names will be the same value. - * For example, calling addMapping("order") will register a mapping that maps between the order field on the source and the order field on the target. - * This is a convenience method for calling {@link #addMapping(String, String)} with the same source and target value.. - * @param field the field mapping expression - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addMapping(String field); - - /** - * Register a mapping between a source field and a target field that first converts the source field value using the provided Converter. - * The source and target field expressions will be the same value. - * For example, calling addMapping("order") will register a mapping that maps between the order field on the source and the order field on the target. - * This is a convenience method for calling {@link #addMapping(String, String, Converter)} with the same source and target value.. - * @param field the field mapping expression - * @param converter the converter that will convert the source field value before mapping the value to the target field - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addMapping(String field, Converter converter); - - /** - * Register a mapping between a source field and multiple target fields. - * Use this method when you need to map a single source field value to multiple fields on the target. - * For example, calling addMapping("name", firstAndLastNameMapper) might register a mapping that maps the name field on the source to the firstName and lastName fields on the target. - * The target field {@link Mapper} will be passed the value of the source field for its source and the target object T for its target. - * @param field the source field expression - * @param mapper the mapper of the target fields - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addMapping(String field, Mapper mapper); - - /** - * Register a mapping between a source field and a target field. - * Use this method when the name of the source field and the name of the target field are different. - * For example, calling addMapping("order", "primaryOrder") will register a mapping that maps between the order field on the source and the primaryOrder field on the target. - * @param sourceField the source field mapping expression - * @param targetField the target field mapping expression - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addMapping(String sourceField, String targetField); - - /** - * Register a mapping between a source field and a target field that first converts the source field value using the provided Converter. - * Use this method when the name of the source field and the name of the target field are different. - * For example, calling addMapping("order", "primaryOrder") will register a mapping that maps between the order field on the source and the primaryOrder field on the target. - * @param sourceField the source field mapping expression - * @param targetField the target field mapping expression - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addMapping(String sourceField, String targetField, Converter converter); - - /** - * Register a flexible mapping between multiple source fields and one or more target fields. - * For example, calling addMapping(dateAndTimeFieldsToDateTimeFieldMapper) might register a mapping that maps the date and time fields on the source to the dateTime field on the target. - * The provided {@link Mapper} will be passed the source object S for its source and the target object T for its target. - * @param fields the source field mapping expressions - * @param mapper the fields to field mapper - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addMapping(String[] fields, Mapper mapper); - - /** - * Register a mapping that delegates to an assembler to convert multiple nested source field values to a single target field value. - * The source field names that will be mapped all begin with the prefix field.. - * For example, adding an assembler mapping for a field named dateTime would pass all nested dateTime.* fields to the assembler. - * Such fields might be year, month, day, etc. - * @param fields the name of the source field whose value will be assembled from multiple nested fields that begin with the same name - * @param assembler the assembler that will perform value assembly from the field values - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addAssemblerMapping(String field, Converter, ?> assembler); - - /** - * Register a conditional mapping between a source field and a target field. - * The source and target field names will be the same value. - * @param field the field mapping expression - * @param condition the boolean expression that determines if this mapping executes - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addConditionalMapping(String field, String condition); - - /** - * Register a condition mapping between a source field and a target field that first converts the source field value using the provided Converter. - * The source and target field expressions will be the same value. - * @param field the field mapping expression - * @param converter the converter that converts the source field value before mapping - * @param condition the boolean expression that determines if this mapping executes - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addConditionalMapping(String field, Converter converter, String condition); - - /** - * Register a conditional mapping between a source field and multiple target fields. - * Use this method when you need to map a single source field value to multiple fields on the target. - * For example, calling addMapping("name", firstAndLastNameMapper) might register a mapping that maps the name field on the source to the firstName and lastName fields on the target. - * The target field {@link Mapper} will be passed the value of the source field for its source and the target object T for its target. - * @param field the source field expression - * @param mapper the mapper of the target fields - * @param condition the boolean expression that determines if this mapping executes - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addConditionalMapping(String field, Mapper mapper, String condition); - - /** - * Register a conditional mapping between a source field and a target field. - * Use this method when the name of the source field and the name of the target field are different. - * For example, calling addMapping("order", "primaryOrder") will register a mapping that maps between the order field on the source and the primaryOrder field on the target. - * @param sourceField the source field mapping expression - * @param targetField the target field mapping expression - * @param condition the boolean expression that determines if this mapping executes - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addConditionalMapping(String sourceField, String targetField, String condition); - - /** - * Register a conditional mapping between a source field and a target field that first converts the source field value using the provided Converter. - * Use this method when the name of the source field and the name of the target field are different. - * For example, calling addMapping("order", "primaryOrder") will register a mapping that maps between the order field on the source and the primaryOrder field on the target. - * @param sourceField the source field mapping expression - * @param targetField the target field mapping expression - * @param converter the converter that converts the source field value before mapping - * @param condition the boolean expression that determines if this mapping executes - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addConditionalMapping(String sourceField, String targetField, Converter converter, String condition); - - /** - * Register a flexible conditional mapping between multiple source fields and one ore more target fields. - * For example, calling addMapping(dateAndTimeFieldsToDateTimeFieldMapper) might register a mapping that maps the date and time fields on the source to the dateTime field on the target. - * The provided {@link Mapper} will be passed the source object S for its source and the target object T for its target. - * @param fields the source field mapping expressions - * @param mapper the fields to field mapper - * @param condition the boolean expression that determines if this mapping executes - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addConditionalMapping(String[] fields, Mapper mapper, String condition); - - /** - * Register a Mapper that will be used to map between nested source and target fields of a specific sourceType/targetType pair. - * The source and target field types are determined by introspecting the parameterized types on the Mapper generic interface. - * The target instance that is mapped is constructed by calling its default constructor. - * @param nestedMapper the nested mapper - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addNestedMapper(Mapper nestedMapper); - - /** - * Register a Mapper that will be used to map between nested source and target fields of a specific sourceType/targetType pair. - * The source and target field types are determined by introspecting the parameterized types on the Mapper generic interface. - * The target instance that is mapped is constructed by calling the provided Converter. - * @param nestedMapper the nested mapper - * @param converter the target converter - * @return this, for configuring additional field mapping options fluently - */ - MapperBuilder addNestedMapper(Mapper nestedMapper, Converter converter); - - /** - * Register a custom type converter to use to convert between two mapped types. - * The Converter may convert between simple types, such as Strings to Dates. - * Alternatively, it may convert between complex types and initiate a recursive mapping operation between two object fields. - * @return this, for configuring additional field mapping options fluently - * @see Converter - * @see MappingConverter - */ - MapperBuilder addConverter(Converter converter); - - /** - * Set the source fields to exclude from mapping. - * @param fields the source fields as var args - */ - MapperBuilder setExcludedFields(String... fields); - - /** - * Get the Mapper produced by this builder. - * Call this method after instructing the builder. - * @return the Mapper between S and T ready for use - */ - Mapper getMapper(); - -} diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/MapperFactory.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/MapperFactory.java deleted file mode 100644 index b98f1f32c00..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/MapperFactory.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import org.springframework.mapping.Mapper; - -/** - * Factory for creating general-purpose Mappers without depending on a concrete Mapper implementation class. - * @see #defaultMapper() - * @see #mapperBuilder() - * @see #mapperBuilder(Class, Class) - * @author Keith Donald - */ -public class MapperFactory { - - private static final SpelMapper DEFAULT_MAPPER = new SpelMapper(); - - /** - * Get the default Mapper instance suitable for mapping between most object types using "auto mapping" based on field names. - * The Mapper returned is shared and immutable and should not be downcast & modified. - * @return the default mapper - */ - public static Mapper defaultMapper() { - return DEFAULT_MAPPER; - } - - /** - * Get a builder for a new Mapper instance, allowing customization of object mapping policy. - * @return the MapperBuilder - */ - public static MapperBuilder mapperBuilder() { - return new SpelMapperBuilder(); - } - - /** - * Get a builder for a new Mapper instance that maps between objects of sourceType and targetType. - * Allows for customization of object mapping policy. - * Use this method as an alterntative to {@link #mapperBuilder()} when you'd like more type-safety and validation when configuring and using the Mapper. - * @return the MapperBuilder - */ - public static MapperBuilder mapperBuilder(Class sourceType, Class targetType) { - return new SpelMapperBuilder(sourceType, targetType); - } - -} diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingContextHolder.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingContextHolder.java deleted file mode 100644 index 2fb39f2a0ea..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingContextHolder.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import java.util.Stack; - -import org.springframework.core.NamedThreadLocal; - -/** - * Holds thread-specific context about a mapping operation - * @author Keith Donald - * @see SpelMapper#map(Object, Object) - */ -abstract class MappingContextHolder { - - private static final ThreadLocal> mappingContextHolder = new NamedThreadLocal>( - "Mapping context"); - - /** - * Push a source object being mapped onto the context. - */ - public static void push(Object source) { - Stack context = getContext(); - if (context == null) { - context = new Stack(); - mappingContextHolder.set(context); - } - context.add(source); - } - - /** - * Is the source being mapped or has already been mapped? - */ - public static boolean contains(Object source) { - Stack context = getContext(); - return context != null ? context.contains(source) : false; - } - - /** - * Pop the source object being mapped off the context; mapping is complete. - */ - public static void pop() { - Stack context = getContext(); - if (context != null) { - context.pop(); - if (context.isEmpty()) { - mappingContextHolder.set(null); - } - } - } - - /** - * Return a level of bullets for indenting mapping debug logs based on the depth in the object graph. - */ - public static String getLevel() { - int size = getContext().size(); - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < size; i++) { - builder.append("*"); - } - builder.append(" "); - return builder.toString(); - } - - private static Stack getContext() { - return mappingContextHolder.get(); - } - -} diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingConversionService.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingConversionService.java deleted file mode 100644 index 95741f58cf6..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingConversionService.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.support.DefaultConversionService; -import org.springframework.core.convert.support.GenericConverter; - -final class MappingConversionService extends DefaultConversionService { - - @Override - protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType) { - return new MappingConverter(new SpelMapper(), null); - } - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingConverter.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingConverter.java deleted file mode 100644 index 0679e91cd20..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingConverter.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import org.springframework.beans.BeanUtils; -import org.springframework.core.convert.ConversionFailedException; -import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.support.GenericConverter; -import org.springframework.mapping.Mapper; - -/** - * Adapts a Mapper to a Converter, allowing the conversion between two object types to be completed by a Mapper. - * Delegates to a {@link MappingTargetFactory} to construct the conversion target object that will be mapped. - * The default MapperTargetFactory instantiates a target by calling its default constructor. - * @author Keith Donald - */ -final class MappingConverter implements GenericConverter { - - private final Mapper mapper; - - private final MappingTargetFactory mappingTargetFactory; - - /** - * Creates a new Converter that delegates to the mapper to complete the type conversion process. - * Uses the specified MappingTargetFactory to create the target object to map and return. - * @param mapper the mapper - */ - public MappingConverter(Mapper mapper, MappingTargetFactory mappingTargetFactory) { - this.mapper = mapper; - if (mappingTargetFactory != null) { - this.mappingTargetFactory = mappingTargetFactory; - } else { - this.mappingTargetFactory = DefaultMappingTargetFactory.getInstance(); - } - } - - public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { - if (source == null) { - return null; - } - if (MappingContextHolder.contains(source)) { - return source; - } - if (sourceType.isAssignableTo(targetType) && isCopyByReference(sourceType, targetType)) { - return source; - } - return createTargetAndMap(source, sourceType, targetType); - } - - private boolean isCopyByReference(TypeDescriptor sourceType, TypeDescriptor targetType) { - return true; - } - - private Object createTargetAndMap(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { - if (this.mappingTargetFactory.supports(targetType)) { - Object target = this.mappingTargetFactory.createTarget(source, sourceType, targetType); - return this.mapper.map(source, target); - } else { - IllegalStateException cause = new IllegalStateException("[" - + this.mappingTargetFactory.getClass().getName() + "] does not support targetType [" - + targetType.getName() + "]"); - throw new ConversionFailedException(sourceType, targetType, source, cause); - } - - } -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingTargetFactory.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingTargetFactory.java deleted file mode 100644 index 1a71bf55a81..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/MappingTargetFactory.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import org.springframework.core.convert.TypeDescriptor; -import org.springframework.mapping.Mapper; - -/** - * A factory for customizing how the target of a map operation is constructed. - * Used by a {@link MappingConverter} when executing a type conversion. - * @author Keith Donald - * @see MappingConverter - * @see Mapper#map(Object, Object) - */ -interface MappingTargetFactory { - - /** - * Does this factory support creating mapping targets of the specified type - * @param targetType the target type - * @return true if so, false otherwise - */ - public boolean supports(TypeDescriptor targetType); - - /** - * Create the target object to be mapped to. - * @param source the source object to map from - * @param sourceType the source object type descriptor - * @param targetType the target object type descriptor - * @return the target - */ - public Object createTarget(Object source, TypeDescriptor sourceType, TypeDescriptor targetType); - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/MultiFieldToFieldMapping.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/MultiFieldToFieldMapping.java deleted file mode 100644 index 1adcc2ff5d5..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/MultiFieldToFieldMapping.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import java.util.Map; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.core.style.StylerUtils; -import org.springframework.expression.Expression; - -/** - * A mapping between several source fields and a target field. - * @author Keith Donald - */ -final class MultiFieldToFieldMapping implements SpelMapping { - - private final String sourceField; - - private final Expression targetField; - - @SuppressWarnings("unchecked") - private final Converter targetFieldValueAssembler; - - private final Expression condition; - - public MultiFieldToFieldMapping(String sourceField, Expression targetField, Converter, ?> assembler, - Expression condition) { - this.sourceField = sourceField; - this.targetField = targetField; - this.targetFieldValueAssembler = assembler; - this.condition = condition; - } - - public String getSourceField() { - return this.sourceField + ".*"; - } - - public String getTargetField() { - return this.targetField.getExpressionString(); - } - - public boolean mapsField(String field) { - return field.startsWith(this.sourceField + "."); - } - - @SuppressWarnings("unchecked") - public void map(SpelMappingContext context) { - if (!context.conditionHolds(this.condition)) { - return; - } - try { - Map nestedFields = context.getSourceNestedFields(this.sourceField); - Object value = this.targetFieldValueAssembler.convert(nestedFields); - context.setTargetFieldValue(this.targetField, value); - } catch (Exception e) { - context.addMappingFailure(e); - } - } - - public int hashCode() { - return getSourceField().hashCode() + getTargetField().hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof MultiFieldToFieldMapping)) { - return false; - } - MultiFieldToFieldMapping m = (MultiFieldToFieldMapping) o; - return getSourceField().equals(m.getSourceField()) && getTargetField().equals(m.getTargetField()); - } - - public String toString() { - return "[MultiFieldToFieldMapping<" + StylerUtils.style(getSourceField()) + " -> " + getTargetField() + ">]"; - } - -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapper.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapper.java deleted file mode 100644 index abb712caaf2..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapper.java +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import java.util.Collections; -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Set; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.core.GenericTypeResolver; -import org.springframework.core.convert.converter.Converter; -import org.springframework.core.convert.converter.ConverterRegistry; -import org.springframework.expression.EvaluationException; -import org.springframework.expression.Expression; -import org.springframework.expression.ParseException; -import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.expression.spel.standard.SpelExpressionParserConfiguration; -import org.springframework.mapping.Mapper; -import org.springframework.util.Assert; - -/** - * A general-purpose object mapper implementation based on the Spring Expression Language (SpEL). - * @author Keith Donald - */ -final class SpelMapper implements Mapper { - - private static final Log logger = LogFactory.getLog(SpelMapper.class); - - private final SpelExpressionParser sourceExpressionParser = new SpelExpressionParser(); - - private final SpelExpressionParser targetExpressionParser = new SpelExpressionParser( - SpelExpressionParserConfiguration.CreateObjectIfAttemptToReferenceNull - | SpelExpressionParserConfiguration.GrowListsOnIndexBeyondSize); - - private final Set mappings = new LinkedHashSet(); - - private MappableTypeFactory mappableTypeFactory = new DefaultMappableTypeFactory(); - - private boolean autoMappingEnabled = true; - - private MappingConversionService conversionService = new MappingConversionService(); - - public SpelMapper() { - - } - - public SpelMapper(Class sourceType, Class targetType) { - // TODO - addMapping assertions based on specified sourceType and targetType - } - - public void setAutoMappingEnabled(boolean autoMappingEnabled) { - this.autoMappingEnabled = autoMappingEnabled; - } - - public void setMappableTypeFactory(MappableTypeFactory mappableTypeFactory) { - this.mappableTypeFactory = mappableTypeFactory; - } - - public void setExcludedFields(String[] fields) { - // TODO - } - - public void addFieldToFieldMapping(String sourceField, String targetField, Converter converter, - String condition) { - this.mappings.add(new FieldToFieldMapping(parseSourceField(sourceField), parseTargetField(targetField), - converter, parseCondition(condition))); - } - - public void addFieldToMultiFieldMapping(String field, Mapper mapper, String condition) { - this.mappings.add(new FieldToMultiFieldMapping(parseSourceField(field), mapper, parseCondition(condition))); - } - - public void addMultiFieldToFieldMapping(String[] fields, Mapper mapper, String condition) { - this.mappings.add(new FlexibleFieldMapping(fields, mapper, parseCondition(condition))); - } - - public void addMultiFieldToFieldMapping(String sourceField, String targetField, - Converter, ?> assembler, String condition) { - this.mappings.add(new MultiFieldToFieldMapping(sourceField, parseTargetField(targetField), assembler, - parseCondition(condition))); - } - - public void addNestedMapper(Mapper nestedMapper, MappingTargetFactory targetFactory) { - Class[] typeInfo = getRequiredTypeInfo(nestedMapper); - addNestedMapper(typeInfo[0], typeInfo[1], nestedMapper, targetFactory); - } - - public void addNestedMapper(Class sourceType, Class targetType, Mapper nestedMapper, - MappingTargetFactory targetFactory) { - this.conversionService.addGenericConverter(sourceType, targetType, new MappingConverter(nestedMapper, - targetFactory)); - } - - public ConverterRegistry getConverterRegistry() { - return conversionService; - } - - public Object map(Object source, Object target) { - Assert.notNull(source, "The source to map from cannot be null"); - Assert.notNull(target, "The target to map to cannot be null"); - try { - MappingContextHolder.push(source); - MappableType sourceType = this.mappableTypeFactory.getMappableType(source); - MappableType targetType = this.mappableTypeFactory.getMappableType(target); - SpelMappingContext context = new SpelMappingContext(source, sourceType, target, targetType, - this.conversionService); - for (SpelMapping mapping : this.mappings) { - if (logger.isDebugEnabled()) { - logger.debug(MappingContextHolder.getLevel() + mapping); - } - mapping.map(context); - } - Set autoMappings = getAutoMappings(context); - for (SpelMapping mapping : autoMappings) { - if (logger.isDebugEnabled()) { - logger.debug(MappingContextHolder.getLevel() + mapping + " (auto)"); - } - mapping.map(context); - } - context.handleFailures(); - return target; - } finally { - MappingContextHolder.pop(); - } - } - - // internal helpers - - private Expression parseSourceField(String sourceFieldExpression) { - try { - return sourceExpressionParser.parseExpression(sourceFieldExpression); - } catch (ParseException e) { - throw new IllegalArgumentException("The mapping source '" + sourceFieldExpression - + "' is not a parseable value expression", e); - } - } - - private Expression parseCondition(String condition) { - if (condition == null) { - return null; - } - try { - return sourceExpressionParser.parseExpression(condition); - } catch (ParseException e) { - throw new IllegalArgumentException("The mapping condition '" + condition - + "' is not a parseable value expression", e); - } - } - - private Expression parseTargetField(String targetFieldExpression) { - try { - return targetExpressionParser.parseExpression(targetFieldExpression); - } catch (ParseException e) { - throw new IllegalArgumentException("The mapping target '" + targetFieldExpression - + "' is not a parseable property expression", e); - } - } - - private Class[] getRequiredTypeInfo(Mapper mapper) { - return GenericTypeResolver.resolveTypeArguments(mapper.getClass(), Mapper.class); - } - - private Set getAutoMappings(SpelMappingContext context) { - if (this.autoMappingEnabled) { - Set autoMappings = new LinkedHashSet(); - Set sourceFields = context.getSourceFieldNames(); - for (String field : sourceFields) { - if (!explicitlyMapped(field)) { - Expression sourceField; - try { - sourceField = sourceExpressionParser.parseExpression(field); - } catch (ParseException e) { - throw new IllegalArgumentException("The source field '" + field - + "' is not a parseable value expression", e); - } - Expression targetField; - try { - targetField = targetExpressionParser.parseExpression(field); - } catch (ParseException e) { - throw new IllegalArgumentException("The target field '" + field - + "' is not a parseable value expression", e); - } - try { - if (context.isTargetFieldWriteable(targetField)) { - autoMappings.add(new FieldToFieldMapping(sourceField, targetField, null, null)); - } - } catch (EvaluationException e) { - - } - } - } - return autoMappings; - } else { - return Collections.emptySet(); - } - } - - private boolean explicitlyMapped(String field) { - for (SpelMapping mapping : this.mappings) { - if (mapping.mapsField(field)) { - return true; - } - } - return false; - } - -} diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapperBuilder.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapperBuilder.java deleted file mode 100644 index cef5ee80bf9..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapperBuilder.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import java.util.Map; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.mapping.Mapper; - -/** - * MapperBuilder that builds {@link SpelMapper} instances. - * @author Keith Donald - */ -final class SpelMapperBuilder implements MapperBuilder { - - private final SpelMapper mapper; - - public SpelMapperBuilder() { - this.mapper = new SpelMapper(); - } - - public SpelMapperBuilder(Class sourceType, Class targetType) { - this.mapper = new SpelMapper(sourceType, targetType); - } - - public MapperBuilder setAutoMappingEnabled(boolean autoMappingEnabled) { - this.mapper.setAutoMappingEnabled(autoMappingEnabled); - return this; - } - - public MapperBuilder addMapping(String field) { - this.mapper.addFieldToFieldMapping(field, field, null, null); - return this; - } - - public MapperBuilder addMapping(String field, Converter converter) { - this.mapper.addFieldToFieldMapping(field, field, converter, null); - return this; - } - - public MapperBuilder addMapping(String field, Mapper mapper) { - this.mapper.addFieldToMultiFieldMapping(field, mapper, null); - return this; - } - - public MapperBuilder addMapping(String sourceField, String targetField) { - this.mapper.addFieldToFieldMapping(sourceField, targetField, null, null); - return this; - } - - public MapperBuilder addMapping(String sourceField, String targetField, Converter converter) { - this.mapper.addFieldToFieldMapping(sourceField, targetField, converter, null); - return this; - } - - public MapperBuilder addMapping(String[] fields, Mapper mapper) { - this.mapper.addMultiFieldToFieldMapping(fields, mapper, null); - return this; - } - - public MapperBuilder addAssemblerMapping(String field, Converter, ?> assembler) { - this.mapper.addMultiFieldToFieldMapping(field, field, assembler, null); - return this; - } - - public MapperBuilder addConditionalMapping(String field, String condition) { - this.mapper.addFieldToFieldMapping(field, field, null, condition); - return this; - } - - public MapperBuilder addConditionalMapping(String field, Converter converter, String condition) { - this.mapper.addFieldToFieldMapping(field, field, converter, condition); - return this; - } - - public MapperBuilder addConditionalMapping(String field, Mapper mapper, String condition) { - this.mapper.addFieldToMultiFieldMapping(field, mapper, condition); - return this; - } - - public MapperBuilder addConditionalMapping(String sourceField, String targetField, String condition) { - this.mapper.addFieldToFieldMapping(sourceField, targetField, null, condition); - return this; - } - - public MapperBuilder addConditionalMapping(String sourceField, String targetField, Converter converter, - String condition) { - this.mapper.addFieldToFieldMapping(sourceField, targetField, converter, condition); - return this; - } - - public MapperBuilder addConditionalMapping(String[] fields, Mapper mapper, String condition) { - this.mapper.addMultiFieldToFieldMapping(fields, mapper, condition); - return this; - } - - public MapperBuilder addNestedMapper(Mapper nestedMapper) { - this.mapper.addNestedMapper(nestedMapper, null); - return this; - } - - public MapperBuilder addNestedMapper(Mapper nestedMapper, Converter converter) { - this.mapper.addNestedMapper(nestedMapper, new ConverterMappingTargetFactory(converter)); - return this; - } - - public MapperBuilder addConverter(Converter converter) { - this.mapper.getConverterRegistry().addConverter(converter); - return this; - } - - public MapperBuilder setExcludedFields(String... fields) { - this.mapper.setExcludedFields(fields); - return this; - } - - @SuppressWarnings("unchecked") - public Mapper getMapper() { - return (Mapper) this.mapper; - } - -} diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapping.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapping.java deleted file mode 100644 index 85317112d97..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMapping.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -/** - * A single {@link SpelMapper} mapping. - * @author Keith Donald - */ -interface SpelMapping { - - /** - * Return true if this maps the source field. - */ - boolean mapsField(String field); - - /** - * Execute this mapping. - * @param context the mapping context - */ - void map(SpelMappingContext context); -} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMappingContext.java b/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMappingContext.java deleted file mode 100644 index 0053587e415..00000000000 --- a/org.springframework.context/src/main/java/org/springframework/mapping/support/SpelMappingContext.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.mapping.support; - -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.springframework.core.convert.ConversionService; -import org.springframework.expression.EvaluationContext; -import org.springframework.expression.Expression; -import org.springframework.mapping.MappingException; -import org.springframework.mapping.MappingFailure; - -final class SpelMappingContext { - - private final MappableType sourceType; - - private final EvaluationContext sourceEvaluationContext; - - private final EvaluationContext targetEvaluationContext; - - private final List failures = new LinkedList(); - - public SpelMappingContext(Object source, MappableType sourceType, Object target, MappableType targetType, - ConversionService conversionService) { - this.sourceType = sourceType; - this.sourceEvaluationContext = sourceType.getEvaluationContext(source, conversionService); - this.targetEvaluationContext = targetType.getEvaluationContext(target, conversionService); - } - - public Object getSource() { - return this.sourceEvaluationContext.getRootObject().getValue(); - } - - public Object getTarget() { - return this.targetEvaluationContext.getRootObject().getValue(); - } - - public boolean conditionHolds(Expression condition) { - if (condition == null) { - return true; - } - return Boolean.TRUE.equals(condition.getValue(this.sourceEvaluationContext)); - } - - public Object getSourceFieldValue(Expression sourceField) { - return sourceField.getValue(this.sourceEvaluationContext); - } - - public Set getSourceFieldNames() { - return this.sourceType.getFieldNames(getSource()); - } - - public Map getSourceNestedFields(String sourceFieldName) { - return this.sourceType.getNestedFields(sourceFieldName, getSource()); - } - - public void setTargetFieldValue(Expression targetField, Object value) { - targetField.setValue(this.targetEvaluationContext, value); - } - - public void addMappingFailure(Throwable cause) { - this.failures.add(new MappingFailure(cause)); - } - - public void handleFailures() { - if (!this.failures.isEmpty()) { - throw new MappingException(this.failures); - } - } - - public boolean isTargetFieldWriteable(Expression targetField) { - return targetField.isWritable(this.targetEvaluationContext); - } - -} diff --git a/org.springframework.context/src/test/java/org/springframework/mapping/support/MappingTests.java b/org.springframework.context/src/test/java/org/springframework/mapping/support/MappingTests.java deleted file mode 100644 index f392bef2a3c..00000000000 --- a/org.springframework.context/src/test/java/org/springframework/mapping/support/MappingTests.java +++ /dev/null @@ -1,1003 +0,0 @@ -package org.springframework.mapping.support; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.MutableDateTime; -import org.joda.time.format.ISODateTimeFormat; -import org.junit.Test; -import org.springframework.core.convert.converter.Converter; -import org.springframework.mapping.Mapper; -import org.springframework.mapping.MappingException; - -public class MappingTests { - - @Test - public void testDefaultMapper() { - EmployeeDto dto = new EmployeeDto(); - dto.setFirstName("Keith"); - dto.setLastName("Donald"); - Employee emp = (Employee) MapperFactory.defaultMapper().map(dto, new Employee()); - assertEquals("Keith", emp.getFirstName()); - assertEquals("Donald", emp.getLastName()); - } - - @Test - public void mapAutomatic() { - Map source = new HashMap(); - source.put("name", "Keith"); - source.put("age", 31); - - Person target = new Person(); - - MapperFactory.defaultMapper().map(source, target); - - assertEquals("Keith", target.name); - assertEquals(31, target.age); - } - - @Test - public void mapExplicit() throws MappingException { - Map source = new HashMap(); - source.put("name", "Keith"); - source.put("age", 31); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder().setAutoMappingEnabled(false).addMapping("name") - .getMapper(); - mapper.map(source, target); - - assertEquals("Keith", target.name); - assertEquals(0, target.age); - } - - @Test - public void mapAutomaticWithExplictOverrides() { - Map source = new HashMap(); - source.put("name", "Keith"); - source.put("test", "3"); - source.put("favoriteSport", "FOOTBALL"); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder().addMapping("test", "age").getMapper(); - mapper.map(source, target); - - assertEquals("Keith", target.name); - assertEquals(3, target.age); - assertEquals(Sport.FOOTBALL, target.favoriteSport); - } - - @Test - public void mapAutomaticIgnoreUnknownField() { - Map source = new HashMap(); - source.put("name", "Keith"); - source.put("age", 31); - source.put("unknown", "foo"); - - Person target = new Person(); - - MapperFactory.defaultMapper().map(source, target); - - assertEquals("Keith", target.name); - assertEquals(31, target.age); - } - - @Test - public void mapSameSourceFieldToMultipleTargets() { - Map source = new HashMap(); - source.put("test", "FOOTBALL"); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder().addMapping("test", "name").addMapping("test", - "favoriteSport").getMapper(); - mapper.map(source, target); - - assertEquals("FOOTBALL", target.name); - assertEquals(0, target.age); - assertEquals(Sport.FOOTBALL, target.favoriteSport); - } - - @Test - public void mapBean() { - PersonDto source = new PersonDto(); - source.setFullName("Keith Donald"); - source.setAge("31"); - source.setSport("FOOTBALL"); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder().addMapping("fullName", "name").addMapping( - "sport", "favoriteSport").getMapper(); - mapper.map(source, target); - - assertEquals("Keith Donald", target.name); - assertEquals(31, target.age); - assertEquals(Sport.FOOTBALL, target.favoriteSport); - } - - @Test - public void mapBeanDeep() { - PersonDto source = new PersonDto(); - source.age = "0"; - NestedDto nested = new NestedDto(); - nested.foo = "bar"; - source.setNested(nested); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder(PersonDto.class, Person.class).addMapping( - "nested.foo").getMapper(); - mapper.map(source, target); - - assertEquals("bar", target.nested.foo); - } - - @Test - public void mapBeanNested() { - PersonDto source = new PersonDto(); - NestedDto nested = new NestedDto(); - nested.foo = "bar"; - source.setNested(nested); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder(PersonDto.class, Person.class) - .setAutoMappingEnabled(false).addMapping("nested").getMapper(); - mapper.map(source, target); - - assertEquals("bar", target.nested.foo); - } - - @Test - public void mapBeanNestedCustomNestedMapper() { - PersonDto source = new PersonDto(); - NestedDto nested = new NestedDto(); - nested.foo = "bar"; - source.setNested(nested); - - Person target = new Person(); - - Mapper nestedMapper = MapperFactory.mapperBuilder(NestedDto.class, Nested.class).addMapping( - "foo", new Converter() { - public String convert(String source) { - return source + " and baz"; - } - }).getMapper(); - - Mapper mapper = MapperFactory.mapperBuilder(PersonDto.class, Person.class) - .setAutoMappingEnabled(false).addMapping("nested").addNestedMapper(nestedMapper).getMapper(); - - mapper.map(source, target); - - assertEquals("bar and baz", target.nested.foo); - } - - @Test - public void mapBeanNestedCustomNestedMapperHandCoded() { - PersonDto source = new PersonDto(); - NestedDto nested = new NestedDto(); - nested.foo = "bar"; - source.setNested(nested); - - Person target = new Person(); - - Mapper nestedMapper = new Mapper() { - public Nested map(NestedDto source, Nested target) { - target.setFoo(source.getFoo() + " and baz"); - return target; - } - }; - - Mapper mapper = MapperFactory.mapperBuilder(PersonDto.class, Person.class) - .setAutoMappingEnabled(false).addMapping("nested").addNestedMapper(nestedMapper).getMapper(); - - mapper.map(source, target); - - assertEquals("bar and baz", target.nested.foo); - } - - @Test - public void mapBeanNestedCustomNestedMapperConverterAsTargetFactory() { - PersonDto source = new PersonDto(); - final NestedDto nested = new NestedDto(); - nested.foo = "bar"; - source.setNested(nested); - - Person target = new Person(); - - Mapper nestedMapper = MapperFactory.mapperBuilder(NestedDto.class, Nested.class).addMapping( - "foo", new Converter() { - public String convert(String source) { - return source + " and baz"; - } - }).getMapper(); - - Mapper mapper = MapperFactory.mapperBuilder(PersonDto.class, Person.class) - .setAutoMappingEnabled(false).addMapping("nested").addNestedMapper(nestedMapper, - new Converter() { - public Nested convert(NestedDto source) { - assertEquals(nested, source); - return new Nested(); - } - }).getMapper(); - - mapper.map(source, target); - - assertEquals("bar and baz", target.nested.foo); - } - - @Test - public void mapBeanNestedCustomConverterDelegatingToMapper() { - PersonDto source = new PersonDto(); - final NestedDto nested = new NestedDto(); - nested.foo = "bar"; - source.setNested(nested); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder(PersonDto.class, Person.class) - .setAutoMappingEnabled(false).addMapping("nested", new Converter() { - public Nested convert(NestedDto source) { - Mapper nestedMapper = MapperFactory.mapperBuilder(NestedDto.class, - Nested.class).addMapping("foo", new Converter() { - public String convert(String source) { - return source + " and baz"; - } - }).getMapper(); - return nestedMapper.map(source, new Nested()); - } - }).getMapper(); - - mapper.map(source, target); - - assertEquals("bar and baz", target.nested.foo); - } - - @Test - public void testCustomMapper() { - Mapper mapper = MapperFactory.mapperBuilder(CreateAccountDto.class, Account.class) - .setAutoMappingEnabled(false) - // field to field of different name - .addMapping("accountNumber", "number") - // field to multiple fields - .addMapping("name", new Mapper() { - public Account map(String name, Account account) { - String[] names = name.split(" "); - account.setFirstName(names[0]); - account.setLastName(names[1]); - return account; - } - }) - // field to field with type conversion - .addMapping("address", new Converter() { - public Address convert(String address) { - String[] fields = address.split(" "); - Address addr = new Address(); - addr.setStreet(fields[0]); - addr.setCity(fields[1]); - addr.setState(fields[2]); - addr.setZip(fields[3]); - return addr; - } - }) - // multiple fields to field - .addMapping(new String[] { "activationDay", "activationTime " }, - new Mapper() { - public Account map(CreateAccountDto source, Account target) { - DateTime dateTime = ISODateTimeFormat.dateTime().parseDateTime( - source.getActivationDay() + "T" + source.getActivationTime()); - target.setActivationDateTime(dateTime); - return target; - } - }).getMapper(); - CreateAccountDto dto = new CreateAccountDto(); - dto.setAccountNumber("123456789"); - dto.setName("Keith Donald"); - dto.setActivationDay("2009-10-12"); - dto.setActivationTime("12:00:00.000Z"); - dto.setAddress("2009BelAireEstates PalmBay FL 35452"); - Account account = mapper.map(dto, new Account()); - assertEquals("Keith", account.getFirstName()); - assertEquals("Donald", account.getLastName()); - assertEquals("2009BelAireEstates", account.getAddress().getStreet()); - assertEquals("PalmBay", account.getAddress().getCity()); - assertEquals("FL", account.getAddress().getState()); - assertEquals("35452", account.getAddress().getZip()); - assertEquals(ISODateTimeFormat.dateTime().parseDateTime("2009-10-12T12:00:00.000Z"), account - .getActivationDateTime()); - } - - @Test - public void testMultiFieldToFieldMappingWithAssembler() { - Mapper mapper = MapperFactory.mapperBuilder(Map.class, Account.class) - .setAutoMappingEnabled(false) - // field to multiple fields - .addAssemblerMapping("activationDateTime", new Converter, DateTime>() { - public DateTime convert(Map source) { - MutableDateTime dateTime = new MutableDateTime(DateTimeZone.forID("-05:00")); - dateTime.setYear(Integer.parseInt(source.get("year"))); - dateTime.setMonthOfYear(Integer.parseInt(source.get("month"))); - dateTime.setDayOfMonth(Integer.parseInt(source.get("day"))); - dateTime.setHourOfDay(Integer.parseInt(source.get("hour"))); - dateTime.setMinuteOfHour(Integer.parseInt(source.get("minute"))); - dateTime.setSecondOfMinute(0); - dateTime.setMillisOfSecond(0); - return dateTime.toDateTime(); - } - }).getMapper(); - Map source = new HashMap(); - source.put("activationDateTime.year", "2009"); - source.put("activationDateTime.month", "10"); - source.put("activationDateTime.day", "12"); - source.put("activationDateTime.hour", "12"); - source.put("activationDateTime.minute", "0"); - Account account = mapper.map(source, new Account()); - assertEquals(ISODateTimeFormat.dateTime().withOffsetParsed().parseDateTime("2009-10-12T12:00:00.000-05:00"), account - .getActivationDateTime()); - } - - @Test - public void conditionalMapping() { - Map domestic = new HashMap(); - domestic.put("international", "false"); - domestic.put("areaCode", "205"); - domestic.put("prefix", "339"); - domestic.put("line", "1234"); - domestic.put("countryCode", "whatever"); - domestic.put("cityCode", "whatever"); - - Mapper mapper = MapperFactory.mapperBuilder(Map.class, PhoneNumber.class) - .addConditionalMapping("countryCode", "international == 'true'").addConditionalMapping("cityCode", - "international == 'true'").getMapper(); - - PhoneNumber number = mapper.map(domestic, new PhoneNumber()); - assertEquals("205", number.getAreaCode()); - assertEquals("339", number.getPrefix()); - assertEquals("1234", number.getLine()); - assertNull(number.getCountryCode()); - assertNull(number.getCityCode()); - - Map international = new HashMap(); - international.put("international", "true"); - international.put("areaCode", "205"); - international.put("prefix", "339"); - international.put("line", "1234"); - international.put("countryCode", "1"); - international.put("cityCode", "2"); - - PhoneNumber number2 = mapper.map(international, new PhoneNumber()); - - assertEquals("205", number2.getAreaCode()); - assertEquals("339", number2.getPrefix()); - assertEquals("1234", number2.getLine()); - assertEquals("1", number2.getCountryCode()); - assertEquals("2", number2.getCityCode()); - } - - @Test - public void mapList() { - PersonDto source = new PersonDto(); - List sports = new ArrayList(); - sports.add("FOOTBALL"); - sports.add("BASKETBALL"); - source.setSports(sports); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder(PersonDto.class, Person.class) - .setAutoMappingEnabled(false).addMapping("sports", "favoriteSports").getMapper(); - mapper.map(source, target); - - assertEquals(Sport.FOOTBALL, target.favoriteSports.get(0)); - assertEquals(Sport.BASKETBALL, target.favoriteSports.get(1)); - } - - @Test - public void mapListFlatten() { - PersonDto source = new PersonDto(); - List sports = new ArrayList(); - sports.add("FOOTBALL"); - sports.add("BASKETBALL"); - source.setSports(sports); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder(PersonDto.class, Person.class) - .setAutoMappingEnabled(false).addMapping("sports[0]", "favoriteSport").getMapper(); - mapper.map(source, target); - - assertEquals(Sport.FOOTBALL, target.favoriteSport); - assertNull(target.favoriteSports); - } - - @Test - public void mapMap() { - PersonDto source = new PersonDto(); - Map friendRankings = new HashMap(); - friendRankings.put("Keri", "1"); - friendRankings.put("Alf", "2"); - source.setFriendRankings(friendRankings); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder(PersonDto.class, Person.class) - .setAutoMappingEnabled(false).addMapping("friendRankings").addConverter( - new Converter() { - public Person convert(String source) { - return new Person(source); - } - }).getMapper(); - mapper.map(source, target); - - mapper.map(source, target); - - assertEquals(new Integer(1), target.friendRankings.get(new Person("Keri"))); - assertEquals(new Integer(2), target.friendRankings.get(new Person("Alf"))); - } - - @Test - public void mapFieldConverter() { - Map source = new HashMap(); - source.put("name", "Keith Donald"); - source.put("age", 31); - - Person target = new Person(); - - Mapper mapper = MapperFactory.mapperBuilder().addMapping("name", - new Converter() { - public String convert(String source) { - String[] names = source.split(" "); - return names[0] + " P. " + names[1]; - } - }).getMapper(); - mapper.map(source, target); - - assertEquals("Keith P. Donald", target.name); - assertEquals(31, target.age); - } - - @Test - public void mapFailure() { - Map source = new HashMap(); - source.put("name", "Keith"); - source.put("age", "invalid"); - Person target = new Person(); - try { - MapperFactory.defaultMapper().map(source, target); - } catch (MappingException e) { - assertEquals(1, e.getMappingFailureCount()); - } - } - - @Test - public void mapCyclic() { - Person source = new Person(); - source.setName("Keith"); - source.setAge(3); - source.setFavoriteSport(Sport.FOOTBALL); - source.cyclic = source; - Person target = new Person(); - MapperFactory.defaultMapper().map(source, target); - assertEquals("Keith", target.getName()); - assertEquals(3, target.getAge()); - assertEquals(Sport.FOOTBALL, target.getFavoriteSport()); - assertEquals(source.cyclic, target.cyclic); - } - - @Test - public void mapCyclicTypicalHibernateDomainModel() { - Order source = new Order(); - source.setNumber(1); - LineItem item = new LineItem(); - item.setAmount(new BigDecimal("30.00")); - item.setOrder(source); - source.setLineItem(item); - - Order target = new Order(); - - MapperFactory.defaultMapper().map(source, target); - assertEquals(1, target.getNumber()); - assertTrue(item == target.getLineItem()); - assertEquals(new BigDecimal("30.00"), target.getLineItem().getAmount()); - assertEquals(source, target.getLineItem().getOrder()); - } - - public static class EmployeeDto { - - private String firstName; - - private String lastName; - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - } - - public static class Employee { - - private String firstName; - - private String lastName; - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - } - - public static class CreateAccountDto { - - private String accountNumber; - - private String name; - - private String address; - - private String activationDay; - - private String activationTime; - - public String getAccountNumber() { - return accountNumber; - } - - public void setAccountNumber(String accountNumber) { - this.accountNumber = accountNumber; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getActivationDay() { - return activationDay; - } - - public void setActivationDay(String activationDay) { - this.activationDay = activationDay; - } - - public String getActivationTime() { - return activationTime; - } - - public void setActivationTime(String activationTime) { - this.activationTime = activationTime; - } - - } - - public static class Account { - - private String number; - - private String firstName; - - private String lastName; - - private Address address; - - private DateTime activationDateTime; - - public String getNumber() { - return number; - } - - public void setNumber(String number) { - this.number = number; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public Address getAddress() { - return address; - } - - public void setAddress(Address address) { - this.address = address; - } - - public DateTime getActivationDateTime() { - return activationDateTime; - } - - public void setActivationDateTime(DateTime activationDateTime) { - this.activationDateTime = activationDateTime; - } - - } - - public static class Address { - - private String street; - - private String city; - - private String state; - - private String zip; - - public String getStreet() { - return street; - } - - public void setStreet(String street) { - this.street = street; - } - - public String getCity() { - return city; - } - - public void setCity(String city) { - this.city = city; - } - - public String getState() { - return state; - } - - public void setState(String state) { - this.state = state; - } - - public String getZip() { - return zip; - } - - public void setZip(String zip) { - this.zip = zip; - } - - } - - public static class PersonDto { - - private String fullName; - - private String age; - - private String sport; - - private List sports; - - private Map friendRankings; - - private NestedDto nested; - - public String getFullName() { - return fullName; - } - - public void setFullName(String fullName) { - this.fullName = fullName; - } - - public String getAge() { - return age; - } - - public void setAge(String age) { - this.age = age; - } - - public String getSport() { - return sport; - } - - public void setSport(String sport) { - this.sport = sport; - } - - public List getSports() { - return sports; - } - - public void setSports(List sports) { - this.sports = sports; - } - - public Map getFriendRankings() { - return friendRankings; - } - - public void setFriendRankings(Map friendRankings) { - this.friendRankings = friendRankings; - } - - public NestedDto getNested() { - return nested; - } - - public void setNested(NestedDto nested) { - this.nested = nested; - } - - } - - public static class NestedDto { - - private String foo; - - public String getFoo() { - return foo; - } - } - - public static class Person { - - private String name; - - private int age; - - private Sport favoriteSport; - - private Nested nested; - - private Person cyclic; - - private List favoriteSports; - - private Map friendRankings; - - public Person() { - - } - - public Person(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public Sport getFavoriteSport() { - return favoriteSport; - } - - public void setFavoriteSport(Sport favoriteSport) { - this.favoriteSport = favoriteSport; - } - - public Nested getNested() { - return nested; - } - - public void setNested(Nested nested) { - this.nested = nested; - } - - public Person getCyclic() { - return cyclic; - } - - public void setCyclic(Person cyclic) { - this.cyclic = cyclic; - } - - public List getFavoriteSports() { - return favoriteSports; - } - - public void setFavoriteSports(List favoriteSports) { - this.favoriteSports = favoriteSports; - } - - public Map getFriendRankings() { - return friendRankings; - } - - public void setFriendRankings(Map friendRankings) { - this.friendRankings = friendRankings; - } - - public int hashCode() { - return name.hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof Person)) { - return false; - } - Person p = (Person) o; - return name.equals(p.name); - } - } - - public static class Nested { - - private String foo; - - public String getFoo() { - return foo; - } - - public void setFoo(String foo) { - this.foo = foo; - } - - } - - public enum Sport { - FOOTBALL, BASKETBALL - } - - public static class Order { - - private int number; - - private LineItem lineItem; - - public int getNumber() { - return number; - } - - public void setNumber(int number) { - this.number = number; - } - - public LineItem getLineItem() { - return lineItem; - } - - public void setLineItem(LineItem lineItem) { - this.lineItem = lineItem; - } - - } - - public static class LineItem { - - private BigDecimal amount; - - private Order order; - - public BigDecimal getAmount() { - return amount; - } - - public void setAmount(BigDecimal amount) { - this.amount = amount; - } - - public Order getOrder() { - return order; - } - - public void setOrder(Order order) { - this.order = order; - } - - } - - public static class PhoneNumber { - - private String areaCode; - - private String prefix; - - private String line; - - private String countryCode; - - private String cityCode; - - public String getAreaCode() { - return areaCode; - } - - public void setAreaCode(String areaCode) { - this.areaCode = areaCode; - } - - public String getPrefix() { - return prefix; - } - - public void setPrefix(String prefix) { - this.prefix = prefix; - } - - public String getLine() { - return line; - } - - public void setLine(String line) { - this.line = line; - } - - public String getCountryCode() { - return countryCode; - } - - public void setCountryCode(String countryCode) { - this.countryCode = countryCode; - } - - public String getCityCode() { - return cityCode; - } - - public void setCityCode(String cityCode) { - this.cityCode = cityCode; - } - - } - -}