KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > groovy > antlr > SourceBuffer


1 /**
2  *
3  * Copyright 2005 Jeremy Rayner
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  **/

18 package org.codehaus.groovy.antlr;
19
20 import java.util.List JavaDoc;
21 import java.util.ArrayList JavaDoc;
22
23 /**
24  * A simple buffer that provides line/col access to chunks of source code
25  * held within itself.
26  *
27  * @author <a HREF="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
28  * @version $Revision: 1.4 $
29  */

30 public class SourceBuffer {
31     private List JavaDoc lines;
32     private StringBuffer JavaDoc current;
33
34     public SourceBuffer() {
35         lines = new ArrayList JavaDoc();
36         //lines.add(new StringBuffer()); // dummy row for position [0] in the List
37

38         current = new StringBuffer JavaDoc();
39         lines.add(current);
40     }
41
42     /**
43      * Obtains a snippet of the source code within the bounds specified
44      * @param start (inclusive line/ inclusive column)
45      * @param end (inclusive line / exclusive column)
46      * @return specified snippet of source code as a String, or null if no source available
47      */

48     public String JavaDoc getSnippet(LineColumn start, LineColumn end) {
49         // preconditions
50
if (start == null || end == null) { return null; } // no text to return
51
if (start.equals(end)) { return null; } // no text to return
52
if (lines.size() == 1 && current.length() == 0) { return null; } // buffer hasn't been filled yet
53

54         // working variables
55
int startLine = start.getLine();
56         int startColumn = start.getColumn();
57         int endLine = end.getLine();
58         int endColumn = end.getColumn();
59
60         // reset any out of bounds requests
61
if (startLine < 1) { startLine = 1;}
62         if (endLine < 1) { endLine = 1;}
63         if (startColumn < 1) { startColumn = 1;}
64         if (endColumn < 1) { endColumn = 1;}
65         if (startLine > lines.size()) { startLine = lines.size(); }
66         if (endLine > lines.size()) { endLine = lines.size(); }
67
68         // obtain the snippet from the buffer within specified bounds
69
StringBuffer JavaDoc snippet = new StringBuffer JavaDoc();
70         for (int i = startLine - 1; i < endLine;i++) {
71             String JavaDoc line = ((StringBuffer JavaDoc)lines.get(i)).toString();
72             if (startLine == endLine) {
73                 // reset any out of bounds requests (again)
74
if (startColumn > line.length()) { startColumn = line.length();}
75                 if (startColumn < 1) { startColumn = 1;}
76                 if (endColumn > line.length()) { endColumn = line.length() + 1;}
77                 if (endColumn < 1) { endColumn = 1;}
78
79                 line = line.substring(startColumn - 1, endColumn - 1);
80             } else {
81                 if (i == startLine - 1) {
82                     if (startColumn - 1 < line.length()) {
83                         line = line.substring(startColumn - 1);
84                     }
85                 }
86                 if (i == endLine - 1) {
87                     if (endColumn - 1 < line.length()) {
88                         line = line.substring(0,endColumn - 1);
89                     }
90                 }
91             }
92             snippet.append(line);
93         }
94         return snippet.toString();
95     }
96
97     /**
98      * Writes the specified character into the buffer
99      * @param c
100      */

101     public void write(int c) {
102         if (c != -1) {
103             current.append((char)c);
104         }
105         if (c == '\n') {
106             current = new StringBuffer JavaDoc();
107             lines.add(current);
108         }
109     }
110 }
111
Popular Tags