source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/ZipScanner.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 8.2 KB
Line 
1/*
2 * Copyright 2001-2005 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
18package org.apache.tools.ant.types;
19
20import java.io.File;
21import java.io.IOException;
22import java.util.Arrays;
23import java.util.Vector;
24import java.util.Hashtable;
25import java.util.Enumeration;
26import java.util.zip.ZipException;
27
28import org.apache.tools.ant.BuildException;
29import org.apache.tools.ant.DirectoryScanner;
30import org.apache.tools.zip.ZipEntry;
31import 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 */
41public class ZipScanner extends DirectoryScanner {
42
43 /**
44 * The zip file which should be scanned.
45 */
46 protected File srcFile;
47 /**
48 * to record the last scanned zip file with its modification date
49 */
50 private Resource lastScannedResource;
51 /**
52 * record list of all zip entries
53 */
54 private Hashtable myentries;
55
56 /**
57 * encoding of file names.
58 *
59 * @since Ant 1.6
60 */
61 private String encoding;
62
63 /**
64 * Sets the srcFile for scanning. This is the jar or zip file that
65 * is scanned for matching entries.
66 *
67 * @param srcFile the (non-null) zip file name for scanning
68 */
69 public void setSrc(File srcFile) {
70 this.srcFile = srcFile;
71 }
72
73 /**
74 * Sets encoding of file names.
75 *
76 * @since Ant 1.6
77 */
78 public void setEncoding(String encoding) {
79 this.encoding = encoding;
80 }
81
82 /**
83 * Returns the names of the files which matched at least one of the
84 * include patterns and none of the exclude patterns.
85 * The names are relative to the base directory.
86 *
87 * @return the names of the files which matched at least one of the
88 * include patterns and none of the exclude patterns.
89 */
90 public String[] getIncludedFiles() {
91 if (srcFile != null) {
92 Vector myvector = new Vector();
93 // first check if the archive needs to be scanned again
94 scanme();
95 for (Enumeration e = myentries.elements(); e.hasMoreElements();) {
96 Resource myresource = (Resource) e.nextElement();
97 if (!myresource.isDirectory() && match(myresource.getName())) {
98 myvector.addElement(myresource.getName());
99 }
100 }
101 String[] files = new String[myvector.size()];
102 myvector.copyInto(files);
103 Arrays.sort(files);
104 return files;
105 } else {
106 return super.getIncludedFiles();
107 }
108 }
109
110 /**
111 * Returns the names of the directories which matched at least one of the
112 * include patterns and none of the exclude patterns.
113 * The names are relative to the base directory.
114 *
115 * @return the names of the directories which matched at least one of the
116 * include patterns and none of the exclude patterns.
117 */
118 public String[] getIncludedDirectories() {
119 if (srcFile != null) {
120 Vector myvector = new Vector();
121 // first check if the archive needs to be scanned again
122 scanme();
123 for (Enumeration e = myentries.elements(); e.hasMoreElements();) {
124 Resource myresource = (Resource) e.nextElement();
125 if (myresource.isDirectory() && match(myresource.getName())) {
126 myvector.addElement(myresource.getName());
127 }
128 }
129 String[] files = new String[myvector.size()];
130 myvector.copyInto(files);
131 Arrays.sort(files);
132 return files;
133 } else {
134 return super.getIncludedDirectories();
135 }
136 }
137
138 /**
139 * Initialize DirectoryScanner data structures.
140 */
141 public void init() {
142 if (includes == null) {
143 // No includes supplied, so set it to 'matches all'
144 includes = new String[1];
145 includes[0] = "**";
146 }
147 if (excludes == null) {
148 excludes = new String[0];
149 }
150 }
151
152 /**
153 * Matches a jar entry against the includes/excludes list,
154 * normalizing the path separator.
155 *
156 * @param path the (non-null) path name to test for inclusion
157 *
158 * @return <code>true</code> if the path should be included
159 * <code>false</code> otherwise.
160 */
161 public boolean match(String path) {
162 String vpath = path.replace('/', File.separatorChar).
163 replace('\\', File.separatorChar);
164 return isIncluded(vpath) && !isExcluded(vpath);
165 }
166
167 /**
168 * @param name path name of the file sought in the archive
169 *
170 * @since Ant 1.5.2
171 */
172 public Resource getResource(String name) {
173 if (srcFile == null) {
174 return super.getResource(name);
175 } else if (name.equals("")) {
176 // special case in ZIPs, we do not want this thing included
177 return new Resource("", true, Long.MAX_VALUE, true);
178 }
179
180 // first check if the archive needs to be scanned again
181 scanme();
182 if (myentries.containsKey(name)) {
183 return (Resource) myentries.get(name);
184 } else if (myentries.containsKey(name + "/")) {
185 return (Resource) myentries.get(name + "/");
186 } else {
187 return new Resource(name);
188 }
189 }
190
191 /**
192 * if the datetime of the archive did not change since
193 * lastScannedResource was initialized returns immediately else if
194 * the archive has not been scanned yet, then all the zip entries
195 * are put into the vector myentries as a vector of the resource
196 * type
197 */
198 private void scanme() {
199 Resource thisresource = new Resource(srcFile.getAbsolutePath(),
200 srcFile.exists(),
201 srcFile.lastModified());
202
203 // spare scanning again and again
204 if (lastScannedResource != null
205 && lastScannedResource.getName().equals(thisresource.getName())
206 && lastScannedResource.getLastModified()
207 == thisresource.getLastModified()) {
208 return;
209 }
210
211 ZipEntry entry = null;
212 ZipFile zf = null;
213 myentries = new Hashtable();
214 try {
215 try {
216 zf = new ZipFile(srcFile, encoding);
217 } catch (ZipException ex) {
218 throw new BuildException("problem reading " + srcFile, ex);
219 } catch (IOException ex) {
220 throw new BuildException("problem opening " + srcFile, ex);
221 }
222
223 Enumeration e = zf.getEntries();
224 while (e.hasMoreElements()) {
225 entry = (ZipEntry) e.nextElement();
226 myentries.put(new String(entry.getName()),
227 new Resource(entry.getName(), true,
228 entry.getTime(),
229 entry.isDirectory(),
230 entry.getSize()));
231 }
232 } finally {
233 if (zf != null) {
234 try {
235 zf.close();
236 } catch (IOException ex) {
237 // swallow
238 }
239 }
240 }
241 // record data about the last scanned resource
242 lastScannedResource = thisresource;
243 }
244}
Note: See TracBrowser for help on using the repository browser.