Author: upayavira
Date: Tue Feb 22 05:00:13 2005
New Revision: 154840

URL: http://svn.apache.org/viewcvs?view=rev&rev=154840
Log:
Adding ability for CocoonBean to process a URL to a content handler
with cocoonBean.process(uri, contentHandler), rather than to an
OutputStream (of course skipping the serializer).

Modified:
    cocoon/trunk/src/java/org/apache/cocoon/bean/CocoonWrapper.java
    
cocoon/trunk/src/java/org/apache/cocoon/environment/commandline/AbstractCommandLineEnvironment.java
    cocoon/trunk/status.xml

Modified: cocoon/trunk/src/java/org/apache/cocoon/bean/CocoonWrapper.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/bean/CocoonWrapper.java?view=diff&r1=154839&r2=154840
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/bean/CocoonWrapper.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/bean/CocoonWrapper.java Tue Feb 22 
05:00:13 2005
@@ -397,6 +397,36 @@
         }
     }
 
+    /**
+     * Process single URI into given content handler, skipping final
+     * serializer
+     *
+     * @param uri to process
+     * @param handler to write generated contents into
+     */
+    public void processURI(String uri, ContentHandler handler)
+        throws Exception {
+
+        if (!initialized) {
+            initialize();
+        }
+        log.info("Processing URI: " + uri);
+
+        // Get parameters, deparameterized URI and path from URI
+        final TreeMap parameters = new TreeMap();
+        final String deparameterizedURI =
+            NetUtils.deparameterize(uri, parameters);
+        parameters.put("user-agent", userAgent);
+        parameters.put("accept", accept);
+
+        int status =
+            getPage(deparameterizedURI, 0L, parameters, null, null, handler);
+
+        if (status >= 400) {
+            throw new ProcessingException("Resource not found: " + status);
+        }
+    }
+
     public void dispose() {
         if (this.initialized) {
             this.initialized = false;
@@ -458,6 +488,47 @@
 
         // Here Cocoon can throw an exception if there are errors in 
processing the page
         cocoon.process(env);
+
+        // if we get here, the page was created :-)
+        int status = env.getStatus();
+        if (!env.isModified()) {
+            status = -1;
+        }
+        return status;
+    }
+
+    /**
+     * Processes an URI for its content.
+     *
+     * @param deparameterizedURI a <code>String</code> value of an URI to 
start sampling from
+     * @param parameters a <code>Map</code> value containing request parameters
+     * @param links a <code>Map</code> value
+     * @param stream an <code>OutputStream</code> to write the content to
+     * @return a <code>String</code> value for the content
+     * @exception Exception if an error occurs
+     */
+    protected int getPage(String deparameterizedURI,
+                          long lastModified,
+                          Map parameters,
+                          Map links,
+                          List gatheredLinks,
+                          ContentHandler handler)
+    throws Exception {
+
+        parameters.put("user-agent", userAgent);
+        parameters.put("accept", accept);
+
+        FileSavingEnvironment env =
+            new FileSavingEnvironment(deparameterizedURI, lastModified, 
context,
+                                      attributes, parameters, links,
+                                      gatheredLinks, cliContext, null, log);
+
+        XMLConsumer consumer = new ContentHandlerWrapper(handler);
+        ProcessingPipeline pipeline = cocoon.buildPipeline(env);
+        CocoonComponentManager.enterEnvironment(env, 
cocoon.getComponentManager(), cocoon);
+        pipeline.prepareInternal(env);
+        CocoonComponentManager.leaveEnvironment();
+        pipeline.process(env, consumer);
 
         // if we get here, the page was created :-)
         int status = env.getStatus();

Modified: 
cocoon/trunk/src/java/org/apache/cocoon/environment/commandline/AbstractCommandLineEnvironment.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/environment/commandline/AbstractCommandLineEnvironment.java?view=diff&r1=154839&r2=154840
==============================================================================
--- 
cocoon/trunk/src/java/org/apache/cocoon/environment/commandline/AbstractCommandLineEnvironment.java
 (original)
+++ 
cocoon/trunk/src/java/org/apache/cocoon/environment/commandline/AbstractCommandLineEnvironment.java
 Tue Feb 22 05:00:13 2005
@@ -39,7 +39,7 @@
  * This environment is used to save the requested file to disk.
  *
  * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
- * @version CVS $Id: AbstractCommandLineEnvironment.java,v 1.7 2004/05/25 
07:28:24 cziegeler Exp $
+ * @version CVS $Id$
  */
 
 public abstract class AbstractCommandLineEnvironment
@@ -173,6 +173,19 @@
      */
     public boolean isExternal() {
         return true;
+    }
+
+    /**
+     * Return an OutputStream, but allow it to be null for when
+     * the pipeline is being streamed to the provided SAX 
+     * content handler (using CocoonBean)
+     */
+    public OutputStream getOutputStream(int bufferSize) throws IOException {
+        if (this.outputStream == null) {
+            return null;
+        } else {
+            return super.getOutputStream(bufferSize);
+        }
     }
 
 }

Modified: cocoon/trunk/status.xml
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/status.xml?view=diff&r1=154839&r2=154840
==============================================================================
--- cocoon/trunk/status.xml (original)
+++ cocoon/trunk/status.xml Tue Feb 22 05:00:13 2005
@@ -202,6 +202,9 @@
 
   <changes>
  <release version="@version@" date="@date@">
+   <action dev="UV" type="add">
+    CocoonBean can now process a URL to a SAX ContentHandler (skipping the 
serialiser), via cocoon.process(uri, handler).
+   </action>
    <action dev="CZ" type="update">
     Create proxies for pooled components. Pooled components can now be used in 
the same
     way as thread safe components, the client code does not need to know if 
they are pooled anymore.


Reply via email to