KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > derived > SubstitutionTextReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.editor.derived;
12
13
14
15 import java.io.IOException JavaDoc;
16 import java.io.Reader JavaDoc;
17
18
19 /**
20  * Reads the text contents from a reader and computes for each character
21  * a potential substitution. The substitution may eat more characters than
22  * only the one passed into the computation routine.
23  *
24  * @see org.eclipse.jdt.internal.ui.text.SubstitutionTextReader
25  */

26 public abstract class SubstitutionTextReader extends SingleCharReader {
27     
28     protected static final String JavaDoc LINE_DELIM= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
29

30     private Reader JavaDoc fReader;
31     private boolean fWasWhiteSpace;
32     private int fCharAfterWhiteSpace;
33
34     /**
35      * Tells whether white space characters are skipped.
36      */

37     private boolean fSkipWhiteSpace= true;
38     
39     private boolean fReadFromBuffer;
40     private StringBuffer JavaDoc fBuffer;
41     private int fIndex;
42
43
44     protected SubstitutionTextReader(Reader JavaDoc reader) {
45         fReader= reader;
46         fBuffer= new StringBuffer JavaDoc();
47         fIndex= 0;
48         fReadFromBuffer= false;
49         fCharAfterWhiteSpace= -1;
50         fWasWhiteSpace= true;
51     }
52     
53     /**
54      * Implement to compute the substitution for the given character and
55      * if necessary subsequent characters. Use <code>nextChar</code>
56      * to read subsequent characters.
57      */

58     protected abstract String JavaDoc computeSubstitution(int c) throws IOException JavaDoc;
59     
60     /**
61      * Returns the internal reader.
62      */

63     protected Reader JavaDoc getReader() {
64         return fReader;
65     }
66      
67     /**
68      * Returns the next character.
69      */

70     protected int nextChar() throws IOException JavaDoc {
71         fReadFromBuffer= (fBuffer.length() > 0);
72         if (fReadFromBuffer) {
73             char ch= fBuffer.charAt(fIndex++);
74             if (fIndex >= fBuffer.length()) {
75                 fBuffer.setLength(0);
76                 fIndex= 0;
77             }
78             return ch;
79         }
80         int ch= fCharAfterWhiteSpace;
81         if (ch == -1) {
82             ch= fReader.read();
83         }
84         if (fSkipWhiteSpace && Character.isWhitespace((char)ch)) {
85             do {
86                 ch= fReader.read();
87             } while (Character.isWhitespace((char)ch));
88             if (ch != -1) {
89                 fCharAfterWhiteSpace= ch;
90                 return ' ';
91             }
92         } else {
93             fCharAfterWhiteSpace= -1;
94         }
95         return ch;
96     }
97     
98     /**
99      * @see Reader#read()
100      */

101     public int read() throws IOException JavaDoc {
102         int c;
103         do {
104             
105             c= nextChar();
106             while (!fReadFromBuffer) {
107                 String JavaDoc s= computeSubstitution(c);
108                 if (s == null)
109                     break;
110                 if (s.length() > 0)
111                     fBuffer.insert(0, s);
112                 c= nextChar();
113             }
114             
115         } while (fSkipWhiteSpace && fWasWhiteSpace && (c == ' '));
116         fWasWhiteSpace= (c == ' ' || c == '\r' || c == '\n');
117         return c;
118     }
119         
120     /**
121      * @see Reader#ready()
122      */

123     public boolean ready() throws IOException JavaDoc {
124         return fReader.ready();
125     }
126         
127     /**
128      * @see Reader#close()
129      */

130     public void close() throws IOException JavaDoc {
131         fReader.close();
132     }
133     
134     /**
135      * @see Reader#reset()
136      */

137     public void reset() throws IOException JavaDoc {
138         fReader.reset();
139         fWasWhiteSpace= true;
140         fCharAfterWhiteSpace= -1;
141         fBuffer.setLength(0);
142         fIndex= 0;
143     }
144
145     protected final void setSkipWhitespace(boolean state) {
146         fSkipWhiteSpace= state;
147     }
148
149     protected final boolean isSkippingWhitespace() {
150         return fSkipWhiteSpace;
151     }
152 }
153
Popular Tags