You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Istvan Beszteri <is...@hut.fi> on 2002/08/01 11:35:51 UTC
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</param></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: <co...@xml.apache.org>
For additional commands, e-mail: <co...@xml.apache.org>