Clover coverage report - Common - 1.0.0
Coverage timestamp: sam. déc. 27 2003 15:13:46 CET
file stats: LOC: 147   Methods: 5
NCLOC: 87   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StreamWriter.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * EJTools, the Enterprise Java Tools
 3   
  *
 4   
  * Distributable under LGPL license.
 5   
  * See terms of license at www.gnu.org.
 6   
  */
 7   
 package org.ejtools.archive.io;
 8   
 
 9   
 import java.io.ByteArrayOutputStream;
 10   
 import java.io.IOException;
 11   
 import java.util.Iterator;
 12   
 import java.util.Stack;
 13   
 import java.util.jar.JarEntry;
 14   
 import java.util.zip.ZipOutputStream;
 15   
 
 16   
 import org.ejtools.archive.Archive;
 17   
 import org.ejtools.archive.Entry;
 18   
 
 19   
 /**
 20   
  * Writer implementation based on stream. Each Archive and each Entry will be output through a ZipOutputStream. This classe serves as parent for derived ones.
 21   
  *
 22   
  * @author    Laurent Etiemble
 23   
  * @version   $Revision: 1.2 $
 24   
  */
 25   
 public abstract class StreamWriter implements Writer
 26   
 {
 27   
    /** Stacks of the current output streams */
 28   
    private Stack outStreams = new Stack();
 29   
 
 30   
 
 31   
    /**
 32   
     * Visit an archive. A new ZipOutputStream is created for each Archive.
 33   
     *
 34   
     * @param archive  The archive to visit
 35   
     */
 36  0
    public void visit(Archive archive)
 37   
    {
 38  0
       try
 39   
       {
 40  0
          ZipOutputStream stream = this.getZipOutputStream();
 41  0
          if (stream == null)
 42   
          {
 43  0
             throw new IllegalStateException("No initial input stream");
 44   
          }
 45   
 
 46   
          // For each archive, output its content
 47  0
          for (Iterator iterator = archive.iterator(); iterator.hasNext(); )
 48   
          {
 49  0
             Entry entry = (Entry) iterator.next();
 50   
 
 51  0
             if (entry.isArchive())
 52   
             {
 53  0
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
 54  0
                ZipOutputStream jos = new ZipOutputStream(baos);
 55   
 
 56  0
                this.pushZipOutputStream(jos);
 57  0
                entry.accept(this);
 58  0
                this.popZipOutputStream();
 59   
 
 60  0
                JarEntry je = new JarEntry(entry.getURI());
 61  0
                stream.putNextEntry(je);
 62  0
                stream.write(baos.toByteArray());
 63   
             }
 64   
             else
 65   
             {
 66  0
                entry.accept(this);
 67   
             }
 68   
          }
 69  0
          stream.close();
 70   
       }
 71   
       catch (IOException ioe)
 72   
       {
 73  0
          ioe.printStackTrace();
 74   
       }
 75   
    }
 76   
 
 77   
 
 78   
    /**
 79   
     * Visit an entry.
 80   
     *
 81   
     * @param entry  The entry to visit
 82   
     */
 83  0
    public void visit(Entry entry)
 84   
    {
 85  0
       try
 86   
       {
 87   
          // Gets the current stream and dump the content
 88  0
          ZipOutputStream stream = this.getZipOutputStream();
 89  0
          if (stream == null)
 90   
          {
 91  0
             throw new IllegalStateException("No initial input stream");
 92   
          }
 93  0
          JarEntry je = new JarEntry(entry.getURI());
 94  0
          stream.putNextEntry(je);
 95  0
          stream.write(entry.getContent());
 96   
       }
 97   
       catch (IOException ioe)
 98   
       {
 99  0
          ioe.printStackTrace();
 100   
       }
 101   
    }
 102   
 
 103   
 
 104   
    /**
 105   
     * Returns the current stream
 106   
     *
 107   
     * @return   The current stream
 108   
     */
 109  0
    protected ZipOutputStream getZipOutputStream()
 110   
    {
 111  0
       return (ZipOutputStream) this.outStreams.lastElement();
 112   
    }
 113   
 
 114   
 
 115   
    /**
 116   
     * Pops the last entered stream
 117   
     *
 118   
     * @return                 The stream
 119   
     * @exception IOException  If no stream is available
 120   
     */
 121  0
    protected ZipOutputStream popZipOutputStream()
 122   
       throws IOException
 123   
    {
 124  0
       if (this.outStreams.isEmpty())
 125   
       {
 126  0
          return null;
 127   
       }
 128   
       else
 129   
       {
 130  0
          return (ZipOutputStream) this.outStreams.pop();
 131   
       }
 132   
    }
 133   
 
 134   
 
 135   
    /**
 136   
     * Pushes a stream onto the stack
 137   
     *
 138   
     * @param stream           The stream
 139   
     * @exception IOException  Shoud never occured
 140   
     */
 141  0
    protected void pushZipOutputStream(ZipOutputStream stream)
 142   
       throws IOException
 143   
    {
 144  0
       this.outStreams.push(stream);
 145   
    }
 146   
 }
 147