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.File; 033import java.io.FileWriter; 034 035import ptolemy.actor.TypedAtomicActor; 036import ptolemy.actor.TypedIOPort; 037import ptolemy.data.StringToken; 038import ptolemy.data.expr.StringParameter; 039import ptolemy.data.type.BaseType; 040import ptolemy.kernel.CompositeEntity; 041import ptolemy.kernel.util.IllegalActionException; 042import ptolemy.kernel.util.NameDuplicationException; 043 044////////////////////////////////////////////////////////////////////////// 045//// TextFileWriter 046 047/** 048 * <p> 049 * This actor reads a string-valued input token and writes it to the 050 * corresponding file without any extra line breaks. It does not include any 051 * enclosing quotation marks in the output. The path and name of the file are 052 * given via an input port. The user can decide if the text is appended to the 053 * file if it exists, if an existing file is overwritten, or left as it is. 054 * </p> 055 * <p> 056 * This actor is based on the Ptolemy II LineWriter actor. 057 * </p> 058 * 059 * @author Wibke Sudholt, University and ETH Zurich, November 2004 060 * @version $Id: TextFileWriter.java 24234 2010-05-06 05:21:26Z welker $ 061 */ 062public class TextFileWriter extends TypedAtomicActor { 063 064 /** 065 * Construct a TextFileWriter with the given container and name. 066 * 067 * @param container 068 * The container. 069 * @param name 070 * The name of this actor. 071 * @exception IllegalActionException 072 * If the actor cannot be contained by the proposed 073 * container. 074 * @exception NameDuplicationException 075 * If the container already has an actor with this name. 076 */ 077 public TextFileWriter(CompositeEntity container, String name) 078 throws IllegalActionException, NameDuplicationException { 079 super(container, name); 080 081 string = new TypedIOPort(this, "string", true, false); 082 string.setTypeEquals(BaseType.STRING); 083 084 fileToWrite = new TypedIOPort(this, "fileToWrite", true, false); 085 fileToWrite.setTypeEquals(BaseType.STRING); 086 087 fileWritten = new TypedIOPort(this, "fileWritten", false, true); 088 fileWritten.setTypeEquals(BaseType.STRING); 089 090 change = new StringParameter(this, "Change existing"); 091 change.setTypeEquals(BaseType.STRING); 092 change.addChoice("No"); 093 change.addChoice("Append"); 094 change.addChoice("Overwrite"); 095 change.setToken(new StringToken("No")); 096 097 _attachText("_iconDescription", "<svg>\n" 098 + "<rect x=\"-25\" y=\"-20\" " + "width=\"50\" height=\"40\" " 099 + "style=\"fill:white\"/>\n" 100 + "<polygon points=\"-15,-10 -12,-10 -8,-14 -1,-14 3,-10" 101 + " 15,-10 15,10, -15,10\" " + "style=\"fill:red\"/>\n" 102 + "</svg>\n"); 103 } 104 105 // ///////////////////////////////////////////////////////////////// 106 // // ports and parameters //// 107 108 /** 109 * The first input port, which contains the text to be written. 110 */ 111 public TypedIOPort string = null; 112 /** 113 * The second input port, which contains the file path and name to which to 114 * write. 115 */ 116 public TypedIOPort fileToWrite = null; 117 /** 118 * The output port, which contains the name and path of the written file. 119 */ 120 public TypedIOPort fileWritten = null; 121 /** 122 * The parameter, which specifies what should happen to existing files. 123 */ 124 public StringParameter change = null; 125 126 // ///////////////////////////////////////////////////////////////// 127 // // public methods //// 128 129 /** 130 * Read an input string and write it to the corresponding file. 131 * 132 * @exception IllegalActionException 133 * If there's no director. 134 */ 135 public void fire() throws IllegalActionException { 136 super.fire(); 137 if (string.hasToken(0) && fileToWrite.hasToken(0)) { 138 _text = ((StringToken) string.get(0)).stringValue(); 139 _path = ((StringToken) fileToWrite.get(0)).stringValue(); 140 _changeValue = change.stringValue(); 141 _handle = new File(_path); 142 _needNew = !_handle.exists(); 143 if (_changeValue.equalsIgnoreCase("Append")) { 144 _doChange = true; 145 _append = true; 146 } else if (_changeValue.equalsIgnoreCase("Overwrite")) { 147 _doChange = true; 148 _append = false; 149 } else { 150 _doChange = _needNew; 151 _append = false; 152 } 153 _writer = null; 154 if (_doChange) { 155 if (_needNew) { 156 try { 157 _parentDir = _handle.getParentFile(); 158 if (!_parentDir.exists()) { 159 _mkdirsSuccess = _parentDir.mkdirs(); 160 if (!_mkdirsSuccess) { 161 throw new IllegalActionException(this, 162 "Parent directory " + _parentDir 163 + " was not successfully made."); 164 } 165 } 166 _handle.createNewFile(); 167 } catch (Exception ex) { 168 _debug("File cannot be created."); 169 } 170 } 171 try { 172 _writer = new FileWriter(_handle, _append); 173 _writer.write(_text); 174 _writer.close(); 175 } catch (Exception ex) { 176 _debug("File cannot be written."); 177 } 178 try { 179 _changedFile = _handle.getCanonicalPath(); 180 } catch (Exception ex) { 181 _debug("Path cannot be determined."); 182 } 183 fileWritten.send(0, new StringToken(_changedFile)); 184 } 185 } 186 } 187 188 // ///////////////////////////////////////////////////////////////// 189 // // protected methods //// 190 191 // ///////////////////////////////////////////////////////////////// 192 // // protected members //// 193 194 // ///////////////////////////////////////////////////////////////// 195 // // private members //// 196 197 private String _path; 198 private String _text; 199 private String _changeValue; 200 private File _handle; 201 private boolean _needNew; 202 private boolean _doChange; 203 private boolean _append; 204 private File _parentDir; 205 private boolean _mkdirsSuccess; 206 private FileWriter _writer; 207 private String _changedFile; 208}