EvalAccessor.java :  » Web-Framework » calyxo » de » odysseus » calyxo » base » misc » Java Open Source

Java Open Source » Web Framework » calyxo 
calyxo » de » odysseus » calyxo » base » misc » EvalAccessor.java
/*
 * Copyright 2004, 2005, 2006 Odysseus Software GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package de.odysseus.calyxo.base.misc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;

import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.access.Accessor;
import de.odysseus.calyxo.base.util.MapFacade;

/**
 * This accessor evaluates an el expression passed as a string.
 * It uses a {@link de.odysseus.calyxo.base.misc.CalyxoVariableResolver}
 * to resolve implicite objects and to search scopes.
 * 
 * @author Christoph Beck
 */
public class EvalAccessor implements Accessor {
  private static class EvalMap extends MapFacade {
    private ExpressionEvaluator evaluator;
    private VariableResolver variables;

    EvalMap(ExpressionEvaluator evaluator, VariableResolver variables) {
      this.evaluator = evaluator;
      this.variables = variables;
    }
    
    /* (non-Javadoc)
     * @see java.util.Map#get(java.lang.Object)
     */
    public Object get(Object key) {
      if (key == null) {
        return null;
      }
      StringBuffer expr = new StringBuffer();
      expr.append("${");
      expr.append(key.toString());
      expr.append("}");
      try {
        return evaluator.evaluate(expr.toString(), Object.class, variables, null);
      } catch (ELException e) {
        return null;
      }
    }
  }

  private final ModuleContext context;

  /**
   * Constructor.
   */
  public EvalAccessor(ModuleContext context) {
    this.context = context;
  }

  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.access.Accessor#get(javax.servlet.http.HttpServletRequest)
   */
  public Object get(HttpServletRequest request) {
    return new EvalMap(context.getExpressionEvaluator(), new CalyxoVariableResolver(request, context));
  }

  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.access.Accessor#isCacheable()
   */
  public boolean isCacheable() {
    return true;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.