001/* 002 * Copyright (c) 2002-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.skidlkit; 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.gui.MessageHandler; 040import ptolemy.kernel.CompositeEntity; 041import ptolemy.kernel.util.IllegalActionException; 042import ptolemy.kernel.util.NameDuplicationException; 043 044////////////////////////////////////////////////////////////////////////// 045//// TextFileReader 046/** 047 * This actor reads a text file and outputs an array of the evaluations of all 048 * lines read. 049 * 050 * @author Longjiang Ding 051 * @version $Id: TextFileReader.java 24234 2010-05-06 05:21:26Z welker $ 052 * @since Ptolemy II 3.0.2 053 */ 054public class TextFileReader extends TypedAtomicActor { 055 /** 056 * Construct an actor with the given container and name. 057 * 058 * @param container 059 * The container. 060 * @param name 061 * The name of this actor. 062 * @exception IllegalActionException 063 * If the actor cannot be contained by the proposed 064 * container. 065 * @exception NameDuplicationException 066 * If the container already has an actor with this name. 067 */ 068 public TextFileReader(CompositeEntity container, String name) 069 throws IllegalActionException, NameDuplicationException { 070 super(container, name); 071 input = new TypedIOPort(this, "input", true, false); 072 input.setTypeEquals(BaseType.STRING); 073 output = new TypedIOPort(this, "output", false, true); 074 output.setTypeEquals(BaseType.STRING); 075 _attachText("_iconDescription", "<svg>\n" 076 + "<rect x=\"-25\" y=\"-20\" " + "width=\"50\" height=\"40\" " 077 + "style=\"fill:white\"/>\n" 078 + "<polygon points=\"-15,-10 -12,-10 -8,-14 -1,-14 3,-10" 079 + " 15,-10 15,10, -15,10\" " + "style=\"fill:orange\"/>\n" 080 + "</svg>\n"); 081 } // end of constructor 082 083 // ///////////////////////////////////////////////////////////////// 084 // // ports and parameters //// 085 /** 086 * The local input & output file names 087 */ 088 public TypedIOPort input; 089 public TypedIOPort output; 090 091 // ///////////////////////////////////////////////////////////////// 092 // // public methods //// 093 /** 094 * Read the input file name string if it is other than null ... 095 * 096 * @exception IllegalActionException 097 * If there's no director. 098 */ 099 public void fire() throws IllegalActionException { 100 super.fire(); 101 StringToken inToken = null; 102 if (input.getWidth() != 0) { // get input from input port 103 try { 104 if (input.hasToken(0)) { 105 // If the inputToken is null set reFire to false. 106 try { 107 inToken = (StringToken) input.get(0); 108 } catch (Exception ex) { 109 } 110 if (inToken != null) { 111 // set up command 112 BufferedReader br = new BufferedReader(new FileReader( 113 inToken.stringValue())); 114 StringBuffer buf = new StringBuffer(); 115 String currentline = br.readLine(); 116 while (currentline != null) { 117 buf.append(currentline + "\n"); 118 currentline = br.readLine(); 119 } 120 output.send(0, new StringToken(buf.toString())); 121 } else { 122 MessageHandler.error("NoTokenException"); 123 } 124 } 125 } catch (Exception e) { 126 MessageHandler.error( 127 "Error opening/updating one of the input parameters: ", 128 e); 129 } 130 } else { // otherwise use FileAttribute filename's value 131 System.out.println("File : " + inToken.stringValue() 132 + " not exist!"); 133 } 134 } 135 136 /** 137 * Post fire the actor. Return false to indicate that the process has 138 * finished. If it returns true, the process will continue indefinitely. 139 */ 140 public boolean postfire() { 141 return false; 142 } 143}