1 28 29 package com.idaremedia.antx.mktemp; 30 31 import java.lang.reflect.Constructor ; 32 import java.io.File ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 import java.io.PrintWriter ; 36 import java.net.URL ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.Properties ; 40 41 import org.apache.tools.ant.BuildException; 42 import org.apache.tools.ant.Project; 43 import org.apache.tools.ant.types.FilterSet; 44 import org.apache.tools.ant.types.FilterSetCollection; 45 import org.apache.tools.ant.types.PropertySet; 46 import org.apache.tools.ant.util.FileUtils; 47 48 import com.idaremedia.antx.AntXFixture; 49 import com.idaremedia.antx.AssertableTask; 50 import com.idaremedia.antx.Iteration; 51 import com.idaremedia.antx.helpers.Tk; 52 import com.idaremedia.antx.ownhelpers.LocalTk; 53 import com.idaremedia.antx.starters.StringItemListHandle; 54 55 66 67 public abstract class MkNewObject extends AssertableTask 68 { 69 72 protected MkNewObject(String iam) 73 { 74 super(iam); 75 } 76 77 78 84 public void setUrlProperty(String urlproperty) 85 { 86 require_(urlproperty!=null,"setUrlProp- nonzro name"); 87 m_updateUrlProperty = urlproperty; 88 } 89 90 91 92 98 public final String getUrlPathProperty() 99 { 100 return m_updateUrlProperty; 101 } 102 103 104 109 public boolean isAutoDelete() 110 { 111 return false; 112 } 113 114 115 120 public void setCopyFile(File copyFrom) 121 { 122 require_(copyFrom!=null,"setCopy- nonzro src file"); 123 require_(copyFrom.canRead(), "setCopy- existing readable file/dir"); 124 m_copyFromFile= copyFrom; 125 } 126 127 128 132 public File getPrototypePlainFile() 133 { 134 return m_copyFromFile; 135 } 136 137 138 143 public void setCopyResource(String copyFrom) 144 { 145 require_(!Tk.isWhitespace(copyFrom),"setCopyRez- nonzro rez name"); 146 m_copyFromResource = copyFrom; 147 } 148 149 150 154 public String getPrototypeResource() 155 { 156 return m_copyFromResource; 157 } 158 159 160 167 public File getPrototypeResourceFile() 168 { 169 if (m_copyFromRezFile==null && getPrototypeResource()!=null) { 170 File rezfile = getFileFromResource(getPrototypeResource()); 171 m_copyFromRezFile = rezfile; 172 } 173 return m_copyFromRezFile; 174 } 175 176 177 181 public void addFilterSet(FilterSet filters) 182 { 183 require_(filters!=null,"addFiltrs- nonzro filtrs"); 184 getCopyFiltersNoNull().addFilterSet(filters); 185 } 186 187 188 193 protected final boolean needToCopyFile() 194 { 195 return (getPrototypePlainFile()!=null || 196 getPrototypeResourceFile()!=null); 197 } 198 199 200 205 protected final File getFileFromResource(String resource) 206 throws BuildException 207 { 208 require_(resource!=null,"rezToFile- nonzro rez"); 209 210 File rezfile = null; 211 URL url = LocalTk.getSystemResource(resource,getProject()); 212 if (url!=null) { 213 rezfile = Tk.toFile(url); 214 if (rezfile!=null && !rezfile.canRead()) { 215 rezfile=null; 216 } 217 } 218 if (rezfile==null) { 219 String ermsg = uistrs().get("mktemp.cant.read.resource",resource); 220 log(ermsg, Project.MSG_ERR); 221 throw new BuildException(ermsg,getLocation()); 222 } 223 return rezfile; 224 } 225 226 227 236 protected File copyPrototypeFile(File fromFile, File inDir) 237 throws IOException 238 { 239 File toFile = new File (inDir, fromFile.getName()); 240 241 getFileUtils().copyFile(fromFile,toFile,getCopyFilters(),false); 242 243 if (isAutoDelete()) { 244 toFile.deleteOnExit(); 245 } 246 return toFile; 247 } 248 249 250 251 254 protected final FileUtils getFileUtils() 255 { 256 return AntXFixture.fileUtils(); 257 } 258 259 260 265 protected final FilterSetCollection getCopyFilters() 266 { 267 return m_copyFilters; 268 } 269 270 271 276 protected final FilterSetCollection getCopyFiltersNoNull() 277 { 278 if (m_copyFilters==null) { 279 m_copyFilters = new FilterSetCollection(); 280 } 281 return m_copyFilters; 282 } 283 284 285 292 protected List getPrototypeLinesNoNull() 293 { 294 if (m_prototypeLines==null) { 295 m_prototypeLines = AntXFixture.newList(); 296 } 297 return m_prototypeLines; 298 } 299 300 301 306 protected List getPrototypeLines() 307 { 308 return m_prototypeLines; 309 } 310 311 312 321 protected void copyPrototypeLines(File newFile, boolean append) 322 { 323 List l = getPrototypeLines(); 324 if (l!=null && !l.isEmpty()) { 325 try { 326 final Project P = getProject(); 327 FileOutputStream fos = newFOS(newFile,append); 328 PrintWriter w = new PrintWriter (fos); 329 for (int i=0,N=l.size();i<N;i++) { 330 Object o = l.get(i); 331 if (o instanceof StringItemListHandle) { 332 Iterator itr= ((StringItemListHandle)o).readonlyStringIterator(P); 333 while (itr.hasNext()) { 334 w.println(itr.next().toString()); 335 } 336 } else if (o instanceof PropertySet) { 337 Properties ps = ((PropertySet)o).getProperties(); 338 if (!ps.isEmpty()) { 339 w.flush(); 340 ps.store(fos,null); 341 } 342 ps.clear(); 343 ps=null; } else { 345 w.println(Iteration.lenientStringifer().stringFrom(o,P)); 346 } 347 } 348 w.flush(); 349 w.close(); 350 } catch(IOException iox) { 351 throw new BuildException(iox,getLocation()); 352 } 353 } 354 } 355 356 357 358 364 protected void saveFinalPath(File finalObject, boolean strict) 365 { 366 if (getUrlPathProperty()!=null) { 367 checkIfProperty_(getUrlPathProperty(),!strict); 368 String urlPath = AntXFixture.fileUtils().toURI(finalObject.getPath()); 369 getProject().setNewProperty(getUrlPathProperty(),urlPath); 370 } 371 } 372 373 374 375 379 private FileOutputStream newFOS(File newFile, boolean append) 380 throws IOException 381 { 382 if (append) { 383 try { 384 Class [] sig = new Class []{File .class,boolean.class}; 385 Constructor ctor = FileOutputStream .class.getConstructor(sig); 386 Object [] args = new Object []{newFile, (append ? Boolean.TRUE : Boolean.FALSE)}; 387 return (FileOutputStream )ctor.newInstance(args); 388 } catch(Exception anyX) {} 389 } 390 return new FileOutputStream (newFile); 391 } 392 393 private File m_copyFromFile, m_copyFromRezFile; 394 private String m_copyFromResource; 395 private FilterSetCollection m_copyFilters; 396 private List m_prototypeLines; 397 private String m_updateUrlProperty; 398 } 399 400 401 | Popular Tags |