Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/html/SubstitutionTextReader.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | 7926b636c282 |
children |
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 | |
129 | 24 import dwt.dwthelper.utils; |
162 | 25 import tango.core.Exception; |
129 | 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 | |
159 | 36 private static String LINE_DELIM_; |
37 protected static String LINE_DELIM() { | |
38 if( LINE_DELIM_ is null ){ | |
39 LINE_DELIM_ = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ | |
40 } | |
41 return LINE_DELIM_; | |
42 } | |
129 | 43 |
44 private Reader fReader; | |
45 protected bool fWasWhiteSpace; | |
46 private int fCharAfterWhiteSpace; | |
47 | |
48 /** | |
49 * Tells whether white space characters are skipped. | |
50 */ | |
51 private bool fSkipWhiteSpace= true; | |
52 | |
53 private bool fReadFromBuffer; | |
54 private StringBuffer fBuffer; | |
55 private int fIndex; | |
56 | |
57 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
58 protected this(Reader reader) { |
129 | 59 fReader= reader; |
60 fBuffer= new StringBuffer(); | |
61 fIndex= 0; | |
62 fReadFromBuffer= false; | |
63 fCharAfterWhiteSpace= -1; | |
64 fWasWhiteSpace= true; | |
65 } | |
66 | |
67 /** | |
68 * Computes the substitution for the given character and if necessary | |
69 * subsequent characters. Implementation should use <code>nextChar</code> | |
70 * to read subsequent characters. | |
71 * | |
72 * @param c the character to be substituted | |
73 * @return the substitution for <code>c</code> | |
74 * @throws IOException in case computing the substitution fails | |
75 */ | |
139
93a6ec48fd28
Regexp throws removal in interfaces
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
76 protected abstract String computeSubstitution(int c) ; |
129 | 77 |
78 /** | |
79 * Returns the internal reader. | |
80 * | |
81 * @return the internal reader | |
82 */ | |
83 protected Reader getReader() { | |
84 return fReader; | |
85 } | |
86 | |
87 /** | |
88 * Returns the next character. | |
89 * @return the next character | |
90 * @throws IOException in case reading the character fails | |
91 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
92 protected int nextChar() { |
129 | 93 fReadFromBuffer= (fBuffer.length() > 0); |
94 if (fReadFromBuffer) { | |
162 | 95 char ch= fBuffer.slice().charAt(fIndex++); |
129 | 96 if (fIndex >= fBuffer.length()) { |
162 | 97 fBuffer.truncate(0); |
129 | 98 fIndex= 0; |
99 } | |
100 return ch; | |
101 } | |
102 | |
103 int ch= fCharAfterWhiteSpace; | |
104 if (ch is -1) { | |
105 ch= fReader.read(); | |
106 } | |
162 | 107 if (fSkipWhiteSpace && Character.isWhitespace(cast(char)ch)) { |
129 | 108 do { |
109 ch= fReader.read(); | |
162 | 110 } while (Character.isWhitespace(cast(char)ch)); |
129 | 111 if (ch !is -1) { |
112 fCharAfterWhiteSpace= ch; | |
113 return ' '; | |
114 } | |
115 } else { | |
116 fCharAfterWhiteSpace= -1; | |
117 } | |
118 return ch; | |
119 } | |
120 | |
162 | 121 /// DWT |
122 protected int nextDChar() { | |
123 char[4] buf = void; | |
124 int ch1 = nextChar(); | |
125 if( ch1 is -1 ) return -1; | |
126 buf[0] = cast(char)ch1; | |
127 if(( ch1 & 0x80 ) is 0x00 ){ | |
128 return ch1; | |
129 } | |
130 else if(( ch1 & 0xE0 ) is 0xC0 ){ | |
131 int ch2 = nextChar(); | |
132 if( ch2 is -1 ) throw new UnicodeException(__FILE__,__LINE__); | |
133 buf[1] = cast(char)ch2; | |
134 } | |
135 else if(( ch1 & 0xF0 ) is 0xE0 ){ | |
136 int ch2 = nextChar(); | |
137 if( ch1 is -1 ) throw new UnicodeException(__FILE__,__LINE__); | |
138 buf[1] = cast(char)ch2; | |
139 int ch3 = nextChar(); | |
140 if( ch3 is -1 ) throw new UnicodeException(__FILE__,__LINE__); | |
141 buf[2] = cast(char)ch3; | |
142 } | |
143 else if(( ch1 & 0xF8 ) is 0xF0 ){ | |
144 int ch2 = nextChar(); | |
145 if( ch1 is -1 ) throw new UnicodeException(__FILE__,__LINE__); | |
146 buf[1] = cast(char)ch2; | |
147 int ch3 = nextChar(); | |
148 if( ch3 is -1 ) throw new UnicodeException(__FILE__,__LINE__); | |
149 buf[2] = cast(char)ch3; | |
150 int ch4 = nextChar(); | |
151 if( ch4 is -1 ) throw new UnicodeException(__FILE__,__LINE__); | |
152 buf[3] = cast(char)ch4; | |
153 } | |
154 else { | |
155 throw new UnicodeException(__FILE__,__LINE__); | |
156 } | |
157 uint ate; | |
158 return tango.text.convert.Utf.decode( buf, ate ); | |
159 } | |
160 | |
129 | 161 /** |
162 * @see Reader#read() | |
163 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
164 public int read() { |
129 | 165 int c; |
166 do { | |
167 | |
168 c= nextChar(); | |
169 while (!fReadFromBuffer) { | |
170 String s= computeSubstitution(c); | |
171 if (s is null) | |
172 break; | |
162 | 173 if (s.length() > 0){ |
174 fBuffer.select(0, 0); | |
175 fBuffer.replace(s); | |
176 } | |
129 | 177 c= nextChar(); |
178 } | |
179 | |
180 } while (fSkipWhiteSpace && fWasWhiteSpace && (c is ' ')); | |
181 fWasWhiteSpace= (c is ' ' || c is '\r' || c is '\n'); | |
182 return c; | |
183 } | |
184 | |
185 /** | |
186 * @see Reader#ready() | |
187 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
188 public bool ready() { |
129 | 189 return fReader.ready(); |
190 } | |
191 | |
192 /** | |
193 * @see Reader#close() | |
194 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
195 public void close() { |
129 | 196 fReader.close(); |
197 } | |
198 | |
199 /** | |
200 * @see Reader#reset() | |
201 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
202 public void reset() { |
129 | 203 fReader.reset(); |
204 fWasWhiteSpace= true; | |
205 fCharAfterWhiteSpace= -1; | |
162 | 206 fBuffer.truncate(0); |
129 | 207 fIndex= 0; |
208 } | |
209 | |
210 protected final void setSkipWhitespace(bool state) { | |
211 fSkipWhiteSpace= state; | |
212 } | |
213 | |
214 protected final bool isSkippingWhitespace() { | |
215 return fSkipWhiteSpace; | |
216 } | |
217 } |