1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.PrintStream ; 23 import java.io.OutputStream ; 24 import java.util.Iterator ; 25 26 import org.apache.tools.ant.Task; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.taskdefs.condition.Condition; 30 import org.apache.tools.ant.types.FileSet; 31 import org.apache.tools.ant.types.Resource; 32 import org.apache.tools.ant.types.Comparison; 33 import org.apache.tools.ant.types.ResourceCollection; 34 import org.apache.tools.ant.types.EnumeratedAttribute; 35 import org.apache.tools.ant.types.resources.Resources; 36 import org.apache.tools.ant.types.resources.FileResource; 37 import org.apache.tools.ant.util.PropertyOutputStream; 38 39 44 public class Length extends Task implements Condition { 45 46 private static final String ALL = "all"; 47 private static final String EACH = "each"; 48 private static final String STRING = "string"; 49 50 private static final String LENGTH_REQUIRED 51 = "Use of the Length condition requires that the length attribute be set."; 52 53 private String property; 54 private String string; 55 private Boolean trim; 56 private String mode = ALL; 57 private Comparison when = Comparison.EQUAL; 58 private Long length; 59 private Resources resources; 60 61 65 public synchronized void setProperty(String property) { 66 this.property = property; 67 } 68 69 73 public synchronized void setFile(File file) { 74 add(new FileResource(file)); 75 } 76 77 81 public synchronized void add(FileSet fs) { 82 add((ResourceCollection) fs); 83 } 84 85 90 public synchronized void add(ResourceCollection c) { 91 if (c == null) { 92 return; 93 } 94 resources = (resources == null) ? new Resources() : resources; 95 resources.add(c); 96 } 97 98 102 public synchronized void setLength(long ell) { 103 length = new Long (ell); 104 } 105 106 111 public synchronized void setWhen(When w) { 112 setWhen((Comparison) w); 113 } 114 115 121 public synchronized void setWhen(Comparison c) { 122 when = c; 123 } 124 125 129 public synchronized void setMode(FileMode m) { 130 this.mode = m.getValue(); 131 } 132 133 137 public synchronized void setString(String string) { 138 this.string = string; 139 this.mode = STRING; 140 } 141 142 146 public synchronized void setTrim(boolean trim) { 147 this.trim = trim ? Boolean.TRUE : Boolean.FALSE; 148 } 149 150 154 public boolean getTrim() { 155 return trim != null && trim.booleanValue(); 156 } 157 158 161 public void execute() { 162 validate(); 163 PrintStream ps = new PrintStream ((property != null) 164 ? (OutputStream ) new PropertyOutputStream(getProject(), property) 165 : (OutputStream ) new LogOutputStream(this, Project.MSG_INFO)); 166 167 if (STRING.equals(mode)) { 168 ps.print(getLength(string, getTrim())); 169 ps.close(); 170 } else if (EACH.equals(mode)) { 171 handleResources(new EachHandler(ps)); 172 } else if (ALL.equals(mode)) { 173 handleResources(new AllHandler(ps)); 174 } 175 } 176 177 182 public boolean eval() { 183 validate(); 184 if (length == null) { 185 throw new BuildException(LENGTH_REQUIRED); 186 } 187 Long ell = null; 188 if (STRING.equals(mode)) { 189 ell = new Long (getLength(string, getTrim())); 190 } else { 191 ConditionHandler h = new ConditionHandler(); 192 handleResources(h); 193 ell = new Long (h.getLength()); 194 } 195 return when.evaluate(ell.compareTo(length)); 196 } 197 198 private void validate() { 199 if (string != null) { 200 if (resources != null) { 201 throw new BuildException("the string length function" 202 + " is incompatible with the file/resource length function"); 203 } 204 if (!(STRING.equals(mode))) { 205 throw new BuildException("the mode attribute is for use" 206 + " with the file/resource length function"); 207 } 208 } else if (resources != null) { 209 if (!(EACH.equals(mode) || ALL.equals(mode))) { 210 throw new BuildException("invalid mode setting for" 211 + " file/resource length function: \"" + mode + "\""); 212 } else if (trim != null) { 213 throw new BuildException("the trim attribute is" 214 + " for use with the string length function only"); 215 } 216 } else { 217 throw new BuildException("you must set either the string attribute" 218 + " or specify one or more files using the file attribute or" 219 + " nested resource collections"); 220 } 221 } 222 223 private void handleResources(Handler h) { 224 for (Iterator i = resources.iterator(); i.hasNext();) { 225 Resource r = (Resource) i.next(); 226 if (!r.isExists()) { 227 log(r + " does not exist", Project.MSG_ERR); 228 } else if (r.isDirectory()) { 229 log(r + " is a directory; length unspecified", 230 Project.MSG_ERR); 231 } else { 232 h.handle(r); 233 } 234 } 235 h.complete(); 236 } 237 238 private static long getLength(String s, boolean t) { 239 return (t ? s.trim() : s).length(); 240 } 241 242 243 public static class FileMode extends EnumeratedAttribute { 244 static final String [] MODES = new String [] {EACH, ALL}; 245 246 250 public String [] getValues() { 251 return MODES; 252 } 253 254 } 255 256 259 public static class When extends Comparison { 260 } 262 263 private abstract class Handler { 264 private PrintStream ps; 265 Handler(PrintStream ps) { 266 this.ps = ps; 267 } 268 269 protected PrintStream getPs() { 270 return ps; 271 } 272 273 protected abstract void handle(Resource r); 274 275 void complete() { 276 ps.close(); 277 } 278 } 279 280 private class EachHandler extends Handler { 281 EachHandler(PrintStream ps) { 282 super(ps); 283 } 284 protected void handle(Resource r) { 285 getPs().print(r.toString()); 286 getPs().print(" : "); 287 long size = r.getSize(); 289 if (size == Resource.UNKNOWN_SIZE) { 290 getPs().println("unknown"); 291 } else { 292 getPs().println(size); 293 } 294 } 295 } 296 297 private class AllHandler extends Handler { 298 private long accum = 0L; 299 AllHandler(PrintStream ps) { 300 super(ps); 301 } 302 protected long getAccum() { 303 return accum; 304 } 305 protected synchronized void handle(Resource r) { 306 long size = r.getSize(); 307 if (size == Resource.UNKNOWN_SIZE) { 308 log("Size unknown for " + r.toString(), Project.MSG_WARN); 309 } else { 310 accum += size; 311 } 312 } 313 void complete() { 314 getPs().print(accum); 315 super.complete(); 316 } 317 } 318 319 private class ConditionHandler extends AllHandler { 320 ConditionHandler() { 321 super(null); 322 } 323 void complete() { 324 } 325 long getLength() { 326 return getAccum(); 327 } 328 } 329 } 330 | Popular Tags |