1 18 package org.apache.tools.ant.filters; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 23 31 public final class StripJavaComments 32 extends BaseFilterReader 33 implements ChainableReader { 34 35 40 private int readAheadCh = -1; 41 42 46 private boolean inString = false; 47 48 51 private boolean quoted = false; 52 53 58 public StripJavaComments() { 59 super(); 60 } 61 62 68 public StripJavaComments(final Reader in) { 69 super(in); 70 } 71 72 82 public int read() throws IOException { 83 int ch = -1; 84 if (readAheadCh != -1) { 85 ch = readAheadCh; 86 readAheadCh = -1; 87 } else { 88 ch = in.read(); 89 if (ch == '"' && !quoted) { 90 inString = !inString; 91 quoted = false; 92 } else if (ch == '\\') { 93 quoted = !quoted; 94 } else { 95 quoted = false; 96 if (!inString) { 97 if (ch == '/') { 98 ch = in.read(); 99 if (ch == '/') { 100 while (ch != '\n' && ch != -1 && ch != '\r') { 101 ch = in.read(); 102 } 103 } else if (ch == '*') { 104 while (ch != -1) { 105 ch = in.read(); 106 if (ch == '*') { 107 ch = in.read(); 108 while (ch == '*' && ch != -1) { 109 ch = in.read(); 110 } 111 112 if (ch == '/') { 113 ch = read(); 114 break; 115 } 116 } 117 } 118 } else { 119 readAheadCh = ch; 120 ch = '/'; 121 } 122 } 123 } 124 } 125 } 126 127 return ch; 128 } 129 130 140 141 public Reader chain(final Reader rdr) { 142 StripJavaComments newFilter = new StripJavaComments(rdr); 143 return newFilter; 144 } 145 } 146 | Popular Tags |