View Javadoc

1   /*
2    * Copyright 2004-2005, 2007 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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 }