cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Grzegorz Sikora <szef...@op.pl>
Subject Re: Could some help in this!
Date Wed, 06 Apr 2005 13:18:40 GMT
Hello fadi,

Tuesday, April 5, 2005, 5:01:15 PM, you wrote:
 
fq> The second match pattern builds a session context and loads
fq> its data from the first match pattern. How can I tell cocoon to
fq> automatically execute the second match pattern (i.e. without
fq> explicitly requesting it via the Internet explorer). is it
fq> possible to do this ?   

Maybe flowscript would be better approach ...  but I've written simple action
which can call another pipeline and eventually creates sitemap
parameter with returned XML as DOM tree:

import org.apache.avalon.excalibur.component.ComponentHandler;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.acting.ConfigurableComposerAction;
import org.apache.cocoon.components.sax.XMLByteStreamCompiler;
import org.apache.cocoon.components.sax.XMLByteStreamFragment;
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.generation.FileGenerator;
import org.apache.cocoon.xml.AbstractXMLConsumer;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

public class CallPipelineAction extends ConfigurableComposerAction implements
    Disposable, ThreadSafe {

  ComponentHandler generatorHandler;

  public void configure(Configuration conf) throws ConfigurationException {
    try {
      this.generatorHandler = ComponentHandler.getComponentHandler(
          FileGenerator.class, conf, this.manager, null,
          null,
          null,
          null,
          "N/A"
      );

      this.generatorHandler.enableLogging(getLogger());
      this.generatorHandler.initialize();

    } catch (Exception e) {
      throw new ConfigurationException("Cannot set up component handler", e);
    }
  }

  public void dispose() {
    if (this.generatorHandler != null) {
      this.generatorHandler.dispose();
      this.generatorHandler = null;
    }
  }

  public Map act(Redirector redirector, SourceResolver resolver,
      Map objectModel, String source, Parameters parameters) throws Exception {

    String pipeline = parameters.getParameter("pipeline");
    String discard = parameters.getParameter("discard", "false");
    String outputParamName = parameters.getParameter("output", "output");

    FileGenerator generator = (FileGenerator) this.generatorHandler.get();

    XMLByteStreamCompiler compiler = null;

    Map map = new HashMap();

    try {
      Parameters genParams = new Parameters();

      generator.enableLogging(getLogger());

      generator.setup(resolver, objectModel, pipeline, genParams);

      if (discard.equals("true")) {
        generator.setConsumer(new AbstractXMLConsumer() {
        });
        generator.generate();
      } else {
        compiler = new XMLByteStreamCompiler();
        generator.setConsumer(compiler);
        generator.generate();
        XMLByteStreamFragment fragment = new XMLByteStreamFragment(compiler
            .getSAXFragment());

        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder();
        Document doc = builder.newDocument();
        fragment.toDOM(doc);
        
        Request request = ObjectModelHelper.getRequest(objectModel);
        request.setAttribute(outputParamName, doc);
      }

    } catch (Exception e) {
      if (getLogger().isErrorEnabled())
        getLogger().error("Error while calling another pipeline " + e);
      map = null;
    } finally {
      generatorHandler.put(generator);
    }

    return map;
  }

}


Sample usage:
<map:act type="call-pipeline">
         <map:parameter name="pipeline"
         value="cocoon:/some-pipe-2-execute"/>
         <map:parameter name="discard" value="true"/>
</map:act>


-- 
Best regards,
 Grzegorz Sikora


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


Mime
View raw message