001/*
002 * Copyright (c) 2005-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: brooks $'
006 * '$Date: 2010-06-10 20:14:30 +0000 (Thu, 10 Jun 2010) $' 
007 * '$Revision: 24798 $'
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 ptolemy.actor.TypedAtomicActor;
033import ptolemy.actor.TypedIOPort;
034import ptolemy.data.ArrayToken;
035import ptolemy.data.StringToken;
036import ptolemy.data.Token;
037import ptolemy.data.expr.StringParameter;
038import ptolemy.data.type.ArrayType;
039import ptolemy.data.type.BaseType;
040import ptolemy.kernel.CompositeEntity;
041import ptolemy.kernel.util.IllegalActionException;
042import ptolemy.kernel.util.NameDuplicationException;
043
044//////////////////////////////////////////////////////////////////////////
045//// StringSplitter
046
047/**
048 * Read a string and write an array filled with the parts of the string. The
049 * regular expression separating the entries can be specified as a parameter.
050 * 
051 * <p>Note that this class uses java.util.String.split(), which means that
052 * trailing empty strings are not included in the results.</p>
053
054 * @author Wibke Sudholt, University and ETH Zurich, November 2004
055 * @version $Id: StringSplitter.java 24798 2010-06-10 20:14:30Z brooks $
056 */
057public class StringSplitter extends TypedAtomicActor {
058
059        // FIXME: we need a StringTokenizer actor
060
061        /**
062         * Construct a StringSplitter with the given container and name.
063         * 
064         * @param container
065         *            The container.
066         * @param name
067         *            The name of this actor.
068         * @exception IllegalActionException
069         *                If the entity cannot be contained by the proposed
070         *                container.
071         * @exception NameDuplicationException
072         *                If the container already has an actor with this name.
073         */
074        public StringSplitter(CompositeEntity container, String name)
075                        throws NameDuplicationException, IllegalActionException {
076                super(container, name);
077
078                string = new TypedIOPort(this, "string", true, false);
079                string.setTypeEquals(BaseType.STRING);
080
081                array = new TypedIOPort(this, "array", false, true);
082                array.setTypeEquals(new ArrayType(BaseType.STRING));
083
084                separator = new StringParameter(this, "Regular expression");
085        }
086
087        // /////////////////////////////////////////////////////////////////
088        // // ports and parameters ////
089
090        /**
091         * The input port, which contains the string.
092         */
093        public TypedIOPort string = null;
094
095        /**
096         * The output port, which contains the array.
097         */
098        public TypedIOPort array = null;
099
100        // FIXME: this should be a PortParameter.
101        /**
102         * The parameter, which specifies the regular expression.
103         */
104        public StringParameter separator = null;
105
106        ///////////////////////////////////////////////////////////////////
107        ////               public methods                              ////
108
109        /**
110         * Take the string and split it into an array.
111         * 
112         * @exception IllegalActionException
113         *                If there's no director.
114         */
115        public void fire() throws IllegalActionException {
116                super.fire();
117                _text = string.get(0);
118                _spacer = separator.stringValue();
119                _parts = (((StringToken) _text).stringValue()).split(_spacer);
120                _size = _parts.length;
121                _tokens = new StringToken[_size];
122                for (int i = 0; i < _size; i++) {
123                        _tokens[i] = new StringToken(_parts[i]);
124                }
125                _all = new ArrayToken(_tokens);
126                array.send(0, _all);
127        }
128
129        ///////////////////////////////////////////////////////////////////
130        ////                private members                            ////
131
132        private Token _text;
133        private String _spacer;
134        private String[] _parts;
135        private int _size;
136        private StringToken[] _tokens;
137        private ArrayToken _all;
138}