1 28 29 package com.idaremedia.antx.mktemp; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.Project; 36 import org.apache.tools.ant.util.FileUtils; 37 38 import com.idaremedia.antx.AntX; 39 import com.idaremedia.antx.ExportedProperties; 40 import com.idaremedia.antx.helpers.Tk; 41 42 61 62 public abstract class MkTempObject extends MkNewObject 63 { 64 65 protected static final String DEFAULT_PREFIX= "qat"; 66 67 68 71 protected MkTempObject() 72 { 73 super(AntX.mktemp); 74 } 75 76 77 80 protected MkTempObject(String iam) 81 { 82 super(iam); 83 } 84 85 86 91 protected char getFillerChar() 92 { 93 return '_'; 94 } 95 96 97 103 protected final String getDefaultPrefix(boolean forFile) 104 { 105 String pfx = Tk.getTheProperty 106 (getProject(), AntX.DEFAULT_TEMPOBJECT_PREFIX_PROP); 107 108 return (pfx!=null) ? pfx : MkTempObject.DEFAULT_PREFIX; 109 } 110 111 112 117 public void setPrefix(String prefix) 118 { 119 require_(prefix!=null,"setPfx- nonzro pfx"); 120 m_prefix = prefix; 121 } 122 123 124 128 public String getPrefix() 129 { 130 return m_prefix; 131 } 132 133 134 140 public final String getEffectivePrefix(boolean forFile) 141 { 142 String pfx = getPrefix(); 143 if (pfx!=null && pfx.length()<3) { 144 char filler = getFillerChar(); 145 for (int i=3-pfx.length();i>0;i--) { 146 pfx += filler; 147 } 148 } 149 return (pfx!=null) ? pfx : getDefaultPrefix(forFile); 150 } 151 152 153 158 public void setSuffix(String suffix) 159 { 160 require_(suffix!=null,"setSfx- nonzro sfx"); 161 m_suffix = suffix; 162 } 163 164 165 169 public String getSuffix() 170 { 171 return m_suffix; 172 } 173 174 175 181 public final String getEffectiveSuffix(boolean forFile) 182 { 183 String sfx = getSuffix(); 184 if (sfx!=null && forFile) { 185 if (sfx.length()>0 && sfx.charAt(0)!='.') { 186 sfx = "."+sfx; 187 } 188 } 189 return (sfx!=null) ? sfx : (forFile ? ".tmp" : ""); 190 } 191 192 193 199 public void setPathProperty(String property) 200 { 201 require_(property!=null,"setProp- nonzro nam"); 202 m_updateProperty = property; 203 } 204 205 206 210 public final String getPathProperty() 211 { 212 return m_updateProperty; 213 } 214 215 216 222 public void setPathVariable(String variable) 223 { 224 require_(variable!=null,"setVar- nonzro nam"); 225 m_updateVar = variable; 226 } 227 228 229 234 public final String getPathVariable() 235 { 236 return m_updateVar; 237 } 238 239 240 245 public void setIn(File inDir) 246 { 247 require_(inDir!=null,"setIn- nonzro dir"); 248 require_(inDir.canWrite(), "setIn- existing writable directory"); 249 m_inDir= inDir; 250 } 251 252 253 258 public File getInDir() 259 { 260 return m_inDir; 261 } 262 263 264 269 public void setPersist(boolean leaveOnExit) 270 { 271 m_autoDelete = !leaveOnExit; 272 } 273 274 275 279 public boolean isAutoDelete() 280 { 281 return m_autoDelete; 282 } 283 284 285 292 protected final File createFile(File inDir) throws BuildException 293 { 294 File newFile=null; 295 FileUtils fsu= getFileUtils(); 296 297 try { 298 newFile = fsu.createTempFile 304 (getEffectivePrefix(true),getEffectiveSuffix(true),inDir); 305 306 if (getPrototypePlainFile()!=null) { 307 fsu.copyFile(getPrototypePlainFile(),newFile, 308 getCopyFilters(),false); 309 } 310 else if (getPrototypeResourceFile()!=null) { 311 fsu.copyFile(getPrototypeResourceFile(),newFile, 312 getCopyFilters(),false); 313 } else { 314 newFile.createNewFile(); 315 copyPrototypeLines(newFile,false); 316 } 317 } catch(IOException iox) { 318 throw new BuildException(iox, getLocation()); 319 } 320 321 if (isAutoDelete()) { 322 newFile.deleteOnExit(); 323 } 324 return newFile; 325 } 326 327 328 335 protected final File createDirectory(File inDir) throws BuildException 336 { 337 File newDir=null; 338 FileUtils fsu= getFileUtils(); 339 340 try { 341 newDir = fsu.createTempFile 342 (getEffectivePrefix(false),getEffectiveSuffix(false),inDir); 343 344 newDir.mkdir(); 345 346 if (!newDir.isDirectory() || !newDir.canWrite()) { 347 String ermsg = uistrs().get 348 ("mktemp.cant.make.tmpdir", 349 inDir.getPath(), newDir.getName()); 350 log(ermsg, Project.MSG_ERR); 351 throw new BuildException(ermsg,getLocation()); 352 } 353 354 if (getPrototypePlainFile()!=null) { 355 copyPrototypeFile(getPrototypePlainFile(), newDir); 356 } 357 else if (getPrototypeResourceFile()!=null) { 358 copyPrototypeFile(getPrototypeResourceFile(), newDir); 359 } 360 361 } catch(IOException iox) { 362 throw new BuildException(iox, getLocation()); 363 } 364 return newDir; 365 } 366 367 368 369 373 protected void saveFinalPath(File finalObject, boolean strict) 374 { 375 String finalPath = finalObject.getPath(); 376 if (getPathProperty()!=null) { 377 checkIfProperty_(getPathProperty(),!strict); 378 getProject().setNewProperty(getPathProperty(),finalPath); 379 } 380 if (getPathVariable()!=null) { 381 ExportedProperties.set(getPathVariable(),finalPath); 382 } 383 384 super.saveFinalPath(finalObject,strict); 385 } 386 387 388 private String m_prefix = null; private String m_suffix = null; private String m_updateProperty; 391 private String m_updateVar; 392 private File m_inDir= TempLocator.getSystemTempDir(); 393 private boolean m_autoDelete=true; 394 } 395 396 397 | Popular Tags |