cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ovi...@apache.org
Subject cvs commit: xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/scheme/sitemap SchemeSitemap.java
Date Tue, 08 Jan 2002 23:15:07 GMT
ovidiu      02/01/08 15:15:07

  Added:       src/scratchpad/schecoon/src/org/apache/cocoon/scheme/sitemap
                        SchemeSitemap.java
  Log:
  Renamed SitemapComponents to SchemeSitemap. This file contains now the definitions of the
Scheme sitemap functions.
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/src/scratchpad/schecoon/src/org/apache/cocoon/scheme/sitemap/SchemeSitemap.java
  
  Index: SchemeSitemap.java
  ===================================================================
  package org.apache.cocoon.scheme.sitemap;
  
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.cocoon.components.pipeline.EventPipeline;
  import org.apache.cocoon.components.pipeline.StreamPipeline;
  import org.apache.cocoon.environment.Environment;
  import sisc.ContinuationException;
  import sisc.Interpreter;
  import sisc.ModuleAdapter;
  import sisc.data.Pair;
  import sisc.data.Symbol;
  import sisc.data.Value;
  import sisc.modules.J2S;
  
  /**
   * This class contains the definition of the Scheme functions:
   *
   * <ul>
   *
   *  <li><b>sitemap:generate</b> - create a new {@link
   *  org.apache.cocoon.components.pipeline.EventPipeline}, and adds the
   *  generator specified by the function arguments to it. The prototype
   *  of Scheme function is:
   *  <pre>
   *   sitemap:generate: SitemapComponents Environment params -> EventPipeline
   *  </pre>
   *
   *
   *  <li><b>sitemap:read</b> - creates a new {@link
   *  org.apache.cocoon.components.pipeline.StreamPipeline} and a new
   *  {@link org.apache.cocoon.reading.Reader}, adds the reader to the
   *  pipeline, and returns the pipeline. The prototype of the Scheme
   *  function is:
   *  <pre>
   *   sitemap:read: SitemapComponents Environment params -> StreamPipeline
   *  </pre>
   *
   *  <li><b>sitemap:transform</b> - creates a transformer object of the
   *  type specified by the arguments, adds it to the pipeline passed as
   *  argument, and returns the pipeline. The prototype of the Scheme
   *  function is:
   *  <pre>
   *   sitemap:transform: SitemapComponents Environment params EventPipeline
   *                      -> EventPipeline
   *  </pre>
   *
   *  <li><b>sitemap:serialize</b> - creates a serializer object of the
   *  type specified by the arguments, adds it to the pipeline passed as
   *  argument, and returns the pipeline. The prototype of the Scheme
   *  function is:
   *  <pre>
   *   sitemap:serialize: SitemapComponents Environment params EventPipeline
   *                      -> EventPipeline
   *  </pre>
   *
   *  <li><b>sitemap:process</b> - processes the given pipeline passed
   *  as argument. This has the side effect of processing the pipeline,
   *  as it was specified in the sitemap.
   *  <pre>
   *   sitemap:process: SitemapComponents Environment params EventPipeline
   *                    -> void
   *  </pre>
   *
   * </ul>
   *
   * <p>With these Scheme functions, a pipeline processing setup would
   * look like this:
   *
   * <pre>
   * (match "some-url/(.*)" (sitemap env file . args)
   *   (sitemap:process sitemap env (list (cons 'type "xml"))
   *     (sitemap:transform sitemap env (list (cons 'type "xslt")
   *                                          (cons 'src "stylesheet/a.xsl"))
   *       (sitemap:generate sitemap env (list (cons 'src (concat "docs/" file))))
   *     )))
   * </pre>
   *
   * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
   * @since December 20, 2001
   *
   * @see org.apache.cocoon.components.pipeline.EventPipeline
   * @see org.apache.cocoon.components.pipeline.StreamPipeline
   * @see org.apache.cocoon.reading.Reader
   */
  public class SchemeSitemap extends ModuleAdapter
  {
    public String getModuleName()
    {
      return "Apache Cocoon Sitemap Components";
    }
  
    public float getModuleVersion()
    {
      return 1.0f;
    }
  
    public static final int
      GENERATE = 1,
      READER = 2,
      TRANSFORM = 3,
      SERIALIZE = 4,
      PROCESS = 5;
  
    protected Parameters emptyParam = new Parameters();
  
    /**
     * Creates a new <code>SchemeSitemap</code> instance. Defines
     * the Scheme functions implemented by this module.
     */
    public SchemeSitemap()
    {
      define("sitemap:generate", GENERATE);
      define("sitemap:read", READER);
      define("sitemap:transform", TRANSFORM);
      define("sitemap:serialize", SERIALIZE);
      define("sitemap:process", PROCESS);
    }
  
    /**
     * 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
     */
    public Value eval(int primid, Interpreter r)
      throws ContinuationException
    {
      try {
        switch (r.vlr.length) {
  
          // Three argument functions
        case 3:
          switch (primid) {
          case GENERATE:
            return generate(r.vlr[0], r.vlr[1], r.vlr[2]);
          case READER:
            return read(r.vlr[0], r.vlr[1], r.vlr[2]);
          }
  
        case 4:
          switch (primid) {
          case TRANSFORM:
            return transform (r.vlr[0], r.vlr[1], r.vlr[2], r.vlr[3]);
          case SERIALIZE:
            return serialize (r.vlr[0], r.vlr[1], r.vlr[2], r.vlr[3]);
          case PROCESS:
            return process (r.vlr[0], r.vlr[1], r.vlr[2], r.vlr[3]);
          }
        }
      }
      catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex.toString());
      }
  
      throw new RuntimeException("Invalid number of arguments to function "
                                 + r.acc + ", got " + r.vlr.length);
    }
  
    /**
     * Type cast function from a Scheme wrapper of a ComponentManager.
     *
     * @param scm a Scheme wrapper instance of a ComponentManager.
     * @return a <code>ComponentManager</code> value
     */
    static public SitemapComponents sitemapComponents(Value scm)
    {
      try {
        return (SitemapComponents)(((J2S.JavaObject)scm).o);
      }
      catch (ClassCastException ex) {
        typeError("SitemapComponents", scm);
      }
      return null;
    }
  
    /**
     * Type cast function from a Scheme wrapper of an Environment
     * instance.
     *
     * @param senv a Scheme wrapper of an Environment instance.
     * @return an <code>Environment</code> value
     */
    static public Environment environment(Value senv)
    {
      try {
        return (Environment)(((J2S.JavaObject)senv).o);
      }
      catch (ClassCastException ex) {
        typeError("Environment", senv);
      }
      return null;
    }
  
    /**
     * Retrieve an entry from an association list. Uses eq? to compare
     * the CAR of each entry.
     *
     * @param l the association list
     * @param v the value to be searched for
     * @return a <code>Pair</code> value representing the entry, or
     * <tt>FALSE</tt> if no such entry is present.
     */
    static public Value assq (Value l, Value v)
    {
      Pair list = pair(l);
      while (list != EMPTYLIST) {
        Pair entry = pair(list.car);
        if (entry.car.eq(v))
          return entry;
        list = pair(list.cdr);
      }
      return FALSE;
    }
  
    /**
     * Assumes the <tt>sparams</tt> is either an association list or the
     * FALSE value. It returns either an empty Avalon
     * <tt>Parameters</tt> instance, or a <tt>Parameters</tt> instance
     * that contains the values extracted from the association list.
     *
     * @param sparams a <code>Value</code> value, either <tt>FALSE</tt>
     * or an association list
     * @return an Avalon <code>Parameters</code> instance
     */
    public Parameters getParameters(Value sparams)
    {
      Parameters params = emptyParam;
  
      if (!sparams.eq(FALSE)) {
        params = new Parameters();
        Pair sparamValues = pair(pair(sparams).cdr);
        while (sparamValues != EMPTYLIST) {
          Pair entry = pair(sparamValues.car);
          String name = string(entry.car);
          String value = string(entry.cdr);
          params.setParameter(name, value);
          sparamValues = pair(sparamValues.cdr);
        }
      }
  
      return params;
    }
  
    /**
     * <p>Creates a new <tt>EventPipeline</tt> instance, and a new
     * Generator instance. Adds the Generator to the pipeline.
     *
     * <p>The type of the generator is specified in the <tt>sargs</tt>
     * list, which is a Scheme association list. The parameters to be
     * passed to the generators, if any, should be present in this
     * association list as the value of the <it>params</it> entry. This
     * value should be another association list, where the CAR of each
     * entry is the name of the parameter, and the CDR of the entry is
     * the actual value.
     *
     * <p>The recognized parameters are:
     *
     * <ul>
     *
     *  <li><b>src</b> - (required) the source of the generator
     *
     *  <li><b>type</b> - (optional) the type of generator. If not
     *  specified, the default generator specified using the
     *  <tt>default</tt> attribute of the <tt>generators</tt> XML
     *  element is used.
     *
     *  <li><b>params</b> - (optional) the parameters to be passed to
     *  the generator. The CDR of this association entry should be
     *  another association entry, that contains the names and values of
     *  the parameters.
     *
     * </ul>
     *
     * <p><b>Example:</b>
     *
     * <pre>
     * (match "generate/(.*)" (sitemap env file . args)
     *  (sitemap:generate sitemap env
     *                    (list (cons 'src (concat "doc/samples" file ".xml")))))
     * </pre>
     *
     * @param scm the Scheme wrapper for the SitemapComponents instance
     * @param senv the Scheme wrapper for the Environment instance
     * @param sargs the Scheme arguments, as list
     * @return a Scheme wrapper for the <tt>EventPipeline</tt> instance
     * @exception Exception if an error occurs
     *
     * @see SitemapComponents
     */
    public Value generate(Value scm, Value senv, Value sargs)
      throws Exception
    {
      SitemapComponents sitemapComponents = sitemapComponents(scm);
      EventPipeline eventPipeline = sitemapComponents.getEventPipeline();
  
      // Obtain the 'src' attribute
      Value ssrc = assq(sargs, Symbol.get("src"));
      if (ssrc.eq(FALSE))
        throw new RuntimeException("No 'src' attribute specified for 'generate'!");
      String src = string(pair(ssrc).cdr);
  
      // Obtain the 'type' attribute
      Value stype = assq(sargs, Symbol.get("type"));
      String type;
      if (!stype.eq(FALSE))
        type = string(pair(ssrc).cdr);
      else
        type = sitemapComponents.getDefaultGeneratorType();
  
      // Obtain the parameters
      Value sparams = assq(sargs, Symbol.get("params"));
      Parameters params = getParameters(sparams);
  
      eventPipeline.setGenerator(type, src, params);
  
      return new J2S.JavaObject(eventPipeline);
    }
  
    /**
     * <p>Creates a new <tt>Reader</tt> and a new
     * <tt>StreamPipeline</tt>. Adds the reader to the pipeline and
     * returns the pipeline newly created.
     *
     * <p>The parameters that describe the reader are specified in the
     * <tt>sargs</tt> list, which is a Scheme association list.
     *
     * <p>The recognized parameters for a reader are:
     *
     * <ul>
     *  <li><b>src</b> - (required) the source for the reader
     *
     *  <li><b>mime-type</b> - (optional) the MIME type associated with
     *  the source. If no MIME type is specified, text/html is assumed.
     *
     *  <li><b>type</b> - (optional) if no type is specified, the
     *  default reader type is used. The default reader type is
     *  specified using the <tt>default</tt> attribute of the
     *  <tt>readers</tt> XML element.
     *
     *  <li><b>params</b> - (optional) additional parameters to be
     *  specified when configuring the reader. The value of this entry
     *  should be a Scheme association list, which contains the
     *  additional parameters.
     *
     * </ul>
     *
     * @param scm the Scheme wrapper for the SitemapComponents instance
     * @param senv the Scheme wrapper for the Environment instance
     * @param sargs the Scheme arguments, as list
     * @return a Scheme wrapper for the <tt>StreamPipeline</tt> instance
     * @exception Exception if an error occurs
     *
     * @see SitemapComponents
     */
    public Value read(Value scm, Value senv, Value sargs)
      throws Exception
    {
      SitemapComponents sitemapComponents = sitemapComponents(scm);
      StreamPipeline streamPipeline = sitemapComponents.getStreamPipeline();
      EventPipeline eventPipeline = sitemapComponents.getEventPipeline();
  
      streamPipeline.setEventPipeline(eventPipeline);
  
      // Obtain the 'src' attribute
      Value ssrc = assq(sargs, Symbol.get("src"));
      if (ssrc.eq(FALSE))
        throw new RuntimeException("No 'src' attribute specified for 'read'!");
      String src = string(pair(ssrc).cdr);
  
      // Obtain the 'mime-type' attribute
      Value smimeType = assq(sargs, Symbol.get("mime-type"));
      String mimeType;
      if (smimeType.eq(FALSE))
        mimeType = "text/html";
      else
        mimeType = string(pair(smimeType).cdr);
  
      // Obtain the 'type' attribute
      Value stype = assq(sargs, Symbol.get("type"));
      String type;
      if (!stype.eq(FALSE))
        type = string(pair(ssrc).cdr);
      else
        type = sitemapComponents.getDefaultReaderType();
  
      // Obtain the parameters
      Value sparams = assq(sargs, Symbol.get("params"));
      Parameters params = getParameters(sparams);
  
      streamPipeline.setReader(type, src, params, mimeType);
  
      return new J2S.JavaObject(streamPipeline);
    }
  
    public Value transform(Value scm, Value senv, Value sargs, Value spipeline)
    {
      return null;
    }
  
    public Value serialize(Value scm, Value senv, Value sargs, Value spipeline)
    {
      return null;
    }
  
    /**
     * Invokes the <tt>process</tt> method of the pipeline instance
     * passed as argument. The pipeline could be either an {@link
     * org.apache.cocoon.components.pipeline.EventPipeline} or a {@link
     * org.apache.cocoon.components.pipeline.StreamPipeline}.
     *
     * <p>This function releases the pipeline, so it must be the last
     * function you call on the pipeline.
     *
     * @param scm the Scheme wrapper for the SitemapComponents instance
     * @param senv the Scheme wrapper for the Environment instance
     * @param sargs the Scheme arguments, as list
     * @param spipeline the Scheme wrapper for pipeline object
     * @return null
     * @exception Exception if an error occurs
     */
    public Value process(Value scm, Value senv, Value sargs, Value spipeline)
      throws Exception
    {
      SitemapComponents sitemapComponents = sitemapComponents(scm);
      Environment env = environment(senv);
      Object pipeline = ((J2S.JavaObject)spipeline).o;
  
      if (pipeline instanceof EventPipeline)
        ((EventPipeline)pipeline).process(env);
      else {
        ((StreamPipeline)pipeline).process(env);
        EventPipeline eventPipeline
          = ((StreamPipeline)pipeline).getEventPipeline();
        sitemapComponents.releasePipeline((Component)eventPipeline);
      }
  
      sitemapComponents.releasePipeline((Component)pipeline);
  
      return null;
    }
  }
  
  
  

----------------------------------------------------------------------
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


Mime
View raw message