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. /**
  21. * This is a Java comment and string stripper reader that filters
  22. * those lexical tokens out for purposes of simple Java parsing.
  23. * (if you have more complex Java parsing needs, use a real lexer).
  24. * Since this class heavily relies on the single char read function,
  25. * you are recommended to make it work on top of a buffered reader.
  26. *
  27. */
  28. public final class StripJavaComments
  29. extends BaseFilterReader
  30. implements ChainableReader {
  31. /**
  32. * The read-ahead character, used for effectively pushing a single
  33. * character back. A value of -1 indicates that no character is in the
  34. * buffer.
  35. */
  36. private int readAheadCh = -1;
  37. /**
  38. * Whether or not the parser is currently in the middle of a string
  39. * literal.
  40. */
  41. private boolean inString = false;
  42. /**
  43. * Whether or not the last char has been a backslash.
  44. */
  45. private boolean quoted = false;
  46. /**
  47. * Constructor for "dummy" instances.
  48. *
  49. * @see BaseFilterReader#BaseFilterReader()
  50. */
  51. public StripJavaComments() {
  52. super();
  53. }
  54. /**
  55. * Creates a new filtered reader.
  56. *
  57. * @param in A Reader object providing the underlying stream.
  58. * Must not be <code>null</code>.
  59. */
  60. public StripJavaComments(final Reader in) {
  61. super(in);
  62. }
  63. /**
  64. * Returns the next character in the filtered stream, not including
  65. * Java comments.
  66. *
  67. * @return the next character in the resulting stream, or -1
  68. * if the end of the resulting stream has been reached
  69. *
  70. * @exception IOException if the underlying stream throws an IOException
  71. * during reading
  72. */
  73. public final int read() throws IOException {
  74. int ch = -1;
  75. if (readAheadCh != -1) {
  76. ch = readAheadCh;
  77. readAheadCh = -1;
  78. } else {
  79. ch = in.read();
  80. if (ch == '"' && !quoted) {
  81. inString = !inString;
  82. quoted = false;
  83. } else if (ch == '\\') {
  84. quoted = !quoted;
  85. } else {
  86. quoted = false;
  87. if (!inString) {
  88. if (ch == '/') {
  89. ch = in.read();
  90. if (ch == '/') {
  91. while (ch != '\n' && ch != -1 && ch != '\r') {
  92. ch = in.read();
  93. }
  94. } else if (ch == '*') {
  95. while (ch != -1) {
  96. ch = in.read();
  97. if (ch == '*') {
  98. ch = in.read();
  99. while (ch == '*' && ch != -1) {
  100. ch = in.read();
  101. }
  102. if (ch == '/') {
  103. ch = read();
  104. break;
  105. }
  106. }
  107. }
  108. } else {
  109. readAheadCh = ch;
  110. ch = '/';
  111. }
  112. }
  113. }
  114. }
  115. }
  116. return ch;
  117. }
  118. /**
  119. * Creates a new StripJavaComments using the passed in
  120. * Reader for instantiation.
  121. *
  122. * @param rdr A Reader object providing the underlying stream.
  123. * Must not be <code>null</code>.
  124. *
  125. * @return a new filter based on this configuration, but filtering
  126. * the specified reader
  127. */
  128. public final Reader chain(final Reader rdr) {
  129. StripJavaComments newFilter = new StripJavaComments(rdr);
  130. return newFilter;
  131. }
  132. }