001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.support;
019    
020    import java.io.PrintWriter;
021    
022    
023    /**
024     * Class that writes events to a PrintWriter (defaults to 
025     * terminal), using Java's <code>toStirng()</code> formatting.
026     */
027    public class SimpleDataWriter implements IDataWriter {
028            
029            // set autoflush property to true
030            private PrintWriter out = new PrintWriter(System.out, true);
031    
032            /**
033             * Determines the output destination.
034             * Defaults to System.out.
035             * 
036             * @return destination.
037             */
038            public PrintWriter getPrintWriter() {
039                    return out;
040            }
041            
042            /**
043             * Sets the output destination.
044             * 
045             * @param val destination.
046             */
047            public void setPrintWriter(PrintWriter val) {
048                    out = val;
049            }
050    
051            public void writeData(String id, Object data) {
052                    if(id != null) out.print("[" + id + "] ");
053                    out.println(data);
054            }
055            
056            public void close() {
057                    out.flush(); // FIXME: maybe we should close() ot?
058            }
059    }