KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > ant > LineCommenterSet


1 package gnu.kawa.ant;
2
3 import org.apache.tools.ant.types.FilterSet;
4
5 import org.apache.tools.ant.Project;
6
7 import java.util.Enumeration JavaDoc;
8 import java.util.Hashtable JavaDoc;
9
10 public class LineCommenterSet extends FilterSet
11 {
12     // The replaceTokens method is stateful.
13
// If this is not null, then we ignore all lines until we find that token.
14
private String JavaDoc commentingUntil = null;
15     private boolean commenting = false;
16     
17     public LineCommenterSet()
18     {
19     }
20     
21     /**
22      * Individual filter component of filterset
23      *
24      * @author Jim White
25      * Created 2001-11-14.
26      */

27     public static class LineCommenter // extends FilterSet.Filter
28
{
29         String JavaDoc first;
30         String JavaDoc last;
31         boolean comment;
32         
33         public LineCommenter()
34         {
35         }
36         
37         public void setFirst(final String JavaDoc s)
38         {
39             first = s;
40         }
41         
42         public void setLast(final String JavaDoc s)
43         {
44             last = s;
45         }
46         
47         public void setComment(final boolean f)
48         {
49             comment = f;
50         }
51         
52         public String JavaDoc getFirst()
53         {
54             return first;
55         }
56         
57         public String JavaDoc getLast()
58         {
59             return last;
60         }
61         
62         public boolean isComment()
63         {
64             return comment;
65         }
66     }
67     
68     /**
69      * Gets the filter hash of the FilterSet.
70      *
71      * @return The hash of the tokens and values for quick lookup.
72      */

73     public Hashtable JavaDoc getCommenterHash() {
74         final int filterSize = getFilters().size();
75         final Hashtable JavaDoc stripperHash = new Hashtable JavaDoc(filterSize);
76         for (final Enumeration JavaDoc e = getFilters().elements(); e.hasMoreElements();) {
77            LineCommenter commenter = (LineCommenter) e.nextElement();
78            stripperHash.put(commenter.getFirst(), commenter);
79         }
80         return stripperHash;
81     }
82     
83     /**
84      * Does replacement on the given string with token matching.
85      * This uses the defined begintoken and endtoken values which default to @ for both.
86      *
87      * @param line The line to process the tokens in.
88      * @return The string with the tokens replaced.
89      */

90     public String JavaDoc replaceTokens(String JavaDoc line)
91     {
92         final String JavaDoc beginToken = getBeginToken();
93         final String JavaDoc endToken = getEndToken();
94         final int index = line.indexOf(beginToken);
95         
96         if (commentingUntil != null) {
97             log( "Commenting until: " + beginToken + commentingUntil + endToken
98                , Project.MSG_VERBOSE );
99         }
100            
101         try {
102             if (index < 0) {
103                if (commentingUntil == null) {
104                    return line;
105                }
106                
107                return comment(line);
108             }
109                 
110             final Hashtable JavaDoc tokens = getCommenterHash();
111         
112             final int endIndex = line.indexOf(endToken, index + beginToken.length() + 1 );
113
114             if (endIndex < 1) {
115                if (commentingUntil == null) {
116                    return line;
117                }
118                
119                return comment(line);
120             }
121             
122             final String JavaDoc token = line.substring(index + beginToken.length(), endIndex );
123             
124             if (commentingUntil == null) {
125                 // We're not commenting, so look for a "first" token.
126
if (tokens.containsKey(token)) {
127                     final LineCommenter commenter = (LineCommenter) tokens.get(token);
128                     commentingUntil = commenter.getLast();
129                     commenting = commenter.isComment();
130                     log( "Commenting: " + beginToken + token + endToken, Project.MSG_VERBOSE );
131                     // We'll begin commenting with the next line.
132
}
133             } else {
134                 // We're commenting until we see "commentingUntil".
135
// This means that commenters do not nest.
136
if (commentingUntil.equals(token)) {
137                     log( "Commenting ends with: " + beginToken + token + endToken
138                        , Project.MSG_VERBOSE );
139                     // We've found it.
140
// Switch back to looking for a new "first".
141
commentingUntil = null;
142                 } else {
143                     return comment(line);
144                 }
145             }
146             
147             return line;
148         } catch (StringIndexOutOfBoundsException JavaDoc e) {
149             return line;
150         }
151     }
152     
153     private final static String JavaDoc commentString = "// ";
154     
155     private String JavaDoc comment(final String JavaDoc line)
156     {
157       final String JavaDoc text = line.trim();
158       final String JavaDoc whitespace = line.substring(0, line.lastIndexOf(text));
159       
160       if (commenting) {
161           if (text.startsWith(commentString))
162              return line;
163           
164           return whitespace + commentString + text;
165       }
166        
167       if (text.startsWith(commentString))
168          return whitespace + text.substring(commentString.length());
169        
170       return line;
171     }
172     
173     /**
174      * Create a new filter
175      *
176      * @param commenter the filter to be added
177      */

178     public void addLineCommenter(LineCommenter commenter)
179     {
180         if (isReference()) {
181             throw noChildrenAllowed();
182         }
183         getFilters().addElement(commenter);
184     }
185     
186 }
187
Popular Tags