cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pruss...@apache.org
Subject cvs commit: xml-cocoon/src/org/apache/cocoon/components/language/markup/sitemap/java sitemap.xsl
Date Sat, 03 Mar 2001 16:05:09 GMT
prussell    01/03/03 08:05:09

  Modified:    src/org/apache/cocoon/sitemap Tag: xml-cocoon2
                        ResourcePipeline.java
               src/org/apache/cocoon/components/language/markup/sitemap/java
                        Tag: xml-cocoon2 sitemap.xsl
  Log:
  Refactored ResourcePipeline for code clarity, and added support for
  SAXConnectors. This class will be _heavily_ refactored for efficiency
  over the next day or so. Added support for resource pipeline disposal
  to sitemap logicsheet.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.25  +226 -84   xml-cocoon/src/org/apache/cocoon/sitemap/Attic/ResourcePipeline.java
  
  Index: ResourcePipeline.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/sitemap/Attic/ResourcePipeline.java,v
  retrieving revision 1.1.2.24
  retrieving revision 1.1.2.25
  diff -u -r1.1.2.24 -r1.1.2.25
  --- ResourcePipeline.java	2001/02/23 14:01:27	1.1.2.24
  +++ ResourcePipeline.java	2001/03/03 16:04:58	1.1.2.25
  @@ -8,16 +8,20 @@
   package org.apache.cocoon.sitemap;
   
   import java.util.ArrayList;
  +import java.util.Iterator;
   import java.io.IOException;
   import java.io.OutputStream;
   
   import org.apache.avalon.Configuration;
   import org.apache.avalon.Configurable;
   import org.apache.avalon.ComponentManager;
  +import org.apache.avalon.ComponentManagerException;
   import org.apache.avalon.ComponentSelector;
   import org.apache.avalon.Component;
   import org.apache.avalon.Composer;
   import org.apache.avalon.Parameters;
  +import org.apache.avalon.Loggable;
  +import org.apache.log.Logger;
   
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.environment.Environment;
  @@ -26,7 +30,9 @@
   import org.apache.cocoon.transformation.Transformer;
   import org.apache.cocoon.serialization.Serializer;
   import org.apache.cocoon.xml.XMLProducer;
  +import org.apache.cocoon.xml.XMLConsumer;
   import org.apache.cocoon.Roles;
  +import org.apache.cocoon.components.saxconnector.SAXConnector;
   
   import org.apache.cocoon.sitemap.ErrorNotifier;
   
  @@ -35,7 +41,7 @@
   
   /**
    * @author <a href="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
  - * @version CVS $Revision: 1.1.2.24 $ $Date: 2001/02/23 14:01:27 $
  + * @version CVS $Revision: 1.1.2.25 $ $Date: 2001/03/03 16:04:58 $
    */
   public class ResourcePipeline implements Composer {
       private Generator generator;
  @@ -50,11 +56,14 @@
       private ArrayList transformers = new ArrayList();
       private ArrayList transformerParams = new ArrayList();
       private ArrayList transformerSources = new ArrayList();
  +    private ArrayList connectors = new ArrayList();
       private Serializer serializer;
       private Parameters serializerParam;
       private String serializerSource;
       private String serializerMimeType;
       private String sitemapSerializerMimeType;
  +    
  +    private Logger log;
   
       /** the component manager */
       private ComponentManager manager;
  @@ -62,6 +71,10 @@
       public void compose (ComponentManager manager) {
           this.manager = manager;
       }
  +    
  +    public void setLogger(Logger log) {
  +        this.log = log;
  +    }
   
       public void setGenerator (String role, String source, Parameters param, Exception e)
       throws Exception {
  @@ -123,94 +136,223 @@
           this.transformerSources.add (source);
           this.transformerParams.add (param);
       }
  -
  -    public boolean process (Environment environment)
  -                            throws Exception {
  -        String mime_type;
  -
  -        if (this.generator == null) {
  -            if (this.reader != null) {
  -                try {
  -                    this.reader.setup ((EntityResolver) environment, environment.getObjectModel(),
readerSource, readerParam);
  -                    mime_type = this.reader.getMimeType();
  -                    if (mime_type != null) {
  -                        // we have a mimeType freom the component itself
  -                        environment.setContentType (mime_type);
  -                    } else if (readerMimeType != null) {
  -                        // there was a mimeType specified in the sitemap pipeline
  -                        environment.setContentType (this.readerMimeType);
  -                    } else {
  -                        // use the mimeType specified in the sitemap component declaration
  -                        environment.setContentType (this.sitemapReaderMimeType);
  -                    }
  -                    reader.setOutputStream (environment.getOutputStream());
  -                    reader.generate();
  -                } catch (Exception e) {
  -                    throw new ProcessingException("There was an error with the reader",
e);
  -                } finally {
  -                    ((ComponentSelector) this.manager.lookup(Roles.READERS)).release((Component)
reader);
  -                }
  -            } else {
  -                throw new ProcessingException ("Generator or Reader not specified");
  -            }
  +    
  +    public boolean process(Environment environment)
  +    throws ProcessingException {
  +        if ( this.reader != null ) {
  +            return processReader(environment);
           } else {
  -            Transformer myTransformer[] = (Transformer []) transformers.toArray(new Transformer[]
{});
  -
  +            if ( !checkPipeline() ) {
  +                throw new ProcessingException("Attempted to process incomplete pipeline.");
  +            }
  +            
  +            setupPipeline(environment);
  +            connectPipeline();
  +            
  +            // execute the pipeline:
               try {
  -                if (this.serializer == null) {
  -                    if (this.generator != null) {
  -                        ((ComponentSelector) this.manager.lookup(Roles.GENERATORS)).release((Component)
generator);
  -                    }
  -
  -                    if (this.transformers.isEmpty() == false) {
  -                        for (int i = 0; i < myTransformer.length; i++) {
  -                            ((ComponentSelector) this.manager.lookup(Roles.TRANSFORMERS)).release((Component)
myTransformer[i]);
  -                        }
  -                    }
  -                    throw new ProcessingException ("Serializer not specified");
  -                }
  -
  -                if (generatorException != null) {
  -                    ((ErrorNotifier)this.generator).setException (generatorException);
  -                }
  -
  -                this.generator.setup ((EntityResolver) environment, environment.getObjectModel(),
generatorSource, generatorParam);
  -                Transformer transformer = null;
  -                XMLProducer producer = this.generator;
  -                for (int i = 0; i < myTransformer.length; i++) {
  -                    myTransformer[i].setup ((EntityResolver) environment, environment.getObjectModel(),
  -                            (String)transformerSources.get (i),
  -                            (Parameters)transformerParams.get (i));
  -                    producer.setConsumer (myTransformer[i]);
  -                    producer = myTransformer[i];
  -                }
  -
  -                mime_type = this.serializer.getMimeType();
  -                if (mime_type != null) {
  -                    // we have a mimeType freom the component itself
  -                    environment.setContentType (mime_type);
  -                } else if (serializerMimeType != null) {
  -                    // there was a mimeType specified in the sitemap pipeline
  -                    environment.setContentType (serializerMimeType);
  -                } else {
  -                    // use the mimeType specified in the sitemap component declaration
  -                    environment.setContentType (this.sitemapSerializerMimeType);
  -                }
  -                this.serializer.setOutputStream (environment.getOutputStream());
  -                producer.setConsumer (this.serializer);
                   this.generator.generate();
  -            } catch (Exception e) {
  -                throw new ProcessingException("Error generating the resource", e);
  -            } finally {
  -                ((ComponentSelector) this.manager.lookup(Roles.GENERATORS)).release((Component)
generator);
  -
  -                for (int i = 0; i < myTransformer.length; i++) {
  -                    ((ComponentSelector) this.manager.lookup(Roles.TRANSFORMERS)).release((Component)
myTransformer[i]);
  -                }
  -
  -                ((ComponentSelector) this.manager.lookup(Roles.SERIALIZERS)).release((Component)
serializer);
  +            } catch ( Exception e ) {
  +                throw new ProcessingException(
  +                    "Failed to execute pipeline.",
  +                    e
  +                );
  +            }
  +            
  +            return true;
  +        }
  +    }
  +    
  +    /** Process the pipeline using a reader.
  +     * @throws ProcessingException if 
  +     */
  +    private boolean processReader(Environment environment)
  +    throws ProcessingException {
  +        String mimeType;
  +        try {
  +            this.reader.setup((EntityResolver) environment,environment.getObjectModel(),readerSource,readerParam);
  +            mimeType = this.reader.getMimeType();
  +            if ( mimeType != null ) {
  +                environment.setContentType(mimeType);
  +            } else if ( readerMimeType != null ) {
  +                environment.setContentType(this.readerMimeType);
  +            } else {
  +                environment.setContentType(this.sitemapReaderMimeType);
               }
  +            this.reader.setOutputStream(environment.getOutputStream());
  +            this.reader.generate();
  +        } catch ( Exception e ) {
  +            throw new ProcessingException("Error reading resource",e);
           }
           return true;
  +    }
  +    
  +    /** Sanity check the non-reader pipeline.
  +     * @return true if the pipeline is 'sane', false otherwise.
  +     */
  +    private boolean checkPipeline() {
  +        if ( this.generator == null ) {
  +            return false;
  +        }
  +        
  +        if ( this.serializer == null ) {
  +            return false;
  +        }
  +        
  +        Iterator itt = this.transformers.iterator();
  +        while ( itt.hasNext() ) {
  +            if ( itt.next() == null) {
  +                return false;
  +            }
  +        }
  +        
  +        return true;
  +    }
  +    
  +    /** Setup pipeline components.
  +     */
  +    private void setupPipeline(Environment environment)
  +    throws ProcessingException {
  +        try {
  +            // setup the generator
  +            this.generator.setup(
  +                (EntityResolver)environment,
  +                environment.getObjectModel(),
  +                generatorSource,
  +                generatorParam
  +            );
  +
  +            Iterator transformerItt = this.transformers.iterator();
  +            Iterator transformerSourceItt = this.transformerSources.iterator();
  +            Iterator transformerParamItt = this.transformerParams.iterator();
  +
  +            while ( transformerItt.hasNext() ) {
  +                Transformer trans = (Transformer)transformerItt.next();
  +                trans.setup(
  +                    (EntityResolver)environment,
  +                    environment.getObjectModel(),
  +                    (String)transformerSourceItt.next(),
  +                    (Parameters)transformerParamItt.next()
  +                );
  +            }
  +            
  +            this.serializer.setOutputStream(environment.getOutputStream());
  +            String mimeType = this.serializer.getMimeType();
  +            if (mimeType != null) {
  +                // we have a mimeType freom the component itself
  +                environment.setContentType (mimeType);
  +            } else if (serializerMimeType != null) {
  +                // there was a mimeType specified in the sitemap pipeline
  +                environment.setContentType (serializerMimeType);
  +            } else {
  +                // use the mimeType specified in the sitemap component declaration
  +                environment.setContentType (this.sitemapSerializerMimeType);
  +            }
  +        } catch (SAXException e) {
  +            throw new ProcessingException(
  +                "Could not setup pipeline.",
  +                e
  +            );
  +        } catch (IOException e) {
  +            throw new ProcessingException(
  +                "Could not setup pipeline.",
  +                e
  +            );
  +        }
  +        
  +        
  +    }
  +    
  +    /** Connect the pipeline.
  +     */
  +    private void connectPipeline() throws ProcessingException {
  +        XMLProducer prev = (XMLProducer) this.generator;
  +        XMLConsumer next;
  +        
  +        try {
  +            Iterator itt = this.transformers.iterator();
  +            while ( itt.hasNext() ) {
  +                // connect SAXConnector
  +                SAXConnector connect = (SAXConnector)
  +                    this.manager.lookup(Roles.SAX_CONNECTOR);
  +                this.connectors.add(connect);
  +                next = (XMLConsumer) connect;
  +                prev.setConsumer(next);
  +                prev = (XMLProducer) connect;
  +
  +                // Connect next component.
  +                Transformer trans = (Transformer) itt.next();
  +                next = (XMLConsumer) trans;
  +                prev.setConsumer(next);
  +                prev = (XMLProducer) trans;
  +            }
  +
  +            // insert SAXConnector
  +            SAXConnector connect = (SAXConnector)
  +                this.manager.lookup(Roles.SAX_CONNECTOR);
  +            this.connectors.add(connect);
  +            next = (XMLConsumer) connect;
  +            prev.setConsumer(next);
  +            prev = (XMLProducer) connect;
  +
  +            // connect serializer.
  +            prev.setConsumer(this.serializer);
  +        } catch ( ComponentManagerException e ) {
  +            throw new ProcessingException(
  +                "Could not connect pipeline.",
  +                e
  +            );
  +        }
  +        
  +    }
  +    
  +    public void dispose() {
  +        this.log.debug("Disposing of ResourcePipeline");
  +        
  +        try {
  +            // release reader.
  +            if ( this.reader != null ) {
  +                ((ComponentSelector) this.manager.lookup(Roles.READERS))
  +                    .release(this.reader);
  +            }
  +
  +            // release generator
  +            if ( this.generator != null ) {
  +                ((ComponentSelector) this.manager.lookup(Roles.GENERATORS))
  +                    .release(this.generator);
  +            }
  +
  +            // release serializer
  +            if ( this.serializer != null ) {
  +                ((ComponentSelector) this.manager.lookup(Roles.SERIALIZERS))
  +                    .release(this.serializer);
  +            }
  +
  +            // Release transformers
  +            ComponentSelector transformerSelector;
  +            transformerSelector = (ComponentSelector)this.manager.lookup(Roles.TRANSFORMERS);
  +            Iterator itt = this.transformers.iterator();
  +            while ( itt.hasNext() ) {
  +                transformerSelector.release((Component)itt.next());
  +            }
  +            this.transformers.clear();
  +        } catch ( ComponentManagerException e ) {
  +            this.log.warn(
  +                "Failed to release components from resource pipeline.",
  +                e
  +            );
  +        } finally {
  +            this.reader = null;
  +            this.generator = null;
  +            this.serializer = null;
  +            this.transformers.clear();
  +        }
  +        
  +        // Release connectors
  +        Iterator itt = this.connectors.iterator();
  +        while ( itt.hasNext() ) {
  +            this.manager.release((Component) itt.next());
  +        }
  +        this.connectors.clear();
       }
   }
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.87  +7 -2      xml-cocoon/src/org/apache/cocoon/components/language/markup/sitemap/java/Attic/sitemap.xsl
  
  Index: sitemap.xsl
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/components/language/markup/sitemap/java/Attic/sitemap.xsl,v
  retrieving revision 1.1.2.86
  retrieving revision 1.1.2.87
  diff -u -r1.1.2.86 -r1.1.2.87
  --- sitemap.xsl	2001/02/28 15:29:45	1.1.2.86
  +++ sitemap.xsl	2001/03/03 16:05:05	1.1.2.87
  @@ -94,7 +94,7 @@
        *
        * @author &lt;a href="mailto:giacomo@apache.org"&gt;Giacomo Pati&lt;/a&gt;
        * @author &lt;a href="mailto:bloritsch@apache.org"&gt;Berin Loritsch&lt;/a&gt;
  -     * @version CVS $Id: sitemap.xsl,v 1.1.2.86 2001/02/28 15:29:45 bloritsch Exp $
  +     * @version CVS $Id: sitemap.xsl,v 1.1.2.87 2001/03/03 16:05:05 prussell Exp $
        */
       public class <xsl:value-of select="@file-name"/> extends AbstractSitemap {
         static final String LOCATION = "<xsl:value-of select="translate(@file-path, '/',
'.')"/>.<xsl:value-of select="@file-name"/>";
  @@ -375,6 +375,7 @@
              components to produce the requested resource */
           ResourcePipeline pipeline = new ResourcePipeline ();
           pipeline.compose(this.manager);
  +        pipeline.setLogger(getLogger());
           /* the &lt;code&gt;List&lt;/code&gt; objects to hold the replacement
values
              delivered from matchers and selectors to replace occurences of
              XPath kind expressions in values of src attribute used with
  @@ -797,7 +798,11 @@
       </xsl:if>
   
       <!-- the "if(true)" is needed to prevent "statement not reachable" error messages
during compile -->
  -    if(true)return pipeline.process (environment);
  +    {
  +      boolean result = pipeline.process(environment);
  +      pipeline.dispose();
  +      if(true) return result;
  +    }
     </xsl:template> <!-- match="map:serialize" -->
   
     <!-- generate the code to invoke a reader -->
  
  
  

Mime
View raw message