001/* Replace an instance of a string with another input string according
002 to simple matching (no regex)
003
004 Copyright (c) 2009-2014 The Regents of the University of California.
005 All rights reserved.
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 above
009 copyright notice and the following two paragraphs appear in all copies
010 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
028 */
029package ptolemy.actor.lib.string;
030
031import ptolemy.actor.TypedAtomicActor;
032import ptolemy.actor.TypedIOPort;
033import ptolemy.actor.parameters.PortParameter;
034import ptolemy.data.BooleanToken;
035import ptolemy.data.StringToken;
036import ptolemy.data.expr.SingletonParameter;
037import ptolemy.data.type.BaseType;
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.Attribute;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NameDuplicationException;
042
043///////////////////////////////////////////////////////////////////
044//// StringSimpleReplace
045
046/**
047 On each firing, look for instances of the pattern specified by <i>pattern</i>
048 in <i>stringToEdit</i> and replace them with the string given by
049 <i>replacement</i>.  If <i>replaceAll</i> is true, then replace
050 all instances that match <i>pattern</i>.  Otherwise, replace only
051 the first instance that matches.  If there is no match, then the
052 output is the string provided by <i>stringToEdit</i>, unchanged.
053 The <i>pattern</i> is <b>not</b> a regular expression, to use
054 a regular expression, see {@link ptolemy.actor.lib.string.StringReplace}.
055 <p>
056 The <i>replacement</i> string, as usual with string-valued parameters
057 in Ptolemy II, can include references to parameter values in scope.
058 E.g., if the enclosing composite actor has a parameter named "x"
059 with value 1, say, then the replacement string a${x}b will become
060 "a1b".</p>
061 <p>Note that if the <i>pattern</i> is the two character string
062 <code>\r</code>, then that pattern is handled specially and
063 collapsed to the single character '\r'.  This is for use in removing
064 \r's from test output.
065
066 @author Christopher Brooks
067 @deprecated This class is primarily used in models that will be code generated so that regular expressions are not needed in the output.  In general, use StringReplace with the regularExpression parameter set to false.
068 @version $Id$
069 @since Ptolemy II 8.0
070 @Pt.ProposedRating ret (cxh)
071 @Pt.AcceptedRating red (cxh)
072 */
073@Deprecated
074public class StringSimpleReplace extends TypedAtomicActor {
075    /** Construct an actor with the given container and name.
076     *  @param container The container.
077     *  @param name The name of this actor.
078     *  @exception IllegalActionException If the actor cannot be contained
079     *   by the proposed container.
080     *  @exception NameDuplicationException If the container already has an
081     *   actor with this name.
082     */
083    public StringSimpleReplace(CompositeEntity container, String name)
084            throws NameDuplicationException, IllegalActionException {
085        super(container, name);
086
087        // Create new parameters and ports.
088        // Set default values of the parameters and type constraints.
089        pattern = new PortParameter(this, "pattern");
090        pattern.setStringMode(true);
091        pattern.setExpression("");
092        new SingletonParameter(pattern.getPort(), "_showName")
093                .setToken(BooleanToken.TRUE);
094
095        replacement = new PortParameter(this, "replacement");
096        replacement.setStringMode(true);
097        replacement.setExpression("");
098        new SingletonParameter(replacement.getPort(), "_showName")
099                .setToken(BooleanToken.TRUE);
100
101        stringToEdit = new PortParameter(this, "stringToEdit");
102        stringToEdit.setStringMode(true);
103        stringToEdit.setExpression("");
104        new SingletonParameter(stringToEdit.getPort(), "_showName")
105                .setToken(BooleanToken.TRUE);
106
107        output = new TypedIOPort(this, "output", false, true);
108        output.setTypeEquals(BaseType.STRING);
109    }
110
111    /** The string to edit by replacing substrings that match the
112     *  specified pattern with the specified replacement. This is
113     *  a string that defaults to the empty string.
114     */
115    public PortParameter stringToEdit;
116
117    /** The output port on which the edited string is produced.
118     *  This has type string.
119     */
120    public TypedIOPort output;
121
122    /** The pattern used to pattern match and replace the stringToEdit
123     *  string. It is an empty string by default.
124     */
125    public PortParameter pattern;
126
127    /** The replacement string that replaces any matched instance of the
128     *  pattern. It is an empty string by default.
129     */
130    public PortParameter replacement;
131
132    ///////////////////////////////////////////////////////////////////
133    ////                         public methods                    ////
134
135    /** Override the base class to compile a regular expression when
136     *  it is changed.
137     *  @param attribute The attribute that changed.
138     *  @exception IllegalActionException If the specified attribute
139     *   is <i>pattern</i> and the regular expression fails to
140     *   compile.
141     */
142    @Override
143    public void attributeChanged(Attribute attribute)
144            throws IllegalActionException {
145        if (attribute == pattern) {
146            // We don't call super here because we don't
147            // want to compile the pattern
148            _patternValue = ((StringToken) pattern.getToken()).stringValue();
149            if (_patternValue.equals("\\r")) {
150                _patternValue = "\r";
151            }
152        } else {
153            super.attributeChanged(attribute);
154        }
155    }
156
157    /** Perform pattern matching and substring replacement, and output
158     *  the modified string. If no match is found, output the
159     *  unmodified stringToEdit string.
160     *  @exception IllegalActionException If there is no director.
161     */
162    @Override
163    public void fire() throws IllegalActionException {
164        super.fire();
165        replacement.update();
166        stringToEdit.update();
167        pattern.update();
168
169        String replacementValue = ((StringToken) replacement.getToken())
170                .stringValue();
171        String stringToEditValue = ((StringToken) stringToEdit.getToken())
172                .stringValue();
173
174        String outputString;
175
176        outputString = stringToEditValue.replace(_patternValue,
177                replacementValue);
178
179        output.send(0, new StringToken(outputString));
180    }
181
182    ///////////////////////////////////////////////////////////////////
183    ////                         private variables                 ////
184
185    // The replacement string.
186    private String _patternValue;
187}