001/*
002 * Copyright (c) 2007-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
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.sdm.spa;
031
032import org.w3c.dom.Document;
033
034import ptolemy.actor.IOPort;
035import ptolemy.actor.TypedAtomicActor;
036import ptolemy.data.BooleanToken;
037import ptolemy.data.Token;
038import ptolemy.data.XMLToken;
039import ptolemy.data.expr.Parameter;
040import ptolemy.data.type.BaseType;
041import ptolemy.kernel.CompositeEntity;
042import ptolemy.kernel.util.Attribute;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.NameDuplicationException;
045
046//////////////////////////////////////////////////////////////////////////
047//// XMLDisassembler
048/**
049 * This actor disassembles an XML document into its child elements. The input
050 * port name must match the document's root element name and is peeled off. Each
051 * child element is sent to the output port with the same name.
052 * 
053 * @author Daniel Crawl
054 * @version $Id: XMLDisassembler.java 24234 2010-05-06 05:21:26Z welker $
055 */
056
057public class XMLDisassembler extends TypedAtomicActor {
058
059        /**
060         * Construct a XMLDisassembler source with the given container and name.
061         * 
062         * @param name
063         *            The name of this actor.
064         * @exception IllegalActionException
065         *                If the entity cannot be contained by the proposed
066         *                container.
067         * @exception NameDuplicationException
068         *                If the container already has an actor with this name.
069         */
070        public XMLDisassembler(CompositeEntity container, String name)
071                        throws NameDuplicationException, IllegalActionException {
072                super(container, name);
073
074                outputNil = new Parameter(this, "outputNil");
075                outputNil.setTypeEquals(BaseType.BOOLEAN);
076                outputNil.setExpression("false");
077
078                arraysWrapped = new Parameter(this, "arraysWrapped");
079                arraysWrapped.setTypeEquals(BaseType.BOOLEAN);
080                arraysWrapped.setExpression("false");
081
082                _helper = new XMLHelper(this);
083
084                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
085                                + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n"
086                                + "</svg>\n");
087        }
088
089        // /////////////////////////////////////////////////////////////////
090        // // ports and parameters ////
091
092        /**
093         * If true, then each output port whose name is not a child element of the
094         * incoming XML document outputs a nil token.
095         */
096        public Parameter outputNil = null;
097
098        /**
099         * If true, then each element of an array is wrapped in an additional
100         * element.
101         */
102        public Parameter arraysWrapped = null;
103
104        // /////////////////////////////////////////////////////////////////
105        // // public methods ////
106
107        /**
108         * React to a change in an attribute. Update the value if one of the
109         * parameters changed.
110         * 
111         * @param attribute
112         *            The changed parameter.
113         * @exception IllegalActionException
114         *                If the parameter set is not valid.
115         */
116        public void attributeChanged(Attribute attribute)
117                        throws IllegalActionException {
118                if (attribute == outputNil) {
119                        Token token = outputNil.getToken();
120                        _helper.setOutputNil(((BooleanToken) token).booleanValue());
121                } else if (attribute == arraysWrapped) {
122                        Token token = arraysWrapped.getToken();
123                        _helper.setArraysWrapped(((BooleanToken) token).booleanValue());
124                }
125                super.attributeChanged(attribute);
126        }
127
128        /**
129         * Read the XML input and send pieces to the correct output ports.
130         * 
131         * @exception IllegalActionException
132         *                If it is thrown by the send() method sending out the
133         *                token.
134         */
135        public void fire() throws IllegalActionException {
136                super.fire();
137
138                Object[] inPortArray = inputPortList().toArray();
139                for (int i = 0; i < inPortArray.length; i++) {
140                        IOPort port = (IOPort) inPortArray[i];
141                        Document doc = ((XMLToken) port.get(0)).getDomTree();
142                        _helper.splitOutXML(port.getName(), doc, outputPortList(), null);
143                }
144        }
145
146        // /////////////////////////////////////////////////////////////////
147        // // private members ////
148
149        private XMLHelper _helper = null;
150}