001: /* Copyright (C) 2003 Internet Archive.
002: *
003: * This file is part of the Heritrix web crawler (crawler.archive.org).
004: *
005: * Heritrix is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU Lesser Public License as published by
007: * the Free Software Foundation; either version 2.1 of the License, or
008: * any later version.
009: *
010: * Heritrix is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser Public License
016: * along with Heritrix; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * PaddingStringBuffer.java
020: * Created on Oct 23, 2003
021: *
022: * $Header$
023: */
024: package org.archive.util;
025:
026: /**
027: * StringBuffer-like utility which can add spaces to reach a certain column. It
028: * allows you to append {@link String}, <code>long</code> and <code>int</code>s
029: * to the buffer.
030: * <p>
031: * Note: This class counts from 1, not 0.
032: * <p>
033: * It uses a StringBuffer behind the scenes.
034: * <p>
035: * To write a string with multiple lines, it is advisible to use the
036: * {@link #newline() newline()} function. Regular appending of strings with
037: * newlines (\n) character should be safe though. Right appending of strings
038: * with such characters is <i>not</i> safe.
039: *
040: * @author Gordon Mohr
041: */
042: public final class PaddingStringBuffer {
043: // The buffer.
044: StringBuffer buffer;
045: // Location in current line
046: int linePos;
047:
048: /**
049: * Create a new PaddingStringBuffer
050: *
051: */
052: public PaddingStringBuffer() {
053: buffer = new StringBuffer();
054: linePos = 0;
055: }
056:
057: /** append a string directly to the buffer
058: * @param string the string to append
059: * @return This wrapped buffer w/ the passed string appended.
060: */
061: public PaddingStringBuffer append(String string) {
062: buffer.append(string);
063: if (string.indexOf('\n') == -1) {
064: linePos += string.length();
065: } else {
066: while (string.indexOf('\n') == -1) {
067: string = string.substring(string.indexOf('\n'));
068: }
069: linePos = string.length();
070: }
071: return this ;
072: }
073:
074: /**
075: * Append a string, right-aligned to the given columm. If the buffer
076: * length is already greater than the column specified, it simply appends
077: * the string
078: *
079: * @param col the column to right-align to
080: * @param string the string, must not contain multiple lines.
081: * @return This wrapped buffer w/ append string, right-aligned to the
082: * given column.
083: */
084: public PaddingStringBuffer raAppend(int col, String string) {
085: padTo(col - string.length());
086: append(string);
087: return this ;
088: }
089:
090: /** Pad to a given column. If the buffer size is already greater than the
091: * column, nothing is done.
092: * @param col
093: * @return The buffer padded to <code>i</code>.
094: */
095: public PaddingStringBuffer padTo(int col) {
096: while (linePos < col) {
097: buffer.append(" ");
098: linePos++;
099: }
100: return this ;
101: }
102:
103: /** append an <code>int</code> to the buffer.
104: * @param i the int to append
105: * @return This wrapped buffer with <code>i</code> appended.
106: */
107: public PaddingStringBuffer append(int i) {
108: append(Integer.toString(i));
109: return this ;
110: }
111:
112: /**
113: * Append an <code>int</code> right-aligned to the given column. If the
114: * buffer length is already greater than the column specified, it simply
115: * appends the <code>int</code>.
116: *
117: * @param col the column to right-align to
118: * @param i the int to append
119: * @return This wrapped buffer w/ appended int, right-aligned to the
120: * given column.
121: */
122: public PaddingStringBuffer raAppend(int col, int i) {
123: return raAppend(col, Integer.toString(i));
124: }
125:
126: /** append a <code>long</code> to the buffer.
127: * @param lo the <code>long</code> to append
128: * @return This wrapped buffer w/ appended long.
129: */
130: public PaddingStringBuffer append(long lo) {
131: append(Long.toString(lo));
132: return this ;
133: }
134:
135: /**Append a <code>long</code>, right-aligned to the given column. If the
136: * buffer length is already greater than the column specified, it simply
137: * appends the <code>long</code>.
138: * @param col the column to right-align to
139: * @param lo the long to append
140: * @return This wrapped buffer w/ appended long, right-aligned to the
141: * given column.
142: */
143: public PaddingStringBuffer raAppend(int col, long lo) {
144: return raAppend(col, Long.toString(lo));
145: }
146:
147: /** reset the buffer back to empty */
148: public void reset() {
149: buffer = new StringBuffer();
150: linePos = 0;
151: }
152:
153: /* (non-Javadoc)
154: * @see java.lang.Object#toString()
155: */
156: public String toString() {
157: return buffer.toString();
158: }
159:
160: /**
161: * Forces a new line in the buffer.
162: */
163: public PaddingStringBuffer newline() {
164: buffer.append("\n");
165: linePos = 0;
166: return this;
167: }
168:
169: }
|