001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.contrib.table.model.ognl;
016    
017    import org.apache.commons.logging.Log;
018    import org.apache.commons.logging.LogFactory;
019    import org.apache.tapestry.contrib.table.model.ITableColumn;
020    import org.apache.tapestry.contrib.table.model.simple.ITableColumnEvaluator;
021    import org.apache.tapestry.services.ExpressionEvaluator;
022    
023    /**
024     * @author mindbridge
025     */
026    public class OgnlTableColumnEvaluator implements ITableColumnEvaluator
027    {
028    
029        private static final long serialVersionUID = 1L;
030    
031        private static final Log LOG = LogFactory.getLog(OgnlTableColumnEvaluator.class);
032        
033        /** @since 4.0 */
034    
035        private ExpressionEvaluator _expressionEvaluator;
036    
037        private String m_strExpression;
038    
039        public OgnlTableColumnEvaluator(String strExpression,
040                ExpressionEvaluator expressionEvaluator)
041        {
042            m_strExpression = strExpression;
043            _expressionEvaluator = expressionEvaluator;
044        }
045    
046        /**
047         * @see org.apache.tapestry.contrib.table.model.simple.ITableColumnEvaluator#getColumnValue(ITableColumn,
048         *      Object)
049         */
050        public synchronized Object getColumnValue(ITableColumn objColumn,
051                Object objRow)
052        {
053            // If no expression is given, then this is a dummy column. Return
054            // something.
055            if (m_strExpression == null || m_strExpression.equals("")) return "";
056    
057            try
058            {
059                return _expressionEvaluator.read(objRow, m_strExpression);
060            }
061            catch (Exception e)
062            {
063                LOG.error("Cannot use column expression '" + m_strExpression
064                        + "' in row", e);
065                return "";
066            }
067        }
068    }