Hi,
Having read the "write a custom generator"
tutorial, I am trying to write one that hooks up a
connection to an xmldb -- eXist; and then get an xml
back based on a request parameter. Everything seems to
work fine for the first time the request is made.
Different request parameters on subsequent request
won't result a correct xml. I suspect that this
generator may have been cached, which is not what I
want. Following is the code, it's ugly but just a
first try...
==========================================
import org.apache.cocoon.generation.AbstractGenerator;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.SAXException;
// for the setup() method
import org.apache.cocoon.environment.SourceResolver;
import java.util.Map;
import
org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import java.io.IOException;
// used to deal with the request parameters.
import
org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import java.util.Enumeration;
import java.util.Date;
//xmldb
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;
public class ThreadGenerator extends AbstractGenerator
{
// Will be initialized in the setup() method and
used in generate()
Request request = null;
String threadID = null; //the thread we are
looking for
//todo: set threadID as a
static final variable
//xmldb
String driver = null;
Database database = null;
Collection col = null;
String[] resources = null;
XMLResource xmlres = null;
String forumURI =
"xmldb:exist:///db/cocoon/forum"; //db root
public void setup(SourceResolver resolver, Map
objectModel,
String src, Parameters parameters)
throws ProcessingException, SAXException,
IOException
{
super.setup(resolver, objectModel, src,
parameters);
request =
ObjectModelHelper.getRequest(objectModel);
forumURI = forumURI + "/" +
request.getParameter("groupID");
forumURI = forumURI + "/" +
request.getParameter("forumID");
//xmldb
try {
driver = "org.exist.xmldb.DatabaseImpl";
//todo: parameters.getParameter("driver");
Class cl = Class.forName(driver);
database = (Database)cl.newInstance();
DatabaseManager.registerDatabase(database);
database.setProperty("create-database",
"true");
col = DatabaseManager.getCollection(forumURI);
col.setProperty("pretty", "true");
col.setProperty("encoding", "UTF-8");
this.getLogger().debug("getting threadID");
threadID = request.getParameter("threadID") +
".xml"; //todo: set threadID as a static final
variable
resources = col.listResources();
this.getLogger().debug("getting threadID
done...");
}
catch(Exception e){}
}
/**
* Implement the generate() method from
AbstractGenerator.
*/
public void generate() throws SAXException
{
if (threadID!=null)
{
try {
xmlres =
(XMLResource)col.getResource(threadID);
if (xmlres==null)
throw new SAXException("Resource not
found:" + threadID + "doesn't not exist");
xmlres.getContentAsSAX(this.contentHandler);
contentHandler.startDocument();
contentHandler.endDocument();
}
catch (Exception e){}
}
}
public void recycle() {
super.recycle();
this.request = null;
driver = null;
try {
col.close();
DatabaseManager.deregisterDatabase(database);
}
catch (Exception e){}
database = null;
resources = null;
xmlres = null;
forumURI = null;
}
}
//end class
__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]