1 28 29 package com.idaremedia.antx.print; 30 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.io.PrintStream ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Properties ; 37 38 import org.apache.tools.ant.BuildException; 39 import org.apache.tools.ant.Project; 40 import org.apache.tools.ant.taskdefs.condition.Condition; 41 42 import com.idaremedia.antx.AntX; 43 import com.idaremedia.antx.ExportedProperties; 44 import com.idaremedia.antx.FixtureExaminer; 45 import com.idaremedia.antx.helpers.Strings; 46 import com.idaremedia.antx.helpers.Tk; 47 import com.idaremedia.antx.parameters.FlexValueSupport; 48 import com.idaremedia.antx.parameters.PropertySource; 49 import com.idaremedia.antx.starters.EchoThingTask; 50 51 78 79 public class EchoItemsTask extends EchoThingTask implements Condition, FlexValueSupport 80 { 81 84 public EchoItemsTask() 85 { 86 super(AntX.starters); 87 } 88 89 90 94 protected EchoItemsTask(String iam) 95 { 96 super(iam); 97 } 98 99 100 104 public void setProject(Project project) 105 { 106 super.setProject(project); 107 108 if (project!=null && getThingRefId()==null) { 109 String name = project.getName(); 110 if (name==null) { 111 name = project.getProperty("ant.project.name"); 112 } 113 if (name!=null) { 114 setThingRefId(name); 115 } 116 } 117 } 118 119 120 127 public void setProperties(String nameList) 128 { 129 require_(nameList!=null,"setProps- nonzro list"); 130 m_propertiesList= nameList; 131 } 132 133 134 137 public final void setProperty(String property) 138 { 139 setProperties(property); 140 } 141 142 143 147 public final String getPropertiesNameList() 148 { 149 return m_propertiesList; 150 } 151 152 153 156 public void setPrefix(String prefix) 157 { 158 require_(prefix!=null && prefix.length()>0,"setPfx- valid str"); 159 m_prefix = prefix; 160 } 161 162 163 167 public final String getPrefix() 168 { 169 return m_prefix; 170 } 171 172 173 179 public void setVariables(String nameList) 180 { 181 require_(nameList!=null,"setVars- nonzro list"); 182 m_variablesList= nameList; 183 } 184 185 186 189 public final void setVariable(String variable) 190 { 191 setVariables(variable); 192 } 193 194 195 198 public final void setVar(String variable) 199 { 200 setVariables(variable); 201 } 202 203 204 208 public final String getVariablesNameList() 209 { 210 return m_variablesList; 211 } 212 213 214 219 public void setReferences(String nameList) 220 { 221 require_(nameList!=null,"setRefs- nonzro list"); 222 m_refsList= nameList; 223 } 224 225 226 229 public final void setReference(String refid) 230 { 231 require_(refid!=null,"setRef- nonzro ref"); 232 setReferences(refid); 233 } 234 235 236 240 public final String getReferencesNameList() 241 { 242 return m_refsList; 243 } 244 245 246 249 public void setKindOf(Class claz) 250 { 251 require_(claz!=null,"setKindOf- nonzro claz"); 252 m_kindOfClass = claz; 253 } 254 255 256 260 public final Class getKindOfFilterClass() 261 { 262 return m_kindOfClass==null ? Object .class : m_kindOfClass; 263 } 264 265 266 272 public void setUnknowns(boolean yn) 273 { 274 m_includeUnknowns= yn; 275 } 276 277 278 283 public final boolean willIncludeUnknowns() 284 { 285 return m_includeUnknowns; 286 } 287 288 289 290 293 protected Object getReferencedThing(Class ofKind, String msgid) 294 { 295 return getProject(); } 297 298 299 307 protected boolean echoProperties(final Project P, OutputStream os) 308 throws IOException 309 { 310 String list= getPropertiesNameList(); 311 if (!Tk.isWhitespace(list)) { 312 313 Properties allP= new Properties (); 314 315 PropertySource domain= PropertySource.from(list); 316 if (domain!=null) { 317 allP.putAll(FixtureExaminer.copyOfProperties(domain,P)); 318 if (getPrefix()!=null) { Iterator itr= allP.keySet().iterator(); 320 while (itr.hasNext()) { 321 String key = itr.next().toString(); 322 if (!key.startsWith(m_prefix)) { 323 itr.remove(); 324 } 325 } 326 } 327 } else { 328 List wanted = Tk.splitList(list); 329 for (int i=0,N=wanted.size();i<N;i++) { 330 String key = (String )wanted.get(i); 331 allP.setProperty(key, String.valueOf(P.getProperty(key))); 332 } 333 wanted=null; 334 } 335 336 String header= getMsg(newMsgGetter(uistrs().get("echo.label.properties"))); 337 allP.store(os,header); 338 339 allP.clear(); 340 allP=null; 341 return true; 342 } 343 return false; 344 } 345 346 347 355 protected boolean echoVariables(final Project P, OutputStream os) 356 throws IOException 357 { 358 String list= getVariablesNameList(); 359 if (!Tk.isWhitespace(list)) { 360 Properties allP; 361 String ll= Tk.lowercaseFrom(list); 362 363 if (Strings.ALL.equals(ll)) { 364 allP= ExportedProperties.copy(null); 365 if (getPrefix()!=null) { Iterator itr= allP.keySet().iterator(); 367 while (itr.hasNext()) { 368 String key = itr.next().toString(); 369 if (!key.startsWith(m_prefix)) { 370 itr.remove(); 371 } 372 } 373 } 374 } else { 375 allP= ExportedProperties.copy(null); 376 List wanted = Tk.splitList(list); 377 allP.keySet().retainAll(wanted); 378 if (wanted.size()>allP.size()) { for (int i=0,N=wanted.size();i<N;i++) { 380 String key = (String )wanted.get(i); 381 if (allP.getProperty(key)==null) { 382 allP.setProperty(key,Strings.NULL); 383 } 384 } 385 } 386 wanted=null; 387 } 388 389 String header= getMsg(newMsgGetter(uistrs().get("echo.label.variables"))); 390 allP.store(os,header); 391 392 allP.clear(); 393 allP=null; 394 return true; 395 } 396 return false; 397 } 398 399 400 401 404 protected final String unresolvedString(Project P, String key) 405 { 406 return P.getName()+":UnresolvedReference@"+key; 407 } 408 409 410 411 420 protected boolean echoReferences(final Project P, OutputStream os) 421 throws IOException 422 { 423 String list= getReferencesNameList(); 424 if (!Tk.isWhitespace(list)) { 425 426 Properties allP= new Properties (); 427 428 Iterator keysitr; 429 if (Strings.ALL.equals(Tk.lowercaseFrom(list))) { 430 keysitr= P.getReferences().keySet().iterator(); } else { 432 keysitr= Tk.splitList(list).iterator(); 433 } 434 while (keysitr.hasNext()) { 435 String key = (String )keysitr.next(); 436 437 Object object = FixtureExaminer.trueReference(P,key); if (object==FixtureExaminer.IGNORED_REFERENCE) { 439 if (!willIncludeUnknowns()) { 440 continue; 441 } 442 object = unresolvedString(P,key); 443 } 444 if (m_kindOfClass==null || object==null || 445 (m_kindOfClass.isInstance(object))) { 446 allP.setProperty(key, Tk.stringFrom(object,P)); 447 } 448 } 449 450 String header= getMsg(newMsgGetter(uistrs().get("echo.label.references"))); 451 allP.store(os,header); 452 453 allP.clear(); 454 allP=null; 455 return true; 456 } 457 return false; 458 } 459 460 461 464 protected void echoThing() 465 { 466 OutputStream os = getOutputStream(); 467 try { 468 boolean p= echoProperties(getProject(),os); 469 boolean v= echoVariables(getProject(),os); 470 boolean r= echoReferences(getProject(),os); 471 472 if (!p && !v && !r) { 473 if (getMsgId()!=null || getDefaultMsg()!=null) { 474 String s = getMsg(); 475 new PrintStream (os).println(s); 476 } 477 } 478 if (tryAntLog(os)) { 479 log(getAntLogString(os),getMsgLevel().getNativeIndex()); 480 } 481 482 } catch(IOException ioX) { 483 String ermsg = uistrs().get("task.echo.unable"); 484 log(ermsg,Project.MSG_ERR); 485 throw new BuildException(ermsg,getLocation()); 486 487 } finally { 488 try { os.close(); } catch(Exception X){} 489 os=null; 490 } 491 } 492 493 494 497 public final boolean eval() 498 { 499 execute(); 500 return true; 501 } 502 503 504 507 protected final void verifyCanExecute_(String calr) 508 { 509 verifyInProject_(calr); 510 } 511 512 513 private String m_propertiesList; 514 private String m_variablesList; 515 private String m_refsList; 516 private Class m_kindOfClass = Object .class; private String m_prefix; private boolean m_includeUnknowns=true;} 520 521 522 | Popular Tags |