KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > util > TextPositionsMapper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.core.util;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Maps offset in a string to line/column
30  */

31 public class TextPositionsMapper {
32     private String JavaDoc text;
33     private int[] offsets;
34
35     /**
36      * Constructs a mapper
37      *
38      * @param text a text
39      */

40     public TextPositionsMapper(String JavaDoc text) {
41         this.text = text;
42
43         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new StringReader JavaDoc(text));
44         List JavaDoc offsets = new ArrayList JavaDoc();
45         offsets.add(new Integer JavaDoc(0));
46
47         for (int i = 0; i < text.length(); i++) {
48             char c = text.charAt(i);
49             if (c == '\r') {
50                 if (i + 1 < text.length() && text.charAt(i + 1) == '\n') {
51                     i++;
52                     offsets.add(new Integer JavaDoc(i + 1));
53                 } else {
54                     offsets.add(new Integer JavaDoc(i + 1));
55                 }
56             } else if (c == '\n') {
57                 offsets.add(new Integer JavaDoc(i + 1));
58             }
59         }
60         
61         this.offsets = new int[offsets.size()];
62         for (int i = 0; i < this.offsets.length; i++) {
63             this.offsets[i] = ((Integer JavaDoc) offsets.get(i)).intValue();
64         }
65     }
66     
67     /**
68      * Returns line/column in the text for the specified offset.
69      *
70      * @param offset an offset. 0 based
71      * @param position int[] {line, column}. Line and column are 0 based.
72      */

73     public void findPosition(int offset, int[] position) {
74         assert offset >= 0 : "offset couldn't be negative"; // NOI18N
75

76         int index = Arrays.binarySearch(offsets, offset);
77         if (index >= 0) {
78             position[0] = index;
79             position[1] = 0;
80         } else {
81             index = -(index + 1);
82             assert index != 0 : "offset couldn't be negative"; // NOI18N
83
position[0] = index - 1;
84             position[1] = offset - offsets[index - 1];
85         }
86     }
87
88     /**
89      * Returns the text
90      *
91      * @return text
92      */

93     public String JavaDoc getText() {
94         return text;
95     }
96     
97     /**
98      * Returns the text for the specified line. Line feed characters at the end
99      * of a line will also be returned.
100      *
101      * @param line line number (0, 1, 2, ..)
102      * @return text of a line
103      */

104     public String JavaDoc getLine(int line) {
105         int offset = offsets[line];
106         int offset2;
107         if (offsets.length > line + 1) {
108             offset2 = offsets[line + 1];
109         } else {
110             offset2 = text.length();
111         }
112         return text.substring(offset, offset2);
113     }
114 }
115
Popular Tags