comparison org.eclipse.jface.text/src/org/eclipse/jface/internal/text/html/SubstitutionTextReader.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 org.eclipse.jface.internal.text.html.SubstitutionTextReader;
14
15 import org.eclipse.jface.internal.text.html.HTML2TextReader; // packageimport
16 import org.eclipse.jface.internal.text.html.HTMLPrinter; // packageimport
17 import org.eclipse.jface.internal.text.html.BrowserInformationControl; // packageimport
18 import org.eclipse.jface.internal.text.html.HTMLTextPresenter; // packageimport
19 import org.eclipse.jface.internal.text.html.BrowserInput; // packageimport
20 import org.eclipse.jface.internal.text.html.SingleCharReader; // packageimport
21 import org.eclipse.jface.internal.text.html.BrowserInformationControlInput; // packageimport
22 import org.eclipse.jface.internal.text.html.HTMLMessages; // packageimport
23
24 import java.lang.all;
25 import java.util.Set;
26 import tango.core.Exception;
27
28 /**
29 * Reads the text contents from a reader and computes for each character
30 * a potential substitution. The substitution may eat more characters than
31 * only the one passed into the computation routine.
32 * <p>
33 * Moved into this package from <code>org.eclipse.jface.internal.text.revisions</code>.</p>
34 */
35 public abstract class SubstitutionTextReader : SingleCharReader {
36
37 private static String LINE_DELIM_;
38 protected static String LINE_DELIM() {
39 if( LINE_DELIM_ is null ){
40 LINE_DELIM_ = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
41 }
42 return LINE_DELIM_;
43 }
44
45 private Reader fReader;
46 protected bool fWasWhiteSpace;
47 private int fCharAfterWhiteSpace;
48
49 /**
50 * Tells whether white space characters are skipped.
51 */
52 private bool fSkipWhiteSpace= true;
53
54 private bool fReadFromBuffer;
55 private StringBuffer fBuffer;
56 private int fIndex;
57
58
59 protected this(Reader reader) {
60 fReader= reader;
61 fBuffer= new StringBuffer();
62 fIndex= 0;
63 fReadFromBuffer= false;
64 fCharAfterWhiteSpace= -1;
65 fWasWhiteSpace= true;
66 }
67
68 /**
69 * Computes the substitution for the given character and if necessary
70 * subsequent characters. Implementation should use <code>nextChar</code>
71 * to read subsequent characters.
72 *
73 * @param c the character to be substituted
74 * @return the substitution for <code>c</code>
75 * @throws IOException in case computing the substitution fails
76 */
77 protected abstract String computeSubstitution(int c) ;
78
79 /**
80 * Returns the internal reader.
81 *
82 * @return the internal reader
83 */
84 protected Reader getReader() {
85 return fReader;
86 }
87
88 /**
89 * Returns the next character.
90 * @return the next character
91 * @throws IOException in case reading the character fails
92 */
93 protected int nextChar() {
94 fReadFromBuffer= (fBuffer.length() > 0);
95 if (fReadFromBuffer) {
96 char ch= fBuffer.slice().charAt(fIndex++);
97 if (fIndex >= fBuffer.length()) {
98 fBuffer.truncate(0);
99 fIndex= 0;
100 }
101 return ch;
102 }
103
104 int ch= fCharAfterWhiteSpace;
105 if (ch is -1) {
106 ch= fReader.read();
107 }
108 if (fSkipWhiteSpace && Character.isWhitespace(cast(char)ch)) {
109 do {
110 ch= fReader.read();
111 } while (Character.isWhitespace(cast(char)ch));
112 if (ch !is -1) {
113 fCharAfterWhiteSpace= ch;
114 return ' ';
115 }
116 } else {
117 fCharAfterWhiteSpace= -1;
118 }
119 return ch;
120 }
121
122 /// SWT
123 protected int nextDChar() {
124 char[4] buf = void;
125 int ch1 = nextChar();
126 if( ch1 is -1 ) return -1;
127 buf[0] = cast(char)ch1;
128 if(( ch1 & 0x80 ) is 0x00 ){
129 return ch1;
130 }
131 else if(( ch1 & 0xE0 ) is 0xC0 ){
132 int ch2 = nextChar();
133 if( ch2 is -1 ) throw new UnicodeException(__FILE__,__LINE__);
134 buf[1] = cast(char)ch2;
135 }
136 else if(( ch1 & 0xF0 ) is 0xE0 ){
137 int ch2 = nextChar();
138 if( ch1 is -1 ) throw new UnicodeException(__FILE__,__LINE__);
139 buf[1] = cast(char)ch2;
140 int ch3 = nextChar();
141 if( ch3 is -1 ) throw new UnicodeException(__FILE__,__LINE__);
142 buf[2] = cast(char)ch3;
143 }
144 else if(( ch1 & 0xF8 ) is 0xF0 ){
145 int ch2 = nextChar();
146 if( ch1 is -1 ) throw new UnicodeException(__FILE__,__LINE__);
147 buf[1] = cast(char)ch2;
148 int ch3 = nextChar();
149 if( ch3 is -1 ) throw new UnicodeException(__FILE__,__LINE__);
150 buf[2] = cast(char)ch3;
151 int ch4 = nextChar();
152 if( ch4 is -1 ) throw new UnicodeException(__FILE__,__LINE__);
153 buf[3] = cast(char)ch4;
154 }
155 else {
156 throw new UnicodeException(__FILE__,__LINE__);
157 }
158 uint ate;
159 return tango.text.convert.Utf.decode( buf, ate );
160 }
161
162 /**
163 * @see Reader#read()
164 */
165 public int read() {
166 int c;
167 do {
168
169 c= nextChar();
170 while (!fReadFromBuffer) {
171 String s= computeSubstitution(c);
172 if (s is null)
173 break;
174 if (s.length() > 0){
175 fBuffer.select(0, 0);
176 fBuffer.replace(s);
177 }
178 c= nextChar();
179 }
180
181 } while (fSkipWhiteSpace && fWasWhiteSpace && (c is ' '));
182 fWasWhiteSpace= (c is ' ' || c is '\r' || c is '\n');
183 return c;
184 }
185
186 /**
187 * @see Reader#ready()
188 */
189 public bool ready() {
190 return fReader.ready();
191 }
192
193 /**
194 * @see Reader#close()
195 */
196 public void close() {
197 fReader.close();
198 }
199
200 /**
201 * @see Reader#reset()
202 */
203 public void reset() {
204 fReader.reset();
205 fWasWhiteSpace= true;
206 fCharAfterWhiteSpace= -1;
207 fBuffer.truncate(0);
208 fIndex= 0;
209 }
210
211 protected final void setSkipWhitespace(bool state) {
212 fSkipWhiteSpace= state;
213 }
214
215 protected final bool isSkippingWhitespace() {
216 return fSkipWhiteSpace;
217 }
218 }