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