1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.util.Date ; 27 import java.util.Enumeration ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Set ; 31 import java.util.Vector ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.Task; 36 import org.apache.tools.ant.types.FileSet; 37 import org.apache.tools.ant.types.Mapper; 38 import org.apache.tools.ant.types.PatternSet; 39 import org.apache.tools.ant.types.Resource; 40 import org.apache.tools.ant.types.ResourceCollection; 41 import org.apache.tools.ant.types.resources.FileResource; 42 import org.apache.tools.ant.types.resources.Union; 43 import org.apache.tools.ant.types.selectors.SelectorUtils; 44 import org.apache.tools.ant.util.FileNameMapper; 45 import org.apache.tools.ant.util.FileUtils; 46 import org.apache.tools.ant.util.IdentityMapper; 47 import org.apache.tools.zip.ZipEntry; 48 import org.apache.tools.zip.ZipFile; 49 50 60 public class Expand extends Task { 61 private File dest; private File source; private boolean overwrite = true; 64 private Mapper mapperElement = null; 65 private Vector patternsets = new Vector (); 66 private Union resources = new Union(); 67 private boolean resourcesSpecified = false; 68 69 private static final String NATIVE_ENCODING = "native-encoding"; 70 71 private String encoding = "UTF8"; 72 73 public static final String ERROR_MULTIPLE_MAPPERS = "Cannot define more than one mapper"; 74 75 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 76 77 82 public void execute() throws BuildException { 83 if ("expand".equals(getTaskType())) { 84 log("!! expand is deprecated. Use unzip instead. !!"); 85 } 86 87 if (source == null && !resourcesSpecified) { 88 throw new BuildException("src attribute and/or resources must be " 89 + "specified"); 90 } 91 92 if (dest == null) { 93 throw new BuildException( 94 "Dest attribute must be specified"); 95 } 96 97 if (dest.exists() && !dest.isDirectory()) { 98 throw new BuildException("Dest must be a directory.", getLocation()); 99 } 100 101 if (source != null) { 102 if (source.isDirectory()) { 103 throw new BuildException("Src must not be a directory." 104 + " Use nested filesets instead.", getLocation()); 105 } else { 106 expandFile(FILE_UTILS, source, dest); 107 } 108 } 109 Iterator iter = resources.iterator(); 110 while (iter.hasNext()) { 111 Resource r = (Resource) iter.next(); 112 if (!r.isExists()) { 113 continue; 114 } 115 116 if (r instanceof FileResource) { 117 expandFile(FILE_UTILS, ((FileResource) r).getFile(), dest); 118 } else { 119 expandResource(r, dest); 120 } 121 } 122 } 123 124 131 protected void expandFile(FileUtils fileUtils, File srcF, File dir) { 132 log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO); 133 ZipFile zf = null; 134 FileNameMapper mapper = getMapper(); 135 try { 136 zf = new ZipFile(srcF, encoding); 137 Enumeration e = zf.getEntries(); 138 while (e.hasMoreElements()) { 139 ZipEntry ze = (ZipEntry) e.nextElement(); 140 extractFile(fileUtils, srcF, dir, zf.getInputStream(ze), 141 ze.getName(), new Date (ze.getTime()), 142 ze.isDirectory(), mapper); 143 } 144 145 log("expand complete", Project.MSG_VERBOSE); 146 } catch (IOException ioe) { 147 throw new BuildException("Error while expanding " + srcF.getPath(), 148 ioe); 149 } finally { 150 ZipFile.closeQuietly(zf); 151 } 152 } 153 154 160 protected void expandResource(Resource srcR, File dir) { 161 throw new BuildException("only filesystem based resources are" 162 + " supported by this task."); 163 } 164 165 169 protected FileNameMapper getMapper() { 170 FileNameMapper mapper = null; 171 if (mapperElement != null) { 172 mapper = mapperElement.getImplementation(); 173 } else { 174 mapper = new IdentityMapper(); 175 } 176 return mapper; 177 } 178 179 191 protected void extractFile(FileUtils fileUtils, File srcF, File dir, 192 InputStream compressedInputStream, 193 String entryName, Date entryDate, 194 boolean isDirectory, FileNameMapper mapper) 195 throws IOException { 196 197 if (patternsets != null && patternsets.size() > 0) { 198 String name = entryName.replace('/', File.separatorChar) 199 .replace('\\', File.separatorChar); 200 boolean included = false; 201 Set includePatterns = new HashSet (); 202 Set excludePatterns = new HashSet (); 203 for (int v = 0, size = patternsets.size(); v < size; v++) { 204 PatternSet p = (PatternSet) patternsets.elementAt(v); 205 String [] incls = p.getIncludePatterns(getProject()); 206 if (incls == null || incls.length == 0) { 207 incls = new String [] {"**"}; 209 } 210 211 for (int w = 0; w < incls.length; w++) { 212 String pattern = incls[w].replace('/', File.separatorChar) 213 .replace('\\', File.separatorChar); 214 if (pattern.endsWith(File.separator)) { 215 pattern += "**"; 216 } 217 includePatterns.add(pattern); 218 } 219 220 String [] excls = p.getExcludePatterns(getProject()); 221 if (excls != null) { 222 for (int w = 0; w < excls.length; w++) { 223 String pattern = excls[w] 224 .replace('/', File.separatorChar) 225 .replace('\\', File.separatorChar); 226 if (pattern.endsWith(File.separator)) { 227 pattern += "**"; 228 } 229 excludePatterns.add(pattern); 230 } 231 } 232 } 233 234 for (Iterator iter = includePatterns.iterator(); 235 !included && iter.hasNext();) { 236 String pattern = (String ) iter.next(); 237 included = SelectorUtils.matchPath(pattern, name); 238 } 239 240 for (Iterator iter = excludePatterns.iterator(); 241 included && iter.hasNext();) { 242 String pattern = (String ) iter.next(); 243 included = !SelectorUtils.matchPath(pattern, name); 244 } 245 246 if (!included) { 247 return; 249 } 250 } 251 String [] mappedNames = mapper.mapFileName(entryName); 252 if (mappedNames == null || mappedNames.length == 0) { 253 mappedNames = new String [] {entryName}; 254 } 255 File f = fileUtils.resolveFile(dir, mappedNames[0]); 256 try { 257 if (!overwrite && f.exists() 258 && f.lastModified() >= entryDate.getTime()) { 259 log("Skipping " + f + " as it is up-to-date", 260 Project.MSG_DEBUG); 261 return; 262 } 263 264 log("expanding " + entryName + " to " + f, 265 Project.MSG_VERBOSE); 266 File dirF = f.getParentFile(); 268 if (dirF != null) { 269 dirF.mkdirs(); 270 } 271 272 if (isDirectory) { 273 f.mkdirs(); 274 } else { 275 byte[] buffer = new byte[1024]; 276 int length = 0; 277 FileOutputStream fos = null; 278 try { 279 fos = new FileOutputStream (f); 280 281 while ((length = 282 compressedInputStream.read(buffer)) >= 0) { 283 fos.write(buffer, 0, length); 284 } 285 286 fos.close(); 287 fos = null; 288 } finally { 289 FileUtils.close(fos); 290 } 291 } 292 293 fileUtils.setFileLastModified(f, entryDate.getTime()); 294 } catch (FileNotFoundException ex) { 295 log("Unable to expand to file " + f.getPath(), Project.MSG_WARN); 296 } 297 298 } 299 300 306 public void setDest(File d) { 307 this.dest = d; 308 } 309 310 315 public void setSrc(File s) { 316 this.source = s; 317 } 318 319 324 public void setOverwrite(boolean b) { 325 overwrite = b; 326 } 327 328 332 public void addPatternset(PatternSet set) { 333 patternsets.addElement(set); 334 } 335 336 340 public void addFileset(FileSet set) { 341 add(set); 342 } 343 344 349 public void add(ResourceCollection rc) { 350 resourcesSpecified = true; 351 resources.add(rc); 352 } 353 354 360 public Mapper createMapper() throws BuildException { 361 if (mapperElement != null) { 362 throw new BuildException(ERROR_MULTIPLE_MAPPERS, 363 getLocation()); 364 } 365 mapperElement = new Mapper(getProject()); 366 return mapperElement; 367 } 368 369 374 public void add(FileNameMapper fileNameMapper) { 375 createMapper().add(fileNameMapper); 376 } 377 378 379 387 public void setEncoding(String encoding) { 388 if (NATIVE_ENCODING.equals(encoding)) { 389 encoding = null; 390 } 391 this.encoding = encoding; 392 } 393 394 } 395 | Popular Tags |