001/* An actor that outputs strings read from a text file or URL. 002 003 @Copyright (c) 2002-2014 The Regents of the University of California. 004 All rights reserved. 005 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the 009 above copyright notice and the following two paragraphs appear in all 010 copies of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION 2 026 COPYRIGHTENDKEY 027 */ 028package ptolemy.actor.lib.string; 029 030import ptolemy.actor.lib.Transformer; 031import ptolemy.data.ArrayToken; 032import ptolemy.data.StringToken; 033import ptolemy.data.Token; 034import ptolemy.data.expr.Parameter; 035import ptolemy.data.expr.StringParameter; 036import ptolemy.data.type.ArrayType; 037import ptolemy.data.type.BaseType; 038import ptolemy.kernel.CompositeEntity; 039import ptolemy.kernel.util.IllegalActionException; 040import ptolemy.kernel.util.NameDuplicationException; 041 042/////////////////////////////////////////////////////////////////// 043//// StringSplit 044 045/** 046 This actor reads an input string and splits it into an array of 047 strings. The <i>separator</i> parameter is a regular expression 048 that determines where the split should occur. The default behavior 049 will split the input at newline characters. 050 051 @author Edward A. Lee 052 @version $Id$ 053 @since Ptolemy II 10.0 054 @Pt.ProposedRating Yellow (eal) 055 @Pt.AcceptedRating Red (cxh) 056 */ 057public class StringSplit extends Transformer { 058 059 /** Construct an actor with the given container and name. 060 * @param container The container. 061 * @param name The name of this actor. 062 * @exception IllegalActionException If the actor cannot be contained 063 * by the proposed container. 064 * @exception NameDuplicationException If the container already has an 065 * actor with this name. 066 */ 067 public StringSplit(CompositeEntity container, String name) 068 throws IllegalActionException, NameDuplicationException { 069 super(container, name); 070 071 separator = new StringParameter(this, "separator"); 072 separator.setExpression("\n"); 073 074 trimSpaces = new Parameter(this, "trimSpaces"); 075 trimSpaces.setTypeEquals(BaseType.BOOLEAN); 076 trimSpaces.setExpression("true"); 077 078 input.setTypeEquals(BaseType.STRING); 079 080 output.setTypeEquals(new ArrayType(BaseType.STRING)); 081 } 082 083 /////////////////////////////////////////////////////////////////// 084 //// ports and parameters //// 085 086 /** A specification of the separator used to split the string. 087 * The default is "\n", which results in splitting the string 088 * at newline characters. 089 */ 090 public StringParameter separator; 091 092 /** If true, then trim spaces around each resulting string. 093 * This is a boolean that defaults to true. 094 */ 095 public Parameter trimSpaces; 096 097 /////////////////////////////////////////////////////////////////// 098 //// public methods //// 099 100 /** Split the input string and send to the output. 101 * If there is no input, do nothing. 102 * @exception IllegalActionException If there's no director. 103 */ 104 @Override 105 public void fire() throws IllegalActionException { 106 super.fire(); 107 108 if (input.hasToken(0)) { 109 String inputValue = ((StringToken) input.get(0)).stringValue(); 110 String[] result = inputValue.split(separator.stringValue()); 111 Token[] resultTokens = new Token[result.length]; 112 for (int i = 0; i < result.length; i++) { 113 resultTokens[i] = new StringToken(result[i]); 114 } 115 output.broadcast(new ArrayToken(resultTokens)); 116 } 117 } 118}