KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > formatter > comment > SubstitutionTextReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Matt McCutchen - add check for EOF handling
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.formatter.comment;
13
14 import java.io.IOException JavaDoc;
15 import java.io.Reader JavaDoc;
16
17 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
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 public abstract class SubstitutionTextReader extends Reader JavaDoc {
25     
26     private Reader JavaDoc fReader;
27     private boolean fWasWhiteSpace;
28     private int fCharAfterWhiteSpace;
29
30     /**
31      * Tells whether white space characters are skipped.
32      */

33     private boolean fSkipWhiteSpace= true;
34     
35     private boolean fReadFromBuffer;
36     private StringBuffer JavaDoc fBuffer;
37     private int fIndex;
38
39
40     protected SubstitutionTextReader(Reader JavaDoc reader) {
41         fReader= reader;
42         fBuffer= new StringBuffer JavaDoc();
43         fIndex= 0;
44         fReadFromBuffer= false;
45         fCharAfterWhiteSpace= -1;
46         fWasWhiteSpace= true;
47     }
48     
49     /**
50      * Gets the content as a String
51      */

52     public String JavaDoc getString() throws IOException JavaDoc {
53         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
54         int ch;
55         while ((ch= read()) != -1) {
56             buf.append((char)ch);
57         }
58         return buf.toString();
59     }
60     
61     /**
62      * Implement to compute the substitution for the given character and
63      * if necessary subsequent characters. Use <code>nextChar</code>
64      * to read subsequent characters.
65      */

66     protected abstract String JavaDoc computeSubstitution(int c) throws IOException JavaDoc;
67     
68     /**
69      * Returns the internal reader.
70      */

71     protected Reader JavaDoc getReader() {
72         return fReader;
73     }
74      
75     /**
76      * Returns the next character.
77      */

78     protected int nextChar() throws IOException JavaDoc {
79         fReadFromBuffer= (fBuffer.length() > 0);
80         if (fReadFromBuffer) {
81             char ch= fBuffer.charAt(fIndex++);
82             if (fIndex >= fBuffer.length()) {
83                 fBuffer.setLength(0);
84                 fIndex= 0;
85             }
86             return ch;
87         } else {
88             int ch= fCharAfterWhiteSpace;
89             if (ch == -1) {
90                 ch= fReader.read();
91             }
92             if (fSkipWhiteSpace && ScannerHelper.isWhitespace((char)ch)) {
93                 do {
94                     ch= fReader.read();
95                 } while (ScannerHelper.isWhitespace((char)ch));
96                 if (ch != -1) {
97                     fCharAfterWhiteSpace= ch;
98                     return ' ';
99                 }
100             } else {
101                 fCharAfterWhiteSpace= -1;
102             }
103             return ch;
104         }
105     }
106     
107     /*
108      * @see Reader#read()
109      */

110     public int read() throws IOException JavaDoc {
111         int c;
112         do {
113             
114             c= nextChar();
115             while (!fReadFromBuffer && c != -1) {
116                 String JavaDoc s= computeSubstitution(c);
117                 if (s == null)
118                     break;
119                 if (s.length() > 0)
120                     fBuffer.insert(0, s);
121                 c= nextChar();
122             }
123             
124         } while (fSkipWhiteSpace && fWasWhiteSpace && (c == ' '));
125         fWasWhiteSpace= (c == ' ' || c == '\r' || c == '\n');
126         return c;
127     }
128         
129     /*
130      * @see Reader#read(char[],int,int)
131      */

132     public int read(char cbuf[], int off, int len) throws IOException JavaDoc {
133         int end= off + len;
134         for (int i= off; i < end; i++) {
135             int ch= read();
136             if (ch == -1) {
137                 if (i == off) {
138                     return -1;
139                 } else {
140                     return i - off;
141                 }
142             }
143             cbuf[i]= (char)ch;
144         }
145         return len;
146     }
147     
148     /*
149      * @see java.io.Reader#ready()
150      */

151     public boolean ready() throws IOException JavaDoc {
152         return fReader.ready();
153     }
154         
155     /*
156      * @see Reader#close()
157      */

158     public void close() throws IOException JavaDoc {
159         fReader.close();
160     }
161     
162     /*
163      * @see Reader#reset()
164      */

165     public void reset() throws IOException JavaDoc {
166         fReader.reset();
167         fWasWhiteSpace= true;
168         fCharAfterWhiteSpace= -1;
169         fBuffer.setLength(0);
170         fIndex= 0;
171     }
172
173     protected final void setSkipWhitespace(boolean state) {
174         fSkipWhiteSpace= state;
175     }
176
177     protected final boolean isSkippingWhitespace() {
178         return fSkipWhiteSpace;
179     }
180 }
181
Popular Tags