1 28 29 package com.idaremedia.antx; 30 31 import java.lang.reflect.Method ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 36 import org.apache.tools.ant.BuildException; 37 import org.apache.tools.ant.Location; 38 import org.apache.tools.ant.Project; 39 import org.apache.tools.ant.PropertyHelper; 40 import org.apache.tools.ant.UnknownElement; 41 import org.apache.tools.ant.types.PropertySet; 42 43 import com.idaremedia.antx.apis.Requester; 44 import com.idaremedia.antx.helpers.GenericParameters; 45 import com.idaremedia.antx.helpers.Tk; 46 import com.idaremedia.antx.parameters.PropertySource; 47 48 60 61 public final class FixtureExaminer implements FixtureCore 62 { 63 private static final String IAM_= AntX.AntX+"FixtureExaminer:"; 64 65 66 private static Location prethrow(Project project, Requester from, 67 String message, int level) 68 { 69 Location loc = Location.UNKNOWN_LOCATION; 70 if (from!=null) { 71 from.log(message, level); 72 loc = from.getLocation(); 73 } else { 74 project.log(message,level); 75 } 76 return loc; 77 } 78 79 80 89 public static final Object getReferencedObject(Project project, 90 Requester from, 91 String refid, 92 Class requiredClass) 93 { 94 AntX.require_(refid!=null,IAM_,"getRefObj- nonzro id"); 95 AntX.require_(project!=null,IAM_,"getRefObj- nonzro project"); 96 97 Object o = project.getReference(refid); 98 String error = null; 99 100 if (o==null) { 101 error = AntX.uistrs().get("task.missing.refid", refid); 102 } 103 else if (!requiredClass.isInstance(o)) { 104 error = AntX.uistrs().get("task.bad.refid", refid, 105 requiredClass.getName(), 106 o.getClass().getName()); 107 } 108 if (error!=null) { 109 Location loc = prethrow(project,from,error,Project.MSG_ERR); 110 throw new BuildException(error,loc); 111 } 112 return o; 113 } 114 115 116 117 125 public static final Object getReferencedObject(Project project, 126 Requester from, 127 String refid) 128 { 129 AntX.require_(refid!=null,IAM_,"getRefObj- nonzro id"); 130 AntX.require_(project!=null,IAM_,"getRefObj- nonzro project"); 131 132 Object value = project.getReference(refid); 133 if (value==null) { 134 String error = AntX.uistrs().get("task.missing.refid", refid); 135 Location loc = prethrow(project,from,error,Project.MSG_ERR); 136 throw new BuildException(error,loc); 137 } 138 139 return value; 140 } 141 142 143 144 private static String isOneOf(String value, String t0, String t1) 145 { 146 if (value.startsWith(t0)) { 147 return value.substring(t0.length()); 148 } 149 if (value.startsWith(t1)) { 150 return value.substring(t1.length()); 151 } 152 return null; 153 } 154 155 156 157 172 public static Properties getReferencedProperties(Project project, 173 String refid, Properties defaultProperties) 174 { 175 AntX.require_(refid!=null,IAM_,"getRefPropertiesObj- nonzro refid"); 176 AntX.require_(project!=null,IAM_,"getRefPropertiesObj- nonzro project"); 177 178 Properties Ps = null; 179 Object o = project.getReference(refid); 180 if (o!=null) { 181 if (o instanceof GenericParameters) { 184 Ps = ((GenericParameters)o).copyOfSimpleKeyValues(project); 185 } else if (o instanceof PropertySet) { 186 Ps = ((PropertySet)o).getProperties(); 187 } else if (o instanceof Properties ) { 188 Ps = ((Properties )o); 189 } 190 } else { 191 Ps = defaultProperties; 192 } 193 return Ps; 194 } 195 196 197 198 206 private static String getProperty(String key, Project project) 207 { 208 String value = project.getProperty(key); 209 int i; 210 if (value==null && (i=key.indexOf("->"))>0) { 211 String holder = key.substring(0,i); 212 i += 2; 213 if (i<key.length()) { 214 key = key.substring(i); 215 Properties Ps = getReferencedProperties(project,holder,null); 216 if (Ps!=null) { 217 value = Ps.getProperty(key); 218 } 219 } } 221 return value; 222 } 223 224 225 226 234 public static boolean isBuiltinValueURIScheme(String name) 235 { 236 if (name.charAt(0)=='$') { 237 int n = name.length(); 238 int i = name.indexOf(':'); 239 if (i>0) { 240 n = i; 241 } 242 name = name.substring(1,n); 243 } 244 245 boolean reserved = false; 246 name = Tk.lowercaseFrom(name); 247 248 switch(name.charAt(0)) { 249 case 'r': { 250 if ("ref".equals(name) || "reference".equals(name)) { 251 reserved = true; 252 } 253 break; 254 } 255 case 'v': { 256 if ("var".equals(name) || "variable".equals(name)) { 257 reserved = true; 258 } 259 break; 260 } 261 case 'p': { 262 if ("prop".equals(name) || "property".equals(name)) { 263 reserved = true; 264 } 265 break; 266 } 267 case 'i': { 268 if ("itprop".equals(name)|| "itproperty".equals(name)) { 269 reserved = true; 270 } 271 break; 272 } 273 } 274 return reserved || "null".equals(name); 275 } 276 277 278 279 286 public static String getIterationProperty(String key, Project project) 287 { 288 Object value = Iteration.getProperty(key); 289 int i; 290 if (value==null && (i=key.indexOf("->"))>0) { 291 String holder = key.substring(0,i); 292 i += 2; 293 if (i<key.length()) { 294 key = key.substring(i); 295 value = Iteration.getProperty(holder,key); 296 } 297 } 298 return Iteration.strictStringifier().stringFrom(value,project); 299 } 300 301 302 303 311 public static boolean valueURIInterpreterInstalled(Project project) 312 { 313 AntX.require_(project!=null,IAM_,"valRdrInstal- nonzro project"); 314 Object interpreter = project.getReference(FixtureIds.VALUEURI_INTERPRETER); 315 if (interpreter instanceof PropertyHelper) { 316 return true; 317 } 318 return false; 319 } 320 321 322 323 331 private static final String funcValue(String value, String obof, 332 Requester clnt) 333 { 334 String dflt=null,key; 335 Project project = clnt.getProject(); 336 337 if ((key = isOneOf(value,"$var:","$variable:"))!=null) { 339 int i = key.lastIndexOf('?'); 340 if (i>0) { 341 dflt = key.substring(i+1); 342 key = key.substring(0,i); 343 } 344 return ExportedProperties.readstring(key,dflt); 345 } 346 if ((key = isOneOf(value,"$ref:","$reference:"))!=null) { 347 Object o = project.getReference(key); 348 return Iteration.lenientStringifer().stringFrom(o,project); 349 } 350 if ((key = isOneOf(value,"$itprop:","$itproperty:"))!=null) { 351 return getIterationProperty(key,project); 352 } 353 if ((key = isOneOf(value,"$prop:","$property:"))!=null) { 354 int i = key.lastIndexOf('?'); 355 if (i>0) { 356 dflt = key.substring(i+1); 357 key = key.substring(0,i); 358 } 359 if (!key.equals(obof)) { String match = getProperty(key,project); 361 return match==null ? dflt : match; 362 } 363 } 364 int i = value.indexOf(":"); 366 if (i>1) { 367 String scheme = value.substring(1,i); 368 return Iteration.valueURIHandler(scheme).valueFrom 369 (value.substring(i+1), value, clnt); 370 } 371 return null; 372 } 373 374 375 376 387 public static final String findValue(String value, String obof, 388 Requester clnt) 389 { 390 AntX.require_(value!=null,IAM_,"getFlexValue- nonzro values"); 391 AntX.require_(clnt!=null && clnt.getProject()!=null,IAM_, 392 "getFlexValue- nonzro project"); 393 394 String result = null; 396 while (value.length()>2 && value.charAt(0)=='$') { 397 int colon = value.indexOf(":"); 398 if (colon <= 1) { 399 break; 400 } 401 String scheme = value.substring(1,colon); 402 String singleuri = value; 403 int piped = value.indexOf("|$",colon+1); 404 String fragment = null; 405 if (piped>colon) { 406 fragment = value.substring(colon+1,piped); 407 singleuri = "$"+scheme+":"+fragment; 408 } else { 409 fragment = value.substring(colon+1); 410 } 411 String nextresult = funcValue(singleuri,obof,clnt); 413 result = nextresult; 414 if (result==null || singleuri==value) { 415 break; 416 } 417 colon = value.indexOf(':',piped+2); 418 if (colon<0) { 419 result = null; break; 421 } 422 String nextvalue = "$"+value.substring(piped+2,colon+1)+result; 423 if (colon<value.length()-1) { 424 nextvalue += value.substring(colon+1); 425 } 426 value = nextvalue; 427 } 429 return result; 430 } 431 432 433 434 444 public static final String findValue(Project project, 445 String value, String obof) 446 { 447 return findValue(value,obof,new Requester.ForProject(project)); 448 } 449 450 451 452 466 public static boolean checkIfProperty(Project project, Requester from, 467 String property, boolean warn) 468 { 469 AntX.require_(project!=null,IAM_,"chkProp- nonzro project"); 470 AntX.require_(property!=null,IAM_,"chkProp- nonzro nam"); 471 472 String it = Tk.getTheProperty(project,property); 473 if (it!=null) { 474 String msg = AntX.uistrs().get("task.warn.property.exists",property); 475 Location loc = prethrow(project, from, msg, 476 (warn ? Project.MSG_WARN : Project.MSG_ERR)); 477 if (!warn) { 478 throw new BuildException(msg,loc); 479 } 480 } 481 return it!=null; 482 } 483 484 485 486 499 public static boolean checkIfReference(Project project, Requester from, 500 String refid, boolean warn) 501 { 502 AntX.require_(project!=null,IAM_,"chkRef- nonzro project"); 503 AntX.require_(refid!=null,IAM_,"chkRef- nonzro refid"); 504 505 Object it = project.getReference(refid); 506 if (it!=null) { 507 String msg = AntX.uistrs().get("task.warn.refid.exists",refid); 508 Location loc = prethrow(project, from, msg, 509 (warn ? Project.MSG_WARN : Project.MSG_ERR)); 510 if (!warn) { 511 throw new BuildException(msg,loc); 512 } 513 } 514 return it!=null; 515 } 516 517 518 519 525 public static final Object IGNORED_REFERENCE= "__ooscope_ref__"; 526 527 528 529 535 public static final Object PROBLEM_REFERENCE= "__problem_ref__"; 536 537 538 539 547 public static final Object PENDING_REFERENCE= "__pending_ref__"; 548 549 550 563 public static Object trueReference(Project P, String id) 564 { 565 AntX.require_(P!=null && id!=null, IAM_, "trueRef- nonzro args"); 566 567 Object ref=null; 568 Object ht = P.getReferences(); 569 Boolean B=null; 570 Method m=null; 571 try { 572 if (AntX.ANT_PRE17) { 574 m = ht.getClass().getMethod("getReal",GET_REAL_METHOD); 575 } else { 576 m = ht.getClass().getDeclaredMethod("getReal",GET_REAL_METHOD); 577 } 578 B = m.isAccessible() ? Boolean.TRUE : Boolean.FALSE; 579 m.setAccessible(true); 580 ref = m.invoke(ht, new Object []{id}); 581 if (ref instanceof UnknownElement) { 582 ref = IGNORED_REFERENCE; 583 } 584 } catch(Exception reflectX) { 585 P.log("Unable to call 'Project$AntRefTable.getReal': "+reflectX.getMessage()); 586 try { 588 ref = P.getReference(id); 589 } catch(Exception lookupX) { 590 P.log(lookupX.getMessage(), Project.MSG_WARN); 591 ref = PROBLEM_REFERENCE; 592 } 593 } finally { 594 if (m!=null && B!=null & !B.booleanValue()) { 595 try { 596 m.setAccessible(false); 597 } catch(Exception anyX) { 598 599 } 600 } 601 } 602 return ref; 603 } 604 605 606 607 614 public static boolean usableReference(Project P, String id) 615 { 616 Object ref = trueReference(P,id); 617 if (ref==IGNORED_REFERENCE || ref==PROBLEM_REFERENCE 618 || ref==PENDING_REFERENCE) { 619 ref= null; 620 } 621 return ref!=null; 622 } 623 624 625 626 634 public static Map copyOfProperties(PropertySource domain, Project P) 635 { 636 AntX.require_(domain!=null,IAM_,"getProps- nonzro domain"); 637 638 Map mp=null; 640 switch (domain.getIndex()) { 641 case PropertySource.ALL_INDEX: 642 case PropertySource.ALLlite_INDEX: { 643 mp = P.getProperties(); 644 if (domain.getIndex()!=PropertySource.ALL_INDEX) { 645 removeImmutableJavaProperties(mp); 646 } 647 break; 648 } 649 case PropertySource.COMMAND_INDEX: { 650 mp = P.getUserProperties(); 651 break; 652 } 653 case PropertySource.CONTROL_INDEX: { 654 Project dumi= new Project(); 655 P.copyInheritedProperties(dumi); 656 mp = dumi.getProperties(); 657 dumi = null; break; 659 } 660 case PropertySource.SCRIPTlite_INDEX: 661 case PropertySource.SCRIPT_INDEX: { 662 mp = P.getProperties(); 664 mp.keySet().removeAll(P.getUserProperties().keySet()); 665 if (domain.getIndex()!=PropertySource.SCRIPT_INDEX) { 666 removeImmutableJavaProperties(mp); 667 } 668 break; 669 } 670 case PropertySource.FIXTURE_INDEX: { 671 mp = AntXFixture.copyOfProperties(); 672 break; 673 } 674 case PropertySource.JRE_INDEX: { 675 mp = (Map )System.getProperties().clone(); 676 break; 677 } 678 default: { 679 AntX.verify_(false,IAM_,"init- known domain"); 680 } 681 } 682 683 return mp; 684 } 685 686 687 688 696 public static void removeImmutableJavaProperties(Map map) 697 { 698 AntX.require_(map!=null,IAM_,"zapSystemProps- nonzro map"); 699 700 Iterator itr= map.keySet().iterator(); 701 while (itr.hasNext()) { 702 String s = itr.next().toString(); 703 if (s.startsWith("java.") || s.startsWith("sun.")) { 704 itr.remove(); 705 continue; 706 } 707 for (int i=0;i<KEEPERS.length;i++) { 708 if (KEEPERS[i].equals(s)) { 709 itr.remove(); 710 continue; 711 } 712 } 713 } 714 } 715 716 717 718 722 private static final String [] KEEPERS= { 723 "os.name", "os.arch", "os.version", 724 "user.name", "user.home", "user.dir", 725 "user.language", "user.timezone", "user.country", "user.variant", 726 "file.separator", "path.separator", 727 "line.separator" 728 }; 729 730 731 732 733 private static final Class [] GET_REAL_METHOD= {Object .class}; 734 735 736 737 private FixtureExaminer() 738 { 739 } 740 } 741 742 743 | Popular Tags |