| 1 18 package org.apache.tools.ant.types.resources; 19 20 import java.io.File ; 21 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.types.Resource; 24 import org.apache.tools.ant.types.ResourceCollection; 25 import org.apache.tools.ant.types.Reference; 26 27 31 public abstract class ArchiveResource extends Resource { 32 private static final int NULL_ARCHIVE 33 = Resource.getMagicNumber("null archive".getBytes()); 34 35 private Resource archive; 36 private boolean haveEntry = false; 37 private boolean modeSet = false; 38 private int mode = 0; 39 40 43 public ArchiveResource() { 44 } 45 46 51 public ArchiveResource(File a) { 52 this(a, false); 53 } 54 55 61 public ArchiveResource(File a, boolean withEntry) { 62 setArchive(a); 63 haveEntry = withEntry; 64 } 65 66 72 public ArchiveResource(Resource a, boolean withEntry) { 73 addConfigured(a); 74 haveEntry = withEntry; 75 } 76 77 81 public void setArchive(File a) { 82 checkAttributesAllowed(); 83 archive = new FileResource(a); 84 } 85 86 90 public void setMode(int mode) { 91 checkAttributesAllowed(); 92 this.mode = mode; 93 modeSet = true; 94 } 95 96 101 public void addConfigured(ResourceCollection a) { 102 checkChildrenAllowed(); 103 if (archive != null) { 104 throw new BuildException("you must not specify more than one" 105 + " archive"); 106 } 107 if (a.size() != 1) { 108 throw new BuildException("only single argument resource collections" 109 + " are supported as archives"); 110 } 111 archive = (Resource) a.iterator().next(); 112 } 113 114 118 public Resource getArchive() { 119 return isReference() 120 ? ((ArchiveResource) getCheckedRef()).getArchive() : archive; 121 } 122 123 127 public long getLastModified() { 128 if (isReference()) { 129 return ((Resource) getCheckedRef()).getLastModified(); 130 } 131 checkEntry(); 132 return super.getLastModified(); 133 } 134 135 139 public long getSize() { 140 if (isReference()) { 141 return ((Resource) getCheckedRef()).getSize(); 142 } 143 checkEntry(); 144 return super.getSize(); 145 } 146 147 151 public boolean isDirectory() { 152 if (isReference()) { 153 return ((Resource) getCheckedRef()).isDirectory(); 154 } 155 checkEntry(); 156 return super.isDirectory(); 157 } 158 159 163 public boolean isExists() { 164 if (isReference()) { 165 return ((Resource) getCheckedRef()).isExists(); 166 } 167 checkEntry(); 168 return super.isExists(); 169 } 170 171 175 public int getMode() { 176 if (isReference()) { 177 return ((ArchiveResource) getCheckedRef()).getMode(); 178 } 179 checkEntry(); 180 return mode; 181 } 182 183 187 public void setRefid(Reference r) { 188 if (archive != null || modeSet) { 189 throw tooManyAttributes(); 190 } 191 super.setRefid(r); 192 } 193 194 200 public int compareTo(Object another) { 201 return this.equals(another) ? 0 : super.compareTo(another); 202 } 203 204 210 public boolean equals(Object another) { 211 if (this == another) { 212 return true; 213 } 214 if (isReference()) { 215 return getCheckedRef().equals(another); 216 } 217 if (!(another.getClass().equals(getClass()))) { 218 return false; 219 } 220 ArchiveResource r = (ArchiveResource) another; 221 return getArchive().equals(r.getArchive()) 222 && getName().equals(r.getName()); 223 } 224 225 229 public int hashCode() { 230 return super.hashCode() 231 * (getArchive() == null ? NULL_ARCHIVE : getArchive().hashCode()); 232 } 233 234 238 public String toString() { 239 return isReference() ? getCheckedRef().toString() 240 : getArchive().toString() + ':' + getName(); 241 } 242 243 private synchronized void checkEntry() throws BuildException { 244 if (haveEntry) { 245 return; 246 } 247 String name = getName(); 248 if (name == null) { 249 throw new BuildException("entry name not set"); 250 } 251 Resource r = getArchive(); 252 if (r == null) { 253 throw new BuildException("archive attribute not set"); 254 } 255 if (!r.isExists()) { 256 throw new BuildException(r.toString() + " does not exist."); 257 } 258 if (r.isDirectory()) { 259 throw new BuildException(r + " denotes a directory."); 260 } 261 fetchEntry(); 262 haveEntry = true; 263 } 264 265 268 protected abstract void fetchEntry(); 269 } 270 | Popular Tags |