cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject cvs commit: xml-cocoon2/src/java/org/apache/cocoon/serialization ZipArchiveSerializer.java
Date Tue, 03 Dec 2002 10:20:02 GMT
cziegeler    2002/12/03 02:20:02

  Modified:    src/java/org/apache/cocoon/serialization
                        ZipArchiveSerializer.java
  Log:
  Avoid private internal calls and use the allowed api instead
  
  Revision  Changes    Path
  1.3       +64 -44    xml-cocoon2/src/java/org/apache/cocoon/serialization/ZipArchiveSerializer.java
  
  Index: ZipArchiveSerializer.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/serialization/ZipArchiveSerializer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ZipArchiveSerializer.java	31 Oct 2002 16:48:55 -0000	1.2
  +++ ZipArchiveSerializer.java	3 Dec 2002 10:20:01 -0000	1.3
  @@ -51,12 +51,11 @@
   
   package org.apache.cocoon.serialization;
   
  -import org.apache.cocoon.caching.CacheValidity;
  -import org.apache.cocoon.caching.Cacheable;
  -import org.apache.cocoon.components.CocoonComponentManager;
  -import org.apache.cocoon.environment.Source;
  -import org.apache.cocoon.environment.SourceResolver;
  -
  +import org.apache.avalon.framework.component.ComponentException;
  +import org.apache.avalon.framework.component.ComponentManager;
  +import org.apache.avalon.framework.component.Composable;
  +import org.apache.excalibur.source.Source;
  +import org.apache.excalibur.source.SourceResolver;
   import org.xml.sax.Attributes;
   import org.xml.sax.SAXException;
   
  @@ -96,63 +95,70 @@
   //              <svg>...</svg>
   //            </entry>
   
  -public class ZipArchiveSerializer extends AbstractSerializer
  -{
  +public class ZipArchiveSerializer
  +    extends AbstractSerializer
  +    implements Composable {
       /**
        * The namespace for elements handled by this serializer, 
        * "http://apache.org/cocoon/zip-archive/1.0".
        */
  -    public static final String ZIP_NAMESPACE = "http://apache.org/cocoon/zip-archive/1.0";
  -    
  +    public static final String ZIP_NAMESPACE =
  +        "http://apache.org/cocoon/zip-archive/1.0";
  +
       /** The Zip stream where entries will be written */
       protected ZipOutputStream zipOutput;
  -    
  +
       /** Have we encountered the toplevel "zip" element ? */
       protected boolean inZip = false;
  -    
  -    /** The resolver to get sources */
  -    protected SourceResolver resolver;
  -    
  +
       /** Temporary byte buffer to read source data */
       protected byte[] buffer = new byte[1024];
  -    
  +
  +    protected ComponentManager manager;
  +
       /**
        * Always return "application/x-zip" which is the default for Zip archives.
        */
  -    public String getMimeType()
  -    {
  +    public String getMimeType() {
           return "application/x-zip";
       }
   
       /**
        * @see org.xml.sax.ContentHandler#startDocument()
        */
  -    public void startDocument() throws SAXException
  -    {
  +    public void startDocument() throws SAXException {
           this.zipOutput = new ZipOutputStream(this.output);
           this.inZip = false;
  -        this.resolver = CocoonComponentManager.getCurrentEnvironment();
       }
   
       /**
        * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
        */
  -    public void startElement(String namespaceURI, String localName, String qName, Attributes
atts) throws SAXException
  -    {
  +    public void startElement(String namespaceURI,
  +                            String localName,
  +                            String qName,
  +                            Attributes atts)
  +    throws SAXException {
           if (!inZip) {
               // expecting "zip" as the first element
  -            if (namespaceURI.equals(ZIP_NAMESPACE) && localName.equals("archive"))
{
  +            if (namespaceURI.equals(ZIP_NAMESPACE)
  +                && localName.equals("archive")) {
                   this.inZip = true;
               } else {
  -                throw new SAXException("Expecting 'archive' root element (got '" + localName
+ "')");
  +                throw new SAXException(
  +                    "Expecting 'archive' root element (got '"
  +                        + localName
  +                        + "')");
               }
           } else {
               // expecting "entry" element
  -            if (namespaceURI.equals(ZIP_NAMESPACE) && localName.equals("entry"))
{
  +            if (namespaceURI.equals(ZIP_NAMESPACE)
  +                && localName.equals("entry")) {
                   // Get the source
                   addEntry(atts);
               } else {
  -                throw new SAXException("Expecting 'entry' element (got '" + localName +
"')");
  +                throw new SAXException(
  +                    "Expecting 'entry' element (got '" + localName + "')");
               }
           }
       }
  @@ -161,56 +167,70 @@
        * Add an entry in the archive.
        * @param atts the attributes that describe the entry
        */
  -    protected void addEntry(Attributes atts) throws SAXException
  -    {
  +    protected void addEntry(Attributes atts) throws SAXException {
           String name = atts.getValue("name");
           if (name == null) {
               throw new SAXException("No name given to the Zip entry");
           }
  -        
  +
           String src = atts.getValue("src");
           if (src == null) {
               throw new SAXException("No source given for the Zip entry");
           }
  -        
  +
  +        SourceResolver resolver = null;
  +        Source         source   = null;
           try {
  +            resolver = (SourceResolver)this.manager.lookup(SourceResolver.ROLE);
  +            
               // Create a new Zip entry
               ZipEntry entry = new ZipEntry(name);
               this.zipOutput.putNextEntry(entry);
  -            
  +
               // Get the source and its data
  -            Source source = resolver.resolve(src);
  +            source = resolver.resolveURI(src);
               InputStream sourceInput = source.getInputStream();
  -            
  +
               // Copy the source to the zip
               int len;
               while ((len = sourceInput.read(this.buffer)) > 0) {
                   this.zipOutput.write(this.buffer, 0, len);
               }
  -            
  +
               // and close the entry
               this.zipOutput.closeEntry();
  -            
  -        } catch(RuntimeException re) {
  +
  +        } catch (RuntimeException re) {
               throw re;
  -        } catch(SAXException se) {
  -            throw se;
  -        } catch(Exception e) {
  +        } catch (Exception e) {
               throw new SAXException(e);
  +        } finally {
  +            if (resolver != null) {
  +                resolver.release(source);
  +            }
  +            this.manager.release(resolver);
           }
       }
   
       /**
        * @see org.xml.sax.ContentHandler#endDocument()
        */
  -    public void endDocument() throws SAXException
  -    {
  +    public void endDocument() throws SAXException {
           try {
               // Close the zip archive
               this.zipOutput.finish();
  -            
  -        } catch(IOException ioe) {
  +
  +        } catch (IOException ioe) {
               throw new SAXException(ioe);
           }
       }
  +
  +    /**
  +     * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager)
  +     */
  +    public void compose(ComponentManager componentManager)
  +        throws ComponentException {
  +        this.manager = componentManager;
  +    }
  +
   }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org


Mime
View raw message