1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.transform.copiers;
17
18 import java.util.List;
19
20 import net.sf.composite.validate.ComponentValidator;
21 import net.sf.composite.validate.validators.SimpleComponentValidator;
22 import net.sf.morph.Defaults;
23 import net.sf.morph.reflect.ContainerReflector;
24 import net.sf.morph.transform.Copier;
25 import net.sf.morph.transform.TransformationException;
26 import net.sf.morph.transform.Transformer;
27 import net.sf.morph.transform.transformers.BaseCompositeTransformer;
28
29 /**
30 * Support for AssemblerCopier/DisassemblerCopier.
31 *
32 * @see http://www.martinfowler.com/eaaCatalog/dataTransferObject.html
33 *
34 * @author Matt Benson
35 * @since Morph 1.1
36 */
37 public abstract class AssemblyCopierSupport extends BaseCompositeTransformer {
38
39 /** Default ComponentValidator; ignores empty component list */
40 protected static final ComponentValidator DEFAULT_VALIDATOR = new SimpleComponentValidator() {
41 protected void validateImpl(Object composite) throws Exception {
42 List components = getComponentAccessor().getComponents(composite);
43 if (components != null) {
44 super.validateImpl(composite);
45 }
46 }
47 };
48
49 /**
50 * Create a new AssemblyCopierSupport.
51 */
52 protected AssemblyCopierSupport() {
53 super();
54 setComponentValidator(DEFAULT_VALIDATOR);
55 }
56
57 /**
58 * Create a new AssemblyCopierSupport.
59 * @param components
60 */
61 protected AssemblyCopierSupport(Object[] components) {
62 this();
63 setComponents(components);
64 }
65
66 /**
67 * {@inheritDoc}
68 */
69 protected void initializeImpl() throws Exception {
70 if (getNestedTransformer() == null) {
71 setNestedTransformer(Defaults.createCopier());
72 }
73 super.initializeImpl();
74 }
75
76 /**
77 * {@inheritDoc}
78 * @see net.sf.morph.transform.transformers.BaseCompositeTransformer#getComponentType()
79 */
80 public Class getComponentType() {
81 return Copier.class;
82 }
83
84 /**
85 * {@inheritDoc}
86 * @see net.sf.morph.transform.transformers.BaseTransformer#setSourceClasses(java.lang.Class[])
87 */
88 public synchronized void setSourceClasses(Class[] sourceClasses) {
89 super.setSourceClasses(sourceClasses);
90 }
91
92 /**
93 * {@inheritDoc}
94 * @see net.sf.morph.transform.transformers.BaseTransformer#setDestinationClasses(java.lang.Class[])
95 */
96 public synchronized void setDestinationClasses(Class[] destinationClasses) {
97 super.setDestinationClasses(destinationClasses);
98 }
99
100 /**
101 * Get the ContainerReflector used by this Transformer.
102 * @return ContainerReflector
103 */
104 protected ContainerReflector getContainerReflector() {
105 return (ContainerReflector) getReflector(ContainerReflector.class);
106 }
107
108 /**
109 * Set the transformer (Copier) which, in the absence of a components list, will be
110 * used to perform nested transformations.
111 *
112 * @param nestedTransformer
113 * the transformer used to perform nested transformations
114 */
115 public void setNestedTransformer(Transformer nestedTransformer) {
116 super.setNestedTransformer(nestedTransformer);
117 }
118
119 /**
120 * Get the transformer (Copier) which, in the absence of a components list, will be
121 * used to perform nested transformations.
122 *
123 * @return nested Transformer
124 */
125 public Transformer getNestedTransformer() {
126 return super.getNestedTransformer();
127 }
128
129 /**
130 * Get the Copier that will perform the copy for index <code>index</code>.
131 * @param index
132 * @return Copier
133 */
134 protected Copier getCopier(int index) {
135 Object[] components = getComponents();
136 if (components != null) {
137 if (components.length <= index) {
138 throw new TransformationException("Invalid copier requested: " + index
139 + " of " + components.length);
140 }
141 return (Copier) components[index];
142 }
143 return (Copier) getNestedTransformer();
144 }
145
146 /**
147 * {@inheritDoc}
148 * @see net.sf.morph.transform.transformers.BaseCompositeTransformer#setComponents(java.lang.Object[])
149 */
150 public synchronized void setComponents(Object[] components) {
151 super.setComponents(components);
152 }
153
154 /**
155 * {@inheritDoc}
156 * @see net.sf.morph.transform.transformers.BaseCompositeTransformer#setComponentValidator(net.sf.composite.validate.ComponentValidator)
157 */
158 public void setComponentValidator(ComponentValidator componentValidator) {
159 super.setComponentValidator(componentValidator == null ? DEFAULT_VALIDATOR
160 : componentValidator);
161 }
162
163 }