1 11 12 package org.eclipse.pde.internal.ui.editor.contentassist.display; 13 14 15 import java.io.IOException ; 16 import java.io.Reader ; 17 18 19 24 abstract class SubstitutionTextReader extends SingleCharReader { 25 26 protected static final String LINE_DELIM= System.getProperty("line.separator", "\n"); 28 private Reader fReader; 29 private boolean fWasWhiteSpace; 30 private int fCharAfterWhiteSpace; 31 32 35 private boolean fSkipWhiteSpace= true; 36 37 private boolean fReadFromBuffer; 38 private StringBuffer fBuffer; 39 private int fIndex; 40 41 42 protected SubstitutionTextReader(Reader reader) { 43 fReader= reader; 44 fBuffer= new StringBuffer (); 45 fIndex= 0; 46 fReadFromBuffer= false; 47 fCharAfterWhiteSpace= -1; 48 fWasWhiteSpace= true; 49 } 50 51 60 protected abstract String computeSubstitution(int c) throws IOException ; 61 62 67 protected Reader getReader() { 68 return fReader; 69 } 70 71 76 protected int nextChar() throws IOException { 77 fReadFromBuffer= (fBuffer.length() > 0); 78 if (fReadFromBuffer) { 79 char ch= fBuffer.charAt(fIndex++); 80 if (fIndex >= fBuffer.length()) { 81 fBuffer.setLength(0); 82 fIndex= 0; 83 } 84 return ch; 85 } 86 87 int ch= fCharAfterWhiteSpace; 88 if (ch == -1) { 89 ch= fReader.read(); 90 } 91 if (fSkipWhiteSpace && Character.isWhitespace((char)ch)) { 92 do { 93 ch= fReader.read(); 94 } while (Character.isWhitespace((char)ch)); 95 if (ch != -1) { 96 fCharAfterWhiteSpace= ch; 97 return ' '; 98 } 99 } else { 100 fCharAfterWhiteSpace= -1; 101 } 102 return ch; 103 } 104 105 108 public int read() throws IOException { 109 int c; 110 do { 111 112 c= nextChar(); 113 while (!fReadFromBuffer) { 114 String s= computeSubstitution(c); 115 if (s == null) 116 break; 117 if (s.length() > 0) 118 fBuffer.insert(0, s); 119 c= nextChar(); 120 } 121 122 } while (fSkipWhiteSpace && fWasWhiteSpace && (c == ' ')); 123 fWasWhiteSpace= (c == ' ' || c == '\r' || c == '\n'); 124 return c; 125 } 126 127 130 public boolean ready() throws IOException { 131 return fReader.ready(); 132 } 133 134 137 public void close() throws IOException { 138 fReader.close(); 139 } 140 141 144 public void reset() throws IOException { 145 fReader.reset(); 146 fWasWhiteSpace= true; 147 fCharAfterWhiteSpace= -1; 148 fBuffer.setLength(0); 149 fIndex= 0; 150 } 151 152 protected final void setSkipWhitespace(boolean state) { 153 fSkipWhiteSpace= state; 154 } 155 156 protected final boolean isSkippingWhitespace() { 157 return fSkipWhiteSpace; 158 } 159 } 160 | Popular Tags |