01: //////////////////////////////////////////////////////////////////////////////
02: // Clirr: compares two versions of a java library for binary compatibility
03: // Copyright (C) 2003 - 2005 Lars Kühne
04: //
05: // This library is free software; you can redistribute it and/or
06: // modify it under the terms of the GNU Lesser General Public
07: // License as published by the Free Software Foundation; either
08: // version 2.1 of the License, or (at your option) any later version.
09: //
10: // This library is distributed in the hope that it will be useful,
11: // but WITHOUT ANY WARRANTY; without even the implied warranty of
12: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: // Lesser General Public License for more details.
14: //
15: // You should have received a copy of the GNU Lesser General Public
16: // License along with this library; if not, write to the Free Software
17: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: //////////////////////////////////////////////////////////////////////////////
19:
20: package net.sf.clirr.core;
21:
22: import java.io.FileNotFoundException;
23: import java.io.FileOutputStream;
24: import java.io.OutputStream;
25: import java.io.PrintStream;
26:
27: /**
28: * Abstract DiffListener that writes output to some textual
29: * output stream. That stream can either be System.out or a textfile.
30: *
31: * @author lkuehne
32: */
33: abstract class FileDiffListener extends DiffListenerAdapter {
34: private PrintStream outputStream;
35:
36: /**
37: * Initializes the outputstream.
38: * @param outFile the filename (System.out is used if null is provided here)
39: * @throws java.io.FileNotFoundException if there are problems with
40: */
41: FileDiffListener(String outFile) throws FileNotFoundException {
42: if (outFile == null) {
43: outputStream = System.out;
44: } else {
45: final OutputStream out = new FileOutputStream(outFile);
46: outputStream = new PrintStream(out);
47: }
48:
49: }
50:
51: /**
52: * Returns the output stream so subclasses can write data.
53: * @return the output stream
54: */
55: protected final PrintStream getOutputStream() {
56: return outputStream;
57: }
58:
59: /**
60: * Writes a footer and closes the
61: * output stream if necessary.
62: *
63: * @see #writeFooter()
64: */
65: public final void stop() {
66: writeFooter();
67:
68: if (outputStream != System.out) {
69: outputStream.close();
70: }
71: super .stop();
72: }
73:
74: /**
75: * A hook to write footer info to the output stream.
76: * This implementation does nothing, subclasses can override
77: * this method if necessary.
78: *
79: * @see #stop()
80: */
81: protected void writeFooter() {
82: }
83: }
|