001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.print.text;
010:
011: import java.util.StringTokenizer;
012: import java.util.ArrayList;
013:
014: /**
015: * Description of the Class
016: *
017: *@author Chris Seguin
018: */
019: public class LineSet {
020: private ArrayList set;
021: private final static int TAB_SIZE = 4;
022:
023: /**
024: * Constructor for the LineSet object
025: *
026: *@param data Description of Parameter
027: */
028: public LineSet(String data) {
029: set = new ArrayList();
030:
031: breakLine(data);
032: }
033:
034: /**
035: * Gets the Line attribute of the LineSet object
036: *
037: *@param index Description of Parameter
038: *@return The Line value
039: */
040: public String getLine(int index) {
041: if ((index < 0) || (index >= set.size())) {
042: return null;
043: }
044:
045: return expandTabs((String) set.get(index));
046: }
047:
048: /**
049: * Description of the Method
050: *
051: *@return Description of the Returned Value
052: */
053: public int size() {
054: return set.size();
055: }
056:
057: /**
058: * Description of the Method
059: *
060: *@param input Description of Parameter
061: */
062: private void breakLine(String input) {
063: int last = -1;
064: int current = 0;
065: int length = input.length();
066:
067: while (last < length) {
068: while ((current < length)
069: && (input.charAt(current) != '\n')) {
070: current++;
071: }
072:
073: String next = input.substring(last + 1, current);
074: set.add(next);
075: last = current;
076: current++;
077: }
078: }
079:
080: /**
081: * Description of the Method
082: *
083: *@param line Description of Parameter
084: *@return Description of the Returned Value
085: */
086: private String expandTabs(String line) {
087: StringBuffer buffer = new StringBuffer();
088: int last = line.length();
089: for (int ndx = 0; ndx < last; ndx++) {
090: char ch = line.charAt(ndx);
091: if (ch == '\t') {
092: int bufferLength = buffer.length();
093: int spaces = bufferLength % TAB_SIZE;
094: if (spaces == 0) {
095: spaces = TAB_SIZE;
096: }
097:
098: for (int ndx2 = 0; ndx2 < spaces; ndx2++) {
099: buffer.append(' ');
100: }
101: } else if ((ch == '\r') || (ch == '\n')) {
102: // Skip this character
103: } else {
104: buffer.append(ch);
105: }
106: }
107:
108: return buffer.toString();
109: }
110: }
|