View Javadoc

1   /*
2    * Copyright 2004-2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.transform.copiers;
17  
18  import java.util.Locale;
19  
20  import net.sf.morph.reflect.IndexedContainerReflector;
21  import net.sf.morph.transform.Converter;
22  import net.sf.morph.transform.Copier;
23  import net.sf.morph.transform.Transformer;
24  import net.sf.morph.util.ReflectorUtils;
25  
26  /**
27   * FIXME this class isn't done
28   * 
29   * @author Matt Sgarlata
30   * @since Dec 20, 2004
31   * @deprecated it's not clear what the intent of this copier was
32   */
33  public class ContainerOfBeansCopier extends ContainerCopier {
34  
35  //	private Transformer beanTransformer;
36  	private Class destinationBeanClass;
37  	
38  	protected void put(int index, Object destination, Object value, Class valueClass, Locale locale, Integer preferredTransformationType) {
39  		Object transformed = null;
40  		// try to do the transformation using a copy operation
41  		if (getBeanTransformer() instanceof Copier &&
42  			ReflectorUtils.isReflectable(getReflector(),
43  				destination.getClass(), IndexedContainerReflector.class)) {
44  
45  			if (((IndexedContainerReflector) getReflector()).getSize(destination) > index) {
46  				transformed = ((IndexedContainerReflector) getReflector()).get(destination, index);
47  				if (transformed != null &&
48  					getDestinationBeanClass().isAssignableFrom(transformed.getClass())) {
49  					((Copier) getBeanTransformer()).copy(transformed, value, locale);				
50  				}
51  			}			
52  		}
53  		// if a copy isn't possible, do a conversion
54  		if (transformed == null) {
55  			transformed = ((Converter) getBeanTransformer()).convert(getDestinationBeanClass(), value, locale);
56  		}
57  		super.put(index, destination, transformed, valueClass, locale, preferredTransformationType);
58  	}
59  	
60  	protected Transformer getBeanTransformer() {
61  //		return (Transformer) CompositeUtils.specialize(getNestedTransformer(), Transformer.class);
62  		return getNestedTransformer();
63  	}
64  	
65  //	public Transformer getBeanTransformer() {
66  //		if (beanTransformer == null) {
67  //			setBeanTransformer(Defaults.getConverter);
68  //		}
69  //		return beanTransformer;
70  //	}
71  //	public void setBeanTransformer(Transformer beanTransformer) throws TransformationException {
72  //		if (!(beanTransformer instanceof Converter || beanTransformer instanceof Copier)) {
73  //			throw new TransformationException(
74  //				"The beanTransformer must implement either "
75  //					+ ObjectUtils.getObjectDescription(Converter.class) + " or "
76  //					+ ObjectUtils.getObjectDescription(Copier.class));
77  //		}
78  //		this.beanTransformer = beanTransformer;
79  //	}
80  	
81  	public Class getDestinationBeanClass() {
82  		return destinationBeanClass;
83  	}
84  	public void setDestinationBeanClass(Class destinationBeanClass) {
85  		this.destinationBeanClass = destinationBeanClass;
86  	}
87  }