001/* An actor that outputs data read from a URL. 002 003 @Copyright (c) 2003-2018 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.conversions; 029 030import ptolemy.actor.lib.Transformer; 031import ptolemy.data.StringToken; 032import ptolemy.data.XMLToken; 033import ptolemy.data.type.BaseType; 034import ptolemy.kernel.CompositeEntity; 035import ptolemy.kernel.util.IllegalActionException; 036import ptolemy.kernel.util.NameDuplicationException; 037 038/////////////////////////////////////////////////////////////////// 039//// StringToXML 040 041/** 042 Convert a string token to an xml token. 043 044 @author Yang Zhao 045 @version $Id$ 046 @since Ptolemy II 4.0 047 @Pt.ProposedRating Red (liuj) 048 @Pt.AcceptedRating Red (liuj) 049 */ 050public class StringToXML extends Transformer { 051 //FIXME: The type of the output ports is set to XmlTOken for now. 052 // It should ??? 053 054 /** Construct an actor with the given container and name. 055 * @param container The container. 056 * @param name The name of this actor. 057 * @exception IllegalActionException If the actor cannot be contained 058 * by the proposed container. 059 * @exception NameDuplicationException If the container already has an 060 * actor with this name. 061 */ 062 public StringToXML(CompositeEntity container, String name) 063 throws IllegalActionException, NameDuplicationException { 064 super(container, name); 065 066 // Set the type of the input port. 067 input.setMultiport(true); 068 input.setTypeEquals(BaseType.STRING); 069 070 // Set the type of the output port. 071 output.setMultiport(true); 072 output.setTypeEquals(BaseType.XMLTOKEN); 073 074 // Bert Rodiers suggests, "... treat this like the Minimum Actor. See 075 // http://www.eecs.berkeley.edu/Pubs/TechRpts/2010/EECS-2010-120.pdf for details." 076 output.setDefaultWidth(1); 077 } 078 079 /////////////////////////////////////////////////////////////////// 080 //// public methods //// 081 082 /** Output the XMLToken constructed from the input string. 083 * @exception IllegalActionException if the superclass throws it.. 084 */ 085 @Override 086 public void fire() throws IllegalActionException { 087 super.fire(); 088 //int k = 0; 089 for (int i = 0; i < input.getWidth(); i++) { 090 if (input.hasToken(i)) { 091 StringToken in = (StringToken) input.get(i); 092 093 try { 094 _outToken = new XMLToken(in.stringValue()); 095 output.broadcast(_outToken); 096 097 //k++; 098 } catch (java.lang.Exception ex) { 099 throw new IllegalActionException(this, ex, 100 "Can't construct an XML Token from '" + in + "'"); 101 } 102 } 103 } 104 105 /*//for test purpose, use the following code when fire. 106 String in = "<?xml version='1.0' encoding='UTF-8'?> <Actors>" + 107 "<Actor> <name>Const</name> <class>ptolemy.actor.lib.Const</class>" 108 + " </Actor> </Actors>"; 109 try { 110 //_outToken = new XmlToken[1]; 111 _outToken = new XmlToken(in); 112 113 } 114 catch (Exception e) { 115 e.printStackTrace(); 116 System.out.println("exception is " + e.getClass()); 117 System.out.println("### can't construct an XmlToken from: " 118 + in + "\n"); 119 throw new IllegalActionException(this, e.getMessage()); 120 } 121 output.broadcast(_outToken); */ 122 } 123 124 /** Return true if there is token at the <i>input</i> input. 125 * Otherwise, return false. 126 * @exception IllegalActionException if the superclass throws it. 127 */ 128 @Override 129 public boolean prefire() throws IllegalActionException { 130 for (int i = 0; i < input.getWidth(); i++) { 131 if (input.hasToken(i)) { 132 return true; 133 } 134 } 135 136 return super.prefire(); 137 } 138 139 /////////////////////////////////////////////////////////////////// 140 //// private members //// 141 private XMLToken _outToken; 142}