001/* Interface for objects that can export themselves in MoML.
002
003 Copyright (c) 2003-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.kernel.util;
029
030import java.io.IOException;
031import java.io.Writer;
032
033//////////////////////////////////////////////////////////////////////////
034//// MoMLExportable
035
036/**
037 This is an interface for objects that have persistent MoML representations.
038 MoML is an XML schema used to represent Ptolemy II models.  The form of
039 the MoML generated by an implementor of this interface should be:
040 <pre>
041 &lt;<i>elementName</i> name="<i>name</i>" class="<i>className</i>" source="<i>source</i>"&gt;
042 <i>body, determined by the implementor</i>
043 &lt;/<i>elementName</i>&gt;
044 </pre>
045 or
046 <pre>
047 &lt;class name="<i>name</i>" extends="<i>className</i>" source="<i>source</i>"&gt;
048 <i>body, determined by the implementor</i>
049 &lt;/class&gt;
050 </pre>
051
052 The <i>elementName</i> is the string returned by getElementName().
053 The <i>name</i> is the string returned by getName().
054 The <i>className</i> is the string returned by getClassName().
055 The <i>source</i> is the string returned by getSource().
056 If either of the last two methods returns null, then that
057 XML attribute is omitted from the description.
058 <p>
059 If this object has no container (and for the exportMoML() methods
060 that take a depth argument, if that argument is zero),
061 then the exportMoML() methods prepend XML file header information,
062 which is:
063 <pre>
064 &lt;?xml version="1.0" standalone="no"?&gt;
065 &lt;!DOCTYPE entity PUBLIC "-//UC Berkeley//DTD MoML 1//EN"
066 "http://ptolemy.eecs.berkeley.edu/xml/dtd/MoML_1.dtd"&gt;
067 </pre>
068 In the above, "entity" may be replaced by "class", "property",
069 or "port".
070
071 @author Edward A. Lee
072 @version $Id$
073 @since Ptolemy II 4.0
074 @Pt.ProposedRating Green (eal)
075 @Pt.AcceptedRating Green (neuendor)
076 @see NamedObj
077 */
078public interface MoMLExportable extends Nameable {
079    /** Return a MoML description of this object.  This might be an empty string
080     *  if there is no MoML description of this object or if this object is
081     *  not persistent or if an implementor has some other reason that the
082     *  object has no persistent description.
083     *  @return A MoML description, or an empty string if there is none.
084     *  @see #isPersistent()
085     */
086    public String exportMoML();
087
088    /** Return a MoML description of this object with its name replaced by
089     *  the specified name.  The description might be an empty string
090     *  if there is no MoML description of this object or if this object
091     *  is not persistent, or if an implementor has some other reason that the
092     *  object has no persistent description.
093     *  @param name The name that is used as a replacement.
094     *  @return A MoML description, or the empty string if there is none.
095     *  @see #isPersistent()
096     */
097    public String exportMoML(String name);
098
099    /** Write a MoML description of this object using the specified
100     *  Writer.  If there is no MoML description, or if the object
101     *  is not persistent, or if an implementor has some other
102     *  reason that the object has no persistent description,
103     *  then nothing is written. To write to standard out, do
104     *  <pre>
105     *      exportMoML(new OutputStreamWriter(System.out))
106     *  </pre>
107     *  @param output The writer to write to.
108     *  @exception IOException If an I/O error occurs.
109     *  @see #isPersistent()
110     */
111    public void exportMoML(Writer output) throws IOException;
112
113    /** Write a MoML description of this entity with the specified
114     *  depth in a hierarchy. The depth in the hierarchy determines
115     *  indenting of the output, but may also be used to indicate
116     *  to an implementor whether this object is being exported
117     *  as part of the export of its container (in which case
118     *  depth &gt; 0), or this object is being exported independently
119     *  of its container (depth == 0). If there is no MoML description,
120     *  or if the object is not persistent, or if an implementor
121     *  has some other reason that the object has no persistent
122     *  description, then nothing is written.
123     *  @param output The output writer to write to.
124     *  @param depth The depth in the hierarchy.
125     *  @exception IOException If an I/O error occurs.
126     *  @see #isPersistent()
127     */
128    public void exportMoML(Writer output, int depth) throws IOException;
129
130    /** Write a MoML description of this entity with the specified
131     *  depth in a hierarchy and with the specified name substituting
132     *  for the name of this object. The depth in the hierarchy determines
133     *  indenting of the output, but may also be used to indicate
134     *  to an implementor whether this object is being exported
135     *  as part of the export of its container (in which case
136     *  depth &gt; 0), or this object is being exported independently
137     *  of its container (depth == 0). If there is no MoML description,
138     *  or if the object is not persistent, or if an implementor
139     *  has some other reason that the object has no persistent
140     *  description, then nothing is written.
141     *  @param output The output writer to write to.
142     *  @param depth The depth in the hierarchy, to determine indenting.
143     *  @param name The name to use in the exported MoML.
144     *  @exception IOException If an I/O error occurs.
145     *  @see #isPersistent()
146     */
147    public void exportMoML(Writer output, int depth, String name)
148            throws IOException;
149
150    /** Return the class name.  This is either the name of the
151     *  class of which this object is an instance, or if this
152     *  object is itself a class, then the class that it extends.
153     *  The class name could be a fully qualified Java class name
154     *  or the name of the parent of this object if it has a parent.
155     *  @return The MoML class name.
156     *  @see Instantiable#getParent()
157     */
158    public String getClassName();
159
160    /** Get the XML element name.  Implementors should provide the
161     *  appropriate XML element name to use when exporting MoML.
162     *  @return The XML element name for this object.
163     */
164    public String getElementName();
165
166    /** Get the source, which gives an external URL
167     *  associated with an entity (presumably from which the entity
168     *  was defined).  This becomes the value in the "source"
169     *  attribute of exported MoML.
170     *  @return The source, or null if there is none.
171     *  @see #setSource(String)
172     */
173    public String getSource();
174
175    /** Return true if this object is persistent.
176     *  A persistent object has a MoML description that can be stored
177     *  in a file and used to re-create the object. A non-persistent
178     *  object has an empty MoML description.
179     *  @return True if the object is persistent.
180     *  @see #setPersistent(boolean)
181     */
182    public boolean isPersistent();
183
184    /** Set the persistence of this object. If the persistence is not
185     *  specified with this method, then by default the object will be
186     *  persistent unless it is derivable by derivation from a class.
187     *  A persistent object has a non-empty MoML description that can be used
188     *  to re-create the object. To make an instance non-persistent,
189     *  call this method with the argument <i>false</i>. To force
190     *  it to always be persistent, irrespective of its relationship
191     *  to a class, then call this with argument <i>true</i>. Note
192     *  that this will have the additional effect that it no longer
193     *  inherits properties from the class, so in effect, calling
194     *  this with <i>true</i> overrides values given by the class.
195     *  @param isPersistent False to make this object non-persistent.
196     *  @see #isPersistent()
197     */
198    public void setPersistent(boolean isPersistent);
199
200    /** Set the source, which gives an external URL
201     *  associated with an entity (presumably from which the entity
202     *  was defined).  This becomes the value in the "source"
203     *  attribute of exported MoML. Call this with null to prevent
204     *  any source attribute from being generated.
205     *  @param source The source, or null if there is none.
206     *  @see #getSource()
207     */
208    public void setSource(String source);
209}