comparison dwtx/jface/internal/text/html/SubstitutionTextReader.d @ 129:eb30df5ca28b

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