Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 214   Methods: 3
NCLOC: 67   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DirectoryIterator.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.taskdefs.optional.depend;
 55   
 
 56   
 import java.io.File;
 57   
 import java.io.FileInputStream;
 58   
 import java.io.IOException;
 59   
 import java.util.Enumeration;
 60   
 import java.util.Stack;
 61   
 import java.util.Vector;
 62   
 
 63   
 /**
 64   
  * An iterator which iterates through the contents of a java directory. The
 65   
  * iterator should be created with the directory at the root of the Java
 66   
  * namespace.
 67   
  *
 68   
  * @author Conor MacNeill
 69   
  */
 70   
 public class DirectoryIterator implements ClassFileIterator {
 71   
 
 72   
     /**
 73   
      * This is a stack of current iterators supporting the depth first
 74   
      * traversal of the directory tree.
 75   
      */
 76   
     private Stack enumStack;
 77   
 
 78   
     /**
 79   
      * The current directory iterator. As directories encounter lower level
 80   
      * directories, the current iterator is pushed onto the iterator stack
 81   
      * and a new iterator over the sub directory becomes the current
 82   
      * directory. This implements a depth first traversal of the directory
 83   
      * namespace.
 84   
      */
 85   
     private Enumeration currentEnum;
 86   
 
 87   
     /**
 88   
      * The length of the root directory. This is used to remove the root
 89   
      * directory from full paths.
 90   
      */
 91   
     private int rootLength;
 92   
 
 93   
     /**
 94   
      * Creates a directory iterator. The directory iterator is created to
 95   
      * scan the root directory. If the changeInto flag is given, then the
 96   
      * entries returned will be relative to this directory and not the
 97   
      * current directory.
 98   
      *
 99   
      * @param rootDirectory the root if the directory namespace which is to
 100   
      *      be iterated over
 101   
      * @param changeInto if true then the returned entries will be relative
 102   
      *      to the rootDirectory and not the current directory.
 103   
      * @exception IOException if there is a problem reading the directory
 104   
      *      information.
 105   
      */
 106  0
     public DirectoryIterator(File rootDirectory, boolean changeInto)
 107   
          throws IOException {
 108  0
         super();
 109   
 
 110  0
         enumStack = new Stack();
 111   
 
 112  0
         if (rootDirectory.isAbsolute() || changeInto) {
 113  0
             rootLength = rootDirectory.getPath().length() + 1;
 114   
         } else {
 115  0
             rootLength = 0;
 116   
         }
 117   
 
 118  0
         Vector filesInRoot = getDirectoryEntries(rootDirectory);
 119   
 
 120  0
         currentEnum = filesInRoot.elements();
 121   
     }
 122   
 
 123   
     /**
 124   
      * Get a vector covering all the entries (files and subdirectories in a
 125   
      * directory).
 126   
      *
 127   
      * @param directory the directory to be scanned.
 128   
      * @return a vector containing File objects for each entry in the
 129   
      *      directory.
 130   
      */
 131  0
     private Vector getDirectoryEntries(File directory) {
 132  0
         Vector files = new Vector();
 133   
 
 134   
         // File[] filesInDir = directory.listFiles();
 135  0
         String[] filesInDir = directory.list();
 136   
 
 137  0
         if (filesInDir != null) {
 138  0
             int length = filesInDir.length;
 139   
 
 140  0
             for (int i = 0; i < length; ++i) {
 141  0
                 files.addElement(new File(directory, filesInDir[i]));
 142   
             }
 143   
         }
 144   
 
 145  0
         return files;
 146   
     }
 147   
 
 148   
     /**
 149   
      * Template method to allow subclasses to supply elements for the
 150   
      * iteration. The directory iterator maintains a stack of iterators
 151   
      * covering each level in the directory hierarchy. The current iterator
 152   
      * covers the current directory being scanned. If the next entry in that
 153   
      * directory is a subdirectory, the current iterator is pushed onto the
 154   
      * stack and a new iterator is created for the subdirectory. If the
 155   
      * entry is a file, it is returned as the next element and the iterator
 156   
      * remains valid. If there are no more entries in the current directory,
 157   
      * the topmost iterator on the statck is popped off to become the
 158   
      * current iterator.
 159   
      *
 160   
      * @return the next ClassFile in the iteration.
 161   
      */
 162  0
     public ClassFile getNextClassFile() {
 163  0
         ClassFile nextElement = null;
 164   
 
 165  0
         try {
 166  0
             while (nextElement == null) {
 167  0
                 if (currentEnum.hasMoreElements()) {
 168  0
                     File element = (File) currentEnum.nextElement();
 169   
 
 170  0
                     if (element.isDirectory()) {
 171   
 
 172   
                         // push the current iterator onto the stack and then
 173   
                         // iterate through this directory.
 174  0
                         enumStack.push(currentEnum);
 175   
 
 176  0
                         Vector files = getDirectoryEntries(element);
 177   
 
 178  0
                         currentEnum = files.elements();
 179   
                     } else {
 180   
 
 181   
                         // we have a file. create a stream for it
 182  0
                         FileInputStream inFileStream 
 183   
                             = new FileInputStream(element);
 184   
 
 185  0
                         if (element.getName().endsWith(".class")) {
 186   
 
 187   
                             // create a data input stream from the jar 
 188   
                             // input stream
 189  0
                             ClassFile javaClass = new ClassFile();
 190   
 
 191  0
                             javaClass.read(inFileStream);
 192   
 
 193  0
                             nextElement = javaClass;
 194   
                         }
 195   
                     }
 196   
                 } else {
 197   
                     // this iterator is exhausted. Can we pop one off the stack
 198  0
                     if (enumStack.empty()) {
 199  0
                         break;
 200   
                     } else {
 201  0
                         currentEnum = (Enumeration) enumStack.pop();
 202   
                     }
 203   
                 }
 204   
             }
 205   
         } catch (IOException e) {
 206  0
             nextElement = null;
 207   
         }
 208   
 
 209  0
         return nextElement;
 210   
     }
 211   
 
 212   
 }
 213   
 
 214