View Javadoc
Minimize
Table

1   /*
2    * Copyright 2003-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.groovy.antlr;
17  
18  import java.util.List;
19  import java.util.ArrayList;
20  
21  /**
22   * A simple buffer that provides line/col access to chunks of source code
23   * held within itself.
24   *
25   * @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
26   * @version $Revision: 7922 $
27   */
28  public class SourceBuffer {
29      private final List lines;
30      private StringBuffer current;
31  
32      public SourceBuffer() {
33          lines = new ArrayList();
34          //lines.add(new StringBuffer()); // dummy row for position [0] in the List
35  
36          current = new StringBuffer();
37          lines.add(current);
38      }
39  
40      /**
41       * Obtains a snippet of the source code within the bounds specified
42       * @param start (inclusive line/ inclusive column)
43       * @param end (inclusive line / exclusive column)
44       * @return specified snippet of source code as a String, or null if no source available
45       */
46      public String getSnippet(LineColumn start, LineColumn end) {
47          // preconditions
48          if (start == null || end == null) { return null; } // no text to return
49          if (start.equals(end)) { return null; } // no text to return
50          if (lines.size() == 1 && current.length() == 0) { return null; } // buffer hasn't been filled yet
51  
52          // working variables
53          int startLine = start.getLine();
54          int startColumn = start.getColumn();
55          int endLine = end.getLine();
56          int endColumn = end.getColumn();
57  
58          // reset any out of bounds requests
59          if (startLine < 1) { startLine = 1;}
60          if (endLine < 1) { endLine = 1;}
61          if (startColumn < 1) { startColumn = 1;}
62          if (endColumn < 1) { endColumn = 1;}
63          if (startLine > lines.size()) { startLine = lines.size(); }
64          if (endLine > lines.size()) { endLine = lines.size(); }
65  
66          // obtain the snippet from the buffer within specified bounds
67          StringBuffer snippet = new StringBuffer();
68          for (int i = startLine - 1; i < endLine;i++) {
69              String line = ((StringBuffer)lines.get(i)).toString();
70              if (startLine == endLine) {
71                  // reset any out of bounds requests (again)
72                  if (startColumn > line.length()) { startColumn = line.length();}
73                  if (startColumn < 1) { startColumn = 1;}
74                  if (endColumn > line.length()) { endColumn = line.length() + 1;}
75                  if (endColumn < 1) { endColumn = 1;}
76  
77                  line = line.substring(startColumn - 1, endColumn - 1);
78              } else {
79                  if (i == startLine - 1) {
80                      if (startColumn - 1 < line.length()) {
81                          line = line.substring(startColumn - 1);
82                      }
83                  }
84                  if (i == endLine - 1) {
85                      if (endColumn - 1 < line.length()) {
86                          line = line.substring(0,endColumn - 1);
87                      }
88                  }
89              }
90              snippet.append(line);
91          }
92          return snippet.toString();
93      }
94  
95      /**
96       * Writes the specified character into the buffer
97       * @param c
98       */
99      public void write(int c) {
100         if (c != -1) {
101             current.append((char)c);
102         }
103         if (c == '\n') {
104             current = new StringBuffer();
105             lines.add(current);
106         }
107     }
108 }