KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > html > SubstitutionTextReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.internal.text.html;
12
13 import java.io.IOException JavaDoc;
14 import java.io.Reader JavaDoc;
15
16
17 /**
18  * Reads the text contents from a reader and computes for each character
19  * a potential substitution. The substitution may eat more characters than
20  * only the one passed into the computation routine.
21  * <p>
22  * Moved into this package from <code>org.eclipse.jface.internal.text.revisions</code>.</p>
23  */

24 public abstract class SubstitutionTextReader extends SingleCharReader {
25
26     protected static final String JavaDoc LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
27

28     private Reader JavaDoc fReader;
29     protected boolean fWasWhiteSpace;
30     private int fCharAfterWhiteSpace;
31
32     /**
33      * Tells whether white space characters are skipped.
34      */

35     private boolean fSkipWhiteSpace= true;
36
37     private boolean fReadFromBuffer;
38     private StringBuffer JavaDoc fBuffer;
39     private int fIndex;
40
41
42     protected SubstitutionTextReader(Reader JavaDoc reader) {
43         fReader= reader;
44         fBuffer= new StringBuffer JavaDoc();
45         fIndex= 0;
46         fReadFromBuffer= false;
47         fCharAfterWhiteSpace= -1;
48         fWasWhiteSpace= true;
49     }
50
51     /**
52      * Computes the substitution for the given character and if necessary
53      * subsequent characters. Implementation should use <code>nextChar</code>
54      * to read subsequent characters.
55      *
56      * @param c the character to be substituted
57      * @return the substitution for <code>c</code>
58      * @throws IOException in case computing the substitution fails
59      */

60     protected abstract String JavaDoc computeSubstitution(int c) throws IOException JavaDoc;
61
62     /**
63      * Returns the internal reader.
64      *
65      * @return the internal reader
66      */

67     protected Reader JavaDoc getReader() {
68         return fReader;
69     }
70
71     /**
72      * Returns the next character.
73      * @return the next character
74      * @throws IOException in case reading the character fails
75      */

76     protected int nextChar() throws IOException JavaDoc {
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     /**
106      * @see Reader#read()
107      */

108     public int read() throws IOException JavaDoc {
109         int c;
110         do {
111
112             c= nextChar();
113             while (!fReadFromBuffer) {
114                 String JavaDoc 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     /**
128      * @see Reader#ready()
129      */

130     public boolean ready() throws IOException JavaDoc {
131         return fReader.ready();
132     }
133
134     /**
135      * @see Reader#close()
136      */

137     public void close() throws IOException JavaDoc {
138         fReader.close();
139     }
140
141     /**
142      * @see Reader#reset()
143      */

144     public void reset() throws IOException JavaDoc {
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