1 18 package org.apache.tools.ant.types; 19 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.math.BigInteger ; 24 import java.util.Iterator ; 25 import java.util.NoSuchElementException ; 26 27 37 public class Resource extends DataType 38 implements Cloneable , Comparable , ResourceCollection { 39 40 41 public static final long UNKNOWN_SIZE = -1; 42 43 44 public static final long UNKNOWN_DATETIME = 0L; 45 46 47 protected static final int MAGIC = getMagicNumber("Resource".getBytes()); 48 49 private static final int NULL_NAME = getMagicNumber("null name".getBytes()); 50 51 56 protected static int getMagicNumber(byte[] seed) { 57 return new BigInteger (seed).intValue(); 58 } 59 60 private String name = null; 61 private Boolean exists = null; 62 private Long lastmodified = null; 63 private Boolean directory = null; 64 private Long size = null; 65 66 69 public Resource() { 70 } 71 72 80 public Resource(String name) { 81 this(name, false, 0, false); 82 } 83 84 92 public Resource(String name, boolean exists, long lastmodified) { 93 this(name, exists, lastmodified, false); 94 } 95 96 105 public Resource(String name, boolean exists, long lastmodified, 106 boolean directory) { 107 this(name, exists, lastmodified, directory, UNKNOWN_SIZE); 108 } 109 110 120 public Resource(String name, boolean exists, long lastmodified, 121 boolean directory, long size) { 122 this.name = name; 123 setName(name); 124 setExists(exists); 125 setLastModified(lastmodified); 126 setDirectory(directory); 127 setSize(size); 128 } 129 130 142 public String getName() { 143 return isReference() ? ((Resource) getCheckedRef()).getName() : name; 144 } 145 146 151 public void setName(String name) { 152 checkAttributesAllowed(); 153 this.name = name; 154 } 155 156 160 public boolean isExists() { 161 if (isReference()) { 162 return ((Resource) getCheckedRef()).isExists(); 163 } 164 return exists == null || exists.booleanValue(); 166 } 167 168 172 public void setExists(boolean exists) { 173 checkAttributesAllowed(); 174 this.exists = exists ? Boolean.TRUE : Boolean.FALSE; 175 } 176 177 183 public long getLastModified() { 184 if (isReference()) { 185 return ((Resource) getCheckedRef()).getLastModified(); 186 } 187 if (!isExists() || lastmodified == null) { 188 return UNKNOWN_DATETIME; 189 } 190 long result = lastmodified.longValue(); 191 return result < UNKNOWN_DATETIME ? UNKNOWN_DATETIME : result; 192 } 193 194 198 public void setLastModified(long lastmodified) { 199 checkAttributesAllowed(); 200 this.lastmodified = new Long (lastmodified); 201 } 202 203 207 public boolean isDirectory() { 208 if (isReference()) { 209 return ((Resource) getCheckedRef()).isDirectory(); 210 } 211 return directory != null && directory.booleanValue(); 213 } 214 215 219 public void setDirectory(boolean directory) { 220 checkAttributesAllowed(); 221 this.directory = directory ? Boolean.TRUE : Boolean.FALSE; 222 } 223 224 229 public void setSize(long size) { 230 checkAttributesAllowed(); 231 this.size = new Long (size > UNKNOWN_SIZE ? size : UNKNOWN_SIZE); 232 } 233 234 240 public long getSize() { 241 if (isReference()) { 242 return ((Resource) getCheckedRef()).getSize(); 243 } 244 return isExists() 245 ? (size != null ? size.longValue() : UNKNOWN_SIZE) 246 : 0L; 247 } 248 249 253 public Object clone() { 254 try { 255 return super.clone(); 256 } catch (CloneNotSupportedException e) { 257 throw new UnsupportedOperationException ( 258 "CloneNotSupportedException for a Resource caught. " 259 + "Derived classes must support cloning."); 260 } 261 } 262 263 270 public int compareTo(Object other) { 271 if (isReference()) { 272 return ((Comparable ) getCheckedRef()).compareTo(other); 273 } 274 if (!(other instanceof Resource)) { 275 throw new IllegalArgumentException ( 276 "Can only be compared with Resources"); 277 } 278 return toString().compareTo(other.toString()); 279 } 280 281 287 public boolean equals(Object other) { 288 if (isReference()) { 289 return getCheckedRef().equals(other); 290 } 291 return other.getClass().equals(getClass()) && compareTo(other) == 0; 292 } 293 294 299 public int hashCode() { 300 if (isReference()) { 301 return getCheckedRef().hashCode(); 302 } 303 String name = getName(); 304 return MAGIC * (name == null ? NULL_NAME : name.hashCode()); 305 } 306 307 316 public InputStream getInputStream() throws IOException { 317 if (isReference()) { 318 return ((Resource) getCheckedRef()).getInputStream(); 319 } 320 throw new UnsupportedOperationException (); 321 } 322 323 332 public OutputStream getOutputStream() throws IOException { 333 if (isReference()) { 334 return ((Resource) getCheckedRef()).getOutputStream(); 335 } 336 throw new UnsupportedOperationException (); 337 } 338 339 344 public Iterator iterator() { 345 return isReference() ? ((Resource) getCheckedRef()).iterator() 346 : new Iterator () { 347 private boolean done = false; 348 public boolean hasNext() { 349 return !done; 350 } 351 public Object next() { 352 if (done) { 353 throw new NoSuchElementException (); 354 } 355 done = true; 356 return Resource.this; 357 } 358 public void remove() { 359 throw new UnsupportedOperationException (); 360 } 361 }; 362 } 363 364 369 public int size() { 370 return isReference() ? ((Resource) getCheckedRef()).size() : 1; 371 } 372 373 378 public boolean isFilesystemOnly() { 379 return isReference() && ((Resource) getCheckedRef()).isFilesystemOnly(); 381 } 382 383 388 public String toString() { 389 if (isReference()) { 390 return getCheckedRef().toString(); 391 } 392 String n = getName(); 393 return n == null ? "(anonymous)" : n; 394 } 395 396 403 public final String toLongString() { 404 return isReference() ? ((Resource) getCheckedRef()).toLongString() 405 : getDataTypeName() + " \"" + toString() + '"'; 406 } 407 408 412 public void setRefid(Reference r) { 413 if (name != null 414 || exists != null 415 || lastmodified != null 416 || directory != null 417 || size != null) { 418 throw tooManyAttributes(); 419 } 420 super.setRefid(r); 421 } 422 423 } 424 | Popular Tags |