Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/html/SubstitutionTextReader.d @ 158:25f1f92fa3df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 26 Aug 2008 02:46:34 +0200 |
parents | 93a6ec48fd28 |
children | 7926b636c282 |
rev | line source |
---|---|
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 /** | |
28 * Reads the text contents from a reader and computes for each character | |
29 * a potential substitution. The substitution may eat more characters than | |
30 * only the one passed into the computation routine. | |
31 * <p> | |
32 * Moved into this package from <code>dwtx.jface.internal.text.revisions</code>.</p> | |
33 */ | |
34 public abstract class SubstitutionTextReader : SingleCharReader { | |
35 | |
36 protected static final String LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ | |
37 | |
38 private Reader fReader; | |
39 protected bool fWasWhiteSpace; | |
40 private int fCharAfterWhiteSpace; | |
41 | |
42 /** | |
43 * Tells whether white space characters are skipped. | |
44 */ | |
45 private bool fSkipWhiteSpace= true; | |
46 | |
47 private bool fReadFromBuffer; | |
48 private StringBuffer fBuffer; | |
49 private int fIndex; | |
50 | |
51 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
52 protected this(Reader reader) { |
129 | 53 fReader= reader; |
54 fBuffer= new StringBuffer(); | |
55 fIndex= 0; | |
56 fReadFromBuffer= false; | |
57 fCharAfterWhiteSpace= -1; | |
58 fWasWhiteSpace= true; | |
59 } | |
60 | |
61 /** | |
62 * Computes the substitution for the given character and if necessary | |
63 * subsequent characters. Implementation should use <code>nextChar</code> | |
64 * to read subsequent characters. | |
65 * | |
66 * @param c the character to be substituted | |
67 * @return the substitution for <code>c</code> | |
68 * @throws IOException in case computing the substitution fails | |
69 */ | |
139
93a6ec48fd28
Regexp throws removal in interfaces
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
70 protected abstract String computeSubstitution(int c) ; |
129 | 71 |
72 /** | |
73 * Returns the internal reader. | |
74 * | |
75 * @return the internal reader | |
76 */ | |
77 protected Reader getReader() { | |
78 return fReader; | |
79 } | |
80 | |
81 /** | |
82 * Returns the next character. | |
83 * @return the next character | |
84 * @throws IOException in case reading the character fails | |
85 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
86 protected int nextChar() { |
129 | 87 fReadFromBuffer= (fBuffer.length() > 0); |
88 if (fReadFromBuffer) { | |
89 char ch= fBuffer.charAt(fIndex++); | |
90 if (fIndex >= fBuffer.length()) { | |
91 fBuffer.setLength(0); | |
92 fIndex= 0; | |
93 } | |
94 return ch; | |
95 } | |
96 | |
97 int ch= fCharAfterWhiteSpace; | |
98 if (ch is -1) { | |
99 ch= fReader.read(); | |
100 } | |
134 | 101 if (fSkipWhiteSpace && Character.isWhitespace(cast(wchar)ch)) { |
129 | 102 do { |
103 ch= fReader.read(); | |
134 | 104 } while (Character.isWhitespace(cast(wchar)ch)); |
129 | 105 if (ch !is -1) { |
106 fCharAfterWhiteSpace= ch; | |
107 return ' '; | |
108 } | |
109 } else { | |
110 fCharAfterWhiteSpace= -1; | |
111 } | |
112 return ch; | |
113 } | |
114 | |
115 /** | |
116 * @see Reader#read() | |
117 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
118 public int read() { |
129 | 119 int c; |
120 do { | |
121 | |
122 c= nextChar(); | |
123 while (!fReadFromBuffer) { | |
124 String s= computeSubstitution(c); | |
125 if (s is null) | |
126 break; | |
127 if (s.length() > 0) | |
128 fBuffer.insert(0, s); | |
129 c= nextChar(); | |
130 } | |
131 | |
132 } while (fSkipWhiteSpace && fWasWhiteSpace && (c is ' ')); | |
133 fWasWhiteSpace= (c is ' ' || c is '\r' || c is '\n'); | |
134 return c; | |
135 } | |
136 | |
137 /** | |
138 * @see Reader#ready() | |
139 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
140 public bool ready() { |
129 | 141 return fReader.ready(); |
142 } | |
143 | |
144 /** | |
145 * @see Reader#close() | |
146 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
147 public void close() { |
129 | 148 fReader.close(); |
149 } | |
150 | |
151 /** | |
152 * @see Reader#reset() | |
153 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
154 public void reset() { |
129 | 155 fReader.reset(); |
156 fWasWhiteSpace= true; | |
157 fCharAfterWhiteSpace= -1; | |
158 fBuffer.setLength(0); | |
159 fIndex= 0; | |
160 } | |
161 | |
162 protected final void setSkipWhitespace(bool state) { | |
163 fSkipWhiteSpace= state; | |
164 } | |
165 | |
166 protected final bool isSkippingWhitespace() { | |
167 return fSkipWhiteSpace; | |
168 } | |
169 } |