1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.channel;
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.apache.commons.io.IOUtils;
/**
* The CodeBuffer class provides all the basic features required to manipulate a source code character stream. Those features are :
* <ul>
* <li>Read and consume next source code character : pop()</li>
* <li>Retrieve last consumed character : lastChar()</li>
* <li>Read without consuming next source code character : peek()</li>
* <li>Read without consuming character at the specified index after the cursor</li>
* <li>Position of the pending cursor : line and column</li>
* </ul>
*/
public class CodeBuffer implements CharSequence {
private int lastChar = -1;
private Cursor cursor;
private char[] buffer;
private int bufferPosition = 0;
private static final char LF = '\n';
private static final char CR = '\r';
private int tabWidth;
private boolean recordingMode = false;
private StringBuilder recordedCharacters = new StringBuilder();
protected CodeBuffer(String code, CodeReaderConfiguration configuration) {
this(new StringReader(code), configuration);
}
/**
* Note that this constructor will read everything from reader and will close it.
*/
protected CodeBuffer(Reader initialCodeReader, CodeReaderConfiguration configuration) {
Reader reader = null;
try {
lastChar = -1;
cursor = new Cursor();
tabWidth = configuration.getTabWidth();
/* Setup the filters on the reader */
reader = initialCodeReader;
for (CodeReaderFilter<?> codeReaderFilter : configuration.getCodeReaderFilters()) {
reader = new Filter(reader, codeReaderFilter, configuration);
}
buffer = IOUtils.toCharArray(reader);
} catch (IOException e) {
throw new ChannelException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(reader);
}
}
/**
* Read and consume the next character
*
* @return the next character or -1 if the end of the stream is reached
*/
public final int pop() {
if (bufferPosition >= buffer.length) {
return -1;
}
int character = buffer[bufferPosition++];
updateCursorPosition(character);
if (recordingMode) {
recordedCharacters.append((char)character);
}
lastChar = character;
return character;
}
private void updateCursorPosition(int character) {
// see Java Language Specification : http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.4
if (character == LF || (character == CR && peek() != LF)) {
cursor.line++;
cursor.column = 0;
} else if (character == '\t') {
cursor.column += tabWidth;
} else {
cursor.column++;
}
}
/**
* Looks at the last consumed character
*
* @return the last character or -1 if the no character has been yet consumed
*/
public final int lastChar() {
return lastChar;
}
/**
* Looks at the next character without consuming it
*
* @return the next character or -1 if the end of the stream has been reached
*/
public final int peek() {
return intAt(0);
}
/**
* @deprecated in 2.12, do not use anymore.
*/
@Deprecated
public final void close() {
}
/**
* @return the current line of the cursor
*/
public final int getLinePosition() {
return cursor.line;
}
public final Cursor getCursor() {
return cursor;
}
/**
* @return the current column of the cursor
*/
public final int getColumnPosition() {
return cursor.column;
}
/**
* Overrides the current column position
*/
public final CodeBuffer setColumnPosition(int cp) {
this.cursor.column = cp;
return this;
}
/**
* Overrides the current line position
*/
public final void setLinePosition(int lp) {
this.cursor.line = lp;
}
public final void startRecording() {
recordingMode = true;
}
public final CharSequence stopRecording() {
recordingMode = false;
CharSequence result = recordedCharacters;
recordedCharacters = new StringBuilder();
return result;
}
/**
* Returns the character at the specified index after the cursor without consuming it
*
* @param index
* the relative index of the character to be returned
* @return the desired character
* @see java.lang.CharSequence#charAt(int)
*/
public final char charAt(int index) {
return (char)intAt(index);
}
protected final int intAt(int index) {
if (bufferPosition + index >= buffer.length) {
return -1;
}
return buffer[bufferPosition + index];
}
/**
* Returns the relative length of the string (i.e. excluding the popped chars)
*/
public final int length() {
return buffer.length - bufferPosition;
}
public final CharSequence subSequence(int start, int end) {
throw new UnsupportedOperationException();
}
@Override
public final String toString() {
StringBuilder result = new StringBuilder();
result.append("CodeReader(");
result.append("line:").append(cursor.line);
result.append("|column:").append(cursor.column);
result.append("|cursor value:'").append((char) peek()).append("'");
result.append(")");
return result.toString();
}
public final class Cursor implements Cloneable {
private int line = 1;
private int column = 0;
public int getLine() {
return line;
}
public int getColumn() {
return column;
}
@Override
public Cursor clone() {
Cursor clone = new Cursor();
clone.column = column;
clone.line = line;
return clone;
}
}
/**
* Bridge class between CodeBuffer and CodeReaderFilter
*/
static final class Filter extends FilterReader {
private CodeReaderFilter<?> codeReaderFilter;
public Filter(Reader in, CodeReaderFilter<?> codeReaderFilter, CodeReaderConfiguration configuration) {
super(in);
this.codeReaderFilter = codeReaderFilter;
this.codeReaderFilter.setConfiguration(configuration.cloneWithoutCodeReaderFilters());
this.codeReaderFilter.setReader(in);
}
@Override
public int read() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
int read = codeReaderFilter.read(cbuf, off, len);
return read == 0 ? -1 : read;
}
@Override
public long skip(long n) throws IOException {
throw new UnsupportedOperationException();
}
}
}
|