1 28 29 package com.idaremedia.antx.starters; 30 31 import java.io.BufferedReader ; 32 import java.io.ByteArrayInputStream ; 33 import java.io.InputStream ; 34 import java.io.InputStreamReader ; 35 import java.io.IOException ; 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 40 import org.apache.tools.ant.BuildException; 41 import org.apache.tools.ant.Project; 42 43 import com.idaremedia.antx.AntX; 44 import com.idaremedia.antx.AssertableDataType; 45 import com.idaremedia.antx.FixtureComponent; 46 import com.idaremedia.antx.FlexString; 47 import com.idaremedia.antx.helpers.ArrayIterator; 48 import com.idaremedia.antx.helpers.Empties; 49 import com.idaremedia.antx.helpers.InnerString; 50 import com.idaremedia.antx.helpers.InputFileLoader; 51 import com.idaremedia.antx.helpers.Tk; 52 import com.idaremedia.antx.ownhelpers.LocalTk; 53 54 70 71 public abstract class StringItemList extends AssertableDataType 72 implements Cloneable , FixtureComponent 73 { 74 75 public final static Boolean PROCESSED=Boolean.FALSE; 76 77 78 public final static Boolean RAW= Boolean.TRUE; 79 80 81 82 85 protected StringItemList() 86 { 87 super(AntX.starters); 88 m_strings= new ArrayList (10); 89 } 90 91 92 93 97 protected StringItemList(String iam) 98 { 99 super(iam); 100 m_strings= new ArrayList (10); 101 } 102 103 104 105 110 protected StringItemList(String iam, String list) 111 { 112 super(); 113 List l = Tk.splitList(list,AntX.DEFAULT_DELIMITER); 114 if (l instanceof ArrayList ) { 115 m_strings = (ArrayList )l; 116 } else { 117 m_strings= new ArrayList (l); 118 l= null; 119 } 120 } 121 122 123 124 129 public Object clone() 130 { 131 if (isReference()) { 132 return getOtherItemList(getClass()).clone(); 133 } 134 try { 135 StringItemList copy = (StringItemList)super.clone(); 136 copy.m_strings = (ArrayList )m_strings.clone(); 137 cloneInternals(copy); 138 return copy; 139 } catch(CloneNotSupportedException clnx) { 140 throw new Error (uistrs().get(AntX.CLONE_BROKEN_MSGID)); 141 } 142 } 143 144 145 146 151 public void setId(String id) 152 { 153 m_Id= id; 154 } 155 156 157 158 164 public final String getId() 165 { 166 if (m_Id!=null) { 167 return m_Id; 168 } 169 if (isReference()) { 170 return getOtherItemList(getClass()).getId(); 171 } 172 return super.getId(); 173 } 174 175 176 177 180 public final boolean isEmpty() 181 { 182 if (isReference()) { 183 return getOtherItemList(getClass()).isEmpty(); 184 } 185 return m_strings.isEmpty(); 186 } 187 188 189 190 193 public final int size() 194 { 195 if (isReference()) { 196 return getOtherItemList(getClass()).size(); 197 } 198 return m_strings.size(); 199 } 200 201 202 203 210 public final Iterator readonlyIterator() 211 { 212 if (isReference()) { 213 return getOtherItemList(getClass()).readonlyIterator(); 214 } 215 return copyOfItems(getProject()).iterator(); 216 } 217 218 219 220 226 public final Iterator readonlyStringIterator() 227 { 228 if (isReference()) { 229 return getOtherItemList(getClass()).readonlyStringIterator(); 230 } 231 return new ArrayIterator(copyOfStrings(getProject(),PROCESSED)); 232 } 233 234 235 236 244 public final Iterator readonlyStringIterator(Project p) 245 { 246 if (isReference()) { 247 return getOtherItemList(getClass()).readonlyStringIterator(p); 248 } 249 return new ArrayIterator(copyOfStrings(p,PROCESSED)); 250 } 251 252 253 254 265 public final Iterator readonlyStringIterator(Project p, Boolean how) 266 { 267 if (isReference()) { 268 return getOtherItemList(getClass()).readonlyStringIterator(p,how); 269 } 270 require_(how!=null,"readonlyItr- nonzro how flag"); 271 return new ArrayIterator(copyOfStrings(p,how)); 272 } 273 274 275 276 283 protected void addItem(InnerString item) 284 { 285 require_(item!=null,"addItm- nonzro itm"); 286 if (isReference()) { 287 throw tooManyAttributes(); 288 } 289 addItemFinal(item.toString()); 290 edited(); 291 } 292 293 294 295 304 protected void addItem(InnerString item, Project theProject) 305 { 306 require_(item!=null,"addItm- nonzro itm"); 307 if (isReference()) { 308 throw tooManyAttributes(); 309 } 310 addItemFinal(item.toString(theProject)); 311 edited(); 312 } 313 314 315 316 324 protected void addItem(FlexString item) 325 { 326 require_(item!=null,"addItm- nonzro itm"); 327 if (isReference()) { 328 throw tooManyAttributes(); 329 } 330 addItemFinal(item.getValue()); 331 edited(); 332 } 333 334 335 336 346 private boolean addBuffer(byte[] allStrings) 347 { 348 require_(allStrings!=null,"addBfr- nonzro byt[]"); 349 BufferedReader r= new BufferedReader 350 (new InputStreamReader (new ByteArrayInputStream (allStrings))); 351 int N=0; 352 try { 353 String ln; 354 while ((ln=r.readLine())!=null) { 355 addItemFinal(ln); 356 N++; 357 } 358 } catch(IOException iox) { 359 throw new BuildException(iox); 360 } finally { 361 if (N!=0) { 362 edited(); 363 } 364 } 365 return N!=0; 366 } 367 368 369 370 379 protected boolean addFileOrURL(String f) 380 { 381 require_(f!=null,"addFil- nonzro filnam"); 382 if (isReference()) { 383 throw tooManyAttributes(); 384 } 385 byte[] bytes=null; 386 try { 387 bytes = new InputFileLoader().loadFile(f); 388 } catch(IOException iox) { 389 String warning = uistrs().get 390 ("solo.list.filenotfound", getId(), f); 391 log(warning,Project.MSG_WARN); 392 return false; 393 } 394 return addBuffer(bytes); 395 } 396 397 398 399 407 protected boolean addResource(String rn) 408 { 409 require_(rn!=null,"addRsrc- nonzro name"); 410 if (isReference()) { 411 throw tooManyAttributes(); 412 } 413 InputStream is = LocalTk.getSystemResourceAsStream(rn, getProject()); 414 if (is==null) { 415 return false; 416 } 417 byte[] bytes=null; 418 try { 419 bytes = new InputFileLoader().load(is); 420 } catch(IOException iox) { 421 String error = uistrs().get 422 ("solo.list.reznotfound", getId(), rn); 423 log(error,Project.MSG_ERR); 424 throw new BuildException(error,iox); 425 } 426 return addBuffer(bytes); 427 } 428 429 430 431 435 protected final void addItemFinal(String string) 436 { 437 if (!includeItem(string)) { 438 String error = uistrs().get("solo.list.invalid.item",getId(), 439 String.valueOf(string)); 440 log(error,Project.MSG_ERR); 441 throw new BuildException(error); 442 } 443 m_strings.add(string); 444 } 445 446 447 448 454 protected boolean includeItem(String candidate) 455 { 456 return candidate!=null; 457 } 458 459 460 461 472 protected String [] copyOfStrings(Project p, Boolean how) 473 { 474 verify_(!isReference(),"copyOfStrs- standalone"); String [] sl=null; 476 if (how==PROCESSED && isCustom()) { 477 sl = new String [m_strings.size()]; 478 StringBuffer sb = new StringBuffer (150); 479 for (int i=0;i<sl.length;i++) { 480 sb.append(getPrefixString()); 481 sb.append(Tk.resolveString(p,m_strings.get(i).toString(),true)); 482 sb.append(getSuffixString()); 483 sl[i]= sb.substring(0); 484 sb.delete(0,sb.length()); 485 } 486 } else if (p!=null) { sl = new String [m_strings.size()]; 488 StringBuffer sb = new StringBuffer (150); 489 for (int i=0;i<sl.length;i++) { 490 sb.append(Tk.resolveString(p,m_strings.get(i).toString(),true)); 491 sl[i]= sb.substring(0); 492 sb.delete(0,sb.length()); 493 } 494 } else { 495 sl = (String [])m_strings.toArray(Empties.EMPTY_STRING_ARRAY); 496 } 497 return sl; 498 } 499 500 501 502 511 protected List copyOfItems(Project p) 512 { 513 verify_(!isReference(),"copyOfItms- standalone"); String [] sl = copyOfStrings(p,PROCESSED); 515 ArrayList copy = new ArrayList (sl.length); 516 for (int i=0;i<sl.length;i++) { 517 copy.add(sl[i]); 518 sl[i]= null; 519 } 520 sl=null; 521 return copy; 522 } 523 524 525 526 531 protected final List rawStringsList() 532 { 533 return m_strings; 534 } 535 536 537 538 547 protected void setPrefixString(String prefix) 548 { 549 if (isReference()) { 550 throw tooManyAttributes(); 551 } 552 m_prefix = prefix==null ? "" : prefix; 553 edited(); 554 } 555 556 557 558 562 protected final String getPrefixString() 563 { 564 return m_prefix; 565 } 566 567 568 569 578 protected void setSuffixString(String suffix) 579 { 580 if (isReference()) { 581 throw tooManyAttributes(); 582 } 583 m_suffix = suffix==null ? "" : suffix; 584 edited(); 585 } 586 587 588 589 593 protected final String getSuffixString() 594 { 595 return m_suffix; 596 } 597 598 599 600 607 protected void cloneInternals(StringItemList cloned) 608 { 609 } 611 612 613 614 618 protected final StringItemList getOtherItemList(Class c) 619 { 620 return (StringItemList)getCheckedRef(c,getTypicalName(c)); 621 } 622 623 624 625 628 private boolean willModify(String it) 629 { 630 return it!=null && it.length()>0; 631 } 632 633 634 635 640 protected final boolean shouldModifyStrings() 641 { 642 return willModify(getPrefixString()) || willModify(getSuffixString()); 643 } 644 645 646 647 652 protected final boolean isCustom() 653 { 654 return (!m_strings.isEmpty()) && shouldModifyStrings(); 655 } 656 657 658 659 664 protected String getTypicalName(Class c) 665 { 666 if (c==null) { c = getClass(); } 667 return Tk.lowercaseFrom(Tk.leafNameFrom(c)); 668 } 669 670 671 672 private String m_Id; 673 private ArrayList m_strings= new ArrayList (10); 674 private String m_prefix="", m_suffix=""; 675 } 676 677 678 | Popular Tags |