KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > LineLengths


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 org.terracotta.dso;
5
6 import org.apache.commons.io.IOUtils;
7 import org.eclipse.core.resources.IFile;
8 import org.eclipse.core.runtime.CoreException;
9
10 import java.io.BufferedReader 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 /**
18  * Keeps information about a text file's line lengths. Used by TcPlugin
19  * to help create SAXMarkers when there are errors on the config document.
20  * This object is serialized to the plugin's private working area, along
21  * with the config document and config object, after successfully parsing
22  * the document into the object.
23  *
24  * Eclipse documents provide this sort of information but we can't rely
25  * on the config document being loaded into an editor.
26  *
27  * @see TcPlugin.loadDomainConfiguration
28  * @see TcPlugin.handleXmlErrors
29  */

30
31 public class LineLengths implements java.io.Serializable JavaDoc {
32   private int[] m_lines;
33   
34   public LineLengths() {
35     super();
36   }
37   
38   public LineLengths(IFile file)
39     throws ConcurrentModificationException JavaDoc,
40            IOException JavaDoc,
41            CoreException
42   {
43     this();
44     setFile(file);
45   }
46
47   public LineLengths(Reader JavaDoc reader)
48     throws ConcurrentModificationException JavaDoc,
49            IOException JavaDoc
50   {
51     this();
52     setReader(reader);
53   }
54   
55   public void setFile(IFile file)
56     throws ConcurrentModificationException JavaDoc,
57            IOException JavaDoc,
58            CoreException
59   {
60     initLines(new InputStreamReader JavaDoc(file.getContents()));
61   }
62
63   public void setReader(Reader JavaDoc reader)
64     throws ConcurrentModificationException JavaDoc,
65            IOException JavaDoc
66   {
67     initLines(reader);
68   }
69   
70   private void initLines(Reader JavaDoc reader)
71     throws ConcurrentModificationException JavaDoc,
72            IOException JavaDoc
73   {
74     ArrayList JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
75     
76     try {
77       BufferedReader JavaDoc br = new BufferedReader JavaDoc(reader);
78       String JavaDoc s;
79       
80       while((s = readLine(br)) != null) {
81         list.add(s);
82       }
83     } catch(IOException JavaDoc e) {
84       IOUtils.closeQuietly(reader);
85       throw e;
86     } catch(ConcurrentModificationException JavaDoc e) {
87       IOUtils.closeQuietly(reader);
88       throw e;
89     } finally {
90       IOUtils.closeQuietly(reader);
91     }
92     
93     int size = list.size();
94     m_lines = new int[size];
95     for(int i = 0; i < size; i++) {
96       m_lines[i] = list.get(i).length();
97     }
98   }
99
100   /**
101    * Reads a complete line of characters from the reader, preserving the
102    * various forms of line-separator that exist.
103    */

104   private String JavaDoc readLine(BufferedReader JavaDoc br) throws IOException JavaDoc {
105     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
106     boolean haveCR = false;
107     boolean done = false;
108     int i;
109     char c;
110     
111     while((i = br.read()) != -1) {
112       c = (char)i;
113       
114       if(haveCR) {
115         switch(c) {
116           case '\n':
117             sb.append(c);
118             done = true;
119             break;
120           default:
121             br.reset();
122             done = true;
123             break;
124         }
125       }
126       else {
127         sb.append(c);
128         switch(c) {
129           case '\n':
130             done = true;
131             break;
132           case '\r':
133             br.mark(1);
134             haveCR = true;
135         }
136       }
137       
138       if(done) {
139         break;
140       }
141     }
142
143     return sb.length() > 0 ? sb.toString() : null;
144   }
145   
146   public int lineSize(int line) {
147     if(line < 0 || line > m_lines.length) {
148       return 0;
149     }
150     return m_lines[line];
151   }
152   
153   public int offset(int line) {
154     int result = 0;
155     
156     if(line == 0) return 0;
157     
158     for(int i = 0; i < line; i++) {
159       result += m_lines[i];
160     }
161
162     return result;
163   }
164   
165   public int offset(int line, int col) {
166     if(line < 0 || line > m_lines.length) {
167       return 0;
168     }
169     int result = offset(line);
170     if(col > 0) {
171       result += col;
172     }
173     return result;
174   }
175 }
176
Popular Tags