001/*
002 * Copyright (c) 2005-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.resurgence.actor;
031
032import java.io.BufferedReader;
033import java.io.FileReader;
034
035import ptolemy.actor.TypedAtomicActor;
036import ptolemy.actor.TypedIOPort;
037import ptolemy.data.StringToken;
038import ptolemy.data.type.BaseType;
039import ptolemy.kernel.CompositeEntity;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NameDuplicationException;
042
043//////////////////////////////////////////////////////////////////////////
044//// SimpleFileReader
045
046/**
047 * <p>
048 * This actor reads a file and outputs its contents as a single string.
049 * </p>
050 * <p>
051 * It is based on the Ptolemy II FileReader actor.
052 * </p>
053 * 
054 * @author Wibke Sudholt, University of Zurich, April 2005
055 * @version $Id: SimpleFileReader.java 24234 2010-05-06 05:21:26Z welker $
056 */
057public class SimpleFileReader extends TypedAtomicActor {
058
059        public SimpleFileReader(CompositeEntity container, String name)
060                        throws IllegalActionException, NameDuplicationException {
061                super(container, name);
062
063                file = new TypedIOPort(this, "file", true, false);
064                file.setTypeEquals(BaseType.STRING);
065
066                content = new TypedIOPort(this, "content", false, true);
067                content.setTypeEquals(BaseType.STRING);
068
069                _attachText("_iconDescription", "<svg>\n"
070                                + "<rect x=\"-25\" y=\"-20\" " + "width=\"50\" height=\"40\" "
071                                + "style=\"fill:white\"/>\n"
072                                + "<polygon points=\"-15,-10 -12,-10 -8,-14 -1,-14 3,-10"
073                                + " 15,-10 15,10, -15,10\" " + "style=\"fill:red\"/>\n"
074                                + "</svg>\n");
075        }
076
077        // /////////////////////////////////////////////////////////////////
078        // // public variables ////
079
080        /**
081         * The input port, which gives the file name from which to read.
082         */
083        public TypedIOPort file;
084        /**
085         * The output port, which provides the file contents.
086         */
087        public TypedIOPort content;
088
089        // /////////////////////////////////////////////////////////////////
090        // // public methods ////
091
092        /**
093         * Output the data read from the file as a string.
094         * 
095         * @exception IllegalActionException
096         *                If there is no director or if reading the file triggers an
097         *                exception.
098         */
099        public void fire() throws IllegalActionException {
100                super.fire();
101
102                if (file.hasToken(0)) {
103                        _path = ((StringToken) file.get(0)).stringValue();
104                }
105
106                try {
107                        BufferedReader reader = new BufferedReader(new FileReader(_path));
108                        StringBuffer lineBuffer = new StringBuffer();
109                        String newline = System.getProperty("line.separator");
110                        while (true) {
111                                String line = reader.readLine();
112                                if (line == null)
113                                        break;
114                                lineBuffer = lineBuffer.append(line);
115                                lineBuffer = lineBuffer.append(newline);
116                        }
117                        content.send(0, new StringToken(lineBuffer.toString()));
118                } catch (Exception ex) {
119                        throw new IllegalActionException(this, ex.getMessage());
120                }
121        }
122
123        // /////////////////////////////////////////////////////////////////
124        // // protected members ////
125
126        // /////////////////////////////////////////////////////////////////
127        // // private methods ////
128
129        // /////////////////////////////////////////////////////////////////
130        // // private members ////
131
132        private String _path;
133}