129
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.internal.text.html.SubstitutionTextReader;
|
|
14
|
131
|
15 import dwtx.jface.internal.text.html.HTML2TextReader; // packageimport
|
|
16 import dwtx.jface.internal.text.html.HTMLPrinter; // packageimport
|
|
17 import dwtx.jface.internal.text.html.BrowserInformationControl; // packageimport
|
|
18 import dwtx.jface.internal.text.html.HTMLTextPresenter; // packageimport
|
|
19 import dwtx.jface.internal.text.html.BrowserInput; // packageimport
|
|
20 import dwtx.jface.internal.text.html.SingleCharReader; // packageimport
|
|
21 import dwtx.jface.internal.text.html.BrowserInformationControlInput; // packageimport
|
|
22 import dwtx.jface.internal.text.html.HTMLMessages; // packageimport
|
|
23
|
|
24
|
129
|
25 import dwt.dwthelper.utils;
|
|
26
|
|
27 import java.io.IOException;
|
|
28 import java.io.Reader;
|
|
29
|
|
30
|
|
31 /**
|
|
32 * Reads the text contents from a reader and computes for each character
|
|
33 * a potential substitution. The substitution may eat more characters than
|
|
34 * only the one passed into the computation routine.
|
|
35 * <p>
|
|
36 * Moved into this package from <code>dwtx.jface.internal.text.revisions</code>.</p>
|
|
37 */
|
|
38 public abstract class SubstitutionTextReader : SingleCharReader {
|
|
39
|
|
40 protected static final String LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
41
|
|
42 private Reader fReader;
|
|
43 protected bool fWasWhiteSpace;
|
|
44 private int fCharAfterWhiteSpace;
|
|
45
|
|
46 /**
|
|
47 * Tells whether white space characters are skipped.
|
|
48 */
|
|
49 private bool fSkipWhiteSpace= true;
|
|
50
|
|
51 private bool fReadFromBuffer;
|
|
52 private StringBuffer fBuffer;
|
|
53 private int fIndex;
|
|
54
|
|
55
|
|
56 protected SubstitutionTextReader(Reader reader) {
|
|
57 fReader= reader;
|
|
58 fBuffer= new StringBuffer();
|
|
59 fIndex= 0;
|
|
60 fReadFromBuffer= false;
|
|
61 fCharAfterWhiteSpace= -1;
|
|
62 fWasWhiteSpace= true;
|
|
63 }
|
|
64
|
|
65 /**
|
|
66 * Computes the substitution for the given character and if necessary
|
|
67 * subsequent characters. Implementation should use <code>nextChar</code>
|
|
68 * to read subsequent characters.
|
|
69 *
|
|
70 * @param c the character to be substituted
|
|
71 * @return the substitution for <code>c</code>
|
|
72 * @throws IOException in case computing the substitution fails
|
|
73 */
|
|
74 protected abstract String computeSubstitution(int c) throws IOException;
|
|
75
|
|
76 /**
|
|
77 * Returns the internal reader.
|
|
78 *
|
|
79 * @return the internal reader
|
|
80 */
|
|
81 protected Reader getReader() {
|
|
82 return fReader;
|
|
83 }
|
|
84
|
|
85 /**
|
|
86 * Returns the next character.
|
|
87 * @return the next character
|
|
88 * @throws IOException in case reading the character fails
|
|
89 */
|
|
90 protected int nextChar() throws IOException {
|
|
91 fReadFromBuffer= (fBuffer.length() > 0);
|
|
92 if (fReadFromBuffer) {
|
|
93 char ch= fBuffer.charAt(fIndex++);
|
|
94 if (fIndex >= fBuffer.length()) {
|
|
95 fBuffer.setLength(0);
|
|
96 fIndex= 0;
|
|
97 }
|
|
98 return ch;
|
|
99 }
|
|
100
|
|
101 int ch= fCharAfterWhiteSpace;
|
|
102 if (ch is -1) {
|
|
103 ch= fReader.read();
|
|
104 }
|
|
105 if (fSkipWhiteSpace && Character.isWhitespace((char)ch)) {
|
|
106 do {
|
|
107 ch= fReader.read();
|
|
108 } while (Character.isWhitespace((char)ch));
|
|
109 if (ch !is -1) {
|
|
110 fCharAfterWhiteSpace= ch;
|
|
111 return ' ';
|
|
112 }
|
|
113 } else {
|
|
114 fCharAfterWhiteSpace= -1;
|
|
115 }
|
|
116 return ch;
|
|
117 }
|
|
118
|
|
119 /**
|
|
120 * @see Reader#read()
|
|
121 */
|
|
122 public int read() throws IOException {
|
|
123 int c;
|
|
124 do {
|
|
125
|
|
126 c= nextChar();
|
|
127 while (!fReadFromBuffer) {
|
|
128 String s= computeSubstitution(c);
|
|
129 if (s is null)
|
|
130 break;
|
|
131 if (s.length() > 0)
|
|
132 fBuffer.insert(0, s);
|
|
133 c= nextChar();
|
|
134 }
|
|
135
|
|
136 } while (fSkipWhiteSpace && fWasWhiteSpace && (c is ' '));
|
|
137 fWasWhiteSpace= (c is ' ' || c is '\r' || c is '\n');
|
|
138 return c;
|
|
139 }
|
|
140
|
|
141 /**
|
|
142 * @see Reader#ready()
|
|
143 */
|
|
144 public bool ready() throws IOException {
|
|
145 return fReader.ready();
|
|
146 }
|
|
147
|
|
148 /**
|
|
149 * @see Reader#close()
|
|
150 */
|
|
151 public void close() throws IOException {
|
|
152 fReader.close();
|
|
153 }
|
|
154
|
|
155 /**
|
|
156 * @see Reader#reset()
|
|
157 */
|
|
158 public void reset() throws IOException {
|
|
159 fReader.reset();
|
|
160 fWasWhiteSpace= true;
|
|
161 fCharAfterWhiteSpace= -1;
|
|
162 fBuffer.setLength(0);
|
|
163 fIndex= 0;
|
|
164 }
|
|
165
|
|
166 protected final void setSkipWhitespace(bool state) {
|
|
167 fSkipWhiteSpace= state;
|
|
168 }
|
|
169
|
|
170 protected final bool isSkippingWhitespace() {
|
|
171 return fSkipWhiteSpace;
|
|
172 }
|
|
173 }
|