001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library 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 GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.io;
018:
019: // Standard I/O
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.io.Writer;
023:
024: // Geotools dependencies
025: import org.geotools.resources.Arguments;
026: import org.geotools.resources.Utilities;
027:
028: /**
029: * A writer that put line number in front of every line.
030: *
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/io/NumberedLineWriter.java $
032: * @version $Id: NumberedLineWriter.java 24164 2007-02-03 05:10:58Z desruisseaux $
033: * @author Martin Desruisseaux
034: *
035: * @since 2.1
036: */
037: public class NumberedLineWriter extends IndentedLineWriter {
038: /**
039: * A default numbered line writer to the {@linkplain System#out standard output stream}.
040: * The {@link #close} method on this stream will only flush it without closing it.
041: */
042: public static final PrintWriter OUT = new PrintWriter(
043: new Uncloseable(Arguments.getWriter(System.out)), true);
044:
045: /**
046: * A stream that can never been closed. Used only for wrapping the
047: * {@linkplain System#out standard output stream}.
048: */
049: private static final class Uncloseable extends NumberedLineWriter {
050: /** Constructs a stream. */
051: public Uncloseable(final Writer out) {
052: super (out);
053: }
054:
055: /** Flush the stream without closing it. */
056: public void close() throws IOException {
057: flush();
058: }
059: }
060:
061: /**
062: * The with reserved for line numbers (not counting the space for "[ ]" brackets).
063: */
064: private int width = 3;
065:
066: /**
067: * The current line number.
068: */
069: private int current = 1;
070:
071: /**
072: * Constructs a stream which will write line number in front of each line.
073: *
074: * @param out The underlying stream to write to.
075: */
076: public NumberedLineWriter(final Writer out) {
077: super (out);
078: }
079:
080: /**
081: * Returns the current line number.
082: */
083: public int getLineNumber() {
084: return current;
085: }
086:
087: /**
088: * Sets the current line number.
089: *
090: * @param line The current line number.
091: */
092: public void setLineNumber(final int line) {
093: synchronized (lock) {
094: this .current = line;
095: }
096: }
097:
098: /**
099: * Invoked when a new line is begining. The default implementation writes the
100: * current line number.
101: *
102: * @throws IOException If an I/O error occurs
103: */
104: protected void beginNewLine() throws IOException {
105: final String number = String.valueOf(current++);
106: out.write('[');
107: out.write(Utilities.spaces(width - number.length()));
108: out.write(number);
109: out.write("] ");
110: }
111: }
|