1 11 package org.eclipse.ant.internal.ui.editor.derived; 12 13 14 15 import java.io.IOException ; 16 import java.io.Reader ; 17 18 19 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 private 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 } 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 101 public int read() throws IOException { 102 int c; 103 do { 104 105 c= nextChar(); 106 while (!fReadFromBuffer) { 107 String s= computeSubstitution(c); 108 if (s == null) 109 break; 110 if (s.length() > 0) 111 fBuffer.insert(0, s); 112 c= nextChar(); 113 } 114 115 } while (fSkipWhiteSpace && fWasWhiteSpace && (c == ' ')); 116 fWasWhiteSpace= (c == ' ' || c == '\r' || c == '\n'); 117 return c; 118 } 119 120 123 public boolean ready() throws IOException { 124 return fReader.ready(); 125 } 126 127 130 public void close() throws IOException { 131 fReader.close(); 132 } 133 134 137 public void reset() throws IOException { 138 fReader.reset(); 139 fWasWhiteSpace= true; 140 fCharAfterWhiteSpace= -1; 141 fBuffer.setLength(0); 142 fIndex= 0; 143 } 144 145 protected final void setSkipWhitespace(boolean state) { 146 fSkipWhiteSpace= state; 147 } 148 149 protected final boolean isSkippingWhitespace() { 150 return fSkipWhiteSpace; 151 } 152 } 153 | Popular Tags |