Home » tiles-2.2.1-src » org.apache.tiles.el » [javadoc | source]

    1   /*
    2    * $Id: ELAttributeEvaluator.java 817357 2009-09-21 18:16:20Z apetrelli $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    * http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   20    */
   21   package org.apache.tiles.el;
   22   
   23   import java.util.Map;
   24   
   25   import javax.el.ArrayELResolver;
   26   import javax.el.BeanELResolver;
   27   import javax.el.CompositeELResolver;
   28   import javax.el.ELResolver;
   29   import javax.el.ExpressionFactory;
   30   import javax.el.ListELResolver;
   31   import javax.el.MapELResolver;
   32   import javax.el.ResourceBundleELResolver;
   33   import javax.el.ValueExpression;
   34   
   35   import org.apache.tiles.TilesApplicationContext;
   36   import org.apache.tiles.awareness.TilesApplicationContextAware;
   37   import org.apache.tiles.context.TilesRequestContext;
   38   import org.apache.tiles.evaluator.AbstractAttributeEvaluator;
   39   import org.apache.tiles.reflect.ClassUtil;
   40   
   41   /**
   42    * Evaluates string expression with typical EL syntax.<br>
   43    * You can use normal EL syntax, knowing that the root objects are
   44    * {@link TilesRequestContext}, {@link TilesApplicationContext} and beans
   45    * contained in request, session and application scope.
   46    *
   47    * @version $Rev: 817357 $ $Date: 2009-09-21 20:16:20 +0200 (lun, 21 set 2009) $
   48    * @since 2.2.1
   49    */
   50   public class ELAttributeEvaluator extends AbstractAttributeEvaluator implements
   51           TilesApplicationContextAware {
   52   
   53       /**
   54        * Initialization parameter to decide the implementation of
   55        * {@link ExpressionFactoryFactory}.
   56        *
   57        * @since 2.2.1
   58        */
   59       public static final String EXPRESSION_FACTORY_FACTORY_INIT_PARAM =
   60           "org.apache.tiles.evaluator.el.ExpressionFactoryFactory";
   61   
   62       /**
   63        * The Tiles application context.
   64        *
   65        * @since 2.2.1
   66        */
   67       protected TilesApplicationContext applicationContext;
   68   
   69       /**
   70        * The EL expression factory.
   71        *
   72        * @since 2.2.1
   73        */
   74       protected ExpressionFactory expressionFactory;
   75   
   76       /**
   77        * The EL resolver to use.
   78        *
   79        * @since 2.2.1
   80        */
   81       protected ELResolver resolver;
   82   
   83       /**
   84        * Constructor.
   85        *
   86        * @since 2.2.1
   87        */
   88       public ELAttributeEvaluator() {
   89       }
   90   
   91       /** {@inheritDoc} */
   92       public void init(Map<String, String> initParameters) {
   93           String expressionFactoryClassName = initParameters
   94                   .get(EXPRESSION_FACTORY_FACTORY_INIT_PARAM);
   95           ExpressionFactoryFactory efFactory;
   96           if (expressionFactoryClassName != null) {
   97               efFactory = (ExpressionFactoryFactory) ClassUtil
   98                       .instantiate(expressionFactoryClassName);
   99           } else {
  100               efFactory = new JspExpressionFactoryFactory();
  101           }
  102           if (efFactory instanceof TilesApplicationContextAware) {
  103               ((TilesApplicationContextAware) efFactory)
  104                       .setApplicationContext(applicationContext);
  105           }
  106           expressionFactory = efFactory.getExpressionFactory();
  107           resolver = new CompositeELResolver() {
  108               {
  109                   add(new TilesContextELResolver());
  110                   add(new TilesContextBeanELResolver());
  111                   add(new ArrayELResolver(false));
  112                   add(new ListELResolver(false));
  113                   add(new MapELResolver(false));
  114                   add(new ResourceBundleELResolver());
  115                   add(new BeanELResolver(false));
  116               }
  117           };
  118       }
  119   
  120       /** {@inheritDoc} */
  121       public void setApplicationContext(TilesApplicationContext applicationContext) {
  122           this.applicationContext = applicationContext;
  123       }
  124   
  125       /**
  126        * Sets the expression factory to use.
  127        *
  128        * @param expressionFactory The expression factory.
  129        * @since 2.2.1
  130        */
  131       public void setExpressionFactory(ExpressionFactory expressionFactory) {
  132           this.expressionFactory = expressionFactory;
  133       }
  134   
  135       /**
  136        * Sets the EL resolver to use.
  137        *
  138        * @param resolver The EL resolver.
  139        * @since 2.2.1
  140        */
  141       public void setResolver(ELResolver resolver) {
  142           this.resolver = resolver;
  143       }
  144   
  145       /** {@inheritDoc} */
  146       public Object evaluate(String expression, TilesRequestContext request) {
  147           ELContextImpl context = new ELContextImpl(resolver);
  148           context.putContext(TilesRequestContext.class, request);
  149           context.putContext(TilesApplicationContext.class,
  150                   applicationContext);
  151           ValueExpression valueExpression = expressionFactory
  152                   .createValueExpression(context, expression, Object.class);
  153   
  154           return valueExpression.getValue(context);
  155       }
  156   }

Home » tiles-2.2.1-src » org.apache.tiles.el » [javadoc | source]