KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > TextLineInfo


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc;
5
6 import org.apache.commons.io.IOUtils;
7
8 import java.io.BufferedReader JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStreamReader JavaDoc;
13 import java.io.Reader JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.ConcurrentModificationException JavaDoc;
16
17 public class TextLineInfo implements java.io.Serializable JavaDoc {
18   private int[] m_lines;
19   
20   public TextLineInfo() {
21     super();
22   }
23   
24   public TextLineInfo(File JavaDoc file)
25     throws ConcurrentModificationException JavaDoc,
26            IOException JavaDoc
27   {
28     this();
29     setFile(file);
30   }
31
32   public TextLineInfo(Reader JavaDoc reader)
33     throws ConcurrentModificationException JavaDoc,
34            IOException JavaDoc
35   {
36     this();
37     setReader(reader);
38   }
39   
40   public void setFile(File JavaDoc file)
41     throws ConcurrentModificationException JavaDoc,
42            IOException JavaDoc
43   {
44     initLines(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(file)));
45   }
46
47   public void setReader(Reader JavaDoc reader)
48     throws ConcurrentModificationException JavaDoc,
49            IOException JavaDoc
50   {
51     initLines(reader);
52   }
53   
54   private void initLines(Reader JavaDoc reader)
55     throws ConcurrentModificationException JavaDoc,
56            IOException JavaDoc
57   {
58     ArrayList JavaDoc list = new ArrayList JavaDoc();
59     
60     try {
61       BufferedReader JavaDoc br = new BufferedReader JavaDoc(reader);
62       String JavaDoc s;
63       
64       while((s = br.readLine()) != null) {
65         list.add(s);
66       }
67     } catch(IOException JavaDoc e) {
68       IOUtils.closeQuietly(reader);
69       throw e;
70     } catch(ConcurrentModificationException JavaDoc e) {
71       IOUtils.closeQuietly(reader);
72       throw e;
73     } finally {
74       IOUtils.closeQuietly(reader);
75     }
76     
77     int size = list.size();
78     m_lines = new int[size];
79     for(int i = 0; i < size; i++) {
80       m_lines[i] = ((String JavaDoc)list.get(i)).length()+1;
81     }
82   }
83
84   public int lineSize(int line) {
85     if(line < 0 || line > m_lines.length) {
86       return 0;
87     }
88     return m_lines[line];
89   }
90   
91   public int offset(int line) {
92     int result = 0;
93     
94     if(line == 0) return 0;
95     
96     for(int i = 0; i < line; i++) {
97       result += m_lines[i];
98     }
99
100     return result;
101   }
102   
103   public int offset(int line, int col) {
104     if(line < 0 || line > m_lines.length) {
105       return 0;
106     }
107     int result = offset(line);
108     if(col > 0) {
109       result += col;
110     }
111     return result;
112   }
113 }
114
Popular Tags