1 /**
2  * @file    LayoutSpeciesReferencePlugin.cpp
3  * @brief   Implementation of LayoutSpeciesReferencePlugin, the plugin
4  *          object of layout package (Level2) for the SpeciesReference
5  *          and ModifierSpeciesReference elements.
6  * @author  Akiya Jouraku
7  *
8  * <!--------------------------------------------------------------------------
9  * This file is part of libSBML.  Please visit http://sbml.org for more
10  * information about SBML, and the latest version of libSBML.
11  *
12  * Copyright (C) 2020 jointly by the following organizations:
13  *     1. California Institute of Technology, Pasadena, CA, USA
14  *     2. University of Heidelberg, Heidelberg, Germany
15  *     3. University College London, London, UK
16  *
17  * Copyright (C) 2019 jointly by the following organizations:
18  *     1. California Institute of Technology, Pasadena, CA, USA
19  *     2. University of Heidelberg, Heidelberg, Germany
20  *
21  * Copyright (C) 2013-2018 jointly by the following organizations:
22  *     1. California Institute of Technology, Pasadena, CA, USA
23  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
24  *     3. University of Heidelberg, Heidelberg, Germany
25  *
26  * Copyright (C) 2009-2013 jointly by the following organizations:
27  *     1. California Institute of Technology, Pasadena, CA, USA
28  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
29  *
30  * Copyright (C) 2006-2008 by the California Institute of Technology,
31  *     Pasadena, CA, USA
32  *
33  * Copyright (C) 2002-2005 jointly by the following organizations:
34  *     1. California Institute of Technology, Pasadena, CA, USA
35  *     2. Japan Science and Technology Agency, Japan
36  *
37  * This library is free software; you can redistribute it and/or modify it
38  * under the terms of the GNU Lesser General Public License as published by
39  * the Free Software Foundation.  A copy of the license agreement is provided
40  * in the file named "LICENSE.txt" included with this software distribution
41  * and also available online as http://sbml.org/software/libsbml/license.html
42  * ------------------------------------------------------------------------ -->
43  */
44 
45 #include <sbml/packages/layout/extension/LayoutSpeciesReferencePlugin.h>
46 #include <sbml/packages/layout/util/LayoutAnnotation.h>
47 
48 
49 #ifdef __cplusplus
50 
51 using namespace std;
52 
53 LIBSBML_CPP_NAMESPACE_BEGIN
54 
LayoutSpeciesReferencePlugin(const std::string & uri,const std::string & prefix,LayoutPkgNamespaces * layoutns)55 LayoutSpeciesReferencePlugin::LayoutSpeciesReferencePlugin (const std::string &uri,
56                                                             const std::string &prefix,
57                                                             LayoutPkgNamespaces* layoutns)
58   : SBasePlugin(uri,prefix,layoutns)
59 {
60 }
61 
62 
63 /*
64  * Copy constructor. Creates a copy of this SBase object.
65  */
LayoutSpeciesReferencePlugin(const LayoutSpeciesReferencePlugin & orig)66 LayoutSpeciesReferencePlugin::LayoutSpeciesReferencePlugin(const LayoutSpeciesReferencePlugin& orig)
67   : SBasePlugin(orig)
68 {
69 }
70 
71 
72 /*
73  * Destroy this object.
74  */
~LayoutSpeciesReferencePlugin()75 LayoutSpeciesReferencePlugin::~LayoutSpeciesReferencePlugin () {}
76 
77 /*
78  * Assignment operator for LayoutSpeciesReferencePlugin.
79  */
80 LayoutSpeciesReferencePlugin&
operator =(const LayoutSpeciesReferencePlugin & orig)81 LayoutSpeciesReferencePlugin::operator=(const LayoutSpeciesReferencePlugin& orig)
82 {
83   if(&orig!=this)
84   {
85     this->SBasePlugin::operator =(orig);
86   }
87 
88   return *this;
89 }
90 
91 
92 /*
93  * Creates and returns a deep copy of this LayoutSpeciesReferencePlugin object.
94  *
95  * @return a (deep) copy of this LayoutSpeciesReferencePlugin object
96  */
97 LayoutSpeciesReferencePlugin*
clone() const98 LayoutSpeciesReferencePlugin::clone () const
99 {
100   return new LayoutSpeciesReferencePlugin(*this);
101 }
102 
103 
104 /** @cond doxygenLibsbmlInternal */
105 bool
readOtherXML(SBase * parentObject,XMLInputStream & stream)106 LayoutSpeciesReferencePlugin::readOtherXML (SBase* parentObject, XMLInputStream& stream)
107 {
108   if (!parentObject) return false;
109 
110   bool readAnnotationFromStream = false;
111 
112   //
113   // This plugin object is used only for SBML Level 2 Version 1.
114   //
115   if ( getURI() != LayoutExtension::getXmlnsL2() ) return false;
116   if ( parentObject->getVersion() > 1 )       return false;
117 
118   XMLNode *pAnnotation = parentObject->getAnnotation();
119 
120   if (!pAnnotation)
121   {
122     //
123     // (NOTES)
124     //
125     // annotation element has not been parsed by the parent element
126     // (SpeciesReference) of this plugin object, thus annotation
127     // element is parsed via the given XMLInputStream object in this block.
128     //
129 
130     const string& name = stream.peek().getName();
131 
132     if (name == "annotation")
133     {
134       pAnnotation = new XMLNode(stream);
135 
136       SpeciesReference *sr = static_cast<SpeciesReference*>(parentObject);
137 
138       parseSpeciesReferenceAnnotation(pAnnotation,*sr);
139       std::string srId = sr->getId();
140 
141       if (!srId.empty())
142       {
143         //
144         // Removes the annotation for layout extension from the annotation
145         // of parent element (pAnnotation) and then set the new annotation
146         // (newAnnotation) to the parent element.
147         //
148         deleteLayoutIdAnnotation(pAnnotation);
149       }
150 
151       parentObject->setAnnotation(pAnnotation);
152       delete pAnnotation;
153 
154       readAnnotationFromStream = true;
155     }
156 
157   }
158   else if (parentObject->getId().empty())
159   {
160     //
161     // (NOTES)
162     //
163     // annotation element has been parsed by the parent element
164     // (SpeciesReference) of this plugin object, thus the annotation element
165     // set to the above pAnnotation variable is parsed in this block.
166     //
167     SpeciesReference *sr = static_cast<SpeciesReference*>(parentObject);
168     parseSpeciesReferenceAnnotation(pAnnotation, *sr);
169     std::string srId = sr->getId();
170 
171     if (!srId.empty())
172     {
173       //
174       // Removes the annotation for layout extension from the annotation
175       // of parent element (pAnnotation) and then set the new annotation
176       // (newAnnotation) to the parent element.
177       //
178       deleteLayoutIdAnnotation(pAnnotation);
179     }
180     readAnnotationFromStream = true;
181   }
182   return readAnnotationFromStream;
183 }
184 /** @endcond */
185 
186 /** @cond doxygenLibsbmlInternal */
187 void
writeAttributes(XMLOutputStream &) const188 LayoutSpeciesReferencePlugin::writeAttributes (XMLOutputStream&) const
189 {
190   SimpleSpeciesReference *parent;
191   parent = static_cast<SimpleSpeciesReference*>(const_cast<SBase*>(getParentSBMLObject()));
192 
193   if (!parent) return;
194 
195   //
196   // This plugin object is used only for SBML Level 2 Version 1.
197   //
198   if ( getURI() != LayoutExtension::getXmlnsL2() ) return;
199   // in a conversion the uri might be L2 but the model will be L3
200   if ( parent->getLevel() != 2)               return;
201   if ( parent->getVersion() > 1 )             return;
202 
203   XMLNode *annt = parseLayoutId(parent);
204   if (annt)
205   {
206     //cout << "[DEBUG] LayoutSpeciesReferencePlugin::writeAttributes (before) "
207     //     << annt->toXMLString() << endl;
208 
209     //XMLNode *pAnnotation   = parent->getAnnotation();
210     //XMLNode *newAnnotation = deleteLayoutAnnotation(pAnnotation);
211     //parent->setAnnotation(newAnnotation);
212     parent->appendAnnotation(annt);
213 
214     //cout << "[DEBUG] LayoutSpeciesReferencePlugin::writeAttributes (result) "
215     //     << result << " (annt) " << parent->getAnnotationString() << endl;
216 
217     //delete newAnnotation;
218     delete annt;
219   }
220 }
221 /** @endcond */
222 
223 /** @cond doxygenLibsbmlInternal */
224 bool
accept(SBMLVisitor &) const225 LayoutSpeciesReferencePlugin::accept (SBMLVisitor&) const
226 {
227   return true;
228 }
229 /** @endcond */
230 
231 
232 LIBSBML_CPP_NAMESPACE_END
233 
234 #endif  /* __cplusplus */
235