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;
15 import java.io.IOException;
16 import java.io.Reader;
18 import net.sourceforge.phpdt.internal.corext.phpdoc.SingleCharReader;
22 * Reads the text contents from a reader and computes for each character
23 * a potential substitution. The substitution may eat more characters than
24 * only the one passed into the computation routine.
26 public abstract class SubstitutionTextReader extends SingleCharReader {
28 protected static final String LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
30 private Reader fReader;
31 private boolean fWasWhiteSpace;
32 private int fCharAfterWhiteSpace;
35 * Tells whether white space characters are skipped.
37 private boolean fSkipWhiteSpace= true;
39 private boolean fReadFromBuffer;
40 private StringBuffer fBuffer;
44 protected SubstitutionTextReader(Reader reader) {
46 fBuffer= new StringBuffer();
48 fReadFromBuffer= false;
49 fCharAfterWhiteSpace= -1;
54 * Implement to compute the substitution for the given character and
55 * if necessary subsequent characters. Use <code>nextChar</code>
56 * to read subsequent characters.
58 protected abstract String computeSubstitution(int c) throws IOException;
61 * Returns the internal reader.
63 protected Reader getReader() {
68 * Returns the next character.
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()) {
80 int ch= fCharAfterWhiteSpace;
84 if (fSkipWhiteSpace && Character.isWhitespace((char)ch)) {
87 } while (Character.isWhitespace((char)ch));
89 fCharAfterWhiteSpace= ch;
93 fCharAfterWhiteSpace= -1;
102 public int read() throws IOException {
107 while (!fReadFromBuffer) {
108 String s= computeSubstitution(c);
112 fBuffer.insert(0, s);
116 } while (fSkipWhiteSpace && fWasWhiteSpace && (c == ' '));
117 fWasWhiteSpace= (c == ' ' || c == '\r' || c == '\n');
122 * @see Reader#ready()
124 public boolean ready() throws IOException {
125 return fReader.ready();
129 * @see Reader#close()
131 public void close() throws IOException {
136 * @see Reader#reset()
138 public void reset() throws IOException {
140 fWasWhiteSpace= true;
141 fCharAfterWhiteSpace= -1;
142 fBuffer.setLength(0);
146 protected final void setSkipWhitespace(boolean state) {
147 fSkipWhiteSpace= state;
150 protected final boolean isSkippingWhitespace() {
151 return fSkipWhiteSpace;