1 11 12 package org.eclipse.jdt.internal.ui.text; 13 14 15 import java.io.IOException ; 16 import java.io.Reader ; 17 18 import org.eclipse.jdt.internal.corext.javadoc.SingleCharReader; 19 20 21 26 public abstract class SubstitutionTextReader extends SingleCharReader { 27 28 protected static final String LINE_DELIM= System.getProperty("line.separator", "\n"); 30 private Reader fReader; 31 protected boolean fWasWhiteSpace; 32 private int fCharAfterWhiteSpace; 33 34 37 private boolean fSkipWhiteSpace= true; 38 39 private boolean fReadFromBuffer; 40 private StringBuffer fBuffer; 41 private int fIndex; 42 43 44 protected SubstitutionTextReader(Reader reader) { 45 fReader= reader; 46 fBuffer= new StringBuffer (); 47 fIndex= 0; 48 fReadFromBuffer= false; 49 fCharAfterWhiteSpace= -1; 50 fWasWhiteSpace= true; 51 } 52 53 58 protected abstract String computeSubstitution(int c) throws IOException ; 59 60 63 protected Reader getReader() { 64 return fReader; 65 } 66 67 70 protected int nextChar() throws IOException { 71 fReadFromBuffer= (fBuffer.length() > 0); 72 if (fReadFromBuffer) { 73 char ch= fBuffer.charAt(fIndex++); 74 if (fIndex >= fBuffer.length()) { 75 fBuffer.setLength(0); 76 fIndex= 0; 77 } 78 return ch; 79 } else { 80 int ch= fCharAfterWhiteSpace; 81 if (ch == -1) { 82 ch= fReader.read(); 83 } 84 if (fSkipWhiteSpace && Character.isWhitespace((char)ch)) { 85 do { 86 ch= fReader.read(); 87 } while (Character.isWhitespace((char)ch)); 88 if (ch != -1) { 89 fCharAfterWhiteSpace= ch; 90 return ' '; 91 } 92 } else { 93 fCharAfterWhiteSpace= -1; 94 } 95 return ch; 96 } 97 } 98 99 102 public int read() throws IOException { 103 int c; 104 do { 105 106 c= nextChar(); 107 while (!fReadFromBuffer) { 108 String s= computeSubstitution(c); 109 if (s == null) 110 break; 111 if (s.length() > 0) 112 fBuffer.insert(0, s); 113 c= nextChar(); 114 } 115 116 } while (fSkipWhiteSpace && fWasWhiteSpace && (c == ' ')); 117 fWasWhiteSpace= (c == ' ' || c == '\r' || c == '\n'); 118 return c; 119 } 120 121 124 public boolean ready() throws IOException { 125 return fReader.ready(); 126 } 127 128 131 public void close() throws IOException { 132 fReader.close(); 133 } 134 135 138 public void reset() throws IOException { 139 fReader.reset(); 140 fWasWhiteSpace= true; 141 fCharAfterWhiteSpace= -1; 142 fBuffer.setLength(0); 143 fIndex= 0; 144 } 145 146 protected final void setSkipWhitespace(boolean state) { 147 fSkipWhiteSpace= state; 148 } 149 150 protected final boolean isSkippingWhitespace() { 151 return fSkipWhiteSpace; 152 } 153 } 154 | Popular Tags |