KickJava   Java API By Example, From Geeks To Geeks.

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


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 LineStripperSet 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 strippingUntil = null;
15     
16     public LineStripperSet()
17     {
18     }
19     
20     /**
21      * Individual filter component of filterset
22      *
23      * @author Jim White
24      * Created 2001-11-14.
25      */

26     public static class LineStripper // extends FilterSet.Filter
27
{
28         String JavaDoc first;
29         String JavaDoc last;
30         
31         public LineStripper()
32         {
33         }
34         
35         public void setFirst(final String JavaDoc s)
36         {
37             first = s;
38         }
39         
40         public void setLast(final String JavaDoc s)
41         {
42             last = s;
43         }
44         
45         public String JavaDoc getFirst()
46         {
47             return first;
48         }
49         
50         public String JavaDoc getLast()
51         {
52             return last;
53         }
54         
55     }
56     
57     /**
58      * Gets the filter hash of the FilterSet.
59      *
60      * @return The hash of the tokens and values for quick lookup.
61      */

62     public Hashtable JavaDoc getStripperHash() {
63         final int filterSize = getFilters().size();
64         final Hashtable JavaDoc stripperHash = new Hashtable JavaDoc(filterSize);
65         for (final Enumeration JavaDoc e = getFilters().elements(); e.hasMoreElements();) {
66            LineStripper stripper = (LineStripper) e.nextElement();
67            stripperHash.put(stripper.getFirst(), stripper.getLast());
68         }
69         return stripperHash;
70     }
71     
72     /**
73      * Does replacement on the given string with token matching.
74      * This uses the defined begintoken and endtoken values which default to @ for both.
75      *
76      * @param line The line to process the tokens in.
77      * @return The string with the tokens replaced.
78      */

79     public String JavaDoc replaceTokens(String JavaDoc line)
80     {
81         final String JavaDoc beginToken = getBeginToken();
82         final String JavaDoc endToken = getEndToken();
83         int index = line.indexOf(beginToken);
84         
85         if (strippingUntil != null) {
86             log( "Stripping until: " + beginToken + strippingUntil + endToken
87                , Project.MSG_VERBOSE );
88         }
89            
90         if (index < 0) {
91            if (strippingUntil == null) {
92                return line;
93            }
94            
95            return "";
96         }
97             
98         final Hashtable JavaDoc tokens = getStripperHash();
99         try {
100             StringBuffer JavaDoc b = new StringBuffer JavaDoc();
101             int i = 0;
102             String JavaDoc token = null;
103             
104             do {
105                 int endIndex = line.indexOf(endToken, index + beginToken.length() + 1 );
106                 if (endIndex == -1) {
107                     break;
108                 }
109                 token = line.substring(index + beginToken.length(), endIndex );
110                 
111                 if (strippingUntil == null) {
112                     // We're not stripping, so look for a "first" token.
113
b.append(line.substring(i, index));
114                     if (tokens.containsKey(token)) {
115                         strippingUntil = (String JavaDoc) tokens.get(token);
116                         log( "Stripping: " + beginToken + token + endToken, Project.MSG_VERBOSE );
117                         i = index + beginToken.length() + token.length() + endToken.length();
118                     } else {
119                         // just append beginToken and search further
120
b.append(beginToken);
121                         i = index + beginToken.length();
122                     }
123                 } else {
124                     // We're stripping until we see "strippingUntil".
125
// This means that strippers do not nest.
126
// That could be an option later.
127
if (strippingUntil.equals(token)) {
128                         log( "Stripping ends with: " + beginToken + token + endToken
129                            , Project.MSG_VERBOSE );
130                         // We've found it.
131
// Skip over the token.
132
i = index + beginToken.length() + token.length() + endToken.length();
133                         // Switch back to looking for a new "first".
134
strippingUntil = null;
135                     } else {
136                         // Keep looking.
137
i = index + beginToken.length();
138                     }
139                 }
140             } while ((index = line.indexOf( beginToken, i )) > -1 );
141             
142             b.append(line.substring(i));
143             return b.toString();
144         } catch (StringIndexOutOfBoundsException JavaDoc e) {
145             return line;
146         }
147     }
148     
149     /**
150      * Create a new filter
151      *
152      * @param stripper the filter to be added
153      */

154     public void addLineStripper(LineStripper stripper)
155     {
156         if (isReference()) {
157             throw noChildrenAllowed();
158         }
159         getFilters().addElement(stripper);
160     }
161     
162 }
163
Popular Tags