001/* An attribute that has an arbitrary MoML description, externally given.
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.moml;
029
030import java.io.IOException;
031import java.io.Writer;
032import java.util.Iterator;
033import java.util.LinkedList;
034import java.util.List;
035import java.util.StringTokenizer;
036
037import ptolemy.kernel.util.Attribute;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.kernel.util.NamedObj;
041import ptolemy.kernel.util.Workspace;
042
043///////////////////////////////////////////////////////////////////
044//// MoMLAttribute
045
046/**
047 This attribute has an arbitrary MoML description that is exported
048 when the exportMoML() methods of the container are called.  Thus,
049 it serves as a convenient way to attach persistent information
050 that will not otherwise be exported to MoML.  To specify its
051 MoML description, call setMoMLDescription().
052
053 @author  Edward A. Lee
054 @version $Id$
055 @since Ptolemy II 1.0
056 @Pt.ProposedRating Red (eal)
057 @Pt.AcceptedRating Red (reviewmoderator)
058 */
059public class MoMLAttribute extends Attribute {
060    /** Construct an attribute in the specified workspace with an empty
061     *  string as a name. You can then change the name with setName().
062     *  If the workspace argument
063     *  is null, then use the default workspace.
064     *  The object is added to the directory of the workspace.
065     *  Increment the version number of the workspace.
066     *  @param workspace The workspace that will list the attribute.
067     */
068    public MoMLAttribute(Workspace workspace) {
069        super(workspace);
070    }
071
072    /** Construct an attribute with the specified container and name.
073     *  @param container The container.
074     *  @param name The name of this attribute.
075     *  @exception IllegalActionException If the attribute is not of an
076     *   acceptable class for the container, or if the name contains a period.
077     *  @exception NameDuplicationException If the name coincides with
078     *   an attribute already in the container.
079     */
080    public MoMLAttribute(NamedObj container, String name)
081            throws IllegalActionException, NameDuplicationException {
082        super(container, name);
083    }
084
085    ///////////////////////////////////////////////////////////////////
086    ////                         public methods                    ////
087
088    /** Append to the MoML description of this object.
089     *  @param moml The MoML description of this object.
090     */
091    public void appendMoMLDescription(String moml) {
092        StringTokenizer tokenizer = new StringTokenizer(moml, "\n");
093
094        while (tokenizer.hasMoreTokens()) {
095            _momlDescription.add(tokenizer.nextToken());
096        }
097    }
098
099    /** Write the MoML description of this object, which consists of
100     *  whatever has been specified using the appendMoMLDescription() method.
101     *  If that method has not been called, then nothing is written.
102     *  The written MoML is indented to the specified depth and terminated
103     *  with a newline.
104     *  @param output The output stream to write to.
105     *  @param depth The depth in the hierarchy, to determine indenting.
106     *  @exception IOException If there is a problem writing the MoML.
107     */
108    public void writeMoMLDescription(Writer output, int depth)
109            throws IOException {
110        if (_momlDescription.size() > 0) {
111            Iterator strings = _momlDescription.iterator();
112
113            while (strings.hasNext()) {
114                String string = (String) strings.next();
115                output.write(_getIndentPrefix(depth) + string + "\n");
116            }
117        }
118    }
119
120    /** Write a MoML description of this object, which in this case is
121     *  whatever has been specified by the setMoMLDescription() method.
122     *  If that method has not been called, then nothing is written.
123     *  The written MoML is indented to the specified depth and terminated
124     *  with a newline. If this object is not persistent, then nothing
125     *  is written.
126     *  @param name The name to use instead of the name of this object.
127     *   This argument is ignored.
128     *  @param output The output stream to write to.
129     *  @param depth The depth in the hierarchy, to determine indenting.
130     *  @see #isPersistent()
131     *  @exception IOException If there is a problem writing the MoML.
132     */
133    @Override
134    public void exportMoML(Writer output, int depth, String name)
135            throws IOException {
136        if (_isMoMLSuppressed(depth)) {
137            return;
138        }
139
140        writeMoMLDescription(output, depth);
141    }
142
143    ///////////////////////////////////////////////////////////////////
144    ////                         private variables                 ////
145    // The MoML description as a list of strings.
146    private List _momlDescription = new LinkedList();
147}