1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }