001/* An attribute that represents a MoML parser. 002 003 Copyright (c) 1998-2018 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.util.List; 031 032import ptolemy.kernel.util.IllegalActionException; 033import ptolemy.kernel.util.InternalErrorException; 034import ptolemy.kernel.util.KernelException; 035import ptolemy.kernel.util.NameDuplicationException; 036import ptolemy.kernel.util.NamedObj; 037import ptolemy.kernel.util.SingletonAttribute; 038import ptolemy.kernel.util.Workspace; 039 040/////////////////////////////////////////////////////////////////// 041//// ParserAttribute 042 043/** 044 This attribute represents a MoML parser. 045 If it is present in an entity that is the context for a MoMLChangeRequest, 046 then that change request will use it to parse the changes. 047 It is not a persistent attribute (exportMoML() writes nothing). 048 It is a singleton, meaning that if it is inserted into a container 049 that already contains a singleton attribute with the same name, 050 then it will replace the previous attribute. 051 <p> 052 By default, this attribute is not persistent, so it will not 053 be present in a MoML representation of its container. 054 055 @see MoMLChangeRequest 056 @author Edward A. Lee 057 @version $Id$ 058 @since Ptolemy II 1.0 059 @Pt.ProposedRating Red (eal) 060 @Pt.AcceptedRating Red (reviewmoderator) 061 */ 062public class ParserAttribute extends SingletonAttribute { 063 /** Construct an attribute with the specified container and name. 064 * @param container The container. 065 * @param name The name of this attribute. 066 * @exception IllegalActionException If the attribute is not of an 067 * acceptable class for the container, or if the name contains a period. 068 * @exception NameDuplicationException If the name coincides with 069 * an attribute already in the container. 070 */ 071 public ParserAttribute(NamedObj container, String name) 072 throws IllegalActionException, NameDuplicationException { 073 super(container, name); 074 setPersistent(false); 075 } 076 077 /////////////////////////////////////////////////////////////////// 078 //// public methods //// 079 080 /** Clone the attribute into the specified workspace. The 081 * resulting object a null value for the value of the parser. 082 083 * @param workspace The workspace for the cloned object. 084 * @return A new attribute. 085 * @exception CloneNotSupportedException If a derived class contains 086 * an attribute that cannot be cloned. 087 */ 088 @Override 089 public Object clone(Workspace workspace) throws CloneNotSupportedException { 090 ParserAttribute newObject = (ParserAttribute) super.clone(workspace); 091 // If we don't set _parser to null, then the master and the 092 // clone share a MoMLParser, which is not good. 093 newObject._parser = null; 094 return newObject; 095 } 096 097 /** Get the parser. If none has been set, then return a new one. 098 * @return A MoML parser. 099 * @see #setParser(MoMLParser) 100 */ 101 public MoMLParser getParser() { 102 if (_parser == null) { 103 _parser = new MoMLParser(workspace()); 104 } 105 106 return _parser; 107 } 108 109 /** Get a parser for the specified object. This searches up the 110 * hierarchy until it finds a container of the specified object 111 * that contains an instance of ParserAttribute. If none is 112 * found, then a new ParserAttribute is created at the top level. 113 * @param object The object for which to find an associated parser. 114 * @return The parser for the specified object. 115 * @exception NullPointerException If the argument is null. 116 * @see #setParser(MoMLParser) 117 */ 118 public static MoMLParser getParser(NamedObj object) { 119 NamedObj container = object; 120 121 while (container != null) { 122 // We used to just get the ParserAttribute from the object. 123 // However, this is wrong, we should get it from the 124 // container. 125 // In r51962 the comment was "Bugfix" and the change was 126 // made. 127 // In r51963, the comment was " 128 // "Revert to previous version because the "bugfix" seems 129 // to break some existing editors (such as GT)." 130 // 131 // List attributes = object.attributeList(ParserAttribute.class); 132 List attributes = container.attributeList(ParserAttribute.class); 133 134 if (attributes != null && attributes.size() > 0) { 135 // Found one. 136 ParserAttribute attribute = (ParserAttribute) attributes.get(0); 137 return attribute.getParser(); 138 } 139 140 container = container.getContainer(); 141 } 142 143 // No parser attribute was found. 144 NamedObj toplevel = object.toplevel(); 145 146 try { 147 ParserAttribute attribute = new ParserAttribute(toplevel, 148 "_parser"); 149 return attribute.getParser(); 150 } catch (KernelException ex) { 151 // This should not occur. 152 throw new InternalErrorException(ex); 153 } 154 } 155 156 /** Set the parser. 157 * @param parser The parser. 158 * @see #getParser() 159 * @see #getParser(NamedObj) 160 */ 161 public void setParser(MoMLParser parser) { 162 _parser = parser; 163 } 164 165 /////////////////////////////////////////////////////////////////// 166 //// private variables //// 167 // The parser. 168 private MoMLParser _parser; 169}