KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > StripLineComments


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.filters;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.util.Vector JavaDoc;
23 import org.apache.tools.ant.types.Parameter;
24
25 /**
26  * This filter strips line comments.
27  *
28  * Example:
29  *
30  * <pre>&lt;striplinecomments&gt;
31  * &lt;comment value=&quot;#&quot;/&gt;
32  * &lt;comment value=&quot;--&quot;/&gt;
33  * &lt;comment value=&quot;REM &quot;/&gt;
34  * &lt;comment value=&quot;rem &quot;/&gt;
35  * &lt;comment value=&quot;//&quot;/&gt;
36  * &lt;/striplinecomments&gt;</pre>
37  *
38  * Or:
39  *
40  * <pre>&lt;filterreader
41  * classname=&quot;org.apache.tools.ant.filters.StripLineComments&quot;&gt;
42  * &lt;param type=&quot;comment&quot; value="#&quot;/&gt;
43  * &lt;param type=&quot;comment&quot; value=&quot;--&quot;/&gt;
44  * &lt;param type=&quot;comment&quot; value=&quot;REM &quot;/&gt;
45  * &lt;param type=&quot;comment&quot; value=&quot;rem &quot;/&gt;
46  * &lt;param type=&quot;comment&quot; value=&quot;//&quot;/&gt;
47  * &lt;/filterreader&gt;</pre>
48  *
49  */

50 public final class StripLineComments
51     extends BaseParamFilterReader
52     implements ChainableReader {
53     /** Parameter name for the comment prefix. */
54     private static final String JavaDoc COMMENTS_KEY = "comment";
55
56     /** Vector that holds the comment prefixes. */
57     private Vector JavaDoc comments = new Vector JavaDoc();
58
59     /** The line that has been read ahead. */
60     private String JavaDoc line = null;
61
62     /**
63      * Constructor for "dummy" instances.
64      *
65      * @see BaseFilterReader#BaseFilterReader()
66      */

67     public StripLineComments() {
68         super();
69     }
70
71     /**
72      * Creates a new filtered reader.
73      *
74      * @param in A Reader object providing the underlying stream.
75      * Must not be <code>null</code>.
76      */

77     public StripLineComments(final Reader JavaDoc in) {
78         super(in);
79     }
80
81     /**
82      * Returns the next character in the filtered stream, only including
83      * lines from the original stream which don't start with any of the
84      * specified comment prefixes.
85      *
86      * @return the next character in the resulting stream, or -1
87      * if the end of the resulting stream has been reached
88      *
89      * @exception IOException if the underlying stream throws an IOException
90      * during reading
91      */

92     public int read() throws IOException JavaDoc {
93         if (!getInitialized()) {
94             initialize();
95             setInitialized(true);
96         }
97
98         int ch = -1;
99
100         if (line != null) {
101             ch = line.charAt(0);
102             if (line.length() == 1) {
103                 line = null;
104             } else {
105                 line = line.substring(1);
106             }
107         } else {
108             line = readLine();
109             final int commentsSize = comments.size();
110
111             while (line != null) {
112                 for (int i = 0; i < commentsSize; i++) {
113                     String JavaDoc comment = (String JavaDoc) comments.elementAt(i);
114                     if (line.startsWith(comment)) {
115                         line = null;
116                         break;
117                     }
118                 }
119
120                 if (line == null) {
121                     // line started with comment
122
line = readLine();
123                 } else {
124                     break;
125                 }
126             }
127
128             if (line != null) {
129                 return read();
130             }
131         }
132
133         return ch;
134     }
135
136     /**
137      * Adds a <code>comment</code> element to the list of prefixes.
138      *
139      * @param comment The <code>comment</code> element to add to the
140      * list of comment prefixes to strip. Must not be <code>null</code>.
141      */

142     public void addConfiguredComment(final Comment comment) {
143         comments.addElement(comment.getValue());
144     }
145
146     /**
147      * Sets the list of comment prefixes to strip.
148      *
149      * @param comments A list of strings, each of which is a prefix
150      * for a comment line. Must not be <code>null</code>.
151      */

152     private void setComments(final Vector JavaDoc comments) {
153         this.comments = comments;
154     }
155
156     /**
157      * Returns the list of comment prefixes to strip.
158      *
159      * @return the list of comment prefixes to strip.
160      */

161     private Vector JavaDoc getComments() {
162         return comments;
163     }
164
165     /**
166      * Creates a new StripLineComments using the passed in
167      * Reader for instantiation.
168      *
169      * @param rdr A Reader object providing the underlying stream.
170      * Must not be <code>null</code>.
171      *
172      * @return a new filter based on this configuration, but filtering
173      * the specified reader
174      */

175     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
176         StripLineComments newFilter = new StripLineComments(rdr);
177         newFilter.setComments(getComments());
178         newFilter.setInitialized(true);
179         return newFilter;
180     }
181
182     /**
183      * Parses the parameters to set the comment prefixes.
184      */

185     private void initialize() {
186         Parameter[] params = getParameters();
187         if (params != null) {
188             for (int i = 0; i < params.length; i++) {
189                 if (COMMENTS_KEY.equals(params[i].getType())) {
190                     comments.addElement(params[i].getValue());
191                 }
192             }
193         }
194     }
195
196     /**
197      * The class that holds a comment representation.
198      */

199     public static class Comment {
200
201         /** The prefix for a line comment. */
202         private String JavaDoc value;
203
204         /**
205          * Sets the prefix for this type of line comment.
206          *
207          * @param comment The prefix for a line comment of this type.
208          * Must not be <code>null</code>.
209          */

210         public final void setValue(String JavaDoc comment) {
211             value = comment;
212         }
213
214         /**
215          * Returns the prefix for this type of line comment.
216          *
217          * @return the prefix for this type of line comment.
218          */

219         public final String JavaDoc getValue() {
220             return value;
221         }
222     }
223 }
224
Popular Tags