001: /*
002: * TextImage.java
003: *
004: * Created on 31. Mai 2001, 22:23
005: */
006:
007: package de.gulden.util.javasource.jjt;
008:
009: import java.io.*;
010: import java.util.*;
011:
012: /**
013: * Added to this package by Jens.
014: *
015: * @author me
016: * @version
017: */
018: public class TextImage {
019: private final int INCREASE_SIZE = 100;
020:
021: private Vector lines;
022: private Line currentline;
023:
024: /** Creates new TextImage */
025: public TextImage() {
026: //System.out.println("--- NEW FILE ------------------------------------------------------");
027: lines = new Vector();
028: nextLine();
029: }
030:
031: public void write(char c) {
032: currentline.write(c);
033: }
034:
035: public void nextLine() {
036: //if (currentline!=null)
037: //System.out.print(new String(currentline.data,0,currentline.length));
038: currentline = new Line();
039: lines.addElement(currentline);
040: }
041:
042: public String getRange(int startLine, int startColumn, int endLine,
043: int endColumn) {
044: startColumn--;
045: endColumn--;
046: startLine--;
047: endLine--;
048: //System.out.println(startLine+" "+startColumn+" "+endLine+" "+endColumn);
049: // find maximum size of all lines involved to create a buffer big enough
050: int bufsize = 0;
051: for (int line = startLine; line <= endLine; line++) {
052: Line l = (Line) lines.elementAt(line);
053: bufsize += l.length;
054: }
055:
056: char[] buf = new char[bufsize];
057: bufsize = 0;
058:
059: // first line
060: Line l = (Line) lines.elementAt(startLine);
061: int colStart = startColumn;
062: int colEnd;
063: if (startLine < endLine) {
064: colEnd = l.length - 1;
065: //colEnd= colStart + l.length-1;
066: } else // startLine==endLine
067: {
068: colEnd = endColumn;
069: }
070: int len = colEnd - colStart + 1;
071: //if (len < 0) {
072: // len = 0;
073: //}
074: /*if ((len>=buf.length)||(colStart+len>=l.data.length)){
075: // TODO remove
076: System.out.println("####");
077: }*/
078: //try {
079: System.arraycopy(l.data, colStart, buf, 0, len);
080:
081: //} catch (Throwable th) {
082: // System.out.println("####");
083: //}
084:
085: bufsize = len;
086:
087: // middle lines
088: for (int line = startLine + 1; line <= endLine - 1; line++) {
089: l = (Line) lines.elementAt(line);
090: len = l.length;
091: System.arraycopy(l.data, 0, buf, bufsize, len);
092: bufsize += len;
093: }
094:
095: if (startLine < endLine) {
096: // last line
097: l = (Line) lines.elementAt(endLine);
098: len = endColumn + 1;
099: //System.out.print(new String(l.data,0,l.length));
100: System.arraycopy(l.data, 0, buf, bufsize, len);
101: bufsize += len;
102: }
103:
104: return new String(buf, 0, bufsize);
105: }
106:
107: // -------------------------------------------------------
108:
109: class Line {
110: char[] data;
111: int length;
112:
113: Line() {
114: data = new char[INCREASE_SIZE];
115: length = 0;
116: }
117:
118: /**
119: * Should be synchronized when used in a multithreaded environment.
120: */
121: void write(char c) {
122: int maxlen = data.length;
123: if (length == maxlen) // buffer full?
124: {
125: char[] olddata = data;
126: int newlen = length + INCREASE_SIZE;
127: data = new char[newlen];
128: System.arraycopy(olddata, 0, data, 0, length);
129: }
130: data[length++] = c;
131: }
132: }
133: }
|