Re: own generator

2002-08-02 Thread Istvan Beszteri

Hi,

I have updated the setup method. here is the code:

public void setup(SourceResolver resolver, 
 Map objectModel,
 String src,
 Parameters par) throws ProcessingException,
SAXException,
IOException
   {
   try
   {
   super.setup(resolver, objectModel, src, par);
   }
   catch(ProcessingException exc)
   {
   throw(exc);
   }
   catch(SAXException exc)
   {
   throw(exc);
   }
   catch(IOException exc)
   {
   throw(exc);
   }   

   request = ObjectModelHelper.getRequest(objectModel);
   paramNames = request.getParameterNames();
   uri = request.getRequestURI();
   }


I had a sily mistake in the pipeline, too (in the generate line I wrote 
source instead of src). The corrected pipeline is:



  


  
  



  
  

  

Now I can get the src in the source field of the generator.
The parameters field of the generator still conatins 0 parameters.
Br,
Istvan



On Thursday 01 August 2002 17:23, you wrote:
> Ah, I think the problem is that I overrode the setup() method from
> AbstractGenerator, but didn't look at what it
> was doing in the original - I presumed incorrectly that it did nothing. 
> You should be able to get at the source by
> adding super.setup() to the first line of the new setup() call below.  Now
> that I've looked more carefully, we could get rid of our own implementation
> of setup and use the instance variable objectModel from the superclass to
> get our parameters from within generate if we really wanted to - on the
> other hand, for demonstration purposes it's better in my view to show that
> it's available as a callback method from the container for performing setup
> tasks.
>
> Thank you by the way for reorganizing the output to be more xml friendly.
> I've already started work turning this example into a how-to for the
> documentation.  Would you mind if I incorporate your modifications for that
> so I don't have to come up with another example?
>
> Geoff

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

To unsubscribe, e-mail: <[EMAIL PROTECTED]>
For additional commands, e-mail:   <[EMAIL PROTECTED]>




RE: own generator

2002-08-01 Thread Geoff Howard

Ah, I think the problem is that I overrode the setup() method from
AbstractGenerator, but didn't look at what it
was doing in the original - I presumed incorrectly that it did nothing.  You
should be able to get at the source by 
adding super.setup() to the first line of the new setup() call below.  Now
that I've looked more carefully, we could get rid of our own implementation
of setup and use the instance variable objectModel from the superclass to
get our parameters from within generate if we really wanted to - on the
other hand, for demonstration purposes it's better in my view to show that
it's available as a callback method from the container for performing setup
tasks.

Thank you by the way for reorganizing the output to be more xml friendly.
I've already started work turning this example into a how-to for the
documentation.  Would you mind if I incorporate your modifications for that
so I don't have to come up with another example?

Geoff

