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.FilenameFilter;
034
035import ptolemy.actor.TypedAtomicActor;
036import ptolemy.actor.TypedIOPort;
037import ptolemy.actor.parameters.PortParameter;
038import ptolemy.data.ArrayToken;
039import ptolemy.data.StringToken;
040import ptolemy.data.expr.StringParameter;
041import ptolemy.data.type.ArrayType;
042import ptolemy.data.type.BaseType;
043import ptolemy.kernel.CompositeEntity;
044import ptolemy.kernel.util.IllegalActionException;
045import ptolemy.kernel.util.NameDuplicationException;
046
047//////////////////////////////////////////////////////////////////////////
048//// FileArrayPrinter
049
050/**
051 * This actor reads a directory and writes a string array of the non-hidden,
052 * readable files in it, including their path names. The files can be filtered
053 * by their extension.
054 * 
055 * @author Wibke Sudholt, University and ETH Zurich, October 2004
056 * @version $Id: FileArrayPrinter.java 24234 2010-05-06 05:21:26Z welker $
057 */
058public class FileArrayPrinter extends TypedAtomicActor {
059
060        /**
061         * Construct a FileArrayPrinter with the given container and name.
062         * 
063         * @param container
064         *            The container.
065         * @param name
066         *            The name of this actor.
067         * @exception IllegalActionException
068         *                If the entity cannot be contained by the proposed
069         *                container.
070         * @exception NameDuplicationException
071         *                If the container already has an actor with this name.
072         */
073        public FileArrayPrinter(CompositeEntity container, String name)
074                        throws NameDuplicationException, IllegalActionException {
075                super(container, name);
076
077                trigger = new TypedIOPort(this, "trigger", true, false);
078                trigger.setTypeEquals(BaseType.UNKNOWN);
079                trigger.setMultiport(true);
080
081                files = new TypedIOPort(this, "files", false, true);
082                files.setTypeEquals(new ArrayType(BaseType.STRING));
083
084                directory = new PortParameter(this, "directory");
085                directory.setStringMode(true);
086
087                filter = new StringParameter(this, "File extension filter");
088
089                _attachText("_iconDescription", "<svg>\n"
090                                + "<rect x=\"-25\" y=\"-20\" " + "width=\"50\" height=\"40\" "
091                                + "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        // // ports and parameters ////
099
100        /**
101         * The input port, which is a trigger.
102         */
103        public TypedIOPort trigger = null;
104        /**
105         * The output port, which is an array with the file paths and names.
106         */
107        public TypedIOPort files = null;
108        /**
109         * The port or parameter, which is a string with the directory name.
110         */
111        public PortParameter directory = null;
112        /**
113         * The parameter, which is a string with the file extension filter.
114         */
115        public StringParameter filter = null;
116
117        // /////////////////////////////////////////////////////////////////
118        // // public methods ////
119
120        /**
121         * Take the directory and print out the file array.
122         * 
123         * @exception IllegalActionException
124         *                If there's no director.
125         */
126        public void fire() throws IllegalActionException {
127                super.fire();
128                // Consume the trigger tokens.
129                for (int i = 0; i < trigger.getWidth(); i++) {
130                        if (trigger.hasToken(i)) {
131                                trigger.get(i);
132                        }
133                }
134                // Get the directory name.
135                directory.update();
136                _content = ((StringToken) directory.getToken()).stringValue();
137                // Get the filter and put the files into an array.
138                _dirPath = new File(_content);
139                if ((_dirPath.isDirectory()) && (_dirPath.canRead())) {
140                        _selection = filter.stringValue();
141                        _filePaths = _dirPath.listFiles(new _ExtFilter(_selection));
142                        _oldSize = _filePaths.length;
143                        _newSize = 0;
144                        for (int i = 0; i < _oldSize; i++) {
145                                if ((_filePaths[i].isFile()) && (_filePaths[i].canRead())
146                                                && (!_filePaths[i].isHidden())) {
147                                        _newSize++;
148                                }
149                        }
150                        _fileArray = new StringToken[_newSize];
151                        int j = 0;
152                        for (int i = 0; i < _oldSize; i++) {
153                                if ((_filePaths[i].isFile()) && (_filePaths[i].canRead())
154                                                && (!_filePaths[i].isHidden())) {
155                                        try {
156                                                _fileArray[j] = new StringToken(_filePaths[i]
157                                                                .getCanonicalPath());
158                                        } catch (Exception ex) {
159                                                _debug("Cannot get the file path.");
160                                        }
161                                        j++;
162                                }
163                        }
164                        files.send(0, new ArrayToken(_fileArray));
165                }
166        }
167
168        /**
169         * Post fire the actor. Return false to indicate that the process has
170         * finished. If it returns true, the process will continue indefinitely.
171         */
172        public boolean postfire() {
173                return false;
174        }
175
176        // /////////////////////////////////////////////////////////////////
177        // // protected members ////
178
179        // /////////////////////////////////////////////////////////////////
180        // // private methods ////
181
182        /**
183         * Define the file filter by extension.
184         */
185        private class _ExtFilter implements FilenameFilter {
186                private String _extension = null;
187
188                private _ExtFilter(String _extension) {
189                        this._extension = _extension;
190                }
191
192                public boolean accept(File dir, String name) {
193                        if (_extension.length() == 0) {
194                                return true;
195                        } else {
196                                return name.endsWith("." + _extension);
197                        }
198                }
199        }
200
201        // /////////////////////////////////////////////////////////////////
202        // // private members ////
203
204        private String _content;
205        private File _dirPath;
206        private String _selection;
207        private File[] _filePaths;
208        private int _oldSize;
209        private int _newSize;
210        private StringToken[] _fileArray;
211}