KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > text > LineMapping


1 package org.incava.text;
2
3 import java.awt.Point JavaDoc;
4 import java.io.*;
5 import java.util.*;
6
7
8 /**
9  * Converts from 0-indexed string positions to line:column values. Lines and
10  * columns are 1-indexed, matching the Java parser.
11  */

12 public class LineMapping extends ArrayList
13 {
14     public class PositionToLocation
15     {
16         public int position;
17         
18         public int line;
19
20         public int column;
21
22         public PositionToLocation(int position, int line, int column)
23         {
24             this.position = position;
25             this.line = line;
26             this.column = column;
27         }
28
29         public String JavaDoc toString()
30         {
31             return "{ position: " + position + " => { line: " + line + ", column: " + column + " } }";
32         }
33     }
34
35     public LineMapping(String JavaDoc text, int startLine, int startColumn)
36     {
37         // tr.Ace.log("position: " + 0 + " => { line: " + startLine + ", column: " + startColumn + " }");
38
add(new PositionToLocation(0, startLine, startColumn));
39
40         int len = text.length();
41
42         // figure out where the lines start:
43
for (int pos = 0, line = startLine; pos < len; ++pos) {
44             // Mac: \r, Unix: \n, DOS: \r\n
45
if (text.charAt(pos) == '\r') {
46                 // might be DOS:
47
if (pos + 1 < len && text.charAt(pos + 1) == '\n') {
48                     ++pos;
49                 }
50                 // otherwise, it's Mac (at least, before OS X)
51
}
52             else if (text.charAt(pos) == '\n') {
53                 // Unix
54
}
55             else {
56                 // we're not at a newline, so go to the next character.
57
continue;
58             }
59
60             ++line;
61
62             // tr.Ace.log("position: " + pos + " => { line: " + line + ", column: " + 1 + " }");
63
add(new PositionToLocation(pos + 1, line, 1));
64         }
65
66         // tr.Ace.log("line positions: " + this);
67
}
68
69     /**
70      * Converts the string position to a line:column start and end location.
71      */

72     public Location[] getLocations(Point JavaDoc pos)
73     {
74         return getLocations(pos.x, pos.y);
75     }
76
77     /**
78      * Converts the string position to a line:column start and end location.
79      */

80     public Location[] getLocations(int startPos, int endPos)
81     {
82         // tr.Ace.log("parsing description");
83
Location start = null;
84         Location end = null;
85
86         // tr.Ace.log("position: " + startPos + ", " + endPos);
87

88         // go backward
89
ListIterator lit = listIterator(size());
90         while ((start == null || end == null) && lit.hasPrevious()) {
91             PositionToLocation pl = (PositionToLocation)lit.previous();
92             // tr.Ace.log("considering position/location " + pl);
93
if (end == null && endPos >= pl.position) {
94                 // tr.Ace.log("assigning end");
95
end = new Location(pl.line, pl.column + endPos - pl.position);
96             }
97             if (start == null && startPos >= pl.position) {
98                 // tr.Ace.log("assigning start");
99
start = new Location(pl.line, pl.column + startPos - pl.position);
100             }
101         }
102
103         // attn Sun: tuples, please!
104
return new Location[] { start, end };
105     }
106     
107     /**
108      * Converts the string position to a line:column location.
109      */

110     public Location getLocation(int pos)
111     {
112         Location location = null;
113
114         // tr.Ace.log("position: " + pos);
115

116         // go backward
117
ListIterator lit = listIterator(size());
118         while (location == null && lit.hasPrevious()) {
119             PositionToLocation pl = (PositionToLocation)lit.previous();
120             // tr.Ace.log("considering position/location " + pl);
121
if (location == null && pos >= pl.position) {
122                 // tr.Ace.log("creating location");
123
return new Location(pl.line, pl.column + pos - pl.position);
124             }
125         }
126
127         return null;
128     }
129     
130 }
131
Popular Tags