1 28 29 package com.idaremedia.antx.ownhelpers; 30 31 import java.lang.reflect.Constructor ; 32 import java.lang.reflect.Method ; 33 import java.io.File ; 34 import java.io.InputStream ; 35 import java.net.URL ; 36 37 import org.apache.tools.ant.BuildEvent; 38 import org.apache.tools.ant.BuildException; 39 import org.apache.tools.ant.DirectoryScanner; 40 import org.apache.tools.ant.Location; 41 import org.apache.tools.ant.Project; 42 import org.apache.tools.ant.launch.Locator; 43 import org.apache.tools.ant.types.Reference; 44 import org.apache.tools.ant.util.ClasspathUtils; 45 import org.apache.tools.ant.util.FileUtils; 46 47 import com.idaremedia.antx.AntX; 48 import com.idaremedia.antx.AntXFixture; 49 import com.idaremedia.antx.Iteration; 50 import com.idaremedia.antx.apis.ScriptLocatable; 51 import com.idaremedia.antx.helpers.Tk; 52 53 63 64 public final class LocalTk 65 { 66 private static final String IAM_= AntX.AntX+"LocalTk:"; 67 68 69 78 public static final Reference referenceFor(String refid, Project project) 79 { 80 return new Reference(refid); 81 } 82 83 84 92 public static final Reference referenceFor(Reference ref) 93 { 94 return referenceFor(ref.getRefId(),null); 95 } 96 97 98 99 102 private static final String ANTX_SYSLOADER_ = 103 AntX.ANTX_INTERNAL_ID+"SystemResourcesClassLoader"; 104 105 106 116 public static final URL getSystemResource(String resource, Project P) 117 { 118 AntX.require_(P!=null,IAM_,"getSysRez- nonzro project"); 119 AntX.require_(resource!=null,IAM_,"getSysRez- nonzro resource"); 120 121 ClassLoader cL = ClasspathUtils.getClassLoaderForPath 122 (P, null, ANTX_SYSLOADER_, false, true); 123 124 return cL.getResource(resource); 125 } 126 127 128 129 136 public static final InputStream getSystemResourceAsStream 137 (String resource, Project P) 138 { 139 AntX.require_(P!=null,IAM_,"getSysRez- nonzro project"); 140 AntX.require_(resource!=null,IAM_,"getSysRez- nonzro resource"); 141 142 ClassLoader cL = ClasspathUtils.getClassLoaderForPath 143 (P, null, ANTX_SYSLOADER_, false, true); 144 145 return cL.getResourceAsStream(resource); 146 } 147 148 149 150 159 public static final File getSystemResourceSource(String resource, Project P) 160 { 161 URL url = getSystemResource(resource,P); 162 return getFilesystemSource(url,resource,P); 163 } 164 165 166 167 175 public static final URL getResource(String resource, String pathref, 176 Project project) 177 { 178 if (pathref==null) { 179 return getSystemResource(resource,project); 180 } 181 AntX.require_(project!=null,IAM_,"getRez- nonzro project"); 182 AntX.require_(resource!=null,IAM_,"getRez- nonzro resource"); 183 184 ClassLoader cL = ClasspathUtils.getClassLoaderForPath 185 (project, referenceFor(pathref, project), false); 186 return cL.getResource(resource); 187 } 188 189 190 191 199 public static final InputStream getResourceAsStream(String resource, 200 String pathref, Project project) 201 { 202 if (pathref==null) { 203 return getSystemResourceAsStream(resource,project); 204 } 205 AntX.require_(project!=null,IAM_,"getRez- nonzro project"); 206 AntX.require_(resource!=null,IAM_,"getRez- nonzro resource"); 207 208 ClassLoader cL = ClasspathUtils.getClassLoaderForPath 209 (project, referenceFor(pathref,project), false); 210 return cL.getResourceAsStream(resource); 211 } 212 213 214 215 223 public static final File getResourceSource(String resource, 224 String pathref, Project project) 225 { 226 URL url = getResource(resource, pathref, project); 227 return getFilesystemSource(url, resource, project); 228 } 229 230 231 232 243 public static final File getFilesystemSource(URL url, String resource, Project P) 244 { 245 249 if (url != null) { 250 String u = url.toString(); 251 if (u.startsWith("jar:file:")) { 252 int pling = u.indexOf("!"); 253 String jarName = u.substring(4, pling); 254 return new File (Locator.fromURI(jarName)); 255 } else if (u.startsWith("file:")) { 256 int tail = u.indexOf(resource); 257 String dirName = u.substring(0, tail); 258 return new File (Locator.fromURI(dirName)); 259 } 260 } 261 return null; 262 } 263 264 265 266 275 public static final void resolveAllFilesOrDirs(String [] filesOrDirs, 276 DirectoryScanner dirscan, boolean basenameOnly) 277 { 278 FileUtils fileUtils = AntXFixture.fileUtils(); 279 File wrt = dirscan.getBasedir(); 280 for (int i=0;i<filesOrDirs.length;i++) { 281 File rf= fileUtils.resolveFile(wrt,filesOrDirs[i]); 282 if (basenameOnly) { 283 filesOrDirs[i]= rf.getName(); 284 } else { 285 filesOrDirs[i]= rf.getAbsolutePath(); 286 } 287 } 288 } 289 290 291 292 297 public static final String purtyEventMsg(BuildEvent e) 298 { 299 if (e.getTask()!=null || e.getTarget()!=null) { 300 StringBuffer sb = new StringBuffer (e.getMessage().length()+50); 301 sb.append("["); 302 if (e.getTask()!=null) { 303 sb.append(e.getTask().getTaskName()); 304 } else { 305 sb.append(e.getTarget().getName()); 306 } 307 sb.append("] "); 308 sb.append(e.getMessage()); 309 return sb.substring(0); 310 } 311 return e.getMessage(); 312 } 313 314 315 316 322 public static String purtyLocation(Location l) 323 { 324 return Tk.shortStringFrom 325 (Iteration.defaultdefaults().isShortLocationsEnabled(),l); 326 } 327 328 329 330 344 public static Exception replaceError(Exception oldX, 345 Location fallbackLoc, 346 Class newXclass) 347 { 348 AntX.require_(oldX!=null && newXclass!=null,IAM_, 349 "replErr- nonzro args"); 350 351 Constructor ctor=null; 352 BuildException newX= null; 353 try { 354 if (!BuildException.class.isAssignableFrom(newXclass)) { 355 return oldX; 356 } 357 Location loc; 358 if (oldX instanceof BuildException) { 359 BuildException oldBX = (BuildException)oldX; 360 loc = oldBX.getLocation(); 361 if (loc==Location.UNKNOWN_LOCATION) { 362 if (oldBX.getCause() instanceof BuildException) { loc = ((BuildException)oldBX.getCause()).getLocation(); 364 if (loc==Location.UNKNOWN_LOCATION) { 365 loc = fallbackLoc; 366 } 367 } else { 368 loc = fallbackLoc; 369 } 370 } 371 } else if (oldX instanceof ScriptLocatable) { 372 loc = ((ScriptLocatable)oldX).getLocation(); 373 if (loc==Location.UNKNOWN_LOCATION) { 374 loc = fallbackLoc; 375 } 376 } else { 377 loc = fallbackLoc; 378 } 379 try { 380 ctor = newXclass.getConstructor(AFE_SIG2); 381 newX = (BuildException)ctor.newInstance 382 (new Object []{oldX.getMessage(),loc}); 383 } catch(Exception igx) { 384 ctor = newXclass.getConstructor(AFE_SIG1); 385 newX = (BuildException)ctor.newInstance 386 (new Object []{oldX.getMessage()}); 387 } 388 try { 389 Method m= newXclass.getMethod("initCause",AFE_SIG0); 390 m.invoke(newX, new Object []{oldX}); 391 } catch(Exception jre13X) {} 392 } catch(Exception anyX) { 393 394 } 395 return (newX!=null) ? newX : oldX; 396 } 397 398 399 400 408 public static Exception replaceError(Exception oldX, 409 Location fallbackLoc, 410 String newXclass) 411 { 412 AntX.require_(oldX!=null && newXclass!=null, IAM_, 413 "replErr- nonzro args"); 414 try { 415 return replaceError(oldX,fallbackLoc,Class.forName(newXclass)); 416 } catch(Exception anyX) { 417 return oldX; 418 } 419 } 420 421 422 429 public static void tryInitCause(Throwable newerX, Throwable causeX) 430 { 431 AntX.require_(newerX!=null && causeX!=null, IAM_, 432 "initCause- nonzro args"); 433 434 try { 435 Method m= newerX.getClass().getMethod("initCause",AFE_SIG0); 436 m.invoke(newerX, new Object []{causeX}); 437 } catch(Exception jre13OrInitedX) {} 438 } 439 440 441 private static final Class [] AFE_SIG0= new Class []{Throwable .class}; 442 private static final Class [] AFE_SIG1= new Class []{String .class}; 443 private static final Class [] AFE_SIG2= new Class []{String .class,Location.class}; 444 445 446 449 private LocalTk() 450 { 451 } 452 } 453 454 455 | Popular Tags |