Clover coverage report - Common - 1.0.0
Coverage timestamp: sam. déc. 27 2003 15:13:46 CET
file stats: LOC: 189   Methods: 6
NCLOC: 116   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StreamReader.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.ByteArrayInputStream;
 10   
 import java.io.ByteArrayOutputStream;
 11   
 import java.io.IOException;
 12   
 import java.util.Stack;
 13   
 import java.util.zip.ZipEntry;
 14   
 import java.util.zip.ZipInputStream;
 15   
 
 16   
 import org.ejtools.archive.Archive;
 17   
 import org.ejtools.archive.Entry;
 18   
 import org.ejtools.archive.JarArchive;
 19   
 import org.ejtools.archive.JarEntry;
 20   
 
 21   
 
 22   
 /**
 23   
  * Reader implementation based on stream.
 24   
  * <p>Each Archive and each Entry will be created through a ZipInputStream. This classe serves as parent for derived ones.</p>
 25   
  * <p>As the structure is dynamically created, the Visitor Pattern cannot be strictly followed.</p>
 26   
  *
 27   
  * @author    Laurent Etiemble
 28   
  * @version   $Revision: 1.2 $
 29   
  */
 30   
 public abstract class StreamReader implements Reader
 31   
 {
 32   
    /** Stacks of the current input streams */
 33   
    private Stack inStreams = new Stack();
 34   
 
 35   
 
 36   
    /**
 37   
     * Visit an entry. Do nothing.
 38   
     *
 39   
     * @param entry  The entry to visit
 40   
     */
 41  0
    public void visit(Entry entry) { }
 42   
 
 43   
 
 44   
    /**
 45   
     * Visit an archive. Each Archive will be build from a ZipInputStream and populated with entries.
 46   
     *
 47   
     * @param archive  The archive to visit
 48   
     */
 49  0
    public void visit(Archive archive)
 50   
    {
 51  0
       try
 52   
       {
 53  0
          ZipInputStream stream = this.getZipInputStream();
 54  0
          if (stream == null)
 55   
          {
 56  0
             throw new IllegalStateException("No initial input stream");
 57   
          }
 58   
 
 59  0
          ZipEntry ze = null;
 60  0
          while ((ze = stream.getNextEntry()) != null)
 61   
          {
 62  0
             if ((ze != null) && (!ze.isDirectory()))
 63   
             {
 64  0
                Entry entry = this.createEntry(ze.getName());
 65  0
                if (entry != null)
 66   
                {
 67  0
                   entry.addTo(archive);
 68   
                }
 69   
             }
 70  0
             stream.closeEntry();
 71   
          }
 72  0
          stream.close();
 73   
       }
 74   
       catch (IOException ioe)
 75   
       {
 76  0
          ioe.printStackTrace();
 77   
       }
 78   
    }
 79   
 
 80   
 
 81   
    /**
 82   
     * Helper method to create an entry.
 83   
     *
 84   
     * @param uri  The URI of the entry
 85   
     * @return     The new Entry
 86   
     */
 87  0
    protected Entry createEntry(String uri)
 88   
    {
 89  0
       try
 90   
       {
 91  0
          ZipInputStream stream = this.getZipInputStream();
 92  0
          if (stream == null)
 93   
          {
 94  0
             throw new IllegalStateException("No initial input stream");
 95   
          }
 96   
 
 97  0
          byte[] buffer = new byte[1024];
 98  0
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
 99  0
          int read = 0;
 100  0
          while ((read = stream.read(buffer)) > 0)
 101   
          {
 102  0
             baos.write(buffer, 0, read);
 103   
          }
 104  0
          byte[] content = baos.toByteArray();
 105   
 
 106  0
          try
 107   
          {
 108  0
             ZipInputStream jis = new ZipInputStream(new ByteArrayInputStream(content));
 109  0
             ZipEntry ze = jis.getNextEntry();
 110  0
             jis.close();
 111  0
             if (ze != null)
 112   
             {
 113  0
                Archive archive = new JarArchive(uri);
 114   
                //
 115   
                // Archive should not have its content !!!
 116   
                //
 117   
                // archive.setContent(content);
 118  0
                this.pushZipInputStream(new ZipInputStream(new ByteArrayInputStream(content)));
 119  0
                archive.accept(this);
 120  0
                this.popZipInputStream();
 121   
 
 122  0
                return archive;
 123   
             }
 124   
             else
 125   
             {
 126  0
                Entry entry = new JarEntry(uri);
 127  0
                entry.setContent(content);
 128  0
                return entry;
 129   
             }
 130   
          }
 131   
          catch (Exception ioe)
 132   
          {
 133  0
             Entry entry = new JarEntry(uri);
 134  0
             entry.setContent(content);
 135  0
             return entry;
 136   
          }
 137   
       }
 138   
       catch (IOException ioe)
 139   
       {
 140  0
          ioe.printStackTrace();
 141   
       }
 142  0
       return null;
 143   
    }
 144   
 
 145   
 
 146   
    /**
 147   
     * Returns the current stream
 148   
     *
 149   
     * @return   The current stream
 150   
     */
 151  0
    protected ZipInputStream getZipInputStream()
 152   
    {
 153  0
       return (ZipInputStream) this.inStreams.lastElement();
 154   
    }
 155   
 
 156   
 
 157   
    /**
 158   
     * Pops the last entered stream
 159   
     *
 160   
     * @return                 The stream
 161   
     * @exception IOException  If no stream is available
 162   
     */
 163  0
    protected ZipInputStream popZipInputStream()
 164   
       throws IOException
 165   
    {
 166  0
       if (this.inStreams.isEmpty())
 167   
       {
 168  0
          return null;
 169   
       }
 170   
       else
 171   
       {
 172  0
          return (ZipInputStream) this.inStreams.pop();
 173   
       }
 174   
    }
 175   
 
 176   
 
 177   
    /**
 178   
     * Pushes a stream onto the stack
 179   
     *
 180   
     * @param stream           The stream
 181   
     * @exception IOException  Shoud never occured
 182   
     */
 183  0
    protected void pushZipInputStream(ZipInputStream stream)
 184   
       throws IOException
 185   
    {
 186  0
       this.inStreams.push(stream);
 187   
    }
 188   
 }
 189