1 17 18 package net.sourceforge.groboutils.codecoverage.v2.ant.zip; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.Arrays ; 23 import java.util.Vector ; 24 import java.util.Hashtable ; 25 import java.util.Enumeration ; 26 import java.util.zip.ZipException ; 27 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.DirectoryScanner; 30 33 43 public class ZipScanner extends DirectoryScanner { 44 45 48 protected File srcFile; 49 52 private Resource lastScannedResource; 53 56 private Hashtable myentries; 57 58 63 private String encoding; 64 65 71 public void setSrc(File srcFile) { 72 this.srcFile = srcFile; 73 } 74 75 80 public void setEncoding(String encoding) { 81 this.encoding = encoding; 82 } 83 84 92 public String [] getIncludedFiles() { 93 if (srcFile != null) { 94 Vector myvector = new Vector (); 95 scanme(); 97 for (Enumeration e = myentries.elements(); e.hasMoreElements();) { 98 Resource myresource = (Resource) e.nextElement(); 99 if (!myresource.isDirectory() && match(myresource.getName())) { 100 myvector.addElement(myresource.getName()); 101 } 102 } 103 String [] files = new String [myvector.size()]; 104 myvector.copyInto(files); 105 Arrays.sort(files); 106 return files; 107 } else { 108 return super.getIncludedFiles(); 109 } 110 } 111 112 120 public String [] getIncludedDirectories() { 121 if (srcFile != null) { 122 Vector myvector = new Vector (); 123 scanme(); 125 for (Enumeration e = myentries.elements(); e.hasMoreElements();) { 126 Resource myresource = (Resource) e.nextElement(); 127 if (myresource.isDirectory() && match(myresource.getName())) { 128 myvector.addElement(myresource.getName()); 129 } 130 } 131 String [] files = new String [myvector.size()]; 132 myvector.copyInto(files); 133 Arrays.sort(files); 134 return files; 135 } else { 136 return super.getIncludedDirectories(); 137 } 138 } 139 140 143 public void init() { 144 if (includes == null) { 145 includes = new String [1]; 147 includes[0] = "**"; 148 } 149 if (excludes == null) { 150 excludes = new String [0]; 151 } 152 } 153 154 163 public boolean match(String path) { 164 String vpath = path.replace('/', File.separatorChar). 165 replace('\\', File.separatorChar); 166 return isIncluded(vpath) && !isExcluded(vpath); 167 } 168 169 176 private void scanme() { 177 Resource thisresource = new Resource(srcFile.getAbsolutePath(), 178 srcFile.exists(), 179 srcFile.lastModified()); 180 181 if (lastScannedResource != null 183 && lastScannedResource.getName().equals(thisresource.getName()) 184 && lastScannedResource.getLastModified() 185 == thisresource.getLastModified()) { 186 return; 187 } 188 189 ZipEntry entry = null; 190 ZipFile zf = null; 191 myentries = new Hashtable (); 192 try { 193 try { 194 zf = new ZipFile(srcFile, encoding); 195 } catch (ZipException ex) { 196 throw new BuildException("problem reading " + srcFile, ex); 197 } catch (IOException ex) { 198 throw new BuildException("problem opening " + srcFile, ex); 199 } 200 201 Enumeration e = zf.getEntries(); 202 while (e.hasMoreElements()) { 203 entry = (ZipEntry) e.nextElement(); 204 myentries.put(new String (entry.getName()), 205 new Resource(entry.getName(), true, 206 entry.getTime(), 207 entry.isDirectory())); 208 } 209 } finally { 210 if (zf != null) { 211 try { 212 zf.close(); 213 } catch (IOException ex) { 214 } 216 } 217 } 218 lastScannedResource = thisresource; 220 } 221 } 222 | Popular Tags |