KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > util > javasource > jjt > TextImage


1 /*
2  * TextImage.java
3  *
4  * Created on 31. Mai 2001, 22:23
5  */

6
7 package de.gulden.util.javasource.jjt;
8
9 import java.io.*;
10 import java.util.*;
11
12 /**
13  * Added to this package by Jens.
14  *
15  * @author me
16  * @version
17  */

18 public class TextImage
19 {
20   private final int INCREASE_SIZE=100;
21   
22   private Vector lines;
23   private Line currentline;
24   
25   /** Creates new TextImage */
26   public TextImage()
27   {
28 //System.out.println("--- NEW FILE ------------------------------------------------------");
29
lines=new Vector();
30     nextLine();
31   }
32   
33   public void write(char c)
34   {
35     currentline.write(c);
36   }
37   
38   public void nextLine()
39   {
40 //if (currentline!=null)
41
//System.out.print(new String(currentline.data,0,currentline.length));
42
currentline=new Line();
43     lines.addElement(currentline);
44   }
45   
46   public String JavaDoc getRange(int startLine,int startColumn,int endLine,int endColumn)
47   {
48     startColumn--;
49     endColumn--;
50     startLine--;
51     endLine--;
52 //System.out.println(startLine+" "+startColumn+" "+endLine+" "+endColumn);
53
// find maximum size of all lines involved to create a buffer big enough
54
int bufsize=0;
55     for (int line=startLine;line<=endLine;line++)
56     {
57       Line l=(Line)lines.elementAt(line);
58       bufsize+=l.length;
59     }
60     
61     char[] buf=new char[bufsize];
62     bufsize=0;
63     
64     // first line
65
Line l=(Line)lines.elementAt(startLine);
66     int colStart=startColumn;
67     int colEnd;
68     if (startLine<endLine)
69     {
70         colEnd= l.length-1;
71         //colEnd= colStart + l.length-1;
72
}
73     else // startLine==endLine
74
{
75       colEnd=endColumn;
76     }
77     int len=colEnd-colStart+1;
78     //if (len < 0) {
79
// len = 0;
80
//}
81
/*if ((len>=buf.length)||(colStart+len>=l.data.length)){
82     // TODO remove
83     System.out.println("####");
84 }*/

85 //try {
86

87     System.arraycopy(l.data,colStart,buf,0,len);
88     
89 //} catch (Throwable th) {
90
// System.out.println("####");
91
//}
92

93     bufsize=len;
94     
95     // middle lines
96
for (int line=startLine+1;line<=endLine-1;line++)
97     {
98       l=(Line)lines.elementAt(line);
99       len=l.length;
100       System.arraycopy(l.data,0,buf,bufsize,len);
101       bufsize+=len;
102     }
103     
104     if (startLine<endLine)
105     {
106       // last line
107
l=(Line)lines.elementAt(endLine);
108       len=endColumn+1;
109 //System.out.print(new String(l.data,0,l.length));
110
System.arraycopy(l.data,0,buf,bufsize,len);
111       bufsize+=len;
112     }
113     
114     return new String JavaDoc(buf,0,bufsize);
115   }
116
117   // -------------------------------------------------------
118

119   class Line
120   {
121     char[] data;
122     int length;
123
124     Line()
125     {
126       data=new char[INCREASE_SIZE];
127       length=0;
128     }
129
130     /**
131      * Should be synchronized when used in a multithreaded environment.
132      */

133     void write(char c)
134     {
135       int maxlen=data.length;
136       if (length==maxlen) // buffer full?
137
{
138         char[] olddata=data;
139         int newlen=length+INCREASE_SIZE;
140         data=new char[newlen];
141         System.arraycopy(olddata,0,data,0,length);
142       }
143       data[length++]=c;
144     }
145   }
146 }
147
Popular Tags