1 /**
2  * @file ListOfGlobalStyles.cpp
3  * @brief Implementation of the ListOfGlobalStyles class.
4  * @author SBMLTeam
5  *
6  * <!--------------------------------------------------------------------------
7  * This file is part of libSBML. Please visit http://sbml.org for more
8  * information about SBML, and the latest version of libSBML.
9  *
10  * Copyright (C) 2020 jointly by the following organizations:
11  *     1. California Institute of Technology, Pasadena, CA, USA
12  *     2. University of Heidelberg, Heidelberg, Germany
13  *     3. University College London, London, UK
14  *
15  * Copyright (C) 2019 jointly by the following organizations:
16  *     1. California Institute of Technology, Pasadena, CA, USA
17  *     2. University of Heidelberg, Heidelberg, Germany
18  *
19  * Copyright (C) 2013-2018 jointly by the following organizations:
20  * 1. California Institute of Technology, Pasadena, CA, USA
21  * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
22  * 3. University of Heidelberg, Heidelberg, Germany
23  *
24  * Copyright (C) 2009-2013 jointly by the following organizations:
25  * 1. California Institute of Technology, Pasadena, CA, USA
26  * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
27  *
28  * Copyright (C) 2006-2008 by the California Institute of Technology,
29  * Pasadena, CA, USA
30  *
31  * Copyright (C) 2002-2005 jointly by the following organizations:
32  * 1. California Institute of Technology, Pasadena, CA, USA
33  * 2. Japan Science and Technology Agency, Japan
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the GNU Lesser General Public License as published by the
37  * Free Software Foundation. A copy of the license agreement is provided in the
38  * file named "LICENSE.txt" included with this software distribution and also
39  * available online as http://sbml.org/software/libsbml/license.html
40  * ------------------------------------------------------------------------ -->
41  */
42 #include <sbml/packages/render/sbml/ListOfGlobalStyles.h>
43 #include <sbml/packages/render/validator/RenderSBMLError.h>
44 #include <sbml/packages/layout/util/LayoutUtilities.h>
45 
46 
47 using namespace std;
48 
49 
50 
51 LIBSBML_CPP_NAMESPACE_BEGIN
52 
53 
54 
55 
56 #ifdef __cplusplus
57 
58 
59 /*
60  * Creates a new ListOfGlobalStyles using the given SBML Level, Version and
61  * &ldquo;render&rdquo; package version.
62  */
ListOfGlobalStyles(unsigned int level,unsigned int version,unsigned int pkgVersion)63 ListOfGlobalStyles::ListOfGlobalStyles(unsigned int level,
64                                        unsigned int version,
65                                        unsigned int pkgVersion)
66   : ListOf(level, version)
67 {
68   setSBMLNamespacesAndOwn(new RenderPkgNamespaces(level, version, pkgVersion));
69 }
70 
71 
72 /*
73  * Creates a new ListOfGlobalStyles using the given RenderPkgNamespaces object.
74  */
ListOfGlobalStyles(RenderPkgNamespaces * renderns)75 ListOfGlobalStyles::ListOfGlobalStyles(RenderPkgNamespaces *renderns)
76   : ListOf(renderns)
77 {
78   setElementNamespace(renderns->getURI());
79 }
80 
81 
ListOfGlobalStyles(const XMLNode & node)82 ListOfGlobalStyles::ListOfGlobalStyles(const XMLNode& node)
83 {
84   const XMLAttributes& attributes = node.getAttributes();
85   const XMLNode* child;
86   ExpectedAttributes ea;
87   mURI = RenderExtension::getXmlnsL3V1V1();
88   addExpectedAttributes(ea);
89   this->readAttributes(attributes, ea);
90   unsigned int n = 0, nMax = node.getNumChildren();
91   while (n<nMax)
92   {
93     child = &node.getChild(n);
94     const std::string& childName = child->getName();
95     if (childName == "style")
96     {
97       GlobalStyle* style = new GlobalStyle(*child);
98       this->appendAndOwn(style);
99     }
100     else if (childName == "annotation")
101     {
102       this->mAnnotation = new XMLNode(*child);
103     }
104     else if (childName == "notes")
105     {
106       this->mNotes = new XMLNode(*child);
107     }
108     ++n;
109   }
110 }
111 
112 
113 /*
114  * Copy constructor for ListOfGlobalStyles.
115  */
ListOfGlobalStyles(const ListOfGlobalStyles & orig)116 ListOfGlobalStyles::ListOfGlobalStyles(const ListOfGlobalStyles& orig)
117   : ListOf( orig )
118 {
119 }
120 
121 
122 /*
123  * Assignment operator for ListOfGlobalStyles.
124  */
125 ListOfGlobalStyles&
operator =(const ListOfGlobalStyles & rhs)126 ListOfGlobalStyles::operator=(const ListOfGlobalStyles& rhs)
127 {
128   if (&rhs != this)
129   {
130     ListOf::operator=(rhs);
131   }
132 
133   return *this;
134 }
135 
136 
137 /*
138  * Creates and returns a deep copy of this ListOfGlobalStyles object.
139  */
140 ListOfGlobalStyles*
clone() const141 ListOfGlobalStyles::clone() const
142 {
143   return new ListOfGlobalStyles(*this);
144 }
145 
146 
147 /*
148  * Destructor for ListOfGlobalStyles.
149  */
~ListOfGlobalStyles()150 ListOfGlobalStyles::~ListOfGlobalStyles()
151 {
152 }
153 
154 
155 /*
156  * Get a GlobalStyle from the ListOfGlobalStyles.
157  */
158 GlobalStyle*
get(unsigned int n)159 ListOfGlobalStyles::get(unsigned int n)
160 {
161   return static_cast<GlobalStyle*>(ListOf::get(n));
162 }
163 
164 
165 /*
166  * Get a GlobalStyle from the ListOfGlobalStyles.
167  */
168 const GlobalStyle*
get(unsigned int n) const169 ListOfGlobalStyles::get(unsigned int n) const
170 {
171   return static_cast<const GlobalStyle*>(ListOf::get(n));
172 }
173 
174 
175 /*
176  * Get a GlobalStyle from the ListOfGlobalStyles based on its identifier.
177  */
178 GlobalStyle*
get(const std::string & sid)179 ListOfGlobalStyles::get(const std::string& sid)
180 {
181   return const_cast<GlobalStyle*>(static_cast<const
182     ListOfGlobalStyles&>(*this).get(sid));
183 }
184 
185 
186 /*
187  * Get a GlobalStyle from the ListOfGlobalStyles based on its identifier.
188  */
189 const GlobalStyle*
get(const std::string & sid) const190 ListOfGlobalStyles::get(const std::string& sid) const
191 {
192   vector<SBase*>::const_iterator result;
193   result = find_if(mItems.begin(), mItems.end(), IdEq<GlobalStyle>(sid));
194   return (result == mItems.end()) ? 0 : static_cast <const GlobalStyle*>
195     (*result);
196 }
197 
198 
199 /*
200  * Removes the nth GlobalStyle from this ListOfGlobalStyles and returns a
201  * pointer to it.
202  */
203 GlobalStyle*
remove(unsigned int n)204 ListOfGlobalStyles::remove(unsigned int n)
205 {
206   return static_cast<GlobalStyle*>(ListOf::remove(n));
207 }
208 
209 
210 /*
211  * Removes the GlobalStyle from this ListOfGlobalStyles based on its identifier
212  * and returns a pointer to it.
213  */
214 GlobalStyle*
remove(const std::string & sid)215 ListOfGlobalStyles::remove(const std::string& sid)
216 {
217   SBase* item = NULL;
218   vector<SBase*>::iterator result;
219 
220   result = find_if(mItems.begin(), mItems.end(), IdEq<GlobalStyle>(sid));
221 
222   if (result != mItems.end())
223   {
224     item = *result;
225     mItems.erase(result);
226   }
227 
228   return static_cast <GlobalStyle*> (item);
229 }
230 
231 
232 /*
233  * Adds a copy of the given GlobalStyle to this ListOfGlobalStyles.
234  */
235 int
addGlobalStyle(const GlobalStyle * gs)236 ListOfGlobalStyles::addGlobalStyle(const GlobalStyle* gs)
237 {
238   if (gs == NULL)
239   {
240     return LIBSBML_OPERATION_FAILED;
241   }
242   else if (gs->hasRequiredAttributes() == false)
243   {
244     return LIBSBML_INVALID_OBJECT;
245   }
246   else if (getLevel() != gs->getLevel())
247   {
248     return LIBSBML_LEVEL_MISMATCH;
249   }
250   else if (getVersion() != gs->getVersion())
251   {
252     return LIBSBML_VERSION_MISMATCH;
253   }
254   else if (matchesRequiredSBMLNamespacesForAddition(static_cast<const
255     SBase*>(gs)) == false)
256   {
257     return LIBSBML_NAMESPACES_MISMATCH;
258   }
259   else
260   {
261     return append(gs);
262   }
263 }
264 
265 
266 /*
267  * Get the number of GlobalStyle objects in this ListOfGlobalStyles.
268  */
269 unsigned int
getNumGlobalStyles() const270 ListOfGlobalStyles::getNumGlobalStyles() const
271 {
272   return size();
273 }
274 
275 
276 /*
277  * Creates a new GlobalStyle object, adds it to this ListOfGlobalStyles object
278  * and returns the GlobalStyle object created.
279  */
280 GlobalStyle*
createGlobalStyle()281 ListOfGlobalStyles::createGlobalStyle()
282 {
283   GlobalStyle* gs = NULL;
284 
285   try
286   {
287     RENDER_CREATE_NS(renderns, getSBMLNamespaces());
288     gs = new GlobalStyle(renderns);
289     delete renderns;
290   }
291   catch (...)
292   {
293   }
294 
295   if (gs != NULL)
296   {
297     appendAndOwn(gs);
298   }
299 
300   return gs;
301 }
302 
303 
304 /*
305  * Returns the XML element name of this ListOfGlobalStyles object.
306  */
307 const std::string&
getElementName() const308 ListOfGlobalStyles::getElementName() const
309 {
310   static const string name = "listOfStyles";
311   return name;
312 }
313 
314 
315 /*
316  * Returns the libSBML type code for this ListOfGlobalStyles object.
317  */
318 int
getTypeCode() const319 ListOfGlobalStyles::getTypeCode() const
320 {
321   return SBML_LIST_OF;
322 }
323 
324 
325 /*
326  * Returns the libSBML type code for the SBML objects contained in this
327  * ListOfGlobalStyles object.
328  */
329 int
getItemTypeCode() const330 ListOfGlobalStyles::getItemTypeCode() const
331 {
332   return SBML_RENDER_GLOBALSTYLE;
333 }
334 
335 
toXML() const336 XMLNode ListOfGlobalStyles::toXML() const
337 {
338   return getXmlNodeForSBase(this);
339 }
340 
341 
342 /** @cond doxygenLibsbmlInternal */
343 
344 /*
345  * Creates a new GlobalStyle in this ListOfGlobalStyles
346  */
347 SBase*
createObject(XMLInputStream & stream)348 ListOfGlobalStyles::createObject(XMLInputStream& stream)
349 {
350   const std::string& name = stream.peek().getName();
351   SBase* object = NULL;
352   RENDER_CREATE_NS(renderns, getSBMLNamespaces());
353 
354   if (name == "style")
355   {
356     object = new GlobalStyle(renderns);
357     appendAndOwn(object);
358   }
359 
360   delete renderns;
361   return object;
362 }
363 
364 /** @endcond */
365 
366 
367 
368 /** @cond doxygenLibsbmlInternal */
369 
370 /*
371  * Writes the namespace for the Render package
372  */
373 void
writeXMLNS(XMLOutputStream & stream) const374 ListOfGlobalStyles::writeXMLNS(XMLOutputStream& stream) const
375 {
376   XMLNamespaces xmlns;
377   std::string prefix = getPrefix();
378 
379   if (prefix.empty())
380   {
381     const XMLNamespaces* thisxmlns = getNamespaces();
382     if (thisxmlns && thisxmlns->hasURI(RenderExtension::getXmlnsL3V1V1()))
383     {
384       xmlns.add(RenderExtension::getXmlnsL3V1V1(), prefix);
385     }
386   }
387 
388   stream << xmlns;
389 }
390 
391 /** @endcond */
392 
393 
394 
395 
396 #endif /* __cplusplus */
397 
398 
399 /*
400  * Get a GlobalStyle_t from the ListOf_t.
401  */
402 LIBSBML_EXTERN
403 GlobalStyle_t*
ListOfGlobalStyles_getGlobalStyle(ListOf_t * lo,unsigned int n)404 ListOfGlobalStyles_getGlobalStyle(ListOf_t* lo, unsigned int n)
405 {
406   if (lo == NULL)
407   {
408     return NULL;
409   }
410 
411   return static_cast <ListOfGlobalStyles*>(lo)->get(n);
412 }
413 
414 
415 /*
416  * Get a GlobalStyle_t from the ListOf_t based on its identifier.
417  */
418 LIBSBML_EXTERN
419 GlobalStyle_t*
ListOfGlobalStyles_getById(ListOf_t * lo,const char * sid)420 ListOfGlobalStyles_getById(ListOf_t* lo, const char *sid)
421 {
422   if (lo == NULL)
423   {
424     return NULL;
425   }
426 
427   return (sid != NULL) ? static_cast <ListOfGlobalStyles*>(lo)->get(sid) :
428     NULL;
429 }
430 
431 
432 /*
433  * Removes the nth GlobalStyle_t from this ListOf_t and returns a pointer to
434  * it.
435  */
436 LIBSBML_EXTERN
437 GlobalStyle_t*
ListOfGlobalStyles_remove(ListOf_t * lo,unsigned int n)438 ListOfGlobalStyles_remove(ListOf_t* lo, unsigned int n)
439 {
440   if (lo == NULL)
441   {
442     return NULL;
443   }
444 
445   return static_cast <ListOfGlobalStyles*>(lo)->remove(n);
446 }
447 
448 
449 /*
450  * Removes the GlobalStyle_t from this ListOf_t based on its identifier and
451  * returns a pointer to it.
452  */
453 LIBSBML_EXTERN
454 GlobalStyle_t*
ListOfGlobalStyles_removeById(ListOf_t * lo,const char * sid)455 ListOfGlobalStyles_removeById(ListOf_t* lo, const char* sid)
456 {
457   if (lo == NULL)
458   {
459     return NULL;
460   }
461 
462   return (sid != NULL) ? static_cast <ListOfGlobalStyles*>(lo)->remove(sid) :
463     NULL;
464 }
465 
466 
467 
468 
469 LIBSBML_CPP_NAMESPACE_END
470 
471 
472