1 /**
2  * @file ListOfLocalStyles.cpp
3  * @brief Implementation of the ListOfLocalStyles 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/ListOfLocalStyles.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 ListOfLocalStyles using the given SBML Level, Version and
61  * &ldquo;render&rdquo; package version.
62  */
ListOfLocalStyles(unsigned int level,unsigned int version,unsigned int pkgVersion)63 ListOfLocalStyles::ListOfLocalStyles(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 ListOfLocalStyles using the given RenderPkgNamespaces object.
74  */
ListOfLocalStyles(RenderPkgNamespaces * renderns)75 ListOfLocalStyles::ListOfLocalStyles(RenderPkgNamespaces *renderns)
76   : ListOf(renderns)
77 {
78   setElementNamespace(renderns->getURI());
79 }
80 
81 
ListOfLocalStyles(const XMLNode & node)82 ListOfLocalStyles::ListOfLocalStyles(const XMLNode& node)
83 {
84   const XMLAttributes& attributes = node.getAttributes();
85   const XMLNode* child;
86   ExpectedAttributes ea;
87   addExpectedAttributes(ea);
88   mURI = RenderExtension::getXmlnsL3V1V1();
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       LocalStyle* style = new LocalStyle(*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 ListOfLocalStyles.
115  */
ListOfLocalStyles(const ListOfLocalStyles & orig)116 ListOfLocalStyles::ListOfLocalStyles(const ListOfLocalStyles& orig)
117   : ListOf( orig )
118 {
119 }
120 
121 
122 /*
123  * Assignment operator for ListOfLocalStyles.
124  */
125 ListOfLocalStyles&
operator =(const ListOfLocalStyles & rhs)126 ListOfLocalStyles::operator=(const ListOfLocalStyles& 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 ListOfLocalStyles object.
139  */
140 ListOfLocalStyles*
clone() const141 ListOfLocalStyles::clone() const
142 {
143   return new ListOfLocalStyles(*this);
144 }
145 
146 
147 /*
148  * Destructor for ListOfLocalStyles.
149  */
~ListOfLocalStyles()150 ListOfLocalStyles::~ListOfLocalStyles()
151 {
152 }
153 
154 
155 /*
156  * Get a LocalStyle from the ListOfLocalStyles.
157  */
158 LocalStyle*
get(unsigned int n)159 ListOfLocalStyles::get(unsigned int n)
160 {
161   return static_cast<LocalStyle*>(ListOf::get(n));
162 }
163 
164 
165 /*
166  * Get a LocalStyle from the ListOfLocalStyles.
167  */
168 const LocalStyle*
get(unsigned int n) const169 ListOfLocalStyles::get(unsigned int n) const
170 {
171   return static_cast<const LocalStyle*>(ListOf::get(n));
172 }
173 
174 /** @cond doxygenLibsbmlInternal */
175 /*
176 * Used by ListOf::get() to lookup an SBase based by its id.
177 */
178 struct IdEqLocalStyle : public std::unary_function<SBase*, bool>
179 {
180   const std::string& id;
181 
IdEqLocalStyleIdEqLocalStyle182   IdEqLocalStyle(const std::string& id) : id(id) { }
operator ()IdEqLocalStyle183   bool operator() (SBase* sb)
184   {
185     return static_cast <LocalStyle *> (sb)->getId() == id;
186   }
187 };
188 /** @endcond */
189 
190 
191 /*
192  * Get a LocalStyle from the ListOfLocalStyles based on its identifier.
193  */
194 LocalStyle*
get(const std::string & sid)195 ListOfLocalStyles::get(const std::string& sid)
196 {
197   return const_cast<LocalStyle*>(static_cast<const
198     ListOfLocalStyles&>(*this).get(sid));
199 }
200 
201 
202 /*
203  * Get a LocalStyle from the ListOfLocalStyles based on its identifier.
204  */
205 const LocalStyle*
get(const std::string & sid) const206 ListOfLocalStyles::get(const std::string& sid) const
207 {
208   vector<SBase*>::const_iterator result;
209   result = find_if(mItems.begin(), mItems.end(), IdEqLocalStyle(sid));
210   return (result == mItems.end()) ? 0 : static_cast <const LocalStyle*>
211     (*result);
212 }
213 
214 
215 /*
216  * Removes the nth LocalStyle from this ListOfLocalStyles and returns a pointer
217  * to it.
218  */
219 LocalStyle*
remove(unsigned int n)220 ListOfLocalStyles::remove(unsigned int n)
221 {
222   return static_cast<LocalStyle*>(ListOf::remove(n));
223 }
224 
225 
226 /*
227  * Removes the LocalStyle from this ListOfLocalStyles based on its identifier
228  * and returns a pointer to it.
229  */
230 LocalStyle*
remove(const std::string & sid)231 ListOfLocalStyles::remove(const std::string& sid)
232 {
233   SBase* item = NULL;
234   vector<SBase*>::iterator result;
235 
236   result = find_if(mItems.begin(), mItems.end(), IdEqLocalStyle(sid));
237 
238   if (result != mItems.end())
239   {
240     item = *result;
241     mItems.erase(result);
242   }
243 
244   return static_cast <LocalStyle*> (item);
245 }
246 
247 
248 /*
249  * Adds a copy of the given LocalStyle to this ListOfLocalStyles.
250  */
251 int
addLocalStyle(const LocalStyle * ls)252 ListOfLocalStyles::addLocalStyle(const LocalStyle* ls)
253 {
254   if (ls == NULL)
255   {
256     return LIBSBML_OPERATION_FAILED;
257   }
258   else if (ls->hasRequiredAttributes() == false)
259   {
260     return LIBSBML_INVALID_OBJECT;
261   }
262   else if (getLevel() != ls->getLevel())
263   {
264     return LIBSBML_LEVEL_MISMATCH;
265   }
266   else if (getVersion() != ls->getVersion())
267   {
268     return LIBSBML_VERSION_MISMATCH;
269   }
270   else if (matchesRequiredSBMLNamespacesForAddition(static_cast<const
271     SBase*>(ls)) == false)
272   {
273     return LIBSBML_NAMESPACES_MISMATCH;
274   }
275   else
276   {
277     return append(ls);
278   }
279 }
280 
281 
282 /*
283  * Get the number of LocalStyle objects in this ListOfLocalStyles.
284  */
285 unsigned int
getNumLocalStyles() const286 ListOfLocalStyles::getNumLocalStyles() const
287 {
288   return size();
289 }
290 
291 
292 /*
293  * Creates a new LocalStyle object, adds it to this ListOfLocalStyles object
294  * and returns the LocalStyle object created.
295  */
296 LocalStyle*
createLocalStyle()297 ListOfLocalStyles::createLocalStyle()
298 {
299   LocalStyle* ls = NULL;
300 
301   try
302   {
303     RENDER_CREATE_NS(renderns, getSBMLNamespaces());
304     ls = new LocalStyle(renderns);
305     delete renderns;
306   }
307   catch (...)
308   {
309   }
310 
311   if (ls != NULL)
312   {
313     appendAndOwn(ls);
314   }
315 
316   return ls;
317 }
318 
319 
320 /*
321  * Returns the XML element name of this ListOfLocalStyles object.
322  */
323 const std::string&
getElementName() const324 ListOfLocalStyles::getElementName() const
325 {
326   static const string name = "listOfStyles";
327   return name;
328 }
329 
330 
331 /*
332  * Returns the libSBML type code for this ListOfLocalStyles object.
333  */
334 int
getTypeCode() const335 ListOfLocalStyles::getTypeCode() const
336 {
337   return SBML_LIST_OF;
338 }
339 
340 
341 /*
342  * Returns the libSBML type code for the SBML objects contained in this
343  * ListOfLocalStyles object.
344  */
345 int
getItemTypeCode() const346 ListOfLocalStyles::getItemTypeCode() const
347 {
348   return SBML_RENDER_LOCALSTYLE;
349 }
350 
351 
toXML() const352 XMLNode ListOfLocalStyles::toXML() const
353 {
354   return getXmlNodeForSBase(this);
355 }
356 
357 
358 /** @cond doxygenLibsbmlInternal */
359 
360 /*
361  * Creates a new LocalStyle in this ListOfLocalStyles
362  */
363 SBase*
createObject(XMLInputStream & stream)364 ListOfLocalStyles::createObject(XMLInputStream& stream)
365 {
366   const std::string& name = stream.peek().getName();
367   SBase* object = NULL;
368   RENDER_CREATE_NS(renderns, getSBMLNamespaces());
369 
370   if (name == "style")
371   {
372     object = new LocalStyle(renderns);
373     appendAndOwn(object);
374   }
375 
376   delete renderns;
377   return object;
378 }
379 
380 /** @endcond */
381 
382 
383 
384 /** @cond doxygenLibsbmlInternal */
385 
386 /*
387  * Writes the namespace for the Render package
388  */
389 void
writeXMLNS(XMLOutputStream & stream) const390 ListOfLocalStyles::writeXMLNS(XMLOutputStream& stream) const
391 {
392   XMLNamespaces xmlns;
393   std::string prefix = getPrefix();
394 
395   if (prefix.empty())
396   {
397     const XMLNamespaces* thisxmlns = getNamespaces();
398     if (thisxmlns && thisxmlns->hasURI(RenderExtension::getXmlnsL3V1V1()))
399     {
400       xmlns.add(RenderExtension::getXmlnsL3V1V1(), prefix);
401     }
402   }
403 
404   stream << xmlns;
405 }
406 
407 /** @endcond */
408 
409 
410 
411 
412 #endif /* __cplusplus */
413 
414 
415 /*
416  * Get a LocalStyle_t from the ListOf_t.
417  */
418 LIBSBML_EXTERN
419 LocalStyle_t*
ListOfLocalStyles_getLocalStyle(ListOf_t * lo,unsigned int n)420 ListOfLocalStyles_getLocalStyle(ListOf_t* lo, unsigned int n)
421 {
422   if (lo == NULL)
423   {
424     return NULL;
425   }
426 
427   return static_cast <ListOfLocalStyles*>(lo)->get(n);
428 }
429 
430 
431 /*
432  * Get a LocalStyle_t from the ListOf_t based on its identifier.
433  */
434 LIBSBML_EXTERN
435 LocalStyle_t*
ListOfLocalStyles_getById(ListOf_t * lo,const char * sid)436 ListOfLocalStyles_getById(ListOf_t* lo, const char *sid)
437 {
438   if (lo == NULL)
439   {
440     return NULL;
441   }
442 
443   return (sid != NULL) ? static_cast <ListOfLocalStyles*>(lo)->get(sid) : NULL;
444 }
445 
446 
447 /*
448  * Removes the nth LocalStyle_t from this ListOf_t and returns a pointer to it.
449  */
450 LIBSBML_EXTERN
451 LocalStyle_t*
ListOfLocalStyles_remove(ListOf_t * lo,unsigned int n)452 ListOfLocalStyles_remove(ListOf_t* lo, unsigned int n)
453 {
454   if (lo == NULL)
455   {
456     return NULL;
457   }
458 
459   return static_cast <ListOfLocalStyles*>(lo)->remove(n);
460 }
461 
462 
463 /*
464  * Removes the LocalStyle_t from this ListOf_t based on its identifier and
465  * returns a pointer to it.
466  */
467 LIBSBML_EXTERN
468 LocalStyle_t*
ListOfLocalStyles_removeById(ListOf_t * lo,const char * sid)469 ListOfLocalStyles_removeById(ListOf_t* lo, const char* sid)
470 {
471   if (lo == NULL)
472   {
473     return NULL;
474   }
475 
476   return (sid != NULL) ? static_cast <ListOfLocalStyles*>(lo)->remove(sid) :
477     NULL;
478 }
479 
480 
481 
482 
483 LIBSBML_CPP_NAMESPACE_END
484 
485 
486