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.geon;
031
032import java.io.File;
033
034import ptolemy.actor.TypedAtomicActor;
035import ptolemy.actor.TypedIOPort;
036import ptolemy.data.BooleanToken;
037import ptolemy.data.StringToken;
038import ptolemy.data.expr.FileParameter;
039import ptolemy.data.expr.Parameter;
040import ptolemy.data.type.BaseType;
041import ptolemy.kernel.CompositeEntity;
042import ptolemy.kernel.util.IllegalActionException;
043import ptolemy.kernel.util.NameDuplicationException;
044
045//////////////////////////////////////////////////////////////////////////
046//// FileExists
047/**
048 * @see FileParameter
049 * @author Efrat Jaeger
050 * @version $Id: FileExists.java 24234 2010-05-06 05:21:26Z welker $
051 * @since Ptolemy II 4.0.1
052 */
053
054public class FileExists extends TypedAtomicActor {
055
056        /**
057         * Construct an actor with the given container and name.
058         * 
059         * @param container
060         *            The container.
061         * @param name
062         *            The name of this actor.
063         * @exception IllegalActionException
064         *                If the actor cannot be contained by the proposed
065         *                container.
066         * @exception NameDuplicationException
067         *                If the container already has an actor with this name.
068         */
069        public FileExists(CompositeEntity container, String name)
070                        throws IllegalActionException, NameDuplicationException {
071                super(container, name);
072
073                filePath = new TypedIOPort(this, "filePath", true, false);
074                filePath.setTypeEquals(BaseType.STRING);
075
076                output = new TypedIOPort(this, "output", false, true);
077                output.setTypeEquals(BaseType.BOOLEAN);
078
079                filePathParam = new FileParameter(this, "filePathParam");
080                filePathParam.setDisplayName("filePath");
081
082                new Parameter(filePathParam, "allowDirectories", BooleanToken.TRUE);
083
084        }
085
086        // /////////////////////////////////////////////////////////////////
087        // // ports and parameters ////
088
089        /**
090         * File path.
091         */
092        public TypedIOPort filePath;
093
094        /**
095         * Output.
096         */
097        public TypedIOPort output;
098
099        /**
100         * File path name or URL. This is a string with any form accepted by
101         * FileParameter.
102         * 
103         * @see FileParameter
104         */
105        public FileParameter filePathParam;
106
107        /**
108         * Verify whether the file exists.
109         * 
110         * @exception IllegalActionException
111         *                If there's no director.
112         */
113        public void fire() throws IllegalActionException {
114                File _filePath = null;
115                String fileName = "";
116
117                // get source file.
118                if (filePath.getWidth() > 0) {
119                        fileName = ((StringToken) filePath.get(0)).stringValue();
120                        int lineEndInd = fileName.indexOf("\n");
121                        if (lineEndInd != -1) { // The string contains a CR.
122                                fileName = fileName.substring(0, lineEndInd);
123                        }
124                        filePathParam.setExpression(fileName);
125                }
126                _filePath = filePathParam.asFile();
127
128                if (_filePath.exists()) {
129                        output.broadcast(new BooleanToken(true));
130                } else {
131                        output.broadcast(new BooleanToken(false));
132                }
133        }
134}