1 | class DocumentReader extends Reader↵ | | 1 | class DocumentReader extends Reader↵
|
2 | {↵ | | |
|
3 |
↵ | | |
|
| | | 2 | {↵
|
|
4 | /**
↵ | | 3 | /**↵
|
5 | * Modifying the document while the reader is working is like
↵ | | 4 | * Modifying the document while the reader is working is like↵
|
6 | * pulling the rug out from under the reader. Alerting the
↵ | | 5 | * pulling the rug out from under the reader. Alerting the↵
|
7 | * reader with this method (in a nice thread safe way, this
↵ | | 6 | * reader with this method (in a nice thread safe way, this↵
|
8 | * should not be called at the same time as a read) allows
↵ | | 7 | * should not be called at the same time as a read) allows↵
|
9 | * the reader to compensate.
↵ | | 8 | * the reader to compensate.↵
|
10 | */
↵ | | 9 | */↵
|
11 | public void update(int position, int adjustment)↵ | | 10 | public void update(int position, int adjustment)↵
|
12 | {↵ | | |
|
|
| | | 11 | {↵
|
13 | if (position < this.position)
↵ | | 12 | if (position < this.position)↵
|
14 | {↵ | | 13 | ↵
|
| | | 14 | {↵
|
15 | if (this.position < position - adjustment)
↵ | | 15 | if (this.position < position - adjustment)↵
|
16 | {
↵ | | 16 | {↵
|
17 | this.position = position;
↵ | | 17 | this.position = position;↵
|
18 | }
↵ | | 18 | }↵
|
19 | else
↵ | | 19 | else↵
|
20 | {
↵ | | 20 | {↵
|
21 | this.position += adjustment;
↵ | | 21 | this.position += adjustment;↵
|
22 | }
↵ | | 22 | }↵
|
23 | }↵ | | 23 | ↵
|
24 | }↵ | | |
|
25 |
↵ | | 24 | }↵
|
| | | 25 | }↵
|
|
26 | /**
↵ | | 26 | /**↵
|
27 | * Current position in the document. Incremented
↵ | | 27 | * Current position in the document. Incremented↵
|
28 | * whenever a character is read.
↵ | | 28 | * whenever a character is read.↵
|
29 | */
↵ | | 29 | */↵
|
30 | private long position = 0;↵ | | 30 | private long position = 0;↵
|
|
|
|
31 | /**
↵ | | 31 | /**↵
|
32 | * Saved position used in the mark and reset methods.
↵ | | 32 | * Saved position used in the mark and reset methods.↵
|
33 | */
↵ | | 33 | */↵
|
34 | private long mark = -1;↵ | | 34 | private long mark = -1;↵
|
|
|
|
35 | /**
↵ | | 35 | /**↵
|
36 | * The document that we are working with.
↵ | | 36 | * The document that we are working with.↵
|
37 | */
↵ | | 37 | */↵
|
38 | private AbstractDocument document;↵ | | 38 | private AbstractDocument document;↵
|
|
|
|
39 | /**
↵ | | 39 | /**↵
|
40 | * Construct a reader on the given document.
↵ | | 40 | * Construct a reader on the given document.↵
|
41 | *
↵ | | 41 | *↵
|
42 | * @param document the document to be read.
↵ | | 42 | * @param document the document to be read.↵
|
43 | */
↵ | | 43 | */↵
|
44 | public DocumentReader(AbstractDocument document)↵ | | 44 | public DocumentReader(AbstractDocument document)↵
|
45 | {↵ | | |
|
|
| | | 45 | {↵
|
46 | this.document = document;↵ | | 46 | this.document = document;↵
|
47 | }↵ | | |
|
48 |
↵ | | |
|
| | | 47 | }↵
|
|
49 | /**
↵ | | 48 | /**↵
|
50 | * Has no effect. This reader can be used even after
↵ | | 49 | * Has no effect. This reader can be used even after↵
|
51 | * it has been closed.
↵ | | 50 | * it has been closed.↵
|
52 | */
↵ | | 51 | */↵
|
53 | public void close()
↵ | | 52 | public void close()↵
|
54 | {
↵ | | 53 | {↵
|
55 | }↵ | | 54 | ↵
|
56 |
↵ | | 55 | }↵
|
|
57 | /**
↵ | | 56 | /**↵
|
58 | * Save a position for reset.
↵ | | 57 | * Save a position for reset.↵
|
59 | *
↵ | | 58 | *↵
|
60 | * @param readAheadLimit ignored.
↵ | | 59 | * @param readAheadLimit ignored.↵
|
61 | */
↵ | | 60 | */↵
|
62 | public void mark(int readAheadLimit)
↵ | | 61 | public void mark(int readAheadLimit)↵
|
63 | {↵ | | 62 | ↵
|
| | | 63 | {↵
|
64 | mark = position;↵ | | 64 | mark = position;↵
|
65 | }↵ | | |
|
66 |
↵ | | |
|
| | | 65 | }↵
|
|
67 | /**
↵ | | 66 | /**↵
|
68 | * This reader support mark and reset.
↵ | | 67 | * This reader support mark and reset.↵
|
69 | *
↵ | | 68 | *↵
|
70 | * @return true
↵ | | 69 | * @return true↵
|
71 | */
↵ | | 70 | */↵
|
72 | public boolean markSupported()
↵ | | 71 | public boolean markSupported()↵
|
73 | {↵ | | 72 | ↵
|
| | | 73 | {↵
|
74 | return true;↵ | | 74 | return true;↵
|
75 | }↵ | | |
|
|
|
| | | 75 | }↵
|
|
76 | /**
↵ | | 76 | /**↵
|
77 | * Read a single character.
↵ | | 77 | * Read a single character.↵
|
78 | *
↵ | | 78 | *↵
|
79 | * @return the character or -1 if the end of the document has been reached.
↵ | | 79 | * @return the character or -1 if the end of the document has been reached.↵
|
80 | */
↵ | | 80 | */↵
|
81 | public int read()
↵ | | 81 | public int read()↵
|
82 | {↵ | | 82 | ↵
|
| | | 83 | {↵
|
83 | if (position < document.getLength())
↵ | | 84 | if (position < document.getLength())↵
|
84 | {
↵ | | 85 | {↵
|
85 | try
↵ | | 86 | try↵
|
86 | {
↵ | | 87 | {↵
|
87 | char c = document.getText((int) position, 1).charAt(0);
↵ | | 88 | char c = document.getText((int) position, 1).charAt(0);↵
|
88 | position++;
↵ | | 89 | position++;↵
|
89 | return c;
↵ | | 90 | return c;↵
|
90 | }
↵ | | 91 | }↵
|
91 | catch (BadLocationException x)
↵ | | 92 | catch (BadLocationException x)↵
|
92 | {
↵ | | 93 | {↵
|
93 | return -1;
↵ | | 94 | return -1;↵
|
94 | }↵ | | 95 | ↵
|
| | | 96 | }↵
|
95 | }
↵ | | 97 | }↵
|
96 | else
↵ | | 98 | else↵
|
97 | {↵ | | 99 | ↵
|
| | | 100 | {↵
|
98 | return -1;
↵ | | 101 | return -1;↵
|
99 | }↵ | | 102 | ↵
|
100 | }↵ | | |
|
101 |
↵ | | 103 | }↵
|
| | | 104 | }↵
|
|
102 | /**
↵ | | 105 | /**↵
|
103 | * Read and fill the buffer.
↵ | | 106 | * Read and fill the buffer.↵
|
104 | * This method will always fill the buffer unless the end of the document is reached.
↵ | | 107 | * This method will always fill the buffer unless the end of the document is reached.↵
|
105 | *
↵ | | 108 | *↵
|
106 | * @param cbuf the buffer to fill.
↵ | | 109 | * @param cbuf the buffer to fill.↵
|
107 | * @return the number of characters read or -1 if no more characters are available in the document.
↵ | | 110 | * @return the number of characters read or -1 if no more characters are available in the document.↵
|
108 | */
↵ | | 111 | */↵
|
109 | public int read(char[] cbuf)↵ | | 112 | public int read(char[] cbuf)↵
|
110 | {↵ | | |
|
|
| | | 113 | {↵
|
111 | return read(cbuf, 0, cbuf.length);↵ | | 114 | return read(cbuf, 0, cbuf.length);↵
|
112 | }↵ | | |
|
|
|
| | | 115 | }↵
|
|
113 | /**
↵ | | 116 | /**↵
|
114 | * Read and fill the buffer.
↵ | | 117 | * Read and fill the buffer.↵
|
115 | * This method will always fill the buffer unless the end of the document is reached.
↵ | | 118 | * This method will always fill the buffer unless the end of the document is reached.↵
|
116 | *
↵ | | 119 | *↵
|
117 | * @param cbuf the buffer to fill.
↵ | | 120 | * @param cbuf the buffer to fill.↵
|
118 | * @param off offset into the buffer to begin the fill.
↵ | | 121 | * @param off offset into the buffer to begin the fill.↵
|
119 | * @param len maximum number of characters to put in the buffer.
↵ | | 122 | * @param len maximum number of characters to put in the buffer.↵
|
120 | * @return the number of characters read or -1 if no more characters are available in the document.
↵ | | 123 | * @return the number of characters read or -1 if no more characters are available in the document.↵
|
121 | */
↵ | | 124 | */↵
|
122 | public int read(char[] cbuf, int off, int len)↵ | | 125 | public int read(char[] cbuf, int off, int len)↵
|
123 | {↵ | | |
|
|
| | | 126 | {↵
|
124 | if (position < document.getLength())
↵ | | 127 | if (position < document.getLength())↵
|
125 | {↵ | | 128 | ↵
|
| | | 129 | {↵
|
126 | int length = len;
↵ | | 130 | int length = len;↵
|
127 | if (position + length >= document.getLength())
↵ | | 131 | if (position + length >= document.getLength())↵
|
128 | {
↵ | | 132 | {↵
|
129 | length = document.getLength() - (int) position;
↵ | | 133 | length = document.getLength() - (int) position;↵
|
130 | }
↵ | | 134 | }↵
|
131 | if (off + length >= cbuf.length)
↵ | | 135 | if (off + length >= cbuf.length)↵
|
132 | {
↵ | | 136 | {↵
|
133 | length = cbuf.length - off;
↵ | | 137 | length = cbuf.length - off;↵
|
134 | }
↵ | | 138 | }↵
|
135 | try
↵ | | 139 | try↵
|
136 | {
↵ | | 140 | {↵
|
137 | String s = document.getText((int) position, length);
↵ | | 141 | String s = document.getText((int) position, length);↵
|
138 | position += length;
↵ | | 142 | position += length;↵
|
139 | for (int i = 0; i < length; i++)
↵ | | 143 | for (int i = 0; i < length; i++)↵
|
140 | {
↵ | | 144 | {↵
|
| | | 145 | // The Ostermiller SQLLexer crashes with an ArrayIndexOutOfBoundsException↵
|
| | | 146 | // if the char is greater then 255. So we prevent the char from being greater.↵
|
| | | 147 | // This is surely not a proper Unicode treatment but it doesn't seem↵
|
| | | 148 | // to do no harm and it keeps the SQLLexer working.↵
|
141 | cbuf[off + i] = s.charAt(i);↵ | | 149 | cbuf[off + i] = (char)((s.charAt(i)↵
|
| | | 150 | ) % 256);↵
|
142 | }
↵ | | 151 | }↵
|
143 | return length;
↵ | | 152 | return length;↵
|
144 | }
↵ | | 153 | }↵
|
145 | catch (BadLocationException x)
↵ | | 154 | catch (BadLocationException x)↵
|
146 | {
↵ | | 155 | {↵
|
147 | return -1;
↵ | | 156 | return -1;↵
|
148 | }
↵ | | 157 | }↵
|
149 | }↵ | | 158 | ↵
|
| | | 159 | }↵
|
150 | else
↵ | | 160 | else↵
|
151 | {
↵ | | 161 | {↵
|
152 | return -1;
↵ | | 162 | return -1;↵
|
153 | }↵ | | 163 | }↵
|
154 | }↵ | | |
|
|
|
| | | 164 | }↵
|
|
155 | /**
↵ | | 165 | /**↵
|
156 | * @return true
↵ | | 166 | * @return true↵
|
157 | */
↵ | | 167 | */↵
|
158 | public boolean ready()↵ | | 168 | public boolean ready()↵
|
159 | {↵ | | |
|
|
| | | 169 | {↵
|
160 | return true;↵ | | 170 | return true;↵
|
161 | }↵ | | |
|
|
|
| | | 171 | }↵
|
|
162 | /**
↵ | | 172 | /**↵
|
163 | * Reset this reader to the last mark, or the beginning of the document if a mark has not been set.
↵ | | 173 | * Reset this reader to the last mark, or the beginning of the document if a mark has not been set.↵
|
164 | */
↵ | | 174 | */↵
|
165 | public void reset()↵ | | 175 | public void reset()↵
|
166 | {↵ | | |
|
|
| | | 176 | {↵
|
167 | if (mark == -1)
↵ | | 177 | if (mark == -1)↵
|
168 | {↵ | | 178 | ↵
|
| | | 179 | {↵
|
169 | position = 0;
↵ | | 180 | position = 0;↵
|
170 | }
↵ | | 181 | }↵
|
171 | else
↵ | | 182 | else↵
|
172 | {
↵ | | 183 | {↵
|
173 | position = mark;
↵ | | 184 | position = mark;↵
|
174 | }↵ | | 185 | ↵
|
| | | 186 | }↵
|
175 | mark = -1;↵ | | 187 | mark = -1;↵
|
176 | }↵ | | |
|
|
|
| | | 188 | }↵
|
|
177 | /**
↵ | | 189 | /**↵
|
178 | * Skip characters of input.
↵ | | 190 | * Skip characters of input.↵
|
179 | * This method will always skip the maximum number of characters unless
↵ | | 191 | * This method will always skip the maximum number of characters unless↵
|
180 | * the end of the file is reached.
↵ | | 192 | * the end of the file is reached.↵
|
181 | *
↵ | | 193 | *↵
|
182 | * @param n number of characters to skip.
↵ | | 194 | * @param n number of characters to skip.↵
|
183 | * @return the actual number of characters skipped.
↵ | | 195 | * @return the actual number of characters skipped.↵
|
184 | */
↵ | | 196 | */↵
|
185 | public long skip(long n)↵ | | 197 | public long skip(long n)↵
|
186 | {↵ | | |
|
|
| | | 198 | {↵
|
187 | if (position + n <= document.getLength())
↵ | | 199 | if (position + n <= document.getLength())↵
|
188 | {
↵ | | 200 | {↵
|
189 | position += n;
↵ | | 201 | position += n;↵
|
190 | return n;
↵ | | 202 | return n;↵
|
191 | }
↵ | | 203 | }↵
|
192 | else
↵ | | 204 | else↵
|
193 | {
↵ | | 205 | {↵
|
194 | long oldPos = position;
↵ | | 206 | long oldPos = position;↵
|
195 | position = document.getLength();
↵ | | 207 | position = document.getLength();↵
|
196 | return (document.getLength() - oldPos);
↵ | | 208 | return (document.getLength() - oldPos);↵
|
197 | }↵ | | 209 | ↵
|
198 | }↵ | | |
|
199 |
↵ | | 210 | }↵
|
| | | 211 | }↵
|
|
200 | /**
↵ | | 212 | /**↵
|
201 | * Seek to the given position in the document.
↵ | | 213 | * Seek to the given position in the document.↵
|
202 | *
↵ | | 214 | *↵
|
203 | * @param n the offset to which to seek.
↵ | | 215 | * @param n the offset to which to seek.↵
|
204 | */
↵ | | 216 | */↵
|
205 | public void seek(long n)↵ | | 217 | public void seek(long n)↵
|
206 | {↵ | | |
|
|
| | | 218 | {↵
|
207 | if (n <= document.getLength())
↵ | | 219 | if (n <= document.getLength())↵
|
208 | {
↵ | | 220 | {↵
|
209 | position = n;
↵ | | 221 | position = n;↵
|
210 | }
↵ | | 222 | }↵
|
211 | else
↵ | | 223 | else↵
|
212 | {↵ | | 224 | ↵
|
| | | 225 | {↵
|
213 | position = document.getLength() | | 226 | position = document.getLength()
|