1 28 29 package com.idaremedia.antx; 30 31 import java.util.Locale ; 32 33 import org.apache.tools.ant.Project; 34 35 import com.idaremedia.antx.helpers.Tk; 36 import com.idaremedia.antx.parameters.Handling; 37 38 70 71 public class FlexString extends AssertableProjectComponent implements Cloneable 72 { 73 76 public FlexString() 77 { 78 m_normalizeLower= true; 79 } 80 81 85 public FlexString(String value) 86 { 87 this(); 88 set(value); 89 } 90 91 96 public FlexString(String value, boolean isP) 97 { 98 this(); 99 set(value); 100 setIsProperty(isP); 101 } 102 103 104 110 public FlexString(boolean uppercaseToNormalize) 111 { 112 m_normalizeLower= !uppercaseToNormalize; 113 } 114 115 116 120 public Object clone() 121 { 122 try { 123 return super.clone(); 124 } catch(CloneNotSupportedException clnx) { 125 throw new Error (uistrs().get(AntX.CLONE_BROKEN_MSGID)); 126 } 127 } 128 129 130 133 public final boolean isUndefined() 134 { 135 return m_actualString==null; 136 } 137 138 139 143 public void set(String value) 144 { 145 m_actualString= value; 146 } 147 148 149 153 public final void setString(String value) 154 { 155 set(value); 156 } 157 158 159 163 public String get() 164 { 165 return m_actualString; 166 } 167 168 169 173 public void setIsProperty(boolean b) 174 { 175 m_isProperty= b; 176 } 177 178 179 183 public boolean isProperty() 184 { 185 return m_isProperty; 186 } 187 188 189 194 public void setIsExported(boolean b) 195 { 196 m_isExported= b; 197 } 198 199 200 204 public boolean isExported() 205 { 206 return m_isExported; 207 } 208 209 210 215 public void setIsReference(boolean b) 216 { 217 m_isReference= b; 218 } 219 220 221 225 public boolean isReference() 226 { 227 return m_isReference; 228 } 229 230 231 236 public void setIsLiteral() 237 { 238 m_isProperty = false; 239 m_isReference = false; 240 m_isExported = false; 241 } 242 243 244 249 public boolean isLiteral() 250 { 251 return !m_isProperty && !m_isExported && !m_isReference; 252 } 253 254 255 263 public void setLenient(boolean lenient) 264 { 265 m_isLenient = lenient; 266 } 267 268 269 276 public boolean isLenient() 277 { 278 return m_isLenient; 279 } 280 281 282 286 public void setTrim(boolean b) 287 { 288 m_isTrimmed= b; 289 } 290 291 292 296 public boolean isTrimmed() 297 { 298 return m_isTrimmed; 299 } 300 301 302 306 public void setIgnoreCase(boolean ignored) 307 { 308 m_ignoreCase= ignored; 309 } 310 311 312 318 public boolean isIgnoreCase() 319 { 320 return m_ignoreCase; 321 } 322 323 324 329 public void setIgnoreWhitespace(boolean ignored) 330 { 331 m_ignoreWS= ignored; 332 } 333 334 335 341 public void setWhitespace(Handling response) 342 { 343 response = Handling.simplifyIgnoreOrNot 344 (response,Handling.ACCEPT); 345 346 setIgnoreWhitespace(Handling.IGNORE.equals(response)); 347 } 348 349 350 355 public boolean isIgnoreWhitespace() 356 { 357 return m_ignoreWS; 358 } 359 360 361 367 public void setIgnorePrefix(String filter) 368 { 369 m_swFilter = filter; 370 } 371 372 373 378 public String getPrefixFilter() 379 { 380 return m_swFilter; 381 } 382 383 384 391 public void setIsPath(boolean isPath) 392 { 393 if (isPath) { 394 m_isMask |= ISPATH; 395 } else { 396 m_isMask &= ~ISPATH; 397 } 398 } 399 400 401 405 public final boolean isPath() 406 { 407 return (m_isMask&ISPATH)==ISPATH; 408 } 409 410 411 425 public String getValue(final Project P) 426 { 427 verify_(!(isExported() && isReference()), "getvalu- exported|reference"); 428 429 String value = sourceString(P); 430 431 if (value!=null) { 432 value = targetString(P,value); 433 if (value!=null) { 434 value = modifiedString(P,value); 435 } 436 } 437 438 return value; 439 } 440 441 442 446 public String getValue() 447 { 448 return getValue(getProject()); 449 } 450 451 452 461 protected String stringFrom(Object o, Project P) 462 { 463 return Stringifier.get(isLenient()).stringFrom(o,P); 464 } 465 466 467 476 public String sourceString(Project P) 477 { 478 String value= get(); 479 480 if (value==null) { 481 return null; 482 } 483 return Tk.resolveString(P,value,true); } 485 486 487 488 495 public String targetString(Project P, String source) 496 { 497 String value = source; 498 499 if (isProperty()) { 500 value = (P!=null) ? P.getProperty(value) : null; 501 if (value==null) { 502 return null; 503 } 504 } 505 506 if (isExported()) { 507 value = ExportedProperties.readstring(value); 508 if (value==null) { 509 return null; 510 } 511 } else if (isReference()) { 512 Object object = P.getReference(value); 513 if (object==null) { 514 return null; 515 } 516 value = stringFrom(object,P); 517 if (value==null) { 518 return null; 519 } 520 } 521 522 if (value!=null && isPath()) { 523 value = AntXFixture.fileUtils().normalize(value).getPath(); 524 } 525 526 return value; 527 } 528 529 530 538 protected String modifiedString(Project P, String value) 539 { 540 if (isIgnoreWhitespace()) { 541 if (Tk.isWhitespace(value)) { 542 return null; 543 } 544 } 545 546 if (isTrimmed()) { 547 value= value.trim(); 548 if (value.length()==0) { 549 return value; 550 } 551 } 552 553 if (isIgnoreCase()) { 554 value= m_normalizeLower ? 555 value.toLowerCase(Locale.US) : value.toUpperCase(Locale.US); 556 } 557 558 if (getPrefixFilter()!=null) { 559 if (value.startsWith(getPrefixFilter())) { 560 return null; 561 } 562 } 563 564 return value; 565 } 566 567 568 572 public String toString() 573 { 574 StringBuffer sb= AntXFixture.newSmallStringBuffer(); 575 if (isLiteral()) { 576 sb.append("Literal/"); 577 } else if (isReference()) { 578 sb.append("Reference/"); 579 } else { 580 if (isProperty()) { 581 sb.append("Property/"); 582 } 583 if (isExported()) { 584 sb.append("Variable/"); 585 } 586 } 587 sb.append("["); 588 sb.append(get()); 589 sb.append("]"); 590 return sb.toString(); 591 } 592 593 594 595 601 public String toAttributeName() 602 { 603 if (isReference()) { 604 return "reference"; 605 } 606 if (isProperty()) { 607 if (isExported()) { 608 return "variable"; 609 } 610 return "property"; 611 } 612 return "value"; 613 } 614 615 616 private final boolean m_normalizeLower; 617 private String m_actualString; 618 private boolean m_isProperty, m_isExported, m_isReference; 619 private boolean m_ignoreCase, m_ignoreWS, m_isTrimmed; 620 private boolean m_isLenient = true; 621 private String m_swFilter; 622 623 private static final int ISPATH=0x01; 624 private int m_isMask; 625 } 626 627 628 | Popular Tags |