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 = "<doc>\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 += "</doc>";
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
//xmlReader.parse(src);
}
public void setup(SourceResolver resolver, Map objectModel, String src,
Parameters par) {
request = ObjectModelHelper.getRequest(objectModel);
paramNames = request.getParameterNames();
}
}
// END
Hope that helps. Maybe I'll try to fix that up and submit it as a sample or
how-to or something for the docs. If that doesn't compile, can someone let
me know? I did this off the top of my head and probably missed something -
exceptions and imports most likely - and don't have time to test it out
right now.
Geoff Howard
> -----Original Message-----
> From: Istvan Beszteri [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, July 31, 2002 1:36 PM
> To: [EMAIL PROTECTED]
> Subject: own generator
>
>
> 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
> {
> /**
> * The only implemented method from AbstarctGenerator.
> * It simply generates SAX events from a string.
> */
>
> public void generate() throws IOException, SAXException,
> ProcessingException
> {
> //This string will be feeded to the transformer
> pipeline as sax events
> String xmlText = "<doc>\nMy first Cocoon 2 generator\n";
>
> //This object will read and parse the XML content
> XMLReader xmlReader = XMLReaderFactory.createXMLReader();
>
> //super.xmlConsumer will receive the SAX events
> xmlReader.setContentHandler(super.xmlConsumer);
>
> String paramNames[] = parameters.getNames();
>
> xmlText += "Number of parameters: " +
> paramNames.length + "\n";
>
> xmlText += "Parameter names:\n";
>
> for(int i = 0; i < paramNames.length; i++)
> xmlText += paramNames[i] + "\n";
>
> xmlText += "Source: " + source + "\n";
>
> xmlText += "</doc>";
>
> //Create an InputSource from the hardcoded string
> InputSource src = new InputSource(new StringReader(xmlText));
>
> //Parse the XML input and generate SAX events to the consumer
> xmlReader.parse(src);
> }
> }
>
> 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.
>
> <map:pipeline>
> <map:match pattern="foo/*">
> <map:act type="request">
> <map:parameter name="parameters" value="true"/>
> <map:generate type="ist" source="{1}"/>
> <map:serialize type="xml"/>
> </map:act>
> </map:match>
>
> <map:handle-errors>
> <map:transform src="stylesheets/system/error2html.xsl"/>
> <map:serialize status-code="500"/>
> </map:handle-errors>
> </map:pipeline>
>
>
> 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. <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]>