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