1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.integration.velocity;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import net.sf.morph.reflect.reflectors.BaseBeanReflector;
22
23 import org.apache.velocity.VelocityContext;
24 import org.apache.velocity.context.Context;
25
26 /**
27 * Exposes the information in a {@link org.apache.velocity.context.Context}.
28 *
29 * @author Matt Sgarlata
30 * @since Dec 29, 2004
31 */
32 public class VelocityContextReflector extends BaseBeanReflector {
33
34 private static final Class[] REFLECTABLE_TYPES = { VelocityContext.class };
35
36 /**
37 * {@inheritDoc}
38 */
39 protected Class[] getReflectableClassesImpl() throws Exception {
40 return REFLECTABLE_TYPES;
41 }
42
43 /**
44 * Get a Velocity Context from the specified bean.
45 * @param bean expected to be the Context in the default implementation.
46 * @return Context
47 */
48 protected Context getVelocityContext(Object bean) {
49 return (Context) bean;
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 protected String[] getPropertyNamesImpl(Object bean) throws Exception {
56 Object[] keys = getVelocityContext(bean).getKeys();
57 List propertyNames = new ArrayList();
58 for (int i=0; i<keys.length; i++) {
59 if (keys[i] instanceof String) {
60 propertyNames.add(keys[i]);
61 }
62 }
63 return (String[]) propertyNames.toArray(new String[propertyNames.size()]);
64 }
65
66 /**
67 * {@inheritDoc}
68 */
69 protected Class getTypeImpl(Object bean, String propertyName) throws Exception {
70 return Object.class;
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 protected boolean isReadableImpl(Object bean, String propertyName) throws Exception {
77 return true;
78 }
79
80 /**
81 * {@inheritDoc}
82 */
83 protected boolean isWriteableImpl(Object bean, String propertyName) throws Exception {
84 return true;
85 }
86
87 /**
88 * {@inheritDoc}
89 */
90 protected Object getImpl(Object bean, String propertyName) throws Exception {
91 return getVelocityContext(bean).get(propertyName);
92 }
93
94 /**
95 * {@inheritDoc}
96 */
97 protected void setImpl(Object bean, String propertyName, Object value) throws Exception {
98 getVelocityContext(bean).put(propertyName, value);
99 }
100
101 }