001: package gnu.kawa.ant;
002:
003: import org.apache.tools.ant.types.FilterSet;
004:
005: import org.apache.tools.ant.Project;
006:
007: import java.util.Enumeration;
008: import java.util.Hashtable;
009:
010: public class LineStripperSet extends FilterSet {
011: // The replaceTokens method is stateful.
012: // If this is not null, then we ignore all lines until we find that token.
013: private String strippingUntil = null;
014:
015: public LineStripperSet() {
016: }
017:
018: /**
019: * Individual filter component of filterset
020: *
021: * @author Jim White
022: * Created 2001-11-14.
023: */
024: public static class LineStripper // extends FilterSet.Filter
025: {
026: String first;
027: String last;
028:
029: public LineStripper() {
030: }
031:
032: public void setFirst(final String s) {
033: first = s;
034: }
035:
036: public void setLast(final String s) {
037: last = s;
038: }
039:
040: public String getFirst() {
041: return first;
042: }
043:
044: public String getLast() {
045: return last;
046: }
047:
048: }
049:
050: /**
051: * Gets the filter hash of the FilterSet.
052: *
053: * @return The hash of the tokens and values for quick lookup.
054: */
055: public Hashtable getStripperHash() {
056: final int filterSize = getFilters().size();
057: final Hashtable stripperHash = new Hashtable(filterSize);
058: for (final Enumeration e = getFilters().elements(); e
059: .hasMoreElements();) {
060: LineStripper stripper = (LineStripper) e.nextElement();
061: stripperHash.put(stripper.getFirst(), stripper.getLast());
062: }
063: return stripperHash;
064: }
065:
066: /**
067: * Does replacement on the given string with token matching.
068: * This uses the defined begintoken and endtoken values which default to @ for both.
069: *
070: * @param line The line to process the tokens in.
071: * @return The string with the tokens replaced.
072: */
073: public String replaceTokens(String line) {
074: final String beginToken = getBeginToken();
075: final String endToken = getEndToken();
076: int index = line.indexOf(beginToken);
077:
078: if (strippingUntil != null) {
079: log("Stripping until: " + beginToken + strippingUntil
080: + endToken, Project.MSG_VERBOSE);
081: }
082:
083: if (index < 0) {
084: if (strippingUntil == null) {
085: return line;
086: }
087:
088: return "";
089: }
090:
091: final Hashtable tokens = getStripperHash();
092: try {
093: StringBuffer b = new StringBuffer();
094: int i = 0;
095: String token = null;
096:
097: do {
098: int endIndex = line.indexOf(endToken, index
099: + beginToken.length() + 1);
100: if (endIndex == -1) {
101: break;
102: }
103: token = line.substring(index + beginToken.length(),
104: endIndex);
105:
106: if (strippingUntil == null) {
107: // We're not stripping, so look for a "first" token.
108: b.append(line.substring(i, index));
109: if (tokens.containsKey(token)) {
110: strippingUntil = (String) tokens.get(token);
111: log("Stripping: " + beginToken + token
112: + endToken, Project.MSG_VERBOSE);
113: i = index + beginToken.length()
114: + token.length() + endToken.length();
115: } else {
116: // just append beginToken and search further
117: b.append(beginToken);
118: i = index + beginToken.length();
119: }
120: } else {
121: // We're stripping until we see "strippingUntil".
122: // This means that strippers do not nest.
123: // That could be an option later.
124: if (strippingUntil.equals(token)) {
125: log("Stripping ends with: " + beginToken
126: + token + endToken, Project.MSG_VERBOSE);
127: // We've found it.
128: // Skip over the token.
129: i = index + beginToken.length()
130: + token.length() + endToken.length();
131: // Switch back to looking for a new "first".
132: strippingUntil = null;
133: } else {
134: // Keep looking.
135: i = index + beginToken.length();
136: }
137: }
138: } while ((index = line.indexOf(beginToken, i)) > -1);
139:
140: b.append(line.substring(i));
141: return b.toString();
142: } catch (StringIndexOutOfBoundsException e) {
143: return line;
144: }
145: }
146:
147: /**
148: * Create a new filter
149: *
150: * @param stripper the filter to be added
151: */
152: public void addLineStripper(LineStripper stripper) {
153: if (isReference()) {
154: throw noChildrenAllowed();
155: }
156: getFilters().addElement(stripper);
157: }
158:
159: }
|