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.Locale;
19
20 import net.sf.morph.lang.DecoratedLanguage;
21 import net.sf.morph.lang.languages.SimpleLanguage;
22 import net.sf.morph.reflect.BeanReflector;
23 import net.sf.morph.transform.DecoratedConverter;
24 import net.sf.morph.transform.DecoratedCopier;
25 import net.sf.morph.transform.transformers.BaseTransformer;
26 import net.sf.morph.util.Assert;
27 import net.sf.morph.util.ClassUtils;
28
29 /**
30 * Uses a DecoratedLanguage to set the property denoted by an expression
31 * on the destination object.
32 *
33 * @author mbenson
34 * @since Morph 1.1
35 */
36 public class SetExpressionCopier extends BaseTransformer implements DecoratedCopier,
37 DecoratedConverter {
38 private String expression;
39 private DecoratedLanguage language;
40
41 /**
42 * Create a new SetExpressionCopier.
43 */
44 public SetExpressionCopier() {
45 super();
46 }
47
48 /**
49 * Create a new SetExpressionCopier.
50 */
51 public SetExpressionCopier(String expression) {
52 this();
53 setExpression(expression);
54 }
55
56 /**
57 * {@inheritDoc}
58 * @see net.sf.morph.transform.transformers.BaseTransformer#initializeImpl()
59 */
60 protected void initializeImpl() throws Exception {
61 super.initializeImpl();
62 Assert.notEmpty(getExpression(), "expression");
63 }
64
65 /**
66 * {@inheritDoc}
67 * @see net.sf.morph.transform.transformers.BaseTransformer#copyImpl(java.lang.Object, java.lang.Object, java.util.Locale, java.lang.Integer)
68 */
69 protected void copyImpl(Object destination, Object source, Locale locale,
70 Integer preferredTransformationType) throws Exception {
71 getLanguage().set(destination, expression, source, locale);
72 }
73
74 /**
75 * {@inheritDoc}
76 * @see net.sf.morph.transform.transformers.BaseTransformer#setDestinationClasses(java.lang.Class[])
77 */
78 public synchronized void setDestinationClasses(Class[] destinationClasses) {
79 super.setDestinationClasses(destinationClasses);
80 }
81
82 /**
83 * {@inheritDoc}
84 * @see net.sf.morph.transform.transformers.BaseTransformer#getDestinationClassesImpl()
85 */
86 protected Class[] getDestinationClassesImpl() throws Exception {
87 return new Class[] { Object.class };
88 }
89
90 /**
91 * {@inheritDoc}
92 * @see net.sf.morph.transform.transformers.BaseTransformer#setSourceClasses(java.lang.Class[])
93 */
94 public synchronized void setSourceClasses(Class[] sourceClasses) {
95 super.setSourceClasses(sourceClasses);
96 }
97
98 /**
99 * {@inheritDoc}
100 * @see net.sf.morph.transform.transformers.BaseTransformer#getSourceClassesImpl()
101 */
102 protected Class[] getSourceClassesImpl() throws Exception {
103 return ClassUtils.getAllClasses();
104 }
105
106 /**
107 * Get the String expression.
108 * @return String
109 */
110 public String getExpression() {
111 return expression;
112 }
113
114 /**
115 * Set the String expression.
116 * @param expression String
117 */
118 public void setExpression(String expression) {
119 this.expression = expression;
120 }
121
122 /**
123 * Get the DecoratedLanguage language.
124 * @return DecoratedLanguage
125 */
126 public synchronized DecoratedLanguage getLanguage() {
127 if (language == null) {
128 language = new SimpleLanguage();
129 ((SimpleLanguage) language)
130 .setReflector((BeanReflector) getReflector(BeanReflector.class));
131 }
132 return language;
133 }
134
135 /**
136 * Set the DecoratedLanguage language.
137 * @param language DecoratedLanguage
138 */
139 public synchronized void setLanguage(DecoratedLanguage language) {
140 this.language = language;
141 }
142
143 /**
144 * {@inheritDoc}
145 */
146 protected boolean isAutomaticallyHandlingNulls() {
147 return false;
148 }
149 }