You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ov...@apache.org on 2002/02/13 02:47:51 UTC

cvs commit: xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/scheme/sitemap SchemeEnvironmentFunctions.java

ovidiu      02/02/12 17:47:51

  Added:       src/scratchpad/schecoon/src/org/apache/cocoon/scheme/sitemap
                        SchemeEnvironmentFunctions.java
  Log:
  Added. Defines primitives for operating on the environment, request and others.
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/scheme/sitemap/SchemeEnvironmentFunctions.java
  
  Index: SchemeEnvironmentFunctions.java
  ===================================================================
  package org.apache.cocoon.scheme.sitemap;
  
  import java.util.Map;
  import org.apache.cocoon.Constants;
  import org.apache.cocoon.environment.Environment;
  import org.apache.cocoon.environment.Request;
  import sisc.ContinuationException;
  import sisc.Interpreter;
  import sisc.ModuleAdapter;
  import sisc.data.ImmutableString;
  import sisc.data.Value;
  
  /**
   * Some environment manipulation functions.
   *
   * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
   * @since February 11, 2002
   */
  public class SchemeEnvironmentFunctions extends CocoonModule
  {
    public String getModuleName()
    {
      return "Schecoon Environment manipulation";
    }
  
    public float getModuleVersion()
    {
      return 1.0f;
    }
  
    public static final int
      ENV_SET_ATTR = 1,
      ENV_GET_ATTR = 2,
      REQ_GET_PARAM = 3;
  
    public SchemeEnvironmentFunctions()
    {
      define("environ:set-attr", ENV_SET_ATTR);
      define("environ:get-attr", ENV_GET_ATTR);
      define("request:get-param", REQ_GET_PARAM);
    }
  
    /**
     * The SISC evaluator function for this module. Executes the actual
     * Java code corresponding to the Scheme functions defined by the
     * module.
     *
     * @param primid an <code>int</code> value
     * @param r an <code>Interpreter</code> value
     * @return a <code>Value</code> value
     * @exception ContinuationException if an error occurs
     */
    public Value eval(int primid, Interpreter r)
      throws ContinuationException
    {
      switch (r.vlr.length) {
        // Two argument functions
      case 2:
        switch (primid) {
        case ENV_GET_ATTR:
          return getEnvironmentAttribute(r.vlr[0], r.vlr[1]);
        case REQ_GET_PARAM:
          return getRequestParameter(r.vlr[0], r.vlr[1]);
        }
  
        // Three argument functions
      case 3:
        switch (primid) {
        case ENV_SET_ATTR:
          return setEnvironmentAttribute(r.vlr[0], r.vlr[1], r.vlr[2]);
        }
      }
  
      throw new RuntimeException("Invalid number of arguments to function "
                                 + r.acc + ", got " + r.vlr.length);
    }
  
    public Value setEnvironmentAttribute(Value senv, Value sattrName, Value svalue)
    {
      Environment environment = environment(senv);
      String attrName = string(sattrName);
  
      environment.setAttribute(attrName, svalue);
      return svalue;
    }
  
    public Value getEnvironmentAttribute(Value senv, Value sattrName)
    {
      Environment environment = environment(senv);
      String attrName = string(sattrName);
  
      return (Value)environment.getAttribute(attrName);
    }
  
    public Value getRequestParameter(Value senv, Value sparamName)
    {
      Environment environment = environment(senv);
      String paramName = string(sparamName);
      Map objectModel = environment.getObjectModel();
      Request request = (Request)objectModel.get(Constants.REQUEST_OBJECT);
  
      String value = request.getParameter(paramName);
      return new ImmutableString(value);
    }
  }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org