1 12 package org.eclipse.jdt.internal.formatter.comment; 13 14 import java.io.IOException ; 15 import java.io.Reader ; 16 17 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper; 18 19 24 public abstract class SubstitutionTextReader extends Reader { 25 26 private Reader fReader; 27 private boolean fWasWhiteSpace; 28 private int fCharAfterWhiteSpace; 29 30 33 private boolean fSkipWhiteSpace= true; 34 35 private boolean fReadFromBuffer; 36 private StringBuffer fBuffer; 37 private int fIndex; 38 39 40 protected SubstitutionTextReader(Reader reader) { 41 fReader= reader; 42 fBuffer= new StringBuffer (); 43 fIndex= 0; 44 fReadFromBuffer= false; 45 fCharAfterWhiteSpace= -1; 46 fWasWhiteSpace= true; 47 } 48 49 52 public String getString() throws IOException { 53 StringBuffer buf= new StringBuffer (); 54 int ch; 55 while ((ch= read()) != -1) { 56 buf.append((char)ch); 57 } 58 return buf.toString(); 59 } 60 61 66 protected abstract String computeSubstitution(int c) throws IOException ; 67 68 71 protected Reader getReader() { 72 return fReader; 73 } 74 75 78 protected int nextChar() throws IOException { 79 fReadFromBuffer= (fBuffer.length() > 0); 80 if (fReadFromBuffer) { 81 char ch= fBuffer.charAt(fIndex++); 82 if (fIndex >= fBuffer.length()) { 83 fBuffer.setLength(0); 84 fIndex= 0; 85 } 86 return ch; 87 } else { 88 int ch= fCharAfterWhiteSpace; 89 if (ch == -1) { 90 ch= fReader.read(); 91 } 92 if (fSkipWhiteSpace && ScannerHelper.isWhitespace((char)ch)) { 93 do { 94 ch= fReader.read(); 95 } while (ScannerHelper.isWhitespace((char)ch)); 96 if (ch != -1) { 97 fCharAfterWhiteSpace= ch; 98 return ' '; 99 } 100 } else { 101 fCharAfterWhiteSpace= -1; 102 } 103 return ch; 104 } 105 } 106 107 110 public int read() throws IOException { 111 int c; 112 do { 113 114 c= nextChar(); 115 while (!fReadFromBuffer && c != -1) { 116 String s= computeSubstitution(c); 117 if (s == null) 118 break; 119 if (s.length() > 0) 120 fBuffer.insert(0, s); 121 c= nextChar(); 122 } 123 124 } while (fSkipWhiteSpace && fWasWhiteSpace && (c == ' ')); 125 fWasWhiteSpace= (c == ' ' || c == '\r' || c == '\n'); 126 return c; 127 } 128 129 132 public int read(char cbuf[], int off, int len) throws IOException { 133 int end= off + len; 134 for (int i= off; i < end; i++) { 135 int ch= read(); 136 if (ch == -1) { 137 if (i == off) { 138 return -1; 139 } else { 140 return i - off; 141 } 142 } 143 cbuf[i]= (char)ch; 144 } 145 return len; 146 } 147 148 151 public boolean ready() throws IOException { 152 return fReader.ready(); 153 } 154 155 158 public void close() throws IOException { 159 fReader.close(); 160 } 161 162 165 public void reset() throws IOException { 166 fReader.reset(); 167 fWasWhiteSpace= true; 168 fCharAfterWhiteSpace= -1; 169 fBuffer.setLength(0); 170 fIndex= 0; 171 } 172 173 protected final void setSkipWhitespace(boolean state) { 174 fSkipWhiteSpace= state; 175 } 176 177 protected final boolean isSkippingWhitespace() { 178 return fSkipWhiteSpace; 179 } 180 } 181 | Popular Tags |