source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/filters/StripLineComments.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 6.6 KB
Line 
1/*
2 * Copyright 2002-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.filters;
18
19import java.io.IOException;
20import java.io.Reader;
21import java.util.Vector;
22import org.apache.tools.ant.types.Parameter;
23
24/**
25 * This filter strips line comments.
26 *
27 * Example:
28 *
29 * <pre>&lt;striplinecomments&gt;
30 * &lt;comment value=&quot;#&quot;/&gt;
31 * &lt;comment value=&quot;--&quot;/&gt;
32 * &lt;comment value=&quot;REM &quot;/&gt;
33 * &lt;comment value=&quot;rem &quot;/&gt;
34 * &lt;comment value=&quot;//&quot;/&gt;
35 * &lt;/striplinecomments&gt;</pre>
36 *
37 * Or:
38 *
39 * <pre>&lt;filterreader
40 * classname=&quot;org.apache.tools.ant.filters.StripLineComments&quot;&gt;
41 * &lt;param type=&quot;comment&quot; value="#&quot;/&gt;
42 * &lt;param type=&quot;comment&quot; value=&quot;--&quot;/&gt;
43 * &lt;param type=&quot;comment&quot; value=&quot;REM &quot;/&gt;
44 * &lt;param type=&quot;comment&quot; value=&quot;rem &quot;/&gt;
45 * &lt;param type=&quot;comment&quot; value=&quot;//&quot;/&gt;
46 * &lt;/filterreader&gt;</pre>
47 *
48 */
49public final class StripLineComments
50 extends BaseParamFilterReader
51 implements ChainableReader {
52 /** Parameter name for the comment prefix. */
53 private static final String COMMENTS_KEY = "comment";
54
55 /** Vector that holds the comment prefixes. */
56 private Vector comments = new Vector();
57
58 /** The line that has been read ahead. */
59 private String line = null;
60
61 /**
62 * Constructor for "dummy" instances.
63 *
64 * @see BaseFilterReader#BaseFilterReader()
65 */
66 public StripLineComments() {
67 super();
68 }
69
70 /**
71 * Creates a new filtered reader.
72 *
73 * @param in A Reader object providing the underlying stream.
74 * Must not be <code>null</code>.
75 */
76 public StripLineComments(final Reader in) {
77 super(in);
78 }
79
80 /**
81 * Returns the next character in the filtered stream, only including
82 * lines from the original stream which don't start with any of the
83 * specified comment prefixes.
84 *
85 * @return the next character in the resulting stream, or -1
86 * if the end of the resulting stream has been reached
87 *
88 * @exception IOException if the underlying stream throws an IOException
89 * during reading
90 */
91 public final int read() throws IOException {
92 if (!getInitialized()) {
93 initialize();
94 setInitialized(true);
95 }
96
97 int ch = -1;
98
99 if (line != null) {
100 ch = line.charAt(0);
101 if (line.length() == 1) {
102 line = null;
103 } else {
104 line = line.substring(1);
105 }
106 } else {
107 line = readLine();
108 final int commentsSize = comments.size();
109
110 while (line != null) {
111 for (int i = 0; i < commentsSize; i++) {
112 String comment = (String) comments.elementAt(i);
113 if (line.startsWith(comment)) {
114 line = null;
115 break;
116 }
117 }
118
119 if (line == null) {
120 // line started with comment
121 line = readLine();
122 } else {
123 break;
124 }
125 }
126
127 if (line != null) {
128 return read();
129 }
130 }
131
132 return ch;
133 }
134
135 /**
136 * Adds a <code>comment</code> element to the list of prefixes.
137 *
138 * @param comment The <code>comment</code> element to add to the
139 * list of comment prefixes to strip. Must not be <code>null</code>.
140 */
141 public final void addConfiguredComment(final Comment comment) {
142 comments.addElement(comment.getValue());
143 }
144
145 /**
146 * Sets the list of comment prefixes to strip.
147 *
148 * @param comments A list of strings, each of which is a prefix
149 * for a comment line. Must not be <code>null</code>.
150 */
151 private void setComments(final Vector comments) {
152 this.comments = comments;
153 }
154
155 /**
156 * Returns the list of comment prefixes to strip.
157 *
158 * @return the list of comment prefixes to strip.
159 */
160 private final Vector getComments() {
161 return comments;
162 }
163
164 /**
165 * Creates a new StripLineComments using the passed in
166 * Reader for instantiation.
167 *
168 * @param rdr A Reader object providing the underlying stream.
169 * Must not be <code>null</code>.
170 *
171 * @return a new filter based on this configuration, but filtering
172 * the specified reader
173 */
174 public final Reader chain(final Reader rdr) {
175 StripLineComments newFilter = new StripLineComments(rdr);
176 newFilter.setComments(getComments());
177 newFilter.setInitialized(true);
178 return newFilter;
179 }
180
181 /**
182 * Parses the parameters to set the comment prefixes.
183 */
184 private final void initialize() {
185 Parameter[] params = getParameters();
186 if (params != null) {
187 for (int i = 0; i < params.length; i++) {
188 if (COMMENTS_KEY.equals(params[i].getType())) {
189 comments.addElement(params[i].getValue());
190 }
191 }
192 }
193 }
194
195 /**
196 * The class that holds a comment representation.
197 */
198 public static class Comment {
199
200 /** The prefix for a line comment. */
201 private String value;
202
203 /**
204 * Sets the prefix for this type of line comment.
205 *
206 * @param comment The prefix for a line comment of this type.
207 * Must not be <code>null</code>.
208 */
209 public final void setValue(String comment) {
210 value = comment;
211 }
212
213 /**
214 * Returns the prefix for this type of line comment.
215 *
216 * @return the prefix for this type of line comment.
217 */
218 public final String getValue() {
219 return value;
220 }
221 }
222}
Note: See TracBrowser for help on using the repository browser.