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 setup(SourceResolver resolver, Map 
> objectModel, String src,
>                    Parameters par)
>    {
>        request = ObjectModelHelper.getRequest(objectModel);
>        paramNames = request.getParameterNames();
>        uri = request.getRequestURI();
>    }
> }
> 
> The pipeline is the following:
> 
> <map:pipeline>
>     <map:match pattern="foo/*">
>       <map:act type="request">
>         <map:parameter name="parameters" value="true"/>
>         <map:generate type="ist" source="{../1}"/>
>       </map:act>
>       <map:serialize type="xml"/>
>     </map:match>
> 
>     <map:handle-errors>
>       <map:transform src="stylesheets/system/error2html.xsl"/>
>       <map:serialize status-code="500"/>
>     </map:handle-errors>
>   </map:pipeline>
> 
> 
> A test reqest:
> http://localhost:8080/cocoon/foo/trial.xml?apple=3
> 
> result:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <doc><uri>/cocoon/foo/trial.xml</uri><params><param>apple</par
> am></params><source/></doc>
> 
> 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.     <http://xml.apache.org/cocoon/faq/index.html>
> 
> To unsubscribe, e-mail:     <[EMAIL PROTECTED]>
> For additional commands, e-mail:   <[EMAIL PROTECTED]>
> 

---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

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

Reply via email to