KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > parser > SourceInfo


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.parser;
26
27 import java.io.File JavaDoc;
28 import java.util.*;
29 import org.aspectj.util.IntList;
30
31 public class SourceInfo {
32     private File JavaDoc file;
33     private int[] lineStarts;
34     private char[] text; // there are some possible memory management issues here
35

36     public SourceInfo(File JavaDoc _file, char[] _text) {
37         file = _file;
38         setText(_text);
39     }
40     
41     public void setText(char[] text) {
42         this.text = text;
43         if (text != null) {
44             lineStarts = findNewLines(text);
45         }
46     }
47     
48     private int[] findNewLines(char[] text) {
49         IntList lineStarts = new IntList();
50         lineStarts.add(0);
51         final int N = text.length;
52         for(int i=0; i<N; i++) {
53             char ch = text[i];
54             if (ch == '\r') {
55                 if (i < N-1 && text[i+1] == '\n') {
56                     text[i] = ' '; // this is needed to keep emacs-mode happy
57
// better handling of position information would
58
// work more effectively (and could handle the \\u cases)
59
} else {
60                     text[i] = '\n';
61                     ch = '\n';
62                 }
63             }
64             if (ch == '\n') {
65                 lineStarts.add(i+1);
66             }
67         }
68         lineStarts.add(N+100);
69         return lineStarts.toIntArray();
70     }
71
72     public File JavaDoc getFile() {
73         return file;
74     }
75
76     // line numbers are given based on 1-indexing, i.e. the 1st line is 1 (not 0)
77
public int getLine(int position) {
78         if (lineStarts == null) return -1;
79         int pos = Arrays.binarySearch(lineStarts, position);
80         if (pos >= 0) return pos+1;
81         else return (-pos)-1;
82     }
83
84     // column numbers are also 1-based
85
public int getColumn(int position) {
86         if (lineStarts == null) return -1;
87
88         int line = 1;
89         for(; line < lineStarts.length; line++) {
90             if (lineStarts[line] > position) break;
91         }
92
93         return (position - lineStarts[line-1]) + 1;
94     }
95 }
96
Popular Tags