001: // Copyright (c) 2002 Per M.A. Bothner.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.lists;
005:
006: import java.io.*;
007:
008: /** A Consumer that extends a PrintWriter. Useful for formatting. */
009:
010: public class PrintConsumer extends PrintWriter implements
011: /* #ifdef JAVA5 */
012: // Appendable,
013: /* #endif */
014: Consumer {
015: public PrintConsumer(Consumer out, boolean autoFlush) {
016: super (out instanceof Writer ? (Writer) out
017: : new ConsumerWriter(out), autoFlush);
018: }
019:
020: public PrintConsumer(OutputStream out, boolean autoFlush) {
021: super (out, autoFlush);
022: }
023:
024: public PrintConsumer(Writer out, boolean autoFlush) {
025: super (out, autoFlush);
026: }
027:
028: public PrintConsumer(Writer out) {
029: super (out);
030: }
031:
032: protected void startNumber() {
033: }
034:
035: protected void endNumber() {
036: }
037:
038: /* #ifdef JAVA5 */
039: // public PrintConsumer append (char c)
040: // {
041: // print(c);
042: // return this;
043: // }
044: // public PrintConsumer append (CharSequence csq)
045: // {
046: // if (csq == null)
047: // csq = "null";
048: // append(csq, 0, csq.length());
049: // return this;
050: // }
051: // public PrintConsumer append (CharSequence csq, int start, int end)
052: // {
053: // if (csq == null)
054: // csq = "null";
055: // for (int i = start; i < end; i++)
056: // append(csq.charAt(i));
057: // return this;
058: // }
059: /* #endif */
060:
061: /* #ifdef use:java.lang.CharSequence */
062: public void write(CharSequence csq, int start, int end) {
063: if (csq instanceof String)
064: write((String) csq, start, end);
065: else {
066: for (int i = start; i < end; i++)
067: write(csq.charAt(i));
068: }
069: }
070:
071: /* #endif */
072:
073: public void writeBoolean(boolean v) {
074: print(v);
075: }
076:
077: public void writeFloat(float v) {
078: startNumber();
079: print(v);
080: endNumber();
081: }
082:
083: public void writeDouble(double v) {
084: startNumber();
085: print(v);
086: endNumber();
087: }
088:
089: public void writeInt(int v) {
090: startNumber();
091: print(v);
092: endNumber();
093: }
094:
095: public void writeLong(long v) {
096: startNumber();
097: print(v);
098: endNumber();
099: }
100:
101: public void startDocument() {
102: }
103:
104: public void endDocument() {
105: }
106:
107: public void startElement(Object type) {
108: }
109:
110: public void endElement() {
111: }
112:
113: public void startAttribute(Object attrType) {
114: }
115:
116: public void endAttribute() {
117: }
118:
119: public void writeObject(Object v) {
120: print(v);
121: }
122:
123: public boolean ignoring() {
124: return false;
125: }
126: }
|