001/* An actor that outputs a string read from a text file or URL. 002 003 Copyright (c) 2003-2014 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 */ 027package ptolemy.actor.lib.io; 028 029import java.io.BufferedReader; 030 031import ptolemy.actor.TypedIOPort; 032import ptolemy.actor.lib.LimitedFiringSource; 033import ptolemy.data.BooleanToken; 034import ptolemy.data.StringToken; 035import ptolemy.data.expr.FileParameter; 036import ptolemy.data.expr.Parameter; 037import ptolemy.data.expr.SingletonParameter; 038import ptolemy.data.type.BaseType; 039import ptolemy.kernel.CompositeEntity; 040import ptolemy.kernel.util.IllegalActionException; 041import ptolemy.kernel.util.NameDuplicationException; 042 043/////////////////////////////////////////////////////////////////// 044//// FileReader 045 046/** 047 This actor reads a file or URL and outputs the entire file 048 as a single string. The file or URL is specified using any form 049 acceptable to FileParameter. 050 051 @see FileParameter 052 @see LineReader 053 054 @author Yang Zhao (contributor: Edward A. Lee) 055 @version $Id$ 056 @since Ptolemy II 3.0 057 @Pt.ProposedRating Yellow (eal) 058 @Pt.AcceptedRating Red (reviewmoderator) 059 */ 060public class FileReader extends LimitedFiringSource { 061 /** Construct an actor with a name and a container. 062 * The container argument must not be null, or a 063 * NullPointerException will be thrown. 064 * @param container The container. 065 * @param name The name of this actor. 066 * @exception IllegalActionException If the container is incompatible 067 * with this actor. 068 * @exception NameDuplicationException If the name coincides with 069 * an actor already in the container. 070 */ 071 public FileReader(CompositeEntity container, String name) 072 throws IllegalActionException, NameDuplicationException { 073 super(container, name); 074 075 output.setTypeEquals(BaseType.STRING); 076 077 fileOrURL = new FileParameter(this, "fileOrURL"); 078 079 fileOrURLPort = new TypedIOPort(this, "fileOrURL", true, false); 080 fileOrURLPort.setTypeEquals(BaseType.STRING); 081 new SingletonParameter(fileOrURLPort, "_showName") 082 .setToken(BooleanToken.TRUE); 083 084 newline = new Parameter(this, "newline"); 085 newline.setExpression("property(\"line.separator\")"); 086 087 // Show the firingCountLimit parameter last. 088 firingCountLimit.moveToLast(); 089 090 _attachText("_iconDescription", "<svg>\n" + "<rect x=\"-25\" y=\"-20\" " 091 + "width=\"50\" height=\"40\" " + "style=\"fill:white\"/>\n" 092 + "<polygon points=\"-15,-10 -12,-10 -8,-14 -1,-14 3,-10" 093 + " 15,-10 15,10, -15,10\" " + "style=\"fill:red\"/>\n" 094 + "</svg>\n"); 095 } 096 097 /////////////////////////////////////////////////////////////////// 098 //// public variables //// 099 100 /** The file name or URL from which to read. This is a string with 101 * any form accepted by FileParameter. 102 * @see FileParameter 103 */ 104 public FileParameter fileOrURL; 105 106 /** An input port for optionally providing a file name. This has 107 * type string. 108 */ 109 public TypedIOPort fileOrURLPort; 110 111 /** The end of line character(s). The default value is the value 112 * of the line.separator property 113 */ 114 public Parameter newline; 115 116 /////////////////////////////////////////////////////////////////// 117 //// public methods //// 118 119 /** Output the data read from the file or URL as a string. 120 * @exception IllegalActionException If there is no director or 121 * if reading the file triggers an exception. 122 */ 123 @Override 124 public void fire() throws IllegalActionException { 125 super.fire(); 126 127 // If the fileOrURL input port is connected and has data, then 128 // get the file name from there. 129 if (fileOrURLPort.isOutsideConnected()) { 130 if (fileOrURLPort.hasToken(0)) { 131 String name = ((StringToken) fileOrURLPort.get(0)) 132 .stringValue(); 133 134 // Using setExpression() rather than setToken() allows 135 // the string to refer to variables defined in the 136 // scope of this actor. 137 fileOrURL.setExpression(name); 138 fileOrURL.validate(); 139 } 140 } 141 142 BufferedReader reader = null; 143 144 StringBuffer lineBuffer = new StringBuffer(); 145 try { 146 reader = fileOrURL.openForReading(); 147 148 String newlineValue = ((StringToken) newline.getToken()) 149 .stringValue(); 150 while (true) { 151 String line = reader.readLine(); 152 153 if (line == null) { 154 break; 155 } 156 157 lineBuffer = lineBuffer.append(line); 158 lineBuffer = lineBuffer.append(newlineValue); 159 } 160 } catch (Throwable throwable) { 161 throw new IllegalActionException(this, throwable, 162 "Failed to read '" + fileOrURL + "'"); 163 } finally { 164 if (fileOrURL != null) { 165 fileOrURL.close(); 166 } 167 } 168 _handleFileData(lineBuffer.toString()); 169 } 170 171 /////////////////////////////////////////////////////////////////// 172 //// protected methods //// 173 174 /** Send the specified string to the output. 175 * @param fileContents The contents of the file or URL that was read. 176 * @exception IllegalActionException If sending the data fails. 177 */ 178 protected void _handleFileData(String fileContents) 179 throws IllegalActionException { 180 output.broadcast(new StringToken(fileContents)); 181 } 182}