KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > ant > zip > ZipScanner


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package net.sourceforge.groboutils.codecoverage.v2.ant.zip;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.zip.ZipException JavaDoc;
27
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.DirectoryScanner;
30 //import org.apache.tools.zip.ZipEntry;
31
//import org.apache.tools.zip.ZipFile;
32

33 /**
34  * ZipScanner accesses the pattern matching algorithm in DirectoryScanner,
35  * which are protected methods that can only be accessed by subclassing.
36  *
37  * This implementation of FileScanner defines getIncludedFiles to return
38  * the matching Zip entries.
39  *
40  * @author Don Ferguson <a HREF="mailto:don@bea.com">don@bea.com</a>
41  * @author <a HREF="mailto:levylambert@tiscali-dsl.de">Antoine Levy-Lambert</a>
42  */

43 public class ZipScanner extends DirectoryScanner {
44
45     /**
46      * The zip file which should be scanned.
47      */

48     protected File JavaDoc srcFile;
49     /**
50      * to record the last scanned zip file with its modification date
51      */

52     private Resource lastScannedResource;
53     /**
54      * record list of all zip entries
55      */

56     private Hashtable JavaDoc myentries;
57
58     /**
59      * encoding of file names.
60      *
61      * @since Ant 1.6
62      */

63     private String JavaDoc encoding;
64
65     /**
66      * Sets the srcFile for scanning. This is the jar or zip file that
67      * is scanned for matching entries.
68      *
69      * @param srcFile the (non-null) zip file name for scanning
70      */

71     public void setSrc(File JavaDoc srcFile) {
72         this.srcFile = srcFile;
73     }
74
75     /**
76      * Sets encoding of file names.
77      *
78      * @since Ant 1.6
79      */

80     public void setEncoding(String JavaDoc encoding) {
81         this.encoding = encoding;
82     }
83
84     /**
85      * Returns the names of the files which matched at least one of the
86      * include patterns and none of the exclude patterns.
87      * The names are relative to the base directory.
88      *
89      * @return the names of the files which matched at least one of the
90      * include patterns and none of the exclude patterns.
91      */

92     public String JavaDoc[] getIncludedFiles() {
93         if (srcFile != null) {
94             Vector JavaDoc myvector = new Vector JavaDoc();
95             // first check if the archive needs to be scanned again
96
scanme();
97             for (Enumeration JavaDoc 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 JavaDoc[] files = new String JavaDoc[myvector.size()];
104             myvector.copyInto(files);
105             Arrays.sort(files);
106             return files;
107         } else {
108             return super.getIncludedFiles();
109         }
110     }
111
112     /**
113      * Returns the names of the directories which matched at least one of the
114      * include patterns and none of the exclude patterns.
115      * The names are relative to the base directory.
116      *
117      * @return the names of the directories which matched at least one of the
118      * include patterns and none of the exclude patterns.
119      */

120     public String JavaDoc[] getIncludedDirectories() {
121         if (srcFile != null) {
122             Vector JavaDoc myvector = new Vector JavaDoc();
123             // first check if the archive needs to be scanned again
124
scanme();
125             for (Enumeration JavaDoc 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 JavaDoc[] files = new String JavaDoc[myvector.size()];
132             myvector.copyInto(files);
133             Arrays.sort(files);
134             return files;
135         } else {
136             return super.getIncludedDirectories();
137         }
138     }
139
140     /**
141      * Initialize DirectoryScanner data structures.
142      */

143     public void init() {
144         if (includes == null) {
145             // No includes supplied, so set it to 'matches all'
146
includes = new String JavaDoc[1];
147             includes[0] = "**";
148         }
149         if (excludes == null) {
150             excludes = new String JavaDoc[0];
151         }
152     }
153
154     /**
155      * Matches a jar entry against the includes/excludes list,
156      * normalizing the path separator.
157      *
158      * @param path the (non-null) path name to test for inclusion
159      *
160      * @return <code>true</code> if the path should be included
161      * <code>false</code> otherwise.
162      */

163     public boolean match(String JavaDoc path) {
164         String JavaDoc vpath = path.replace('/', File.separatorChar).
165             replace('\\', File.separatorChar);
166         return isIncluded(vpath) && !isExcluded(vpath);
167     }
168
169     /**
170      * if the datetime of the archive did not change since
171      * lastScannedResource was initialized returns immediately else if
172      * the archive has not been scanned yet, then all the zip entries
173      * are put into the vector myentries as a vector of the resource
174      * type
175      */

176     private void scanme() {
177         Resource thisresource = new Resource(srcFile.getAbsolutePath(),
178                                              srcFile.exists(),
179                                              srcFile.lastModified());
180
181         // spare scanning again and again
182
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 JavaDoc();
192         try {
193             try {
194                 zf = new ZipFile(srcFile, encoding);
195             } catch (ZipException JavaDoc ex) {
196                 throw new BuildException("problem reading " + srcFile, ex);
197             } catch (IOException JavaDoc ex) {
198                 throw new BuildException("problem opening " + srcFile, ex);
199             }
200
201             Enumeration JavaDoc e = zf.getEntries();
202             while (e.hasMoreElements()) {
203                 entry = (ZipEntry) e.nextElement();
204                 myentries.put(new String JavaDoc(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 JavaDoc ex) {
214                     // swallow
215
}
216             }
217         }
218         // record data about the last scanned resource
219
lastScannedResource = thisresource;
220     }
221 }
222
Popular Tags