Changeset 1047 for trunk/src/org/expeditee/items/widgets
- Timestamp:
- 04/28/16 19:37:35 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/widgets/InteractiveWidget.java
r919 r1047 197 197 // @iw: <class name> [options] width height : <rest...> 198 198 // 199 // Parsethe 'core' part of the X-rayable text item199 // the 'core' part of the X-rayable text item 200 200 // i.e, the text between the first ':' and the second ':' 201 201 // … … 204 204 String tokens_str = (index == -1) ? text : text.substring(0, index); 205 205 206 String[] tokens = parseArgsApache(tokens_str);206 String[] tokens = Text.parseArgsApache(tokens_str); 207 207 208 208 // make anything starting with a '-' lowercase … … 342 342 String args_str = text.substring(index + 1); 343 343 344 args = parseArgsApache(args_str);344 args = Text.parseArgsApache(args_str); 345 345 346 346 } … … 559 559 560 560 561 /** 562 * Similar to parseArgs() above. 563 * Code based on routine used in Apache Ant: 564 * org.apache.tools.ant.types.Commandline::translateCommandline() 565 * @param toProcess the command line to process. 566 * @return the command line broken into strings. 567 * An empty or null toProcess parameter results in a zero sized array. 568 */ 569 public static String[] parseArgsApache(String toProcess) { 570 if (toProcess == null || toProcess.length() == 0) { 571 //no command? no string 572 return new String[0]; 573 } 574 // parse with a simple finite state machine 575 576 final int normal = 0; 577 final int inQuote = 1; 578 final int inDoubleQuote = 2; 579 int state = normal; 580 final StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true); 581 final ArrayList<String> result = new ArrayList<String>(); 582 final StringBuilder current = new StringBuilder(); 583 boolean lastTokenHasBeenQuoted = false; 584 585 while (tok.hasMoreTokens()) { 586 String nextTok = tok.nextToken(); 587 switch (state) { 588 case inQuote: 589 if ("\'".equals(nextTok)) { 590 lastTokenHasBeenQuoted = true; 591 state = normal; 592 } else { 593 current.append(nextTok); 594 } 595 break; 596 case inDoubleQuote: 597 if ("\"".equals(nextTok)) { 598 lastTokenHasBeenQuoted = true; 599 state = normal; 600 } else { 601 current.append(nextTok); 602 } 603 break; 604 default: 605 if ("\'".equals(nextTok)) { 606 state = inQuote; 607 } else if ("\"".equals(nextTok)) { 608 state = inDoubleQuote; 609 } else if (" ".equals(nextTok)) { 610 if (lastTokenHasBeenQuoted || current.length() != 0) { 611 result.add(current.toString()); 612 current.setLength(0); 613 } 614 } else { 615 current.append(nextTok); 616 } 617 lastTokenHasBeenQuoted = false; 618 break; 619 } 620 } 621 if (lastTokenHasBeenQuoted || current.length() != 0) { 622 result.add(current.toString()); 623 } 624 if (state == inQuote || state == inDoubleQuote) { 625 System.err.println("Error: Unbalanced quotes -- failed to parse '" + toProcess + "'"); 626 return null; 627 } 628 629 return result.toArray(new String[result.size()]); 630 } 631 561 632 562 633 563 … … 1716 1646 return defaultValue; 1717 1647 } 1718 1719 /** 1720 * Looks fors a dataline in the current representation of the widget. 1648 1649 1650 1651 /** 1652 * Looks for a dataline in the current representation of the widget. 1721 1653 * 1722 1654 * @see #getCurrentRepresentation … … 1759 1691 } 1760 1692 1693 /** 1694 * Looks for a data-line in the current representation of the widget. 1695 * 1696 * @see #getCurrentRepresentation 1697 * @see #getStrippedDataString(String) 1698 * @see #getStrippedDataLong(String, long) 1699 * 1700 * @param tag 1701 * The prefix of a data-line that will be matched. Must be larger 1702 * the zero and not null. 1703 * 1704 * @param defaultValue 1705 * The default value if the tag does not exist or contains 1706 * invalid data. 1707 * 1708 * @return The <i>first</i> data-line that matched the prefix - if 1709 * case insensitive match is 'true' then return true, otherwise 1710 * false. defaultValue if their was no data that 1711 * matched the given prefix or the data was invalid. 1712 * 1713 * @throws IllegalArgumentException 1714 * If tag is null. 1715 * 1716 * @throws NullPointerException 1717 * If tag is empty. 1718 * 1719 */ 1720 protected Boolean getStrippedDataBool(String tag, Boolean defaultValue) { 1721 1722 String strippedStr = getStrippedDataString(tag); 1723 1724 if (strippedStr != null) { 1725 strippedStr = strippedStr.trim(); 1726 if (strippedStr.length() > 0) { 1727 return strippedStr.equalsIgnoreCase("true") ? true : false; 1728 } 1729 } 1730 1731 return defaultValue; 1732 } 1733 1734 1761 1735 /** 1762 1736 * All data is removed that is prefixed with the given tag.
Note:
See TracChangeset
for help on using the changeset viewer.