cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geoff Howard" <>
Subject RE: Different stylesheets called on runtime?
Date Thu, 23 Jan 2003 21:24:39 GMT
> -----Original Message-----
> From: []
> Sent: Thursday, January 23, 2003 11:42 AM
> To:
> Subject: RE: Different stylesheets called on runtime?
> Hi Geff,
> thanks for your help. Ok.

Sure.  Ok,

> Example:
> _____________________________________________________________
> My XSP, that should choose which XSL to use.:

As an xsp is usually used to create a generator, you don't want to use it to
select anything about pipeline behavior.  You can write actions in xsp, but
for now that will probably confuse things.  So, you'll take the logic from
the xsp you had, and reuse it in an action.

Before getting into that, though look at the sitemap.  I think Christian
already gave you your answer there but again, you'll want to do something
like this:

> Part of sitemap:
> ...
> <map:match pattern="differentXSLs">
<!-- you'll now need to strip out the transformer selection logic from the
	and just generate the xml that your xsl needs -->
>    <map:generate type="serverpages" src="differentXSLs.xsp"/>
     <map:act type="myAction">
>             <map:transform src="{whichXSL}.xsl"/>
>              ...
>             <map:serialize/>
<!-- normally you'd put fall back behavior here - executed only when the
  fails.  For the example though we'll just make the action always succeed.
  You may want to change things around - putting everything inside the
action tag,
  putting only the transform in the action tag.  For the example it doesn't
matter though.
> </map:match>
> ....
> So how and where should I place my action (and how should it look like)?
If you mean the compiled action, it goes in WEB-INF/classes or inside a jar

> How should my xsl look like then?
this shouldn't have to change.

Now the java action.  this is more complicated than I originally indicated
because of the database access, but it's still not that bad.  I simplified a
few things, but resisted the urge to dumb it down.  For instance, you could
hard code the datasource name and get rid of configure().  You could skip
dispose() but I think that would lead to memory problems.  (avalon gurus -
is that right?) I have combined this quickly from a DB Action super class I
have and one subclass.

When this is compiled, it gets defined in the map:components section of the
<map:actions> ....
 <map:act name="myAction" src="you.yourcompany.acting.YourAction"

package you.yourcompany.acting;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentSelector;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.activity.Disposable;

import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.acting.ConfigurableComposerAction;

import org.apache.avalon.excalibur.datasource.DataSourceComponent;

public class YourAction extends ConfigurableComposerAction implements
Disposable {

    protected ComponentSelector dbselector;
    DataSourceComponent datasource;

  public Map act(Redirector redirector, SourceResolver resolver, Map
  		String source, Parameters parameters) throws Exception {
      HashMap results = new HashMap();
      Request request = ObjectModelHelper.getRequest(objectModel);

>       <!-- Get the passed parameters (if there are any) -->
>      int whichXSL;
>       String report_id = request.getParameter("report_id");
>       String service_id = request.getParameter("service_id");
>       String nbt_pattern_id = request.getParameter("nbt_pattern_id");
>       String search_txt = request.getParameter("search_txt");
>       ...
>      // Check against a database, bla, bla
>      ...
>     // Now I want to choose the suitable XSL
>     if( whichXSL == 1)
>       // Choose number1.xsl
>    else if( whichXSL == 2)
>       // Choose number2.xsl
>   else if( whichXSL == 3)
>       // Choose number3.xsl
    else {
    	// not strictly necessary
	// could also return null to signal sitemap to execute fallback.  up to
>    ...

  return results;
  } // end of act()

// Now, you glossed over the database stuff but there's a few methods you'll
need to
// handle that.  They will be called by cocoon during the normal component
// and are designed to give us a chance to get at the resources and
information we need.

     * Compose the Actions so that we can select our databases.
    public void compose(ComponentManager manager) throws ComponentException
        this.dbselector = (ComponentSelector)
manager.lookup(DataSourceComponent.ROLE + "Selector");

	 * Configure lets you specify which datasource to use in the sitemap
	 * definition rather than hardcoded in the compiled source
    public void configure(Configuration conf) throws ConfigurationException
      datasource = null;
      String dataSourceConfig = (String)settings.get("data-source");
      try {
	    datasource = (DataSourceComponent);
	    getLogger().debug("DB Action using datasource: " + dataSourceConfig);
      } catch(ComponentException e) {
	    getLogger().warn("Could not get data-source '" + dataSourceConfig + "'
configured for DB Action");

	 * This is called when the instance of this action is taken out of the
       * Releasing resources is a good idea here.
    public void dispose() {
      this.datasource = null;
      this.dbselector = null;

Now, the database stuff would happen like this:

	String sql = "select something from somewhere";

      try {
	      Connection conn = this.datasource.getConnection();
	      PreparedStatement statement = null;
	      statement = conn.prepareStatement(sql);
		// normal jdbc stuff

      } catch(SQLException sqle) {
	      getLogger().error("SQL Exception: " + sqle.getMessage());
	      getLogger().error("SQL State: " + sqle.getSQLState());
	      // return null; - if you want to have the sitemap do the fallback on
sql errors

That's it.  Feel free to point out the stuff that makes no sense.  It's
easier to answer specific questions than to try to write a whole tutorial
here (though this could get turned into one)


> Cheers
> Jonny

Please check that your question  has not already been answered in the
FAQ before posting.     <>

To unsubscribe, e-mail:     <>
For additional commands, e-mail:   <>

View raw message