> -Original Message-
> From: Istvan Beszteri [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 01, 2002 5:36 AM
> To: [EMAIL PROTECTED]
> Subject: Re: own generator
> 
> 
> Hi All,
> 
> Thanks for the help.
> I tried Geoff's modified version, and it works with some 
> small corrections.
> Here is the code (Maybe some of the imports are not used at all):
> 
> import java.io.IOException;
> import java.io.StringReader;
> import org.xml.sax.XMLReader;
> import org.xml.sax.InputSource;
> import org.xml.sax.SAXException;
> import org.xml.sax.helpers.XMLReaderFactory;
> import org.apache.cocoon.ProcessingException;
> import org.apache.cocoon.generation.AbstractGenerator;
> import java.lang.String;
> 
> // for the setup() method
> import org.apache.cocoon.environment.SourceResolver;
> import java.util.Map;
> import org.apache.avalon.framework.parameters.Parameters;
> 
> // used to get and hold the request parameters.
> import org.apache.cocoon.environment.ObjectModelHelper;
> import org.apache.cocoon.environment.Request;
> import java.util.Enumeration;
> 
> // SAX helper
> import org.xml.sax.helpers.AttributesImpl;
> 
> 
> public class NewAnyName extends AbstractGenerator 
> {
> 
> // will be initialized in the setup() method and used in 
> generate()
> Request request = null;
> Enumeration paramNames = null;
> String uri = null;
> 
> /**
>  * Implement the generate() method from AbstractGenerator.
>  * It simply generates SAX events using SAX methods.
>  */
> 
> public void generate() throws IOException, SAXException, 
> ProcessingException
> {
>   // xspAttr will stay empty for this simple implementation
>   // If we had any attributes in our tags, we would add 
> them to this variable
>   // before the call to startElement(), and either create 
> a new AttributesImpl
>   // instance for each element, or reuse one being 
> careful to reset it before 
>   // each use.
>   AttributesImpl xspAttr = new AttributesImpl();
> 
>   // contentHandler is inherited from 
> org.apache.cocoon.xml.AbstractXMLProducer
>   contentHandler.startDocument();
>   
>   // Do it with SAX:
>   contentHandler.startElement("", "doc", "doc", xspAttr);
> 
>   contentHandler.startElement("", "uri", "uri", xspAttr);
> 
>   contentHandler.characters(uri.toCharArray(),0,uri.length());
> 
>   contentHandler.endElement("", "uri", "uri");
>   
>   contentHandler.startElement("", "params", "params", xspAttr);
>  
>   while (paramNames.hasMoreElements())
>   {
>   contentHandler.startElement("", "param", "param", xspAttr);
>   
> String param = (String)paramNames.nextElement();
> 
> contentHandler.characters(param.toCharArray(),0,param.length());
> 
>   contentHandler.endElement("","param", "param");
>   }
> 
>   contentHandler.endElement("","params", "params");
> 
>   contentHandler.startElement("", "source", "source", xspAttr);
> 
>   if(source != null)
>   contentHandler.characters(source.toCharArray(),0,
>   source.length());
> 
>   contentHandler.endElement("", "source", "source");
> 
>   contentHandler.endElement("","doc", "doc");
> 
>   contentHandler.endDocument();
>}
>
>public void s

Re: own generator

2002-08-01 Thread Istvan Beszteri

Hi All,

Thanks for the help.
I tried Geoff's modified version, and it works with some small corrections.
Here is the code (Maybe some of the imports are not used at all):

import java.io.IOException;
import java.io.StringReader;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLReaderFactory;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.generation.AbstractGenerator;
import java.lang.String;

// for the setup() method
import org.apache.cocoon.environment.SourceResolver;
import java.util.Map;
import org.apache.avalon.framework.parameters.Parameters;

// used to get and hold the request parameters.
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import java.util.Enumeration;

// SAX helper
import org.xml.sax.helpers.AttributesImpl;


public class NewAnyName extends AbstractGenerator 
{

// will be initialized in the setup() method and used in generate()
Request request = null;
Enumeration paramNames = null;
String uri = null;

/**
 * Implement the generate() method from AbstractGenerator.
 * It simply generates SAX events using SAX methods.
 */

public void generate() throws IOException, SAXException, 
ProcessingException
{
// xspAttr will stay empty for this simple implementation
// If we had any attributes in our tags, we would add them to this variable
// before the call to startElement(), and either create a new AttributesImpl
// instance for each element, or reuse one being careful to reset it before 
// each use.
AttributesImpl xspAttr = new AttributesImpl();

  // contentHandler is inherited from 
org.apache.cocoon.xml.AbstractXMLProducer
  contentHandler.startDocument();
  
  // Do it with SAX:
  contentHandler.startElement("", "doc", "doc", xspAttr);

  contentHandler.startElement("", "uri", "uri", xspAttr);

  contentHandler.characters(uri.toCharArray(),0,uri.length());

  contentHandler.endElement("", "uri", "uri");
  
  contentHandler.startElement("", "params", "params", xspAttr);
 
  while (paramNames.hasMoreElements())
  {
  contentHandler.startElement("", "param", "param", xspAttr);
  
  String param = (String)paramNames.nextElement();
  contentHandler.characters(param.toCharArray(),0,param.length());

  contentHandler.endElement("","param", "param");
  }

  contentHandler.endElement("","params", "params");

  contentHandler.startElement("", "source", "source", xspAttr);

  if(source != null)
  contentHandler.characters(source.toCharArray(),0,
source.length());

  contentHandler.endElement("", "source", "source");

  contentHandler.endElement("","doc", "doc");

  contentHandler.endDocument();
   }
   
   public void setup(SourceResolver resolver, Map objectModel, String src,
 Parameters par)
   {
   request = ObjectModelHelper.getRequest(objectModel);
   paramNames = request.getParameterNames();
   uri = request.getRequestURI();
   }
}

The pipeline is the following:



  


  
  



  
  

  


A test reqest:
http://localhost:8080/cocoon/foo/trial.xml?apple=3

result:


/cocoon/foo/trial.xmlapple

As you can see, the source filed is still null.
Anyway, my problem is solved. I can get the request in the generator. I'm 
just curious what is the solution for this "source" thing.

Br,
Istvan

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

To unsubscribe, e-mail: <[EMAIL PROTECTED]>
For additional commands, e-mail:   <[EMAIL PROTECTED]>




RE: own generator

2002-07-31 Thread Geoff Howard

How about this?  If I read you right, what you're really going for is not
getting the sitemap parameters but the request parameters.  There's a
difference between doing that from within the sitemap and within java code
like this.  I'm not positive this will compile, and I'm not 100% positive
that this keeps up with any changes in the last few months but I think it's
OK on both counts.  Of course, one would never want to put all that text
formatted data in there - I just wanted to change as little as possible for
the sake of comparison.

// for the setup() method
import org.apache.cocoon.environment.SourceResolver;
import java.util.Map;
import org.apache.avalon.framework.parameters.Parameters;
// used to get and hold the request parameters.
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import java.util.Enumeration;


public class NewAnyName extends AbstractGenerator 
{

// will be initialized in the setup() method and used in generate()
Request request = null;
Enumeration paramNames = null;

/**
 * Implement the generate() method from AbstractGenerator.
 * It simply generates SAX events using SAX methods.  I haven't
 * tested it, but this has to be faster than parsing them from a string.
 * I've made comments on many of the original comments (which have been
promoted
 * to "really commented out" status: )
 */

public void generate() throws IOException, SAXException, 
ProcessingException
{
  // xspAttr will stay empty for this simple implementation
  // If we had any attributes in our tags, we would add them to this
variable
  // before the call to startElement(), and either create a new
AttributesImpl
  // instance for each element, or reuse one being careful to reset it
before 
  // each use.
  
  AttributesImpl xspAttr = new AttributesImpl();
  // contentHandler is inherited from
org.apache.cocoon.xml.AbstractXMLProducer
  contentHandler.startDocument();
  
This string will be feeded to the transformer pipeline as sax
events
// String xmlText = "\nMy first Cocoon 2 generator\n";
// - nope, do this with SAX:
  contentHandler.startElement("", "doc", "doc", xspAttr);
  
  // NOTE: xml is not the place to worry about line break characters.
For the sake of the
  // simple example, I left many of yours in, but you really would
handle the parameter information
  // using nested tags (which you can do easily by nesting
startElement()-endElement() paired calls inside
  // this element).
  // By the way, the characters(char[] chars, int start, int end) begs
to be overloaded with 
  // a version like characters(String justPutTheWholeThingIn) that
handles the conversion
  // to a character array and assumes you want from beginning to end.  I
thought some of the cocoon
  // Generators did this, but I can't find any right now.  You'll
probably want to set up a convenient 
  // BaseGenerator with helpers like that and extend it for your real
Generators.
  contentHandler.characters("My first Cocoon 2
generator\n".toCharArray(),0,"My first Cocoon 2 generator\n".length());

This object will read and parse the XML content
// no longer needed since we're using SAX methods
//XMLReader xmlReader = XMLReaderFactory.createXMLReader();

super.xmlConsumer will receive the SAX events
xmlReader.setContentHandler(super.xmlConsumer);

// This is _not_ how you get your request parameters.
//String paramNames[] = parameters.getNames();

// text way
//xmlText += "Number of parameters: " + paramNames.length + "\n";
// SAX way
contentHandler.characters(("Number of parameters: " +
paramNames.length + "\n").toCharArray(),0,("Number of parameters: " +
paramNames.length + "\n").length());

//same thing:
//xmlText += "Parameter names:\n";
contentHandler.characters(("Parameter names:
\n").toCharArray(),0,("Parameter names: \n").length());

//for(int i = 0; i < paramNames.length; i++)
while (paramNames.hasMoreElements()) {
//xmlText += paramNames[i] + "\n";
String param = (String)paramNames.nextElement() + "\n";
contentHandler.characters(param.toCharArray(),0,param.length());
}

//xmlText += "Source: " + source + "\n";
contentHandler.characters(("Source: " + source +
"\n").toCharArray(),0,("Source: " + source + "\n").length());

//xmlText += "";
contentHandler.endElement("","doc", "doc");

// no longer needed with SAX
Create an InputSource from the hardcoded string
//InputSource src = new InputSource(new StringReader(xmlText));

// no longer needed with SAX
Parse the XML input and generate SAX events to the consumer
//xmlRe

RE: own generator

2002-07-31 Thread Vadim Gritsenko

> From: Istvan Beszteri [mailto:[EMAIL PROTECTED]]
> 
> Hi All,
> 
> I'd like to write my own generator. I have written the following
generator
> according to the documents on the Cocoon 2 site, by extending an
example.
> I can start the generator properly, but I can not pass any parameter
to it.
> Somehow I'd like to reach the HTTP request itself (requested URI,
headers
> etc.) within the generator.
> 
> public class AnyName extends AbstractGenerator
> {

ServletGenerator has:

protected Request request;

...

> I use the following pipeline. In the generated XML I could see the
> parameters, and the source, but it seems that the source is null, and
no
> parameters are passed to the generator.
> 
> 
> 
>   
> 
> 

http://xml.apache.org/cocoon/faq/faq-actions.html#faq-2

See also recent threads.

Vadim


> 
>   
> 
> 
> 
>   
>   
> 
>   
> 
> 
> How could I get the request data in the generator?
> 
> Thanks in advance!
> Br,
>   Istvan
> 


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

To unsubscribe, e-mail: <[EMAIL PROTECTED]>
For additional commands, e-mail:   <[EMAIL PROTECTED]>