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. import java.io.IOException;
  19. import java.io.Reader;
  20. import java.util.Vector;
  21. import org.apache.tools.ant.types.Parameter;
  22. /**
  23. * This filter strips line comments.
  24. *
  25. * Example:
  26. *
  27. * <pre><striplinecomments>
  28. * <comment value="#"/>
  29. * <comment value="--"/>
  30. * <comment value="REM "/>
  31. * <comment value="rem "/>
  32. * <comment value="//"/>
  33. * </striplinecomments></pre>
  34. *
  35. * Or:
  36. *
  37. * <pre><filterreader
  38. * classname="org.apache.tools.ant.filters.StripLineComments">
  39. * <param type="comment" value="#"/>
  40. * <param type="comment" value="--"/>
  41. * <param type="comment" value="REM "/>
  42. * <param type="comment" value="rem "/>
  43. * <param type="comment" value="//"/>
  44. * </filterreader></pre>
  45. *
  46. */
  47. public final class StripLineComments
  48. extends BaseParamFilterReader
  49. implements ChainableReader {
  50. /** Parameter name for the comment prefix. */
  51. private static final String COMMENTS_KEY = "comment";
  52. /** Vector that holds the comment prefixes. */
  53. private Vector comments = new Vector();
  54. /** The line that has been read ahead. */
  55. private String line = null;
  56. /**
  57. * Constructor for "dummy" instances.
  58. *
  59. * @see BaseFilterReader#BaseFilterReader()
  60. */
  61. public StripLineComments() {
  62. super();
  63. }
  64. /**
  65. * Creates a new filtered reader.
  66. *
  67. * @param in A Reader object providing the underlying stream.
  68. * Must not be <code>null</code>.
  69. */
  70. public StripLineComments(final Reader in) {
  71. super(in);
  72. }
  73. /**
  74. * Returns the next character in the filtered stream, only including
  75. * lines from the original stream which don't start with any of the
  76. * specified comment prefixes.
  77. *
  78. * @return the next character in the resulting stream, or -1
  79. * if the end of the resulting stream has been reached
  80. *
  81. * @exception IOException if the underlying stream throws an IOException
  82. * during reading
  83. */
  84. public final int read() throws IOException {
  85. if (!getInitialized()) {
  86. initialize();
  87. setInitialized(true);
  88. }
  89. int ch = -1;
  90. if (line != null) {
  91. ch = line.charAt(0);
  92. if (line.length() == 1) {
  93. line = null;
  94. } else {
  95. line = line.substring(1);
  96. }
  97. } else {
  98. line = readLine();
  99. final int commentsSize = comments.size();
  100. while (line != null) {
  101. for (int i = 0; i < commentsSize; i++) {
  102. String comment = (String) comments.elementAt(i);
  103. if (line.startsWith(comment)) {
  104. line = null;
  105. break;
  106. }
  107. }
  108. if (line == null) {
  109. // line started with comment
  110. line = readLine();
  111. } else {
  112. break;
  113. }
  114. }
  115. if (line != null) {
  116. return read();
  117. }
  118. }
  119. return ch;
  120. }
  121. /**
  122. * Adds a <code>comment</code> element to the list of prefixes.
  123. *
  124. * @param comment The <code>comment</code> element to add to the
  125. * list of comment prefixes to strip. Must not be <code>null</code>.
  126. */
  127. public final void addConfiguredComment(final Comment comment) {
  128. comments.addElement(comment.getValue());
  129. }
  130. /**
  131. * Sets the list of comment prefixes to strip.
  132. *
  133. * @param comments A list of strings, each of which is a prefix
  134. * for a comment line. Must not be <code>null</code>.
  135. */
  136. private void setComments(final Vector comments) {
  137. this.comments = comments;
  138. }
  139. /**
  140. * Returns the list of comment prefixes to strip.
  141. *
  142. * @return the list of comment prefixes to strip.
  143. */
  144. private final Vector getComments() {
  145. return comments;
  146. }
  147. /**
  148. * Creates a new StripLineComments using the passed in
  149. * Reader for instantiation.
  150. *
  151. * @param rdr A Reader object providing the underlying stream.
  152. * Must not be <code>null</code>.
  153. *
  154. * @return a new filter based on this configuration, but filtering
  155. * the specified reader
  156. */
  157. public final Reader chain(final Reader rdr) {
  158. StripLineComments newFilter = new StripLineComments(rdr);
  159. newFilter.setComments(getComments());
  160. newFilter.setInitialized(true);
  161. return newFilter;
  162. }
  163. /**
  164. * Parses the parameters to set the comment prefixes.
  165. */
  166. private final void initialize() {
  167. Parameter[] params = getParameters();
  168. if (params != null) {
  169. for (int i = 0; i < params.length; i++) {
  170. if (COMMENTS_KEY.equals(params[i].getType())) {
  171. comments.addElement(params[i].getValue());
  172. }
  173. }
  174. }
  175. }
  176. /**
  177. * The class that holds a comment representation.
  178. */
  179. public static class Comment {
  180. /** The prefix for a line comment. */
  181. private String value;
  182. /**
  183. * Sets the prefix for this type of line comment.
  184. *
  185. * @param comment The prefix for a line comment of this type.
  186. * Must not be <code>null</code>.
  187. */
  188. public final void setValue(String comment) {
  189. value = comment;
  190. }
  191. /**
  192. * Returns the prefix for this type of line comment.
  193. *
  194. * @return the prefix for this type of line comment.
  195. */
  196. public final String getValue() {
  197. return value;
  198. }
  199. }
  200. }