You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by co...@apache.org on 2003/06/22 20:48:20 UTC

cvs commit: cocoon-2.1/src/scratchpad/webapp/samples/garbage/calc/screens displayResult.gt getNumberA.gt getNumberB.gt getOperator.gt

coliver     2003/06/22 11:48:20

  Modified:    src/scratchpad/webapp/samples scratchpad-samples.xml
  Added:       src/scratchpad/lib apache-garbage-0.0.jar
               src/scratchpad/src/org/apache/cocoon/generation
                        GarbageGenerator.java
               src/scratchpad/webapp/samples/garbage sitemap.xmap
               src/scratchpad/webapp/samples/garbage/calc calc.js
                        sitemap.xmap
               src/scratchpad/webapp/samples/garbage/calc/screens
                        displayResult.gt getNumberA.gt getNumberB.gt
                        getOperator.gt
  Log:
  Added GarbageGenerator and sample
  
  Revision  Changes    Path
  1.1                  cocoon-2.1/src/scratchpad/lib/apache-garbage-0.0.jar
  
  	<<Binary file>>
  
  
  1.1                  cocoon-2.1/src/scratchpad/src/org/apache/cocoon/generation/GarbageGenerator.java
  
  Index: GarbageGenerator.java
  ===================================================================
  /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.generation;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.cocoon.ProcessingException;
  import org.apache.cocoon.components.flow.FlowHelper;
  import org.apache.cocoon.components.flow.WebContinuation;
  import org.apache.cocoon.components.flow.javascript.JavaScriptFlow;
  import org.apache.cocoon.components.source.SourceUtil;
  import org.apache.cocoon.environment.ObjectModelHelper;
  import org.apache.cocoon.environment.Request;
  import org.apache.cocoon.environment.Response;
  import org.apache.cocoon.environment.SourceResolver;
  import org.apache.cocoon.transformation.AbstractTransformer;
  import org.apache.cocoon.xml.XMLConsumer;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.Variables;
  import org.apache.garbage.parser.Parser;
  import org.apache.garbage.tree.Tree;
  import org.apache.garbage.tree.TreeException;
  import org.apache.garbage.Processor;
  import org.apache.garbage.Processor;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXParseException;
  import java.util.Map;
  import java.util.HashMap;
  import java.io.IOException;
  import org.apache.cocoon.ProcessingException;
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.SourceException;
  
  public class GarbageGenerator extends ComposerGenerator {
  
      private XMLConsumer consumer;
      private JXPathContext jxpathContext;
      private static Map cache = new HashMap();
      private Source source;
  
      public void recycle() {
          super.recycle();
          consumer = null;
          jxpathContext = null;
          source = null;
      }
  
      private static class CacheEntry {
  	Tree tree;
  	long compileTime;
      }
  
      public void setup(SourceResolver resolver, Map objectModel,
                        String src, Parameters parameters)
          throws ProcessingException, SAXException, IOException {
  
          super.setup(resolver, objectModel, src, parameters);
          if (src != null) {
              try {
                  this.source = resolver.resolveURI(src);
              } catch (SourceException se) {
                  throw SourceUtil.handle("Error during resolving of '" + src + "'.", se);
              }
              long lastMod = source.getLastModified();
              String uri = source.getURI();
              synchronized (cache) {
                  CacheEntry t = (CacheEntry)cache.get(uri);
                  if (t != null &&
                      lastMod > t.compileTime) {
                      cache.remove(uri);
                  }
              }
          }
          Object bean = FlowHelper.getContextObject(objectModel);
          WebContinuation kont = FlowHelper.getWebContinuation(objectModel);
          setContext(bean, kont,
  		   ObjectModelHelper.getRequest(objectModel),
  		   ObjectModelHelper.getResponse(objectModel),
  		   ObjectModelHelper.getContext(objectModel),
  		   parameters);
      }
      
      private void setContext(Object contextObject,
  			    WebContinuation kont,
  			    Request request,
  			    Response response,
  			    org.apache.cocoon.environment.Context app,
  			    Parameters parameters) {
          jxpathContext = JXPathContext.newContext(contextObject);
  	Variables varScope = jxpathContext.getVariables();
  	varScope.declareVariable("flowContext", contextObject);
  	varScope.declareVariable("continuation", kont);
  	varScope.declareVariable("request", request);
  	varScope.declareVariable("response", response);
  	varScope.declareVariable("context", app);
  	varScope.declareVariable("parameters", parameters);
  	varScope.declareVariable("session", request.getSession(false));
  	jxpathContext.setVariables(varScope);
      }
  
      public void setConsumer(XMLConsumer consumer) {
          this.consumer = consumer;
      }
  
      public void generate() 
          throws IOException, SAXException, ProcessingException {
  	try {
  	    CacheEntry t;
  	    synchronized (cache) {
  		t = (CacheEntry)cache.get(source.getURI());
  	    }
  	    if (t == null) {
  		t = new CacheEntry();
  		t.compileTime = source.getLastModified();
  		Parser parser = new Parser();
  		InputSource is = new InputSource(source.getInputStream());
  		is.setSystemId(source.getURI());
  		t.tree = parser.parse(is);
  		synchronized (cache) {
  		    cache.put(source.getURI(), t);
  		}
  	    }
  	    new Processor(consumer, consumer).process(t.tree, jxpathContext);
  	} catch (TreeException exc) {
  	    throw new SAXParseException(exc.getMessage(), exc, exc);
  	}
      }
  }
  
  
  
  1.8       +7 -1      cocoon-2.1/src/scratchpad/webapp/samples/scratchpad-samples.xml
  
  Index: scratchpad-samples.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/scratchpad/webapp/samples/scratchpad-samples.xml,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- scratchpad-samples.xml	13 Jun 2003 10:27:38 -0000	1.7
  +++ scratchpad-samples.xml	22 Jun 2003 18:48:20 -0000	1.8
  @@ -19,6 +19,12 @@
         JXForms Feedback Wizard Sample 
       </sample>
     </group>
  +
  + <group name="Garbage">
  +    <sample name="Garbage" href="garbage/">
  +      Flowscript Calculator Sample using Garbage Template Generator
  +    </sample>
  +  </group>
     
     <group name="Castor">
       <sample name="CastorTransformer" href="castor/">
  
  
  
  1.1                  cocoon-2.1/src/scratchpad/webapp/samples/garbage/sitemap.xmap
  
  Index: sitemap.xmap
  ===================================================================
  <?xml version="1.0"?>
  
  <!-- CVS $Id: sitemap.xmap,v 1.1 2003/06/22 18:48:20 coliver Exp $ -->
  
  <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
  
    <map:pipelines>
      <map:pipeline>
  
        <map:match pattern="">
          <map:generate src="samples.xml"/>
          <map:transform src="context://samples/common/style/xsl/html/simple-samples2html.xsl">
             <map:parameter name="contextPath" value="{request:contextPath}"/>
          </map:transform>
          <map:serialize/>
        </map:match>
  
        <map:match pattern="*">
          <map:redirect-to uri="{1}/"/>
        </map:match>
  
        <map:match pattern="*/**">
          <map:mount uri-prefix="{1}" src="{1}/" check-reload="yes"/>
        </map:match>
  
      </map:pipeline>
    </map:pipelines>
  </map:sitemap>
  
  
  
  1.1                  cocoon-2.1/src/scratchpad/webapp/samples/garbage/calc/calc.js
  
  Index: calc.js
  ===================================================================
  var a, b, op;
  
  function calculator()
  {
    a = getNumber("a");
    b = getNumber("b", a);
    op = getOperator(a, b);
  
    if (op == "plus")
      sendResult(a, b, op, a + b);
    else if (op == "minus")
      sendResult(a, b, op, a - b);
    else if (op == "multiply")
      sendResult(a, b, op, a * b);
    else if (op == "divide")
      sendResult(a, b, op, a / b);
    else
      sendResult("Error: Unkown operator!");
  }
  
  function getNumber(name, a, b)
  {
    var uri = "page/getNumber" + name.toUpperCase();
    cocoon.sendPageAndWait(uri, { "a" : a, "b" : b });
    print(cocoon.request);
    for (i in cocoon.request) {
      print(i + " = " + cocoon.request[i]);
    }
    return parseFloat(cocoon.request.getParameter(name));
  }
  
  function getOperator(a, b)
  {
    cocoon.sendPageAndWait("page/getOperator", { "a" : a, "b" : b });
    return cocoon.request.getParameter("operator");
  }
  
  function sendResult(a, b, op, result)
  {
    cocoon.sendPage("page/displayResult",
             { "a" : a, "b" : b, "operator" : op, "result" : result });
  }
  
  
  
  1.1                  cocoon-2.1/src/scratchpad/webapp/samples/garbage/calc/sitemap.xmap
  
  Index: sitemap.xmap
  ===================================================================
  <?xml version="1.0"?>
  
  <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
    <map:components>
      <map:generators>
         <map:generator name="garbage" logger="garbage.sitemap.generator"
                         src="org.apache.cocoon.generation.GarbageGenerator"/>
      </map:generators>
    </map:components>
  
    <!-- indicates what flowscript to attach to this sitemap -->
    <map:flow language="FOM_JavaScript">
      <map:script src="calc.js"/>
    </map:flow>
  
    <map:pipelines>
      <map:pipeline>
        <!--+
            | produces the screens called by the flowscript
            +-->     
        <map:match pattern="page/*">
          <map:generate type="garbage" src="screens/{1}.gt"/>
          <map:transform src="context://samples/common/style/xsl/html/simple-page2html.xsl">
            <map:parameter name="servletPath" value="{request:servletPath}"/>
            <map:parameter name="sitemapURI" value="{request:sitemapURI}"/>
            <map:parameter name="contextPath" value="{request:contextPath}"/>
            <map:parameter name="file" value="/samples/flow/calc/screens/{1}.gt"/>
            <map:parameter name="remove" value="{0}"/>
          </map:transform>
          <map:serialize/>
        </map:match>
      </map:pipeline>
  
      <map:pipeline>
        <!--+
            | matches the page with the continuation ID and calls the flowscript
            | associated to this sitemap with the given continuation ID. The flow
            | engine will then look into the continuation store, retrieve
            | the correct continuation and resume execution of the flowscript
            | with that continuation. This guarantees transparent state 
            | resumption between requests without the need for anything else
            | (cookies or URL-encoded session IDs)
            +-->
        <map:match pattern="continue.*">
          <map:call continuation="{1}"/>
        </map:match>
  
        <!--+
            | matches the call to the beginning of the flow and calls the flow
            | from its entry point which, in this case is the 'calculator()'
            | javascript function.
            +-->
        <map:match pattern="">
          <map:call function="calculator"/>
        </map:match>
      </map:pipeline>
    </map:pipelines>
  </map:sitemap>
  
  
  
  1.1                  cocoon-2.1/src/scratchpad/webapp/samples/garbage/calc/screens/displayResult.gt
  
  Index: displayResult.gt
  ===================================================================
  <?xml version="1.0"?>
  
  <!--+
      | CVS: $Id: displayResult.gt,v 1.1 2003/06/22 18:48:20 coliver Exp $
      | Author: Ovidiu Predescu "ovidiu@cup.hp.com"
      | Date: March 23, 2002
      +-->
  
    <page>
      <resources>
        <resource type="file" href="/samples/flow/calc/calc.js">Flowscript</resource>
      </resources>
      <title>Calculator</title>
      <content>
        <form action="./" method="post">
          <para>a = <strong>#{a}</strong></para>
          <para>b = <strong>#{b}</strong></para>
          <para>Operator = <strong>#{operator}</strong></para>
          <para>Result = <strong>#{result}</strong></para>
          <input type="submit" name="submit" value="Start over"/>
        </form>
      </content>
    </page>
  
  
  
  
  1.1                  cocoon-2.1/src/scratchpad/webapp/samples/garbage/calc/screens/getNumberA.gt
  
  Index: getNumberA.gt
  ===================================================================
  <?xml version="1.0"?>
  
  <!--+
      | CVS: $Id: getNumberA.gt,v 1.1 2003/06/22 18:48:20 coliver Exp $
      | Author: Ovidiu Predescu "ovidiu@cup.hp.com"
      | Date: March 23, 2002
      +-->
  
    <page>
      <resources>
        <resource type="file" href="/samples/flow/calc/calc.js">Flowscript</resource>
      </resources>
      <title>Calculator</title>
      <content>
        <form method="post" action="continue.{$continuation/id}">
          <para>Enter value of <strong>a</strong>: <input type="text" name="a"/></para>
          <input type="submit" name="submit" value="Enter"/>
        </form>
      </content>
    </page>
  
  
  
  
  1.1                  cocoon-2.1/src/scratchpad/webapp/samples/garbage/calc/screens/getNumberB.gt
  
  Index: getNumberB.gt
  ===================================================================
  <?xml version="1.0"?>
  
  <!--+
      | CVS: $Id: getNumberB.gt,v 1.1 2003/06/22 18:48:20 coliver Exp $
      | Author: Ovidiu Predescu "ovidiu@cup.hp.com"
      | Date: March 23, 2002
      +-->
  
    <page>
      <resources>
        <resource type="file" href="/samples/flow/calc/calc.js">Flowscript</resource>
      </resources>
      <title>Calculator</title>
      <content>
        <form method="post" action="continue.{$continuation/id}">
          <para>a = <strong>#{a}</strong></para>
          <para>Enter value of <strong>b</strong>: <input type="text" name="b"/></para>
          <input type="submit" name="submit" value="Enter"/>
        </form>
      </content>
    </page>
  
  
  
  
  1.1                  cocoon-2.1/src/scratchpad/webapp/samples/garbage/calc/screens/getOperator.gt
  
  Index: getOperator.gt
  ===================================================================
  <?xml version="1.0"?>
  
  <!--+
      | CVS: $Id: getOperator.gt,v 1.1 2003/06/22 18:48:20 coliver Exp $
      | Author: Ovidiu Predescu "ovidiu@cup.hp.com"
      | Date: March 23, 2002
      +-->
  
    <page>
      <resources>
        <resource type="file" href="/samples/flow/calc/calc.js">Flowscript</resource>
      </resources>
      <title>Calculator</title>
      <content>
        <form  method="post" action="continue.{$continuation/id}">
          <para>a = <strong>#{a}</strong></para>
          <para>b = <strong>#{b}</strong></para>
          <para>Enter operator
            <select name="operator">
              <option>plus</option>
              <option>minus</option>
              <option>multiply</option>
              <option>divide</option>
            </select>
          </para>
          <input type="submit" name="submit" value="Do it!"/>
        </form>
      </content>
    </page>