1 /**
2 * @file SBMLNamespaces.cpp
3 * @brief SBMLNamespaces class to store level/version and namespace
4 * @author Sarah Keating
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
37 * the Free Software Foundation. A copy of the license agreement is provided
38 * in the file named "LICENSE.txt" included with this software distribution
39 * and also available online as http://sbml.org/software/libsbml/license.html
40 * ---------------------------------------------------------------------- -->
41 */
42
43 #include <sbml/SBMLNamespaces.h>
44 #include <sbml/extension/SBMLExtensionRegistry.h>
45 #include <sbml/extension/SBMLExtensionException.h>
46 #include <sstream>
47 #include <sbml/common/common.h>
48 #include <iostream>
49
50 /** @cond doxygenIgnored */
51 using namespace std;
52 /** @endcond */
53
54
55 LIBSBML_CPP_NAMESPACE_BEGIN
56 #ifdef __cplusplus
57
58 /** @cond doxygenLibsbmlInternal */
59 void
initSBMLNamespace()60 SBMLNamespaces::initSBMLNamespace()
61 {
62 mNamespaces = new XMLNamespaces();
63
64 switch (mLevel)
65 {
66 case 1:
67 switch (mVersion)
68 {
69 case 1:
70 case 2:
71 mNamespaces->add(SBML_XMLNS_L1);
72 break;
73 }
74 break;
75 case 2:
76 switch (mVersion)
77 {
78 case 1:
79 mNamespaces->add(SBML_XMLNS_L2V1);
80 break;
81 case 2:
82 mNamespaces->add(SBML_XMLNS_L2V2);
83 break;
84 case 3:
85 mNamespaces->add(SBML_XMLNS_L2V3);
86 break;
87 case 4:
88 mNamespaces->add(SBML_XMLNS_L2V4);
89 break;
90 case 5:
91 mNamespaces->add(SBML_XMLNS_L2V5);
92 break;
93 }
94 break;
95 case 3:
96 switch (mVersion)
97 {
98 case 1:
99 mNamespaces->add(SBML_XMLNS_L3V1);
100 break;
101 case 2:
102 mNamespaces->add(SBML_XMLNS_L3V2);
103 break;
104 }
105 break;
106 }
107
108 if (mNamespaces->getLength() == 0)
109 {
110 mLevel = SBML_INT_MAX;
111 mVersion = SBML_INT_MAX;
112 delete mNamespaces;
113 mNamespaces = NULL;
114 }
115 }
116 /** @endcond */
117
118
SBMLNamespaces(unsigned int level,unsigned int version)119 SBMLNamespaces::SBMLNamespaces(unsigned int level, unsigned int version)
120 : mLevel(level)
121 ,mVersion(version)
122 {
123 initSBMLNamespace();
124 }
125
126
127 /**
128 * (For Extension)
129 *
130 * Creates a new SBMLNamespaces object corresponding to the combination of
131 * (1) the given SBML @p level and @p version, and (2) the given @p package
132 * with the @p package @p version.
133 *
134 */
SBMLNamespaces(unsigned int level,unsigned int version,const std::string & pkgName,unsigned int pkgVersion,const std::string pkgPrefix)135 SBMLNamespaces::SBMLNamespaces(unsigned int level, unsigned int version,
136 const std::string &pkgName, unsigned int pkgVersion,
137 const std::string pkgPrefix)
138 : mLevel(level)
139 ,mVersion(version)
140 {
141 initSBMLNamespace();
142
143 //
144 // checks the URI of the given package
145 //
146 const SBMLExtension* sbmlext = SBMLExtensionRegistry::getInstance().getExtensionInternal(pkgName);
147 if (sbmlext)
148 {
149 const std::string uri = sbmlext->getURI(level, version, pkgVersion);
150 const std::string prefix = (pkgPrefix.empty()) ? pkgName : pkgPrefix;
151
152 if (!uri.empty() && mNamespaces != NULL)
153 {
154 mNamespaces->add(uri,prefix);
155 }
156 else
157 {
158 std::ostringstream errMsg;
159
160 errMsg << "Package \"" << pkgName << "\" SBML level " << level << " SBML version "
161 << version << " package version " << pkgVersion << " is not supported.";
162
163 throw SBMLExtensionException(errMsg.str());
164 }
165 }
166 else
167 {
168 std::ostringstream errMsg;
169
170 errMsg << pkgName << " : No such package registered.";
171
172 throw SBMLExtensionException(errMsg.str());
173 }
174 }
175
~SBMLNamespaces()176 SBMLNamespaces::~SBMLNamespaces()
177 {
178 if (mNamespaces != NULL)
179 delete mNamespaces;
180 }
181
182
183 /*
184 * Copy constructor; creates a copy of a SBMLNamespaces.
185 */
SBMLNamespaces(const SBMLNamespaces & orig)186 SBMLNamespaces::SBMLNamespaces(const SBMLNamespaces& orig)
187 : mLevel(orig.mLevel)
188 , mVersion(orig.mVersion)
189 , mNamespaces(NULL)
190 {
191 if(orig.mNamespaces != NULL)
192 this->mNamespaces =
193 new XMLNamespaces(*const_cast<SBMLNamespaces&>(orig).mNamespaces);
194 }
195
196
197 const List *
getSupportedNamespaces()198 SBMLNamespaces::getSupportedNamespaces()
199 {
200 List *result = new List();
201 result->add(new SBMLNamespaces(1,1));
202 result->add(new SBMLNamespaces(1,2));
203 result->add(new SBMLNamespaces(2,1));
204 result->add(new SBMLNamespaces(2,2));
205 result->add(new SBMLNamespaces(2,3));
206 result->add(new SBMLNamespaces(2,4));
207 result->add(new SBMLNamespaces(2,5));
208 result->add(new SBMLNamespaces(3,1));
209 result->add(new SBMLNamespaces(3,2));
210 return result;
211 }
212
213
214 void
freeSBMLNamespaces(List * supportedNS)215 SBMLNamespaces::freeSBMLNamespaces(List * supportedNS)
216 {
217 if (supportedNS == NULL) return;
218 for (unsigned int i = 0; i < supportedNS->getSize(); i++)
219 {
220 delete (SBMLNamespaces*)supportedNS->get(i);
221 }
222 delete supportedNS;
223 }
224
225 /*
226 * Assignment operator for SBMLNamespaces.
227 */
228 SBMLNamespaces&
operator =(const SBMLNamespaces & rhs)229 SBMLNamespaces::operator=(const SBMLNamespaces& rhs)
230 {
231 if (&rhs != this)
232 {
233 mLevel = rhs.mLevel;
234 mVersion = rhs.mVersion;
235 delete this->mNamespaces;
236 if(rhs.mNamespaces != NULL)
237 this->mNamespaces =
238 new XMLNamespaces(*const_cast<SBMLNamespaces&>(rhs).mNamespaces);
239 else
240 this->mNamespaces = NULL;
241 }
242
243 return *this;
244 }
245
246
247
248 /*
249 * Creates and returns a deep copy of this SBMLNamespaces.
250 */
251 SBMLNamespaces *
clone() const252 SBMLNamespaces::clone () const
253 {
254 return new SBMLNamespaces(*this);
255 }
256
257
258 std::string
getSBMLNamespaceURI(unsigned int level,unsigned int version)259 SBMLNamespaces::getSBMLNamespaceURI(unsigned int level,
260 unsigned int version)
261 {
262 std::string uri = "";
263 switch (level)
264 {
265 case 1:
266 uri = SBML_XMLNS_L1;
267 break;
268 case 3:
269 switch(version)
270 {
271 case 1:
272 uri = SBML_XMLNS_L3V1;
273 break;
274 case 2:
275 default:
276 uri = SBML_XMLNS_L3V2;
277 break;
278 }
279 break;
280 case 2:
281 default:
282 switch (version)
283 {
284 case 1:
285 uri = SBML_XMLNS_L2V1;
286 break;
287 case 2:
288 uri = SBML_XMLNS_L2V2;
289 break;
290 case 3:
291 uri = SBML_XMLNS_L2V3;
292 break;
293 case 4:
294 uri = SBML_XMLNS_L2V4;
295 break;
296 case 5:
297 default:
298 uri = SBML_XMLNS_L2V5;
299 break;
300 }
301 break;
302 }
303 return uri;
304 }
305
306
307 std::string
getURI() const308 SBMLNamespaces::getURI() const
309 {
310 return getSBMLNamespaceURI(mLevel,mVersion);
311 }
312
313
314 unsigned int
getLevel()315 SBMLNamespaces::getLevel()
316 {
317 return mLevel;
318 }
319
320
321 unsigned int
getLevel() const322 SBMLNamespaces::getLevel() const
323 {
324 return mLevel;
325 }
326
327
328 unsigned int
getVersion()329 SBMLNamespaces::getVersion()
330 {
331 return mVersion;
332 }
333
334
335 unsigned int
getVersion() const336 SBMLNamespaces::getVersion() const
337 {
338 return mVersion;
339 }
340
341
342 XMLNamespaces *
getNamespaces()343 SBMLNamespaces::getNamespaces()
344 {
345 return mNamespaces;
346 }
347
348
349 const XMLNamespaces *
getNamespaces() const350 SBMLNamespaces::getNamespaces() const
351 {
352 return mNamespaces;
353 }
354
355
356 int
addNamespaces(const XMLNamespaces * xmlns)357 SBMLNamespaces::addNamespaces(const XMLNamespaces * xmlns)
358 {
359 int success = LIBSBML_OPERATION_SUCCESS;
360
361 if (xmlns == NULL)
362 return LIBSBML_INVALID_OBJECT;
363
364 if (!mNamespaces)
365 {
366 initSBMLNamespace();
367 }
368
369 /* check whether the namespace already exists
370 * add if it does not
371 */
372 for (int i = 0; i < xmlns->getLength(); i++)
373 {
374 if (mNamespaces != NULL && !(mNamespaces->hasNS(xmlns->getURI(i), xmlns->getPrefix(i))))
375 {
376 success = mNamespaces->add(xmlns->getURI(i), xmlns->getPrefix(i));
377 }
378 }
379
380 return success;
381 }
382
383 /*
384 * (For Extension)
385 *
386 * Add an XML namespace (a pair of URI and prefix) of a package extension
387 * to the set of namespaces within this SBMLNamespaces object.
388 *
389 */
390 int
addPackageNamespace(const std::string & pkgName,unsigned int pkgVersion,const std::string & pkgPrefix)391 SBMLNamespaces::addPackageNamespace(const std::string &pkgName, unsigned int pkgVersion,
392 const std::string &pkgPrefix)
393 {
394 if (!mNamespaces)
395 {
396 initSBMLNamespace();
397 }
398
399 //
400 // checks the URI of the given package
401 //
402 const SBMLExtension* sbmlext = SBMLExtensionRegistry::getInstance().getExtensionInternal(pkgName);
403 if (sbmlext)
404 {
405 const std::string uri = sbmlext->getURI(mLevel, mVersion, pkgVersion);
406 const std::string prefix = (pkgPrefix.empty()) ? pkgName : pkgPrefix;
407 if (!uri.empty() && mNamespaces != NULL)
408 {
409 return mNamespaces->add(uri,prefix);
410 }
411 else
412 {
413 return LIBSBML_INVALID_ATTRIBUTE_VALUE;
414 }
415 }
416 else
417 {
418 return LIBSBML_INVALID_ATTRIBUTE_VALUE;
419 }
420
421 // return LIBSBML_OPERATION_SUCCESS;
422 }
423
424
425 /** @cond doxygenLibsbmlInternal */
426 /*
427 * (For Extension)
428 *
429 * Add an XML namespace (a pair of URI and prefix) of a package extension
430 * to the set of namespaces within this SBMLNamespaces object.
431 *
432 */
433 int
addPkgNamespace(const std::string & pkgName,unsigned int pkgVersion,const std::string & pkgPrefix)434 SBMLNamespaces::addPkgNamespace(const std::string &pkgName, unsigned int pkgVersion,
435 const std::string &pkgPrefix)
436 {
437
438 return addPackageNamespace(pkgName, pkgVersion, pkgPrefix);
439 }
440 /** @endcond */
441
442 /** @cond doxygenLibsbmlInternal */
443 /*
444 * Add the XML namespaces of package extensions in the given
445 * XMLNamespace object to the set of namespaces within this
446 * SBMLNamespaces object (Non-package XML namespaces are not added
447 * by this function).
448 */
449 int
addPackageNamespaces(const XMLNamespaces * xmlns)450 SBMLNamespaces::addPackageNamespaces (const XMLNamespaces *xmlns)
451 {
452 if (!mNamespaces)
453 {
454 initSBMLNamespace();
455 }
456
457 if (!xmlns)
458 {
459 return LIBSBML_INVALID_ATTRIBUTE_VALUE;
460 }
461
462 for (int i=0; i < xmlns->getLength(); i++)
463 {
464 const std::string uri = xmlns->getURI(i);
465
466 if (mNamespaces != NULL && SBMLExtensionRegistry::getInstance().isRegistered(uri))
467 {
468 mNamespaces->add(uri, xmlns->getPrefix(i));
469 }
470 }
471
472 return LIBSBML_OPERATION_SUCCESS;
473 }
474 /** @endcond */
475
476 /** @cond doxygenLibsbmlInternal */
477 int
addPkgNamespaces(const XMLNamespaces * xmlns)478 SBMLNamespaces::addPkgNamespaces (const XMLNamespaces *xmlns)
479 {
480 return addPackageNamespaces(xmlns);
481 }
482 /** @endcond */
483
484 int
addNamespace(const std::string & uri,const std::string & prefix)485 SBMLNamespaces::addNamespace(const std::string &uri, const std::string &prefix)
486 {
487 if (!mNamespaces)
488 {
489 initSBMLNamespace();
490 }
491
492 return mNamespaces != NULL ? mNamespaces->add(uri, prefix) : LIBSBML_INVALID_OBJECT;
493 }
494
495
496 int
removeNamespace(const std::string & uri)497 SBMLNamespaces::removeNamespace(const std::string &uri)
498 {
499 if (!mNamespaces)
500 {
501 initSBMLNamespace();
502 }
503
504 return mNamespaces != NULL ? mNamespaces->remove(mNamespaces->getIndex(uri)) : LIBSBML_INVALID_OBJECT;
505 }
506
507
508 /*
509 * Removes an XML namespace of a package extension from the set of namespaces
510 * within this SBMLNamespaces object.
511 */
512 int
removePackageNamespace(unsigned int level,unsigned version,const std::string & pkgName,unsigned int pkgVersion)513 SBMLNamespaces::removePackageNamespace(unsigned int level, unsigned version, const std::string &pkgName,
514 unsigned int pkgVersion)
515 {
516 //
517 // checks the URI of the given package
518 //
519 const SBMLExtension* sbmlext = SBMLExtensionRegistry::getInstance().getExtensionInternal(pkgName);
520 if (sbmlext)
521 {
522 if (!mNamespaces)
523 {
524 return LIBSBML_OPERATION_SUCCESS;
525 }
526
527 const std::string uri = sbmlext->getURI(level, version, pkgVersion);
528 if (!uri.empty())
529 {
530 return mNamespaces->remove(mNamespaces->getIndex(uri));
531 }
532 else
533 {
534 return LIBSBML_INVALID_ATTRIBUTE_VALUE;
535 }
536 }
537 else
538 {
539 return LIBSBML_INVALID_ATTRIBUTE_VALUE;
540 }
541 }
542
543 /** @cond doxygenLibsbmlInternal */
544 int
removePkgNamespace(unsigned int level,unsigned version,const std::string & pkgName,unsigned int pkgVersion)545 SBMLNamespaces::removePkgNamespace(unsigned int level, unsigned version, const std::string &pkgName,
546 unsigned int pkgVersion)
547 {
548 return removePackageNamespace(level, version, pkgName, pkgVersion);
549 }
550 /** @endcond */
551
552 /*
553 * Predicate returning @c true if the given
554 * URL is one of SBML XML namespaces.
555 */
556 bool
isSBMLNamespace(const std::string & uri)557 SBMLNamespaces::isSBMLNamespace(const std::string& uri)
558 {
559 if (uri == SBML_XMLNS_L1) return true;
560 if (uri == SBML_XMLNS_L2V1) return true;
561 if (uri == SBML_XMLNS_L2V2) return true;
562 if (uri == SBML_XMLNS_L2V3) return true;
563 if (uri == SBML_XMLNS_L2V4) return true;
564 if (uri == SBML_XMLNS_L2V5) return true;
565 if (uri == SBML_XMLNS_L3V1) return true;
566 if (uri == SBML_XMLNS_L3V2) return true;
567
568 return false;
569 }
570
571 bool
isValidCombination()572 SBMLNamespaces::isValidCombination()
573 {
574 bool valid = true;
575 bool sbmlDeclared = false;
576 std::string declaredURI("");
577 unsigned int version = getVersion();
578 XMLNamespaces *xmlns = getNamespaces();
579
580 if (xmlns != NULL)
581 {
582 //
583 // checks defined SBML XMLNamespace
584 // returns false if different SBML XMLNamespaces
585 // (e.g. SBML_XMLNS_L2V1 and SBML_XMLNS_L2V3) are defined.
586 //
587 int numNS = 0;
588
589 if (xmlns->hasURI(SBML_XMLNS_L3V2))
590 {
591 ++numNS;
592 declaredURI.assign(SBML_XMLNS_L3V2);
593 }
594
595 if (xmlns->hasURI(SBML_XMLNS_L3V1))
596 {
597 ++numNS;
598 declaredURI.assign(SBML_XMLNS_L3V1);
599 }
600
601 if (xmlns->hasURI(SBML_XMLNS_L2V5))
602 {
603 if (numNS > 0) return false;
604 ++numNS;
605 declaredURI.assign(SBML_XMLNS_L2V5);
606 }
607
608 if (xmlns->hasURI(SBML_XMLNS_L2V4))
609 {
610 if (numNS > 0) return false;
611 ++numNS;
612 declaredURI.assign(SBML_XMLNS_L2V4);
613 }
614
615 if (xmlns->hasURI(SBML_XMLNS_L2V3))
616 {
617 // checks different SBML XMLNamespaces
618 if (numNS > 0) return false;
619 ++numNS;
620 declaredURI.assign(SBML_XMLNS_L2V3);
621 }
622
623 if (xmlns->hasURI(SBML_XMLNS_L2V2))
624 {
625 // checks different SBML XMLNamespaces
626 if (numNS > 0) return false;
627 ++numNS;
628 declaredURI.assign(SBML_XMLNS_L2V2);
629 }
630
631 if (xmlns->hasURI(SBML_XMLNS_L2V1))
632 {
633 // checks different SBML XMLNamespaces
634 if (numNS > 0) return false;
635 ++numNS;
636 declaredURI.assign(SBML_XMLNS_L2V1);
637 }
638
639 if (xmlns->hasURI(SBML_XMLNS_L1))
640 {
641 // checks different SBML XMLNamespaces
642 if (numNS > 0) return false;
643 ++numNS;
644 declaredURI.assign(SBML_XMLNS_L1);
645 }
646
647 // checks if the SBML Namespace is explicitly defined.
648 for (int i=0; i < xmlns->getLength(); i++)
649 {
650 if (!declaredURI.empty() &&
651 xmlns->getURI(i) == declaredURI)
652 {
653 sbmlDeclared = true;
654 break;
655 }
656 }
657 }
658
659
660 switch (getLevel())
661 {
662 case 1:
663 switch (version)
664 {
665 case 1:
666 case 2:
667 // the namespaces contains the sbml namespaces
668 // check it is the correct ns for the level/version
669 if (sbmlDeclared)
670 {
671 if (declaredURI != string(SBML_XMLNS_L1))
672 {
673 valid = false;
674 }
675 }
676 break;
677 default:
678 valid = false;
679 break;
680 }
681 break;
682 case 2:
683 switch (version)
684 {
685 case 1:
686 // the namespaces contains the sbml namespaces
687 // check it is the correct ns for the level/version
688 if (sbmlDeclared)
689 {
690 if (declaredURI != string(SBML_XMLNS_L2V1))
691 {
692 valid = false;
693 }
694 }
695 break;
696 case 2:
697 // the namespaces contains the sbml namespaces
698 // check it is the correct ns for the level/version
699 if (sbmlDeclared)
700 {
701 if (declaredURI != string(SBML_XMLNS_L2V2))
702 {
703 valid = false;
704 }
705 }
706 break;
707 case 3:
708 // the namespaces contains the sbml namespaces
709 // check it is the correct ns for the level/version
710 if (sbmlDeclared)
711 {
712 if (declaredURI != string(SBML_XMLNS_L2V3))
713 {
714 valid = false;
715 }
716 }
717 break;
718 case 4:
719 // the namespaces contains the sbml namespaces
720 // check it is the correct ns for the level/version
721 if (sbmlDeclared)
722 {
723 if (declaredURI != string(SBML_XMLNS_L2V4))
724 {
725 valid = false;
726 }
727 }
728 break;
729 case 5:
730 // the namespaces contains the sbml namespaces
731 // check it is the correct ns for the level/version
732 if (sbmlDeclared)
733 {
734 if (declaredURI != string(SBML_XMLNS_L2V5))
735 {
736 valid = false;
737 }
738 }
739 break;
740 default:
741 valid = false;
742 break;
743 }
744 break;
745 case 3:
746 switch (version)
747 {
748 case 1:
749 // the namespaces contains the sbml namespaces
750 // check it is the correct ns for the level/version
751 if (sbmlDeclared)
752 {
753 if (declaredURI != string(SBML_XMLNS_L3V1))
754 {
755 valid = false;
756 }
757 }
758 break;
759 case 2:
760 // the namespaces contains the sbml namespaces
761 // check it is the correct ns for the level/version
762 if (sbmlDeclared)
763 {
764 if (declaredURI != string(SBML_XMLNS_L3V2))
765 {
766 valid = false;
767 }
768 }
769 break;
770 default:
771 valid = false;
772 break;
773 }
774 break;
775 default:
776 valid = false;
777 break;
778 }
779
780 return valid;
781 }
782
783
784 /** @cond doxygenLibsbmlInternal */
785 void
setLevel(unsigned int level)786 SBMLNamespaces::setLevel(unsigned int level)
787 {
788 mLevel = level;
789 }
790
791
792 void
setVersion(unsigned int version)793 SBMLNamespaces::setVersion(unsigned int version)
794 {
795 mVersion = version;
796 }
797
798 const std::string&
getPackageName() const799 SBMLNamespaces::getPackageName () const
800 {
801 static const std::string pkgName = "core";
802 return pkgName;
803 }
804
805 void
setNamespaces(XMLNamespaces * xmlns)806 SBMLNamespaces::setNamespaces(XMLNamespaces * xmlns)
807 {
808 delete mNamespaces;
809 if (xmlns != NULL)
810 mNamespaces = xmlns->clone();
811 else
812 mNamespaces = NULL;
813 }
814 /** @endcond */
815
816 #endif /* __cplusplus */
817 /** @cond doxygenIgnored */
818 LIBSBML_EXTERN
819 SBMLNamespaces_t *
SBMLNamespaces_create(unsigned int level,unsigned int version)820 SBMLNamespaces_create(unsigned int level, unsigned int version)
821 {
822 return new SBMLNamespaces(level, version);
823 }
824
825
826 LIBSBML_EXTERN
827 void
SBMLNamespaces_free(SBMLNamespaces_t * ns)828 SBMLNamespaces_free(SBMLNamespaces_t* ns)
829 {
830 if (ns == NULL) return;
831 delete static_cast<SBMLNamespaces*>(ns);
832 }
833
834
835 LIBSBML_EXTERN
836 unsigned int
SBMLNamespaces_getLevel(SBMLNamespaces_t * sbmlns)837 SBMLNamespaces_getLevel(SBMLNamespaces_t *sbmlns)
838 {
839 return (sbmlns != NULL) ? sbmlns->getLevel() : SBML_INT_MAX;
840 }
841
842
843 LIBSBML_EXTERN
844 unsigned int
SBMLNamespaces_getVersion(SBMLNamespaces_t * sbmlns)845 SBMLNamespaces_getVersion(SBMLNamespaces_t *sbmlns)
846 {
847 return (sbmlns != NULL) ? sbmlns->getVersion() : SBML_INT_MAX;
848 }
849
850
851 LIBSBML_EXTERN
852 XMLNamespaces_t *
SBMLNamespaces_getNamespaces(SBMLNamespaces_t * sbmlns)853 SBMLNamespaces_getNamespaces(SBMLNamespaces_t *sbmlns)
854 {
855 return (sbmlns != NULL) ? sbmlns->getNamespaces() : NULL;
856 }
857
858
859 LIBSBML_EXTERN
860 char *
SBMLNamespaces_getSBMLNamespaceURI(unsigned int level,unsigned int version)861 SBMLNamespaces_getSBMLNamespaceURI(unsigned int level, unsigned int version)
862 {
863 return safe_strdup(SBMLNamespaces::getSBMLNamespaceURI(level, version).c_str());
864 }
865
866
867 LIBSBML_EXTERN
868 int
SBMLNamespaces_addNamespaces(SBMLNamespaces_t * sbmlns,const XMLNamespaces_t * xmlns)869 SBMLNamespaces_addNamespaces(SBMLNamespaces_t *sbmlns,
870 const XMLNamespaces_t * xmlns)
871 {
872 if (sbmlns != NULL)
873 return sbmlns->addNamespaces(xmlns);
874 else
875 return LIBSBML_INVALID_OBJECT;
876 }
877
878 LIBSBML_EXTERN
879 SBMLNamespaces_t **
SBMLNamespaces_getSupportedNamespaces(int * length)880 SBMLNamespaces_getSupportedNamespaces(int *length)
881 {
882 if (length == NULL) return NULL;
883 const List* supported = SBMLNamespaces::getSupportedNamespaces();
884
885 *length = (int) supported->getSize();
886 SBMLNamespaces_t ** result = (SBMLNamespaces_t**)safe_malloc(sizeof(SBMLNamespaces_t*)*((unsigned long)(*length)));
887 for (int i = 0; i < *length; i++)
888 {
889 result[i] = ((SBMLNamespaces*)supported->get((unsigned int)i))->clone();
890 }
891 SBMLNamespaces::freeSBMLNamespaces(const_cast<List*>(supported));
892 return result;
893 }
894 /** @endcond */
895
896 LIBSBML_CPP_NAMESPACE_END
897
898