1 28 29 package com.idaremedia.antx.mktemp; 30 31 import java.io.File ; 32 import java.io.FileOutputStream ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 import org.apache.tools.ant.BuildException; 37 import org.apache.tools.ant.Project; 38 import org.apache.tools.ant.types.FileSet; 39 40 import com.idaremedia.antx.AntX; 41 import com.idaremedia.antx.AntXFixture; 42 import com.idaremedia.antx.AssertableTask; 43 import com.idaremedia.antx.helpers.InnerString; 44 import com.idaremedia.antx.ownhelpers.FileSetsIterator; 45 import com.idaremedia.antx.parameters.FeedbackLevel; 46 import com.idaremedia.antx.parameters.RecoveryEnabled; 47 48 83 84 public class TruncateFileTask extends AssertableTask 85 implements RecoveryEnabled 86 { 87 88 91 public TruncateFileTask() 92 { 93 super(AntX.mktemp+"TruncateFileTask:"); 94 } 95 96 97 101 protected TruncateFileTask(String iam) 102 { 103 super(iam); 104 } 105 106 107 110 public void init() 111 { 112 super.init(); 113 m_filerefs = AntXFixture.newList(); 114 } 115 116 117 118 125 public void setFeedback(String level) 126 { 127 require_(level!=null,"setFeedback- nonzro level"); 128 FeedbackLevel fbl = FeedbackLevel.from(level); 129 if (fbl==null) { 130 String e = getAntXMsg("task.illegal.param.value", 131 getTaskName(), level,"feedback"); 132 log(e, Project.MSG_ERR); 133 throw new BuildException(e, getLocation()); 134 } 135 m_fbLevel = fbl; 136 } 137 138 139 140 144 public final FeedbackLevel getFeedbackLevel() 145 { 146 return m_fbLevel; 147 } 148 149 150 151 157 public void setHaltIfError(boolean halt) 158 { 159 m_haltIfError= halt; 160 } 161 162 163 168 public boolean isHaltIfError() 169 { 170 return m_haltIfError; 171 } 172 173 174 179 public void setMustExist(boolean must) 180 { 181 m_mustExist = must; 182 } 183 184 185 189 public boolean getMustExist() 190 { 191 return m_mustExist; 192 } 193 194 195 200 public void setCreateIfMissing(boolean create) 201 { 202 m_createIfMissing = create; 203 } 204 205 206 207 211 public final boolean willCreateIfMissing() 212 { 213 return m_createIfMissing; 214 } 215 216 217 218 222 public void setFile(String filepath) 223 { 224 require_(filepath!=null,"setFile- nonzro path"); 225 m_filerefs.add(getProject().resolveFile(filepath)); 226 } 227 228 229 230 234 public void setProjectFile(File file) 235 { 236 require_(file!=null,"setFile- nonzro file"); 237 m_filerefs.add(file); 238 } 239 240 241 242 246 public void addConfiguredFiles(FileSet fs) 247 { 248 require_(fs!=null,"addFiles- nonzro set"); 249 m_filerefs.add(fs); 250 } 251 252 253 254 258 public void addConfiguredFile(InnerString path) 259 { 260 require_(path!=null,"addFile- nonzro path"); 261 m_filerefs.add(getProject().resolveFile(path.toString(getProject()))); 262 } 263 264 265 272 public final Iterator getPaths() 273 { 274 if (m_filerefs.isEmpty()) { 275 return null; 276 } 277 return new FileSetsIterator(m_filerefs,getProject()); 278 } 279 280 281 282 287 protected void verifyCanExecute_(String calr) 288 { 289 verifyInProject_(calr); 290 291 if (m_filerefs.isEmpty()) { 292 String error = getAntXMsg("task.needs.oneof.these.nested", 293 getTaskName(),"files|file"); 294 log(error,Project.MSG_ERR); 295 throw new BuildException(error,getLocation()); 296 } 297 } 298 299 300 301 307 public void execute() 308 { 309 verifyCanExecute_("exec"); 310 311 Iterator itr = getPaths(); 312 313 while (itr.hasNext()) { 314 File f= new File (itr.next().toString()); 315 String problem = truncateFile(f); 316 if (problem!=null) { 317 throw new BuildException(problem,getLocation()); 318 } 319 } 320 } 321 322 323 324 330 protected String truncateFile(File f) 331 { 332 boolean notThere= !f.exists(); 333 334 if (notThere) { 335 if (getMustExist()) { 336 String error = getAntXMsg("task.err.filenotfound",f.getPath()); 337 log(error,Project.MSG_ERR); 338 return error; 339 } 340 else if (!willCreateIfMissing()) { 341 return null; } 343 } 344 try { 345 if (!FeedbackLevel.isQuietish(m_fbLevel,true)) { 346 log("Trying: "+f.getPath(), Project.MSG_VERBOSE); 347 } 348 if (notThere) { 349 f.createNewFile(); 350 } else { 351 if (f.length()!=0) { 352 FileOutputStream fos = new FileOutputStream (f); 353 fos.write(new byte[0]); fos.close(); 355 } 356 } 357 } 358 catch(Exception anyX) { 359 String problem = getAntXMsg("mktemp.truncfile.cant", 360 f.getPath(), anyX.getMessage()); 361 if (isHaltIfError()) { 362 log(problem,Project.MSG_ERR); 363 return problem; 364 } 365 log(problem, Project.MSG_WARN); 366 } 367 return null; 368 } 369 370 371 private FeedbackLevel m_fbLevel=FeedbackLevel.NORMAL; 372 private boolean m_haltIfError; private boolean m_mustExist; private boolean m_createIfMissing; private List m_filerefs; 376 } 377 378 379 | Popular Tags |