1 6 7 package de.gulden.util.javasource.jjt; 8 9 import java.io.*; 10 import java.util.*; 11 12 18 public class TextImage 19 { 20 private final int INCREASE_SIZE=100; 21 22 private Vector lines; 23 private Line currentline; 24 25 26 public TextImage() 27 { 28 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 currentline=new Line(); 43 lines.addElement(currentline); 44 } 45 46 public String getRange(int startLine,int startColumn,int endLine,int endColumn) 47 { 48 startColumn--; 49 endColumn--; 50 startLine--; 51 endLine--; 52 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 Line l=(Line)lines.elementAt(startLine); 66 int colStart=startColumn; 67 int colEnd; 68 if (startLine<endLine) 69 { 70 colEnd= l.length-1; 71 } 73 else { 75 colEnd=endColumn; 76 } 77 int len=colEnd-colStart+1; 78 85 87 System.arraycopy(l.data,colStart,buf,0,len); 88 89 93 bufsize=len; 94 95 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 l=(Line)lines.elementAt(endLine); 108 len=endColumn+1; 109 System.arraycopy(l.data,0,buf,bufsize,len); 111 bufsize+=len; 112 } 113 114 return new String (buf,0,bufsize); 115 } 116 117 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 133 void write(char c) 134 { 135 int maxlen=data.length; 136 if (length==maxlen) { 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 |