001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.io;
024:
025: import java.io.*;
026:
027: public class IndentedWriter extends FilterWriter {
028: final static String EOL;
029:
030: static {
031: String eol = System.getProperty("line.separator");
032: EOL = (eol != null ? eol : "\r\n");
033: }
034:
035: int indent_level = 0;
036: boolean at_line_start = true;
037:
038: public IndentedWriter(Writer out) {
039: super (out);
040: }
041:
042: private boolean isEol(char c) {
043: return (c == '\r' || c == '\n');
044: }
045:
046: public void upIndent() {
047: ++indent_level;
048: }
049:
050: public void downIndent() {
051: --indent_level;
052: }
053:
054: public void write(int c) throws IOException {
055: out.write(c);
056: at_line_start = isEol((char) c);
057: }
058:
059: public void write(char[] chars, int off, int len)
060: throws IOException {
061: out.write(chars, off, len);
062: at_line_start = isEol(chars[off + len - 1]);
063: }
064:
065: public void write(String s, int off, int len) throws IOException {
066: if (len > 0) {
067: out.write(s, off, len);
068: at_line_start = isEol(s.charAt(off + len - 1));
069: }
070: }
071:
072: private void printIndent() throws IOException {
073: for (int i = 0; i < indent_level; ++i)
074: out.write('\t');
075: }
076:
077: public void print(String s) throws IOException {
078: if (at_line_start)
079: printIndent();
080: out.write(s);
081: char last = s.charAt(s.length() - 1);
082: at_line_start = isEol(last);
083: }
084:
085: public void println(String s) throws IOException {
086: if (at_line_start)
087: printIndent();
088: out.write(s);
089: out.write(EOL);
090: at_line_start = true;
091: }
092:
093: public void print(boolean x) throws IOException {
094: print(String.valueOf(x));
095: }
096:
097: public void print(byte x) throws IOException {
098: print(String.valueOf(x));
099: }
100:
101: public void print(char x) throws IOException {
102: print(String.valueOf(x));
103: }
104:
105: public void print(short x) throws IOException {
106: print(String.valueOf(x));
107: }
108:
109: public void print(int x) throws IOException {
110: print(String.valueOf(x));
111: }
112:
113: public void print(long x) throws IOException {
114: print(String.valueOf(x));
115: }
116:
117: public void print(float x) throws IOException {
118: print(String.valueOf(x));
119: }
120:
121: public void print(double x) throws IOException {
122: print(String.valueOf(x));
123: }
124:
125: public void print(Object x) throws IOException {
126: print(String.valueOf(x));
127: }
128:
129: public void println(boolean x) throws IOException {
130: println(String.valueOf(x));
131: }
132:
133: public void println(byte x) throws IOException {
134: println(String.valueOf(x));
135: }
136:
137: public void println(char x) throws IOException {
138: println(String.valueOf(x));
139: }
140:
141: public void println(short x) throws IOException {
142: println(String.valueOf(x));
143: }
144:
145: public void println(int x) throws IOException {
146: println(String.valueOf(x));
147: }
148:
149: public void println(long x) throws IOException {
150: println(String.valueOf(x));
151: }
152:
153: public void println(float x) throws IOException {
154: println(String.valueOf(x));
155: }
156:
157: public void println(double x) throws IOException {
158: println(String.valueOf(x));
159: }
160:
161: public void println(Object x) throws IOException {
162: println(String.valueOf(x));
163: }
164:
165: public void println() throws IOException {
166: println("");
167: }
168: }
|