1 18 19 package org.apache.tools.ant.types; 20 21 import java.io.File ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import java.util.TreeMap ; 25 import java.util.Iterator ; 26 27 import org.apache.tools.ant.DirectoryScanner; 28 import org.apache.tools.ant.types.resources.FileResource; 29 import org.apache.tools.ant.types.resources.FileResourceIterator; 30 31 40 public abstract class ArchiveScanner extends DirectoryScanner { 41 43 46 protected File srcFile; 47 48 50 53 private Resource src; 54 55 58 private Resource lastScannedResource; 59 60 63 private TreeMap fileEntries = new TreeMap (); 64 65 68 private TreeMap dirEntries = new TreeMap (); 69 70 73 private TreeMap matchFileEntries = new TreeMap (); 74 75 78 private TreeMap matchDirEntries = new TreeMap (); 79 80 85 private String encoding; 86 87 91 public void scan() { 92 if (src == null) { 93 return; 94 } 95 super.scan(); 96 } 97 98 104 public void setSrc(File srcFile) { 105 setSrc(new FileResource(srcFile)); 106 } 107 108 114 public void setSrc(Resource src) { 115 this.src = src; 116 if (src instanceof FileResource) { 117 srcFile = ((FileResource) src).getFile(); 118 } 119 } 120 121 126 public void setEncoding(String encoding) { 127 this.encoding = encoding; 128 } 129 130 138 public String [] getIncludedFiles() { 139 if (src == null) { 140 return super.getIncludedFiles(); 141 } 142 scanme(); 143 Set s = matchFileEntries.keySet(); 144 return (String []) (s.toArray(new String [s.size()])); 145 } 146 147 152 public int getIncludedFilesCount() { 153 if (src == null) { 154 return super.getIncludedFilesCount(); 155 } 156 scanme(); 157 return matchFileEntries.size(); 158 } 159 160 168 public String [] getIncludedDirectories() { 169 if (src == null) { 170 return super.getIncludedDirectories(); 171 } 172 scanme(); 173 Set s = matchDirEntries.keySet(); 174 return (String []) (s.toArray(new String [s.size()])); 175 } 176 177 182 public int getIncludedDirsCount() { 183 if (src == null) { 184 return super.getIncludedDirsCount(); 185 } 186 scanme(); 187 return matchDirEntries.size(); 188 } 189 190 195 Iterator getResourceFiles() { 196 if (src == null) { 197 return new FileResourceIterator(getBasedir(), getIncludedFiles()); 198 } 199 scanme(); 200 return matchFileEntries.values().iterator(); 201 } 202 203 208 Iterator getResourceDirectories() { 209 if (src == null) { 210 return new FileResourceIterator(getBasedir(), getIncludedDirectories()); 211 } 212 scanme(); 213 return matchDirEntries.values().iterator(); 214 } 215 216 219 public void init() { 220 if (includes == null) { 221 includes = new String [1]; 223 includes[0] = "**"; 224 } 225 if (excludes == null) { 226 excludes = new String [0]; 227 } 228 } 229 230 239 public boolean match(String path) { 240 String vpath = path.replace('/', File.separatorChar). 241 replace('\\', File.separatorChar); 242 return isIncluded(vpath) && !isExcluded(vpath); 243 } 244 245 251 public Resource getResource(String name) { 252 if (src == null) { 253 return super.getResource(name); 254 } 255 if (name.equals("")) { 256 return new Resource("", true, Long.MAX_VALUE, true); 258 } 259 scanme(); 261 if (fileEntries.containsKey(name)) { 262 return (Resource) fileEntries.get(name); 263 } 264 name = trimSeparator(name); 265 266 if (dirEntries.containsKey(name)) { 267 return (Resource) dirEntries.get(name); 268 } 269 return new Resource(name); 270 } 271 272 288 protected abstract void fillMapsFromArchive(Resource archive, 289 String encoding, 290 Map fileEntries, 291 Map matchFileEntries, 292 Map dirEntries, 293 Map matchDirEntries); 294 295 301 private void scanme() { 302 Resource thisresource = new Resource(src.getName(), 304 src.isExists(), 305 src.getLastModified()); 306 if (lastScannedResource != null 308 && lastScannedResource.getName().equals(thisresource.getName()) 309 && lastScannedResource.getLastModified() 310 == thisresource.getLastModified()) { 311 return; 312 } 313 init(); 314 315 fileEntries.clear(); 316 dirEntries.clear(); 317 matchFileEntries.clear(); 318 matchDirEntries.clear(); 319 fillMapsFromArchive(src, encoding, fileEntries, matchFileEntries, 320 dirEntries, matchDirEntries); 321 322 lastScannedResource = thisresource; 324 } 325 326 331 protected static final String trimSeparator(String s) { 332 return s.endsWith("/") ? s.substring(0, s.length() - 1) : s; 333 } 334 335 } 336 | Popular Tags |