You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@lenya.apache.org by "Linczak, Jonathan W." <Li...@hiram.edu> on 2004/08/23 08:39:08 UTC

Actions in a pipeline

Hi all,

Here's another probably simple question: I created a very simple action that returns a random number between 1 and 5.  The action looks like the following:

--------------------------

package hiram;

import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.Constants;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.acting.AbstractAction;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class RandomNumberAction extends AbstractAction implements ThreadSafe
{
    public Map act( Redirector redirector, SourceResolver resolver,
                            Map objectModel, String source, Parameters param )
        throws Exception
    {
        Request request = ObjectModelHelper.getRequest(objectModel);

        // get a random number between 1 and 5 inclusive
        String randnum = new Integer((((int)((Math.random())*10)) / 2) + 1).toString();

        getLogger().debug("RandomNumberAction: num is " + randnum);

        Map map = new HashMap();
        map.put("rand", randnum);

        return(map);
    }

}

-----------------------------

I added it to my pipeline for my home page thusly, only after adding it as an action at the top:

    <map:actions>
      <map:action name="randnum" src="hiram.RandomNumberAction"/>
    </map:actions>


    <map:pipeline>
      <!-- /lenyabody-{rendertype}/{publication-id}/{area}/{doctype}/{url} -->
      <map:match pattern="lenyabody-*/*/*/*/index.html">
        <map:aggregate element="cmsbody">
          <map:part src="cocoon://navigation/{2}/{3}/search/{5}.xml"/>
          <map:part src="cocoon:/lenya-document-{1}/{3}/{4}/{page-envelope:document-path}"/>
        </map:aggregate>
        <map:act type="randnum">
          <map:transform src="xslt/home.xsl">
            <map:parameter name="root" value="{page-envelope:context-prefix}/{2}/{3}"/>
            <map:parameter name="url" value="{5}"/>
            <map:parameter name="document-id" value="{page-envelope:document-id}"/>
            <map:parameter name="document-type" value="{page-envelope:document-type}"/>
            <map:parameter name="rand" value="{rand}"/>
          </map:transform>
        </map:act>
        <map:select type="parameter">
          <map:parameter name="parameter-selector-test" value="{1}"/>
          <map:when test="view">
            <map:transform type="link-rewrite"/>
          </map:when>
        </map:select>
        <map:serialize type="xml"/>
      </map:match>
    </map:pipeline>


Notice how I wrapped it around the transform statement?  When doing this, the random number generates and is passed through the rand parameter (I added the param statement along with the originals - root and url, for example - in the XSL file), but none of the others (like root, url, document-id, etc.) are being passed, so they end up empty.  I thought this was because I wrapped it around just the transform, so I wrapped up all the parts inside of the matcher in the action tag, but then the page is listed as not found.  How can I get the action to work and still pass the typical parameters through so that my page can be displayed properly?

Jon

Re: Actions in a pipeline

Posted by Jonathan Linczak <li...@hiram.edu>.
On Aug 23, 2004, at 3:31 AM, Andreas Hartmann wrote:

> Andreas Hartmann wrote:
>
>> Linczak, Jonathan W. wrote:
>>> Hi all,
>>>
>>> Here's another probably simple question: I created a very simple 
>>> action that returns a random number between 1 and 5.
>> Just a hint: IMO an input module would be more suitable
>> for this task.
>
> BTW: http://wiki.apache.org/cocoon/InputModules
> (RandomNumberModule)

Thanks Andreas, your answers are spot-on, as always.  Sorry about the 
Cocoon question - I thought it might be more specific to Lenya with the 
added parts in the pipeline, but I guess not.  :)

Jon


---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-user-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-user-help@cocoon.apache.org


Re: Actions in a pipeline

Posted by Andreas Hartmann <an...@apache.org>.
Andreas Hartmann wrote:

> Linczak, Jonathan W. wrote:
> 
>> Hi all,
>>
>> Here's another probably simple question: I created a very simple 
>> action that returns a random number between 1 and 5.
> 
> 
> Just a hint: IMO an input module would be more suitable
> for this task.

BTW: http://wiki.apache.org/cocoon/InputModules
(RandomNumberModule)

-- Andreas


---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-user-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-user-help@cocoon.apache.org


Re: Actions in a pipeline

Posted by Andreas Hartmann <an...@apache.org>.
Linczak, Jonathan W. wrote:
> Hi all,
> 
> Here's another probably simple question: I created a very simple action 
> that returns a random number between 1 and 5.

Just a hint: IMO an input module would be more suitable
for this task.

[...]

>         <map:act type="randnum">
>           <map:transform src="xslt/home.xsl">
>             <map:parameter name="root" 
> value="{page-envelope:context-prefix}/{2}/{3}"/>

You're switching the context here (one level deeper), which
means that sitemap variables from the parent context have to
be accessed as {../2} and so on. There's some documentation on
the Cocoon website too (I'm too lazy to search ATM :) )

BTW, this is a quite pure Cocoon question, I think it would
make sense to ask it on cocoon-users. But I understand that
it is not always easy to know whether a problem is related
to Cocoon or Lenya.

-- Andreas


---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-user-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-user-help@cocoon.apache.org