001/* An actor that writes input data to the specified writer.
002
003 @Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the
009 above copyright notice and the following two paragraphs appear in all
010 copies of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION 2
026 COPYRIGHTENDKEY
027 */
028package ptolemy.actor.lib;
029
030import java.io.IOException;
031import java.io.OutputStreamWriter;
032
033import ptolemy.data.StringToken;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039///////////////////////////////////////////////////////////////////
040//// Writer
041
042/**
043 This actor reads tokens from any number of input channels and writes
044 their string values to the specified writer.  A newline character
045 is written between firings.  If the width of the input port is greater
046 than one, then values read from the distinct channels are separated
047 by tab characters. If no writer is specified using setWriter(), then
048 this actor writes to the standard output.
049
050 @deprecated FileWriter actor replaces this.
051 @author  Yuhong Xiong, Edward A. Lee
052 @version $Id$
053 @since Ptolemy II 0.4
054 @Pt.ProposedRating Yellow (yuhong)
055 @Pt.AcceptedRating Yellow (mudit)
056 */
057@Deprecated
058public class Writer extends Sink {
059    /** Construct an actor with the given container and name.
060     *  @param container The container.
061     *  @param name The name of this actor.
062     *  @exception IllegalActionException If the actor cannot be contained
063     *   by the proposed container.
064     *  @exception NameDuplicationException If the container already has an
065     *   actor with this name.
066     */
067    public Writer(CompositeEntity container, String name)
068            throws IllegalActionException, NameDuplicationException {
069        super(container, name);
070
071        if (_stdOut == null) {
072            _stdOut = new OutputStreamWriter(System.out);
073        }
074
075        setWriter(_stdOut);
076
077        input.setTypeEquals(BaseType.STRING);
078    }
079
080    ///////////////////////////////////////////////////////////////////
081    ////                         public methods                    ////
082
083    /** Read at most one token from each input channel and write its
084     *  string value.  These values are separated by tab characters.
085     *  If an input channel has no data, then two consecutive tab
086     *  characters are written.
087     *  @exception IllegalActionException If an IO error occurs.
088     */
089    @Override
090    public boolean postfire() throws IllegalActionException {
091        try {
092            String last = "";
093            int width = input.getWidth();
094
095            for (int i = 0; i < width; i++) {
096                if (i > 0) {
097                    _writer.write("\t");
098                }
099
100                if (input.hasToken(i)) {
101                    StringToken inputToken = (StringToken) input.get(i);
102                    last = inputToken.stringValue();
103                    _writer.write(last);
104                }
105            }
106            // Write a newline character only if the last
107            // string does not already have one.
108            if (!last.endsWith("\n")) {
109                _writer.write("\n");
110            }
111            _writer.flush();
112            return super.postfire();
113        } catch (IOException ex) {
114            throw new IllegalActionException(this, ex, "postfire() failed");
115        }
116    }
117
118    /** Set the writer.  If there was a previous writer, close it.
119     *  To set standard output, call this method with argument null.
120     *  @param writer The writer to write to.
121     *  @exception IllegalActionException If an IO error occurs.
122     */
123    public void setWriter(java.io.Writer writer) throws IllegalActionException {
124        try {
125            if (_writer != null && _writer != _stdOut) {
126                _writer.close();
127            }
128        } catch (IOException ex) {
129            throw new IllegalActionException(this, ex,
130                    "setWriter(" + writer + ") failed");
131        }
132
133        if (writer != null) {
134            _writer = writer;
135        } else {
136            _writer = _stdOut;
137        }
138    }
139
140    /** Flush the writer, if there is one.
141     *  @exception IllegalActionException If an IO error occurs.
142     */
143    @Override
144    public void wrapup() throws IllegalActionException {
145        try {
146            if (_writer != null) {
147                // FIXME: Findbugs warns out derived classes such as
148                // FileWriter.attributeChanged() not closing the
149                // _writer.  Should wrapup() close the _writer?
150                _writer.flush();
151            }
152        } catch (IOException ex) {
153            throw new IllegalActionException(this, ex,
154                    "wrapup(" + _writer + ") failed");
155        }
156    }
157
158    ///////////////////////////////////////////////////////////////////
159    ////                         private members                   ////
160    // The writer to write to.
161    private java.io.Writer _writer = null;
162
163    // Standard out as a writer.
164    private static java.io.Writer _stdOut = null;
165}