1 18 19 package org.apache.tools.ant.taskdefs; 20 21 22 import java.io.File ; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.Task; 25 import org.apache.tools.ant.types.Resource; 26 import org.apache.tools.ant.types.ResourceCollection; 27 import org.apache.tools.ant.types.resources.FileResource; 28 29 34 35 public abstract class Unpack extends Task { 36 protected File source; 38 protected File dest; 39 protected Resource srcResource; 40 42 51 public void setSrc(String src) { 52 log("DEPRECATED - The setSrc(String) method has been deprecated." 53 + " Use setSrc(File) instead."); 54 setSrc(getProject().resolveFile(src)); 55 } 56 57 66 public void setDest(String dest) { 67 log("DEPRECATED - The setDest(String) method has been deprecated." 68 + " Use setDest(File) instead."); 69 setDest(getProject().resolveFile(dest)); 70 } 71 72 76 public void setSrc(File src) { 77 setSrcResource(new FileResource(src)); 78 } 79 80 84 public void setSrcResource(Resource src) { 85 if (!src.isExists()) { 86 throw new BuildException("the archive doesn't exist"); 87 } 88 if (src.isDirectory()) { 89 throw new BuildException("the archive can't be a directory"); 90 } 91 if (src instanceof FileResource) { 92 source = ((FileResource) src).getFile(); 93 } else if (!supportsNonFileResources()) { 94 throw new BuildException("Only FileSystem resources are" 95 + " supported."); 96 } 97 srcResource = src; 98 } 99 100 104 public void addConfigured(ResourceCollection a) { 105 if (a.size() != 1) { 106 throw new BuildException("only single argument resource collections" 107 + " are supported as archives"); 108 } 109 setSrcResource((Resource) a.iterator().next()); 110 } 111 112 116 public void setDest(File dest) { 117 this.dest = dest; 118 } 119 120 private void validate() throws BuildException { 121 if (srcResource == null) { 122 throw new BuildException("No Src specified", getLocation()); 123 } 124 125 if (dest == null) { 126 dest = new File (source.getParent()); 127 } 128 129 if (dest.isDirectory()) { 130 String defaultExtension = getDefaultExtension(); 131 createDestFile(defaultExtension); 132 } 133 } 134 135 private void createDestFile(String defaultExtension) { 136 String sourceName = source.getName(); 137 int len = sourceName.length(); 138 if (defaultExtension != null 139 && len > defaultExtension.length() 140 && defaultExtension.equalsIgnoreCase( 141 sourceName.substring(len - defaultExtension.length()))) { 142 dest = new File (dest, sourceName.substring(0, 143 len - defaultExtension.length())); 144 } else { 145 dest = new File (dest, sourceName); 146 } 147 } 148 149 153 public void execute() throws BuildException { 154 File savedDest = dest; try { 156 validate(); 157 extract(); 158 } finally { 159 dest = savedDest; 160 } 161 } 162 163 168 protected abstract String getDefaultExtension(); 169 170 174 protected abstract void extract(); 175 176 183 protected boolean supportsNonFileResources() { 184 return false; 185 } 186 187 } 188 | Popular Tags |