001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.text;
034:
035: public class PrinterBuffer {
036: private final static String EOL = System.getProperty(
037: "line.separator", "\n");
038:
039: private StringBuffer buffer = new StringBuffer();
040: private String indentText = " ";
041: private int indentLevel = 0;
042:
043: public String getIndentText() {
044: return indentText;
045: }
046:
047: public void setIndentText(String indentText) {
048: this .indentText = indentText;
049: }
050:
051: public PrinterBuffer append(boolean b) {
052: buffer.append(b);
053: return this ;
054: }
055:
056: public PrinterBuffer append(char c) {
057: buffer.append(c);
058: return this ;
059: }
060:
061: public PrinterBuffer append(char[] str, int offset, int len) {
062: buffer.append(str, offset, len);
063: return this ;
064: }
065:
066: public PrinterBuffer append(char[] s) {
067: buffer.append(s);
068: return this ;
069: }
070:
071: public PrinterBuffer append(double d) {
072: buffer.append(d);
073: return this ;
074: }
075:
076: public PrinterBuffer append(float f) {
077: buffer.append(f);
078: return this ;
079: }
080:
081: public PrinterBuffer append(int i) {
082: buffer.append(i);
083: return this ;
084: }
085:
086: public PrinterBuffer append(long l) {
087: buffer.append(l);
088: return this ;
089: }
090:
091: public PrinterBuffer append(Object obj) {
092: buffer.append(obj);
093: return this ;
094: }
095:
096: public PrinterBuffer append(String s) {
097: buffer.append(s);
098: return this ;
099: }
100:
101: public PrinterBuffer indent() {
102: for (int i = 0; i < indentLevel; i++) {
103: buffer.append(getIndentText());
104: }
105:
106: return this ;
107: }
108:
109: public PrinterBuffer eol() {
110: buffer.append(EOL);
111: return this ;
112: }
113:
114: public void raiseIndent() {
115: indentLevel++;
116: }
117:
118: public void lowerIndent() {
119: indentLevel--;
120: }
121:
122: public int length() {
123: return buffer.length();
124: }
125:
126: public String toString() {
127: return buffer.toString();
128: }
129: }
|