/* An actor that outputs data read from a URL.
@Copyright (c) 2003-2014 The Regents of the University of California.
All rights reserved.
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the
above copyright notice and the following two paragraphs appear in all
copies of this software.
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS.
PT_COPYRIGHT_VERSION 2
COPYRIGHTENDKEY
*/
package ptolemy.actor.lib.conversions;
import ptolemy.actor.lib.Transformer;
import ptolemy.data.StringToken;
import ptolemy.data.XMLToken;
import ptolemy.data.type.BaseType;
import ptolemy.kernel.CompositeEntity;
import ptolemy.kernel.util.IllegalActionException;
import ptolemy.kernel.util.NameDuplicationException;
///////////////////////////////////////////////////////////////////
//// StringToXML
/**
Convert a string token to an xml token.
@author Yang Zhao
@version $Id: StringToXML.java 70398 2014-10-22 23:44:32Z cxh $
@since Ptolemy II 4.0
@Pt.ProposedRating Red (liuj)
@Pt.AcceptedRating Red (liuj)
*/
public class StringToXML extends Transformer {
//FIXME: The type of the output ports is set to XmlTOken for now.
// It should ???
/** Construct an actor with the given container and name.
* @param container The container.
* @param name The name of this actor.
* @exception IllegalActionException If the actor cannot be contained
* by the proposed container.
* @exception NameDuplicationException If the container already has an
* actor with this name.
*/
public StringToXML(CompositeEntity container, String name)
throws IllegalActionException, NameDuplicationException {
super(container, name);
// Set the type of the input port.
input.setMultiport(true);
input.setTypeEquals(BaseType.STRING);
// Set the type of the output port.
output.setMultiport(true);
output.setTypeEquals(BaseType.XMLTOKEN);
// Bert Rodiers suggests, "... treat this like the Minimum Actor. See
// http://www.eecs.berkeley.edu/Pubs/TechRpts/2010/EECS-2010-120.pdf for details."
output.setDefaultWidth(1);
}
///////////////////////////////////////////////////////////////////
//// public methods ////
/** Output the XMLToken constructed from the input string.
* @exception IllegalActionException if the superclass throws it..
*/
@Override
public void fire() throws IllegalActionException {
super.fire();
//int k = 0;
for (int i = 0; i < input.getWidth(); i++) {
if (input.hasToken(i)) {
StringToken in = (StringToken) input.get(i);
try {
_outToken = new XMLToken(in.stringValue());
output.broadcast(_outToken);
//k++;
} catch (java.lang.Exception ex) {
throw new IllegalActionException(this, ex,
"Can't construct an XML Token from '" + in + "'");
}
}
}
/*//for test purpose, use the following code when fire.
String in = " " +
" Const ptolemy.actor.lib.Const"
+ " ";
try {
//_outToken = new XmlToken[1];
_outToken = new XmlToken(in);
}
catch (Exception e) {
e.printStackTrace();
System.out.println("exception is " + e.getClass());
System.out.println("### can't construct an XmlToken from: "
+ in + "\n");
throw new IllegalActionException(this, e.getMessage());
}
output.broadcast(_outToken); */
}
/** Return true if there is token at the input input.
* Otherwise, return false.
* @exception IllegalActionException if the superclass throws it.
*/
@Override
public boolean prefire() throws IllegalActionException {
for (int i = 0; i < input.getWidth(); i++) {
if (input.hasToken(i)) {
return true;
}
}
return super.prefire();
}
///////////////////////////////////////////////////////////////////
//// private members ////
private XMLToken _outToken;
}