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 LineCommenterSet 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 commentingUntil = null;
014: private boolean commenting = false;
015:
016: public LineCommenterSet() {
017: }
018:
019: /**
020: * Individual filter component of filterset
021: *
022: * @author Jim White
023: * Created 2001-11-14.
024: */
025: public static class LineCommenter // extends FilterSet.Filter
026: {
027: String first;
028: String last;
029: boolean comment;
030:
031: public LineCommenter() {
032: }
033:
034: public void setFirst(final String s) {
035: first = s;
036: }
037:
038: public void setLast(final String s) {
039: last = s;
040: }
041:
042: public void setComment(final boolean f) {
043: comment = f;
044: }
045:
046: public String getFirst() {
047: return first;
048: }
049:
050: public String getLast() {
051: return last;
052: }
053:
054: public boolean isComment() {
055: return comment;
056: }
057: }
058:
059: /**
060: * Gets the filter hash of the FilterSet.
061: *
062: * @return The hash of the tokens and values for quick lookup.
063: */
064: public Hashtable getCommenterHash() {
065: final int filterSize = getFilters().size();
066: final Hashtable stripperHash = new Hashtable(filterSize);
067: for (final Enumeration e = getFilters().elements(); e
068: .hasMoreElements();) {
069: LineCommenter commenter = (LineCommenter) e.nextElement();
070: stripperHash.put(commenter.getFirst(), commenter);
071: }
072: return stripperHash;
073: }
074:
075: /**
076: * Does replacement on the given string with token matching.
077: * This uses the defined begintoken and endtoken values which default to @ for both.
078: *
079: * @param line The line to process the tokens in.
080: * @return The string with the tokens replaced.
081: */
082: public String replaceTokens(String line) {
083: final String beginToken = getBeginToken();
084: final String endToken = getEndToken();
085: final int index = line.indexOf(beginToken);
086:
087: if (commentingUntil != null) {
088: log("Commenting until: " + beginToken + commentingUntil
089: + endToken, Project.MSG_VERBOSE);
090: }
091:
092: try {
093: if (index < 0) {
094: if (commentingUntil == null) {
095: return line;
096: }
097:
098: return comment(line);
099: }
100:
101: final Hashtable tokens = getCommenterHash();
102:
103: final int endIndex = line.indexOf(endToken, index
104: + beginToken.length() + 1);
105:
106: if (endIndex < 1) {
107: if (commentingUntil == null) {
108: return line;
109: }
110:
111: return comment(line);
112: }
113:
114: final String token = line.substring(index
115: + beginToken.length(), endIndex);
116:
117: if (commentingUntil == null) {
118: // We're not commenting, so look for a "first" token.
119: if (tokens.containsKey(token)) {
120: final LineCommenter commenter = (LineCommenter) tokens
121: .get(token);
122: commentingUntil = commenter.getLast();
123: commenting = commenter.isComment();
124: log("Commenting: " + beginToken + token + endToken,
125: Project.MSG_VERBOSE);
126: // We'll begin commenting with the next line.
127: }
128: } else {
129: // We're commenting until we see "commentingUntil".
130: // This means that commenters do not nest.
131: if (commentingUntil.equals(token)) {
132: log("Commenting ends with: " + beginToken + token
133: + endToken, Project.MSG_VERBOSE);
134: // We've found it.
135: // Switch back to looking for a new "first".
136: commentingUntil = null;
137: } else {
138: return comment(line);
139: }
140: }
141:
142: return line;
143: } catch (StringIndexOutOfBoundsException e) {
144: return line;
145: }
146: }
147:
148: private final static String commentString = "// ";
149:
150: private String comment(final String line) {
151: final String text = line.trim();
152: final String whitespace = line.substring(0, line
153: .lastIndexOf(text));
154:
155: if (commenting) {
156: if (text.startsWith(commentString))
157: return line;
158:
159: return whitespace + commentString + text;
160: }
161:
162: if (text.startsWith(commentString))
163: return whitespace + text.substring(commentString.length());
164:
165: return line;
166: }
167:
168: /**
169: * Create a new filter
170: *
171: * @param commenter the filter to be added
172: */
173: public void addLineCommenter(LineCommenter commenter) {
174: if (isReference()) {
175: throw noChildrenAllowed();
176: }
177: getFilters().addElement(commenter);
178: }
179:
180: }
|