| 1 18 package org.apache.tools.ant.types.resources; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.util.FileUtils; 30 import org.apache.tools.ant.types.Resource; 31 import org.apache.tools.ant.types.Reference; 32 33 37 public class FileResource extends Resource implements Touchable { 38 39 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 40 private static final int NULL_FILE 41 = Resource.getMagicNumber("null file".getBytes()); 42 43 private File file; 44 private File baseDir; 45 46 49 public FileResource() { 50 } 51 52 57 public FileResource(File b, String name) { 58 setFile(FILE_UTILS.resolveFile(b, name)); 59 setBaseDir(b); 60 } 61 62 66 public FileResource(File f) { 67 setFile(f); 68 } 69 70 76 public FileResource(Project p, String s) { 77 this(p.resolveFile(s)); 78 setProject(p); 79 } 80 81 85 public void setFile(File f) { 86 checkAttributesAllowed(); 87 file = f; 88 } 89 90 94 public File getFile() { 95 return isReference() ? ((FileResource) getCheckedRef()).getFile() : file; 96 } 97 98 102 public void setBaseDir(File b) { 103 checkAttributesAllowed(); 104 baseDir = b; 105 } 106 107 111 public File getBaseDir() { 112 return isReference() 113 ? ((FileResource) getCheckedRef()).getBaseDir() : baseDir; 114 } 115 116 120 public void setRefid(Reference r) { 121 if (file != null || baseDir != null) { 122 throw tooManyAttributes(); 123 } 124 super.setRefid(r); 125 } 126 127 133 public String getName() { 134 if (isReference()) { 135 return ((Resource) getCheckedRef()).getName(); 136 } 137 File b = getBaseDir(); 138 return b == null ? getNotNullFile().getName() 139 : FILE_UTILS.removeLeadingPath(b, getNotNullFile()); 140 } 141 142 146 public boolean isExists() { 147 return isReference() ? ((Resource) getCheckedRef()).isExists() 148 : getNotNullFile().exists(); 149 } 150 151 155 public long getLastModified() { 156 return isReference() 157 ? ((Resource) getCheckedRef()).getLastModified() 158 : getNotNullFile().lastModified(); 159 } 160 161 165 public boolean isDirectory() { 166 return isReference() ? ((Resource) getCheckedRef()).isDirectory() 167 : getNotNullFile().isDirectory(); 168 } 169 170 174 public long getSize() { 175 return isReference() ? ((Resource) getCheckedRef()).getSize() 176 : getNotNullFile().length(); 177 } 178 179 184 public InputStream getInputStream() throws IOException { 185 return isReference() 186 ? ((Resource) getCheckedRef()).getInputStream() 187 : new FileInputStream (getNotNullFile()); 188 } 189 190 198 public OutputStream getOutputStream() throws IOException { 199 if (isReference()) { 200 return ((Resource) getCheckedRef()).getOutputStream(); 201 } 202 File f = getNotNullFile(); 203 if (f.exists()) { 204 if (f.isFile()) { 205 f.delete(); 206 } 207 } else { 208 File p = f.getParentFile(); 209 if (p != null && !(p.exists())) { 210 p.mkdirs(); 211 } 212 } 213 return new FileOutputStream (f); 214 } 215 216 222 public int compareTo(Object another) { 223 if (isReference()) { 224 return ((Comparable ) getCheckedRef()).compareTo(another); 225 } 226 if (this.equals(another)) { 227 return 0; 228 } 229 if (another.getClass().equals(getClass())) { 230 FileResource otherfr = (FileResource) another; 231 File f = getFile(); 232 if (f == null) { 233 return -1; 234 } 235 File of = otherfr.getFile(); 236 if (of == null) { 237 return 1; 238 } 239 return f.compareTo(of); 240 } 241 return super.compareTo(another); 242 } 243 244 249 public boolean equals(Object another) { 250 if (this == another) { 251 return true; 252 } 253 if (isReference()) { 254 return getCheckedRef().equals(another); 255 } 256 if (!(another.getClass().equals(getClass()))) { 257 return false; 258 } 259 FileResource otherfr = (FileResource) another; 260 return getFile() == null 261 ? otherfr.getFile() == null 262 : getFile().equals(otherfr.getFile()); 263 } 264 265 269 public int hashCode() { 270 if (isReference()) { 271 return getCheckedRef().hashCode(); 272 } 273 return MAGIC * (getFile() == null ? NULL_FILE : getFile().hashCode()); 274 } 275 276 280 public String toString() { 281 if (isReference()) { 282 return getCheckedRef().toString(); 283 } 284 if (file == null) { 285 return "(unbound file resource)"; 286 } 287 String absolutePath = file.getAbsolutePath(); 288 return FILE_UTILS.normalize(absolutePath).getAbsolutePath(); 289 } 290 291 295 public boolean isFilesystemOnly() { 296 return !isReference() 297 || ((FileResource) getCheckedRef()).isFilesystemOnly(); 298 } 299 300 304 public void touch(long modTime) { 305 if (isReference()) { 306 ((FileResource) getCheckedRef()).touch(modTime); 307 return; 308 } 309 getNotNullFile().setLastModified(modTime); 310 } 311 312 317 protected File getNotNullFile() { 318 if (getFile() == null) { 319 throw new BuildException("file attribute is null!"); 320 } 321 return getFile(); 322 } 323 324 } 325 | Popular Tags |