View Javadoc

1   /*
2    * Copyright 2004-2005, 2008 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.converters;
17  
18  import java.util.Iterator;
19  import java.util.Locale;
20  
21  import net.sf.morph.Defaults;
22  import net.sf.morph.transform.Converter;
23  import net.sf.morph.transform.DecoratedConverter;
24  import net.sf.morph.transform.transformers.BaseReflectorTransformer;
25  
26  /**
27   * Converts an object to a traverser type (an Iterator or an Enumeration).  If
28   * the source object is reflectable as a container object by the <code>reflector</code> of this
29   * 
30   * @author Matt Sgarlata
31   * @since Dec 18, 2004
32   */
33  public class ContainerToTraverserConverter extends BaseReflectorTransformer implements DecoratedConverter {
34  
35  	private static final Class[] SOURCE_TYPES = new Class[] {
36  		Object.class
37  	};
38  
39  	private Converter traverserConverter;
40  
41  	/**
42  	 * {@inheritDoc}
43  	 */
44  	protected Object convertImpl(Class destinationClass, Object source,
45  		Locale locale) throws Exception {
46  
47  		Iterator iterator = getContainerReflector().getIterator(source);
48  		return getTraverserConverter().convert(destinationClass, iterator, locale);
49  	}
50  
51  	/**
52  	 * {@inheritDoc}
53  	 */
54  	protected Class[] getSourceClassesImpl() throws Exception {
55  		return SOURCE_TYPES;
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	protected Class[] getDestinationClassesImpl() throws Exception {
62  		return getTraverserConverter().getDestinationClasses();
63  	}
64  
65  	/**
66  	 * Get the traverser converter used by this ContainerToTraverserConverter.
67  	 * @return Converter
68  	 */
69  	public Converter getTraverserConverter() {
70  		if (traverserConverter == null) {
71  			setTraverserConverter(Defaults.createConverter());
72  		}
73  		return traverserConverter;
74  	}
75  
76  	/**
77  	 * Set the traverser converter used by this ContainerToTraverserConverter.
78  	 * @param traverserConverter
79  	 */
80  	public void setTraverserConverter(Converter traverserConverter) {
81  		this.traverserConverter = traverserConverter;
82  	}
83  }