001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.filters;
019:
020: import java.io.IOException;
021: import java.io.Reader;
022: import java.util.Vector;
023: import org.apache.tools.ant.types.Parameter;
024:
025: /**
026: * This filter strips line comments.
027: *
028: * Example:
029: *
030: * <pre><striplinecomments>
031: * <comment value="#"/>
032: * <comment value="--"/>
033: * <comment value="REM "/>
034: * <comment value="rem "/>
035: * <comment value="//"/>
036: * </striplinecomments></pre>
037: *
038: * Or:
039: *
040: * <pre><filterreader
041: * classname="org.apache.tools.ant.filters.StripLineComments">
042: * <param type="comment" value="#"/>
043: * <param type="comment" value="--"/>
044: * <param type="comment" value="REM "/>
045: * <param type="comment" value="rem "/>
046: * <param type="comment" value="//"/>
047: * </filterreader></pre>
048: *
049: */
050: public final class StripLineComments extends BaseParamFilterReader
051: implements ChainableReader {
052: /** Parameter name for the comment prefix. */
053: private static final String COMMENTS_KEY = "comment";
054:
055: /** Vector that holds the comment prefixes. */
056: private Vector comments = new Vector();
057:
058: /** The line that has been read ahead. */
059: private String line = null;
060:
061: /**
062: * Constructor for "dummy" instances.
063: *
064: * @see BaseFilterReader#BaseFilterReader()
065: */
066: public StripLineComments() {
067: super ();
068: }
069:
070: /**
071: * Creates a new filtered reader.
072: *
073: * @param in A Reader object providing the underlying stream.
074: * Must not be <code>null</code>.
075: */
076: public StripLineComments(final Reader in) {
077: super (in);
078: }
079:
080: /**
081: * Returns the next character in the filtered stream, only including
082: * lines from the original stream which don't start with any of the
083: * specified comment prefixes.
084: *
085: * @return the next character in the resulting stream, or -1
086: * if the end of the resulting stream has been reached
087: *
088: * @exception IOException if the underlying stream throws an IOException
089: * during reading
090: */
091: public int read() throws IOException {
092: if (!getInitialized()) {
093: initialize();
094: setInitialized(true);
095: }
096:
097: int ch = -1;
098:
099: 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 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 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 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 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: }
|