001/*
002 * Copyright (c) 2009-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2014-07-14 22:26:04 +0000 (Mon, 14 Jul 2014) $' 
007 * '$Revision: 32837 $'
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.kepler.date;
031
032import java.text.SimpleDateFormat;
033import java.util.Date;
034
035import ptolemy.actor.lib.Transformer;
036import ptolemy.actor.parameters.PortParameter;
037import ptolemy.data.DateToken;
038import ptolemy.data.StringToken;
039import ptolemy.data.type.BaseType;
040import ptolemy.kernel.CompositeEntity;
041import ptolemy.kernel.util.Attribute;
042import ptolemy.kernel.util.IllegalActionException;
043import ptolemy.kernel.util.NameDuplicationException;
044
045/**
046 * Convert a date token to a string using a specific format. See
047 * java.text.SimpleDateFormat for formatting syntax.
048 * 
049 * @author Daniel Crawl
050 * @version $Id: DateToString.java 32837 2014-07-14 22:26:04Z crawl $
051 */
052
053public class DateToString extends Transformer {
054
055        /**
056         * Construct a DateToString with the given container and name.
057         * 
058         * @param name
059         *            The name of this actor.
060         * @exception IllegalActionException
061         *                If the entity cannot be contained by the proposed
062         *                container.
063         * @exception NameDuplicationException
064         *                If the container already has an actor with this name.
065         */
066        public DateToString(CompositeEntity container, String name)
067                        throws NameDuplicationException, IllegalActionException {
068                super(container, name);
069
070                input.setTypeEquals(BaseType.DATE);
071                new Attribute(input, "_showName");
072
073                outputFormat = new PortParameter(this, "format");
074                outputFormat.setStringMode(true);
075                outputFormat.getPort().setTypeEquals(BaseType.STRING);
076                new Attribute(outputFormat.getPort(), "_showName");
077                for (int i = 0; i < CreateDate.dateFormats.length; i++) {
078                        outputFormat.addChoice(CreateDate.dateFormats[i]);
079                }
080
081                output.setTypeEquals(BaseType.STRING);
082                output.setMultiport(true);
083        output.setDefaultWidth(1);
084
085                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
086                                + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n"
087                                + "</svg>\n");
088        }
089
090        // /////////////////////////////////////////////////////////////////
091        // // ports and parameters ////
092
093        /**
094         * The format of the string. See java.text.SimpleDateFormat for formatting
095         * syntax.
096         */
097        public PortParameter outputFormat;
098
099        // /////////////////////////////////////////////////////////////////
100        // // public methods ////
101
102        /**
103         * Convert the input date to a formatted output string.
104         * 
105         * @exception IllegalActionException
106         *                If it is thrown by the send() method sending out the
107         *                token.
108         */
109        @Override
110    public void fire() throws IllegalActionException {
111                super.fire();
112
113                // read the input
114                Date date = new Date(((DateToken) input.get(0)).getValue());
115
116                // read the format
117                outputFormat.update();
118                String formatStr = ((StringToken) outputFormat.getToken())
119                                .stringValue();
120
121                
122                String outStr;
123                
124                if (formatStr.equals(CreateDate.MS_SINCE_EPOCH)) {
125                outStr = String.valueOf(date.getTime());
126                } else if (formatStr.equals(CreateDate.S_SINCE_EPOCH)) {
127                    outStr = String.valueOf(date.getTime() / 1000);
128                } else {
129                    SimpleDateFormat sdf;
130
131                    if (formatStr.equals("")) {
132                        sdf = new SimpleDateFormat();
133                    } else {
134                        sdf = new SimpleDateFormat(formatStr);
135                    }
136                    outStr = sdf.format(date);
137                }
138
139                output.broadcast(new StringToken(outStr));
140        }
141}