001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */ 
017    
018    package org.apache.commons.betwixt.expression;
019    
020    import org.apache.commons.beanutils.DynaBean;
021    
022    /**
023     * An Expression that gets a property value from a DynaBean.
024     * 
025     * @see org.apache.commons.beanutils.DynaBean
026     * 
027     * @author Michael Becke
028     * @since 0.5
029     */
030    public class DynaBeanExpression implements Expression {
031    
032        /** The name of the DynaBean property to get */
033        private String propertyName;
034    
035        /**
036         * Crates a new DynaBeanExpression.
037         */
038        public DynaBeanExpression() {
039            super();
040        }
041    
042        /**
043         * Crates a new DynaBeanExpression.
044         * 
045         * @param propertyName the name of the DynaBean property to use
046         */
047        public DynaBeanExpression(String propertyName) {
048            super();
049            setPropertyName(propertyName);
050        }
051    
052        /**
053         * Returns the value of a DynaBean property from the bean stored in 
054         * the Context.  Returns <code>null</code> if no DynaBean is stored 
055         * in the Context or if the propertyName has not been set.
056         * 
057         * @param context the content containing the DynaBean
058         * 
059         * @return the DynaBean property value or <code>null</code>
060         */
061        public Object evaluate(Context context) {
062            
063            if (context.getBean() instanceof DynaBean && propertyName != null) {
064                return ((DynaBean)context.getBean()).get(propertyName);
065            } else {
066                return null;
067            }
068        }
069    
070        /**
071         * Do nothing.
072         * @see Expression#update
073         */
074        public void update(Context context, String newValue) {
075            // do nothing
076        }
077    
078        /**
079         * Gets the name of the property to get from the DynaBean.
080         * @return the name of the property that this expression reads
081         */
082        public String getPropertyName() {
083            return propertyName;
084        }
085    
086        /**
087         * Sets the name of the property to get from the DynaBean.
088         * @param propertyName the property that this expression reads, not null
089         */
090        public void setPropertyName(String propertyName) {
091            if (propertyName == null) {
092                throw new IllegalArgumentException("propertyName is null");
093            }
094            this.propertyName = propertyName;
095        }
096    
097    }