1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
9 IBM Corporation - Initial implementation
10 **********************************************************************/
12 package net.sourceforge.phpdt.internal.ui.text;
14 import java.io.IOException;
15 import java.io.Reader;
17 import net.sourceforge.phpdt.internal.corext.phpdoc.SingleCharReader;
20 * Reads the text contents from a reader and computes for each character a
21 * potential substitution. The substitution may eat more characters than only
22 * the one passed into the computation routine.
24 public abstract class SubstitutionTextReader extends SingleCharReader {
26 protected static final String LINE_DELIM = System.getProperty(
27 "line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
29 private Reader fReader;
31 private boolean fWasWhiteSpace;
33 private int fCharAfterWhiteSpace;
36 * Tells whether white space characters are skipped.
38 private boolean fSkipWhiteSpace = true;
40 private boolean fReadFromBuffer;
42 private StringBuffer fBuffer;
46 protected SubstitutionTextReader(Reader reader) {
48 fBuffer = new StringBuffer();
50 fReadFromBuffer = false;
51 fCharAfterWhiteSpace = -1;
52 fWasWhiteSpace = true;
56 * Implement to compute the substitution for the given character and if
57 * necessary subsequent characters. Use <code>nextChar</code> to read
58 * subsequent characters.
60 protected abstract String computeSubstitution(int c) throws IOException;
63 * Returns the internal reader.
65 protected Reader getReader() {
70 * Returns the next character.
72 protected int nextChar() throws IOException {
73 fReadFromBuffer = (fBuffer.length() > 0);
74 if (fReadFromBuffer) {
75 char ch = fBuffer.charAt(fIndex++);
76 if (fIndex >= fBuffer.length()) {
82 int ch = fCharAfterWhiteSpace;
86 if (fSkipWhiteSpace && Character.isWhitespace((char) ch)) {
89 } while (Character.isWhitespace((char) ch));
91 fCharAfterWhiteSpace = ch;
95 fCharAfterWhiteSpace = -1;
104 public int read() throws IOException {
109 while (!fReadFromBuffer) {
110 String s = computeSubstitution(c);
114 fBuffer.insert(0, s);
118 } while (fSkipWhiteSpace && fWasWhiteSpace && (c == ' '));
119 fWasWhiteSpace = (c == ' ' || c == '\r' || c == '\n');
124 * @see Reader#ready()
126 public boolean ready() throws IOException {
127 return fReader.ready();
131 * @see Reader#close()
133 public void close() throws IOException {
138 * @see Reader#reset()
140 public void reset() throws IOException {
142 fWasWhiteSpace = true;
143 fCharAfterWhiteSpace = -1;
144 fBuffer.setLength(0);
148 protected final void setSkipWhitespace(boolean state) {
149 fSkipWhiteSpace = state;
152 protected final boolean isSkippingWhitespace() {
153 return fSkipWhiteSpace;