001/* A filter for  MoML (modeling markup language)
002
003 Copyright (c) 2002-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 ptolemy.kernel.util.NamedObj;
031
032//////////////////////////////////////////////////////////////////////////
033//// MoMLFilter
034
035/**
036 This class filters MoML (modeling markup language) identifiers.
037 It can be used to
038 <menu>
039 <li> Remove graphical classes for use in a non-graphical environment
040 <li> Change the names of actors and ports for backward compatibility.
041 </menu>
042 @author Christopher Hylands, Edward A. Lee
043 @version $Id$
044 @since Ptolemy II 2.0
045 @Pt.ProposedRating Red (cxh)
046 @Pt.AcceptedRating Red (cxh)
047 */
048public interface MoMLFilter {
049    /** Given a container, attribute name and attribute value,
050     *  return a new attribute value.  Note that "attribute"
051     *  means XML attribute, not Ptolemy II attribute. Also,
052     *  the container is the context of the current of XML
053     *  element.  So, for example, if you have:
054     *  <pre>
055     *    &lt;entity name="foo" class="..."&gt;
056     *       &lt;property name="x" value="10"/&gt;
057     *    &lt;/entity&gt;
058     *  </pre>
059     *  then this method will be called twice with the container
060     *  being the instance "foo". On the first call, the
061     *  <i>attributeName</i> will be "name" and the
062     *  <i>attributeValue</i> will be "x".  On the second call,
063     *  <i>attributeName</i> will be "value" and the
064     *  <i>attributeValue</i> will be "10".
065     *  To make no change to the attribute value, an implementer
066     *  should simply return the same attributeValue.
067     *  To cause the MoMLParser to ignore the current element
068     *  altogether, an implementer should return null. For
069     *  example, to skip a graphical class, create a filter that
070     *  looks for <i>attributeName</i> equal to "class" and
071     *  <i>attributeValue</i> equal to the class name to skip.
072     *  Note that if the <i>attributeValue</i> argument is null,
073     *  then returning null is interpreted as no change, rather than
074     *  as an indication to skip the element.
075     *  To change the value of the attribute, simply return a
076     *  a new value for the attribute.
077     *  <p>
078     *  If modifies the attribute value, then it should call
079     *  the static method MoMLParser.setModified(true), which indicates
080     *  that the model was modified so that the user can optionally
081     *  save the modified model.
082     *
083     *  @param container  The container for XML element.
084     *  @param element The XML element name.
085     *  @param attributeName The name of the attribute.
086     *  @param attributeValue The value of the attribute.
087     *  @param xmlFile The file currently being parsed.
088     *  @return A new value for the attribute, or the same value
089     *   to leave it unchanged, or null to cause the current element
090     *   to be ignored (unless the attributeValue argument is null).
091     */
092    public String filterAttributeValue(NamedObj container, String element,
093            String attributeName, String attributeValue, String xmlFile);
094
095    /** Given a container, attribute name and attribute value,
096     *  return a new attribute value.  Note that "attribute"
097     *  means XML attribute, not Ptolemy II attribute. Also,
098     *  the container is the context of the current of XML
099     *  element.  So, for example, if you have:
100     *  <pre>
101     *    &lt;entity name="foo" class="..."&gt;
102     *       &lt;property name="x" value="10"/&gt;
103     *    &lt;/entity&gt;
104     *  </pre>
105     *  then this method will be called twice with the container
106     *  being the instance "foo". On the first call, the
107     *  <i>attributeName</i> will be "name" and the
108     *  <i>attributeValue</i> will be "x".  On the second call,
109     *  <i>attributeName</i> will be "value" and the
110     *  <i>attributeValue</i> will be "10".
111     *  To make no change to the attribute value, an implementer
112     *  should simply return the same attributeValue.
113     *  To cause the MoMLParser to ignore the current element
114     *  altogether, an implementer should return null. For
115     *  example, to skip a graphical class, create a filter that
116     *  looks for <i>attributeName</i> equal to "class" and
117     *  <i>attributeValue</i> equal to the class name to skip.
118     *  Note that if the <i>attributeValue</i> argument is null,
119     *  then returning null is interpreted as no change, rather than
120     *  as an indication to skip the element.
121     *  To change the value of the attribute, simply return a
122     *  a new value for the attribute.
123     *  <p>
124     *  If modifies the attribute value, then it should call
125     *  the static method MoMLParser.setModified(true), which indicates
126     *  that the model was modified so that the user can optionally
127     *  save the modified model.
128     *
129     *  <p>This method takes a MoMLParser argument, which is optionally
130     *  used to parse MoML.  We have to parse the MoML as opposed to
131     *  calling the Java classes directly so that ptolemy.moml.filter
132     *  does not depend on other packages, such as ptolemy.vergil.  Derived
133     *  classes usually call parser.setContext(container) so that the
134     *  MoML is parsed in the correct context.  Note that it is probably
135     *  not correct to call this method and pass in the current MoMLParser
136     *  because setContext() calls reset().  Instead, the caller (MoMLParser),
137     *  creates a MoMLParser instance that is shared amongst all the calls
138     *  to the MoMLFilter methods that take a MoMLParser argument.</p>
139     *
140     *  @param container  The container for XML element.
141     *  @param element The XML element name.
142     *  @param attributeName The name of the attribute.
143     *  @param attributeValue The value of the attribute.
144     *  @param xmlFile The file currently being parsed.
145     *  @param parser The MoMLParser that is sometimes used by derived
146     *  classes.  MoMLParser shares a separate MoMLParser between
147     *  the filters.  Each filter should call setContext(container) on
148     *  the passed-in filter.  Since setContext() calls reset(), we
149     *  don't want to pass in the main MoMLParser.  We share one
150     *  MoMLParser so as to avoid the expense of constructing one each
151     *  time we read an attribute.
152     *  @return A new value for the attribute, or the same value
153     *   to leave it unchanged, or null to cause the current element
154     *   to be ignored (unless the attributeValue argument is null).
155     */
156    public String filterAttributeValue(NamedObj container, String element,
157            String attributeName, String attributeValue, String xmlFile,
158            MoMLParser parser);
159
160    /** Make modifications to the specified container, which is
161     *  defined in a MoML element with the specified name.
162     *  This method is called when an end element in MoML is
163     *  encountered. A typical use of this method is to make
164     *  some modification to the object (the container) that
165     *  was constructed.
166     *  <p>
167     *  If an implementor makes changes to the specified container,
168     *  then it should call MoMLParser.setModified(true) which indicates
169     *  that the model was modified so that the user can optionally
170     *  save the modified model.
171     *
172     *  @param container The object defined by the element that this
173     *   is the end of.
174     *  @param elementName The element name.
175     *  @param currentCharData The character data, which appears
176     *   only in the doc and configure elements
177     *  @param xmlFile The file currently being parsed.
178     *  @exception Exception If there is a problem modifying the
179     *  specified container.
180     */
181    public void filterEndElement(NamedObj container, String elementName,
182            StringBuffer currentCharData, String xmlFile) throws Exception;
183
184    /** Make modifications to the specified container, which is
185     *  defined in a MoML element with the specified name.
186     *  This method is called when an end element in MoML is
187     *  encountered. A typical use of this method is to make
188     *  some modification to the object (the container) that
189     *  was constructed.
190     *  <p>
191     *  If an implementor makes changes to the specified container,
192     *  then it should call MoMLParser.setModified(true) which indicates
193     *  that the model was modified so that the user can optionally
194     *  save the modified model.
195     *
196     *  <p>This method takes a MoMLParser argument, which is optionally
197     *  used to parse MoML.  We have to parse the MoML as opposed to
198     *  calling the Java classes directly so that ptolemy.moml.filter
199     *  does not depend on other packages, such as ptolemy.vergil.  Derived
200     *  classes usually call parser.setContext(container) so that the
201     *  MoML is parsed in the correct context.  Note that it is probably
202     *  not correct to call this method and pass in the current MoMLParser
203     *  because setContext() calls reset().  Instead, the caller (MoMLParser),
204     *  creates a MoMLParser instance that is shared amongst all the calls
205     *  to the MoMLFilter methods that take a MoMLParser argument.</p>
206     *
207     *  @param container The object defined by the element that this
208     *   is the end of.
209     *  @param elementName The element name.
210     *  @param currentCharData The character data, which appears
211     *   only in the doc and configure elements
212     *  @param xmlFile The file currently being parsed.
213     *  @param parser The parser in which MoML is optionally evaluated.
214     *  @exception Exception If there is a problem modifying the
215     *  specified container.
216     */
217    public void filterEndElement(NamedObj container, String elementName,
218            StringBuffer currentCharData, String xmlFile, MoMLParser parser)
219            throws Exception;
220
221    /** Return a string that describes what the filter does.
222     *  @return A description of the filter (ending with a newline).
223     */
224    @Override
225    public String toString();
226}