1 /**
2  * @file    ColorDefinition.cpp
3  * @brief Implementation of the ColorDefinition class.
4  * @author  Ralph Gauges
5  * @author  Frank T. Bergmann
6  *
7  * <!--------------------------------------------------------------------------
8  * This file is part of libSBML.  Please visit http://sbml.org for more
9  * information about SBML, and the latest version of libSBML.
10  *
11  * Copyright (C) 2020 jointly by the following organizations:
12  *     1. California Institute of Technology, Pasadena, CA, USA
13  *     2. University of Heidelberg, Heidelberg, Germany
14  *     3. University College London, London, UK
15  *
16  * Copyright (C) 2019 jointly by the following organizations:
17  *     1. California Institute of Technology, Pasadena, CA, USA
18  *     2. University of Heidelberg, Heidelberg, Germany
19  *
20  * Copyright (C) 2013-2018 jointly by the following organizations:
21  *     1. California Institute of Technology, Pasadena, CA, USA
22  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
23  *     3. University of Heidelberg, Heidelberg, Germany
24  *
25  * Copyright (C) 2011-2013 jointly by the following organizations:
26  *     1. California Institute of Technology, Pasadena, CA, USA
27  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
28  *
29  * Copyright 2010 Ralph Gauges
30  *     Group for the modeling of biological processes
31  *     University of Heidelberg
32  *     Im Neuenheimer Feld 267
33  *     69120 Heidelberg
34  *     Germany
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the GNU Lesser General Public License as published by the
38  * Free Software Foundation. A copy of the license agreement is provided in the
39  * file named "LICENSE.txt" included with this software distribution and also
40  * available online as http://sbml.org/software/libsbml/license.html
41  * ------------------------------------------------------------------------ -->
42  */
43 #include <sstream>
44 #include <iomanip>
45 #include <algorithm>
46 #include <functional>
47 #include <string.h>
48 #ifndef OMIT_DEPRECATED
49 #ifdef DEPRECATION_WARNINGS
50 #include <iostream>
51 #endif // DEPRECATION_WARNINGS
52 #endif // OMIT_DEPRECATED
53 #include "sbml/packages/layout/util/LayoutUtilities.h"
54 #include "sbml/xml/XMLInputStream.h"
55 #include <sbml/packages/render/extension/RenderExtension.h>
56 
57 #include <assert.h>
58 
59 #include <sbml/packages/render/sbml/ColorDefinition.h>
60 #include <sbml/packages/render/sbml/ListOfColorDefinitions.h>
61 #include <sbml/packages/render/validator/RenderSBMLError.h>
62 
63 
64 using namespace std;
65 
66 LIBSBML_CPP_NAMESPACE_BEGIN
67 
68 
69 
70 
71 #ifdef __cplusplus
72 
73 
74 /*
75  * Creates a new ColorDefinition using the given SBML Level, Version and
76  * &ldquo;render&rdquo; package version.
77  */
ColorDefinition(unsigned int level,unsigned int version,unsigned int pkgVersion)78 ColorDefinition::ColorDefinition(unsigned int level,
79                                  unsigned int version,
80                                  unsigned int pkgVersion)
81   : SBase(level, version)
82     ,mRed(0)
83     ,mGreen(0)
84     ,mBlue(0)
85     ,mAlpha(255)
86   , mValue ("")
87 {
88   mValue = createValueString();
89   setSBMLNamespacesAndOwn(new RenderPkgNamespaces(level, version, pkgVersion));
90 }
91 
92 
93 /*
94  * Creates a new ColorDefinition using the given RenderPkgNamespaces object.
95  */
ColorDefinition(RenderPkgNamespaces * renderns)96 ColorDefinition::ColorDefinition(RenderPkgNamespaces *renderns)
97    : SBase(renderns)
98     ,mRed(0)
99     ,mGreen(0)
100     ,mBlue(0)
101     ,mAlpha(255)
102   , mValue ("")
103 {
104   mValue = createValueString();
105   setElementNamespace(renderns->getURI());
106   loadPlugins(renderns);
107  }
108 
109 
110 /*
111  * Creates a new ColorDefinition object from the given XMLNode object.
112  * The XMLNode object has to contain a valid XML representation of a
113  * ColorDefinition object as defined in the render extension specification.
114  * This method is normally called when render information is read from a file and
115  * should normally not have to be called explicitly.
116  *
117  * @param node the XMLNode object reference that describes the ColorDefinition
118  * object to be instantiated.
119  */
ColorDefinition(const XMLNode & node,unsigned int l2version)120 ColorDefinition::ColorDefinition(const XMLNode& node, unsigned int l2version)
121   : SBase(2, l2version)
122 {
123     const XMLAttributes& attributes=node.getAttributes();
124     const XMLNode* child;
125     ExpectedAttributes ea;
126     addExpectedAttributes(ea);
127     this->readAttributes(attributes, ea);
128     unsigned int n=0,nMax = node.getNumChildren();
129     while(n<nMax)
130     {
131         child=&node.getChild(n);
132         const std::string& childName=child->getName();
133         if(childName=="annotation")
134         {
135             this->mAnnotation=new XMLNode(*child);
136         }
137         else if(childName=="notes")
138         {
139             this->mNotes=new XMLNode(*child);
140         }
141         ++n;
142     }
143 
144   setSBMLNamespacesAndOwn(new RenderPkgNamespaces(2,l2version));
145 
146   connectToChild();
147 
148 }
149 /** @endcond */
150 
151 #ifndef OMIT_DEPRECATED
152 /** @cond doxygenLibsbmlInternal */
153 /*
154  * Constructor which sets the ColorDefinition to completely opaque
155  * black and sets the id to the given string.
156  *
157  * @param id the id of the color definition. The user has to make sure
158  * that the id is unique within the given set of color definitions.
159  *
160  * This constructor is deprecated. The new libsbml API only has
161  * constructors which take the SBML level and version or one that takes
162  * an SBMLNamespaces object.
163  */
ColorDefinition(RenderPkgNamespaces * renderns,const std::string & id)164 ColorDefinition::ColorDefinition(RenderPkgNamespaces* renderns, const std::string& id)
165    :SBase(renderns)
166 //    ,mId(id)
167   , mRed(0)
168   , mGreen(0)
169   , mBlue(0)
170   , mAlpha(255)
171   , mValue("")
172 {
173   mValue = createValueString();
174   setId(id);
175 
176   // set the element namespace of this object
177   setElementNamespace(renderns->getURI());
178 
179   // connect child elements to this element.
180   connectToChild();
181 
182   // load package extensions bound with this object (if any)
183   loadPlugins(renderns);
184 
185 #ifdef DEPRECATION_WARNINGS
186     std::cerr << "Warning. ColorDefinition::ColorDefinition(const std::string& id) is deprecated." << std::endl;
187 #endif // DEPRECATION_WARNINGS
188 }
189 /** @endcond */
190 #endif // OMIT_DEPRECATED
191 
192 #ifndef OMIT_DEPRECATED
193 /** @cond doxygenLibsbmlInternal */
194 /*
195  * Constructor which sets the ColorDefinition to the given RGBA values
196  * and sets the id.
197  *
198  * @param id the id of the color definition. The user has to make sure
199  * that the id is unique within the given set of color definitions.
200  * @param r Red component value. Has to be in the range of 0 to 255.
201  * @param g Green component value. Has to be in the range of 0 to 255.
202  * @param b Blue component value. Has to be in the range of 0 to 255.
203  * @param a Alpha component value. Has to be in the range of 0 to 255.
204  * The alpha component can be omitted. In that case it has a default value of 255.
205  *
206  * This constructor is deprecated. The new libsbml API only has
207  * constructors which take the SBML level and version or one that takes
208  * an SBMLNamespaces object.
209  */
ColorDefinition(RenderPkgNamespaces * renderns,const std::string & id,unsigned char r,unsigned char g,unsigned char b,unsigned char a)210 ColorDefinition::ColorDefinition(RenderPkgNamespaces* renderns, const std::string& id,unsigned char r,unsigned char g,unsigned char b,unsigned char a)
211    :SBase(renderns)
212 //    ,mId(id)
213     ,mRed(r)
214     ,mGreen(g)
215     ,mBlue(b)
216     ,mAlpha(a)
217   , mValue("")
218 {
219   mValue = createValueString();
220   setId(id);
221 
222   // set the element namespace of this object
223   setElementNamespace(renderns->getURI());
224 
225   // connect child elements to this element.
226   connectToChild();
227 
228   // load package extensions bound with this object (if any)
229   loadPlugins(renderns);
230 
231 #ifdef DEPRECATION_WARNINGS
232     std::cerr << "Warning. ColorDefinition::ColorDefinition(const std::string& id,unsigned char r,unsigned char g,unsigned char b,unsigned char a) is deprecated." << std::endl;
233 #endif // DEPRECATION_WARNINGS
234 }
235 /** @endcond */
236 #endif // OMIT_DEPRECATED
237 
238 
239 #ifndef OMIT_DEPRECATED
240 /** @cond doxygenLibsbmlInternal */
241 /*
242  * Constructor which sets the ColorDefinition to the given RGBA values.
243  *
244  * @param r Red component value. Has to be in the range of 0 to 255.
245  * @param g Green component value. Has to be in the range of 0 to 255.
246  * @param b Blue component value. Has to be in the range of 0 to 255.
247  * @param a Alpha component value. Has to be in the range of 0 to 255.
248  * The alpha component can be omitted. In that case it has a default value of 255.
249  *
250  * This constructor is deprecated. The new libsbml API only has
251  * constructors which take the SBML level and version or one that takes
252  * an SBMLNamespaces object.
253  */
ColorDefinition(RenderPkgNamespaces * renderns,unsigned char r,unsigned char g,unsigned char b,unsigned char a)254 ColorDefinition::ColorDefinition(RenderPkgNamespaces* renderns, unsigned char r,unsigned char g,unsigned char b,unsigned char a)
255    :SBase(renderns)
256 //    ,mId()
257     ,mRed(r)
258     ,mGreen(g)
259     ,mBlue(b)
260     ,mAlpha(a)
261   , mValue("")
262 {
263   mValue = createValueString();
264 
265   // set the element namespace of this object
266   setElementNamespace(renderns->getURI());
267 
268   // connect child elements to this element.
269   connectToChild();
270 
271   // load package extensions bound with this object (if any)
272   loadPlugins(renderns);
273 
274 #ifdef DEPRECATION_WARNINGS
275     std::cerr << "Warning. ColorDefinition::ColorDefinition(unsigned char r,unsigned char g,unsigned char b,unsigned char a) is deprecated." << std::endl;
276 #endif // DEPRECATION_WARNINGS
277 }
278 /** @endcond */
279 #endif // OMIT_DEPRECATED
280 
281 /*
282  * Copy constructor for ColorDefinition.
283  */
ColorDefinition(const ColorDefinition & orig)284 ColorDefinition::ColorDefinition(const ColorDefinition& orig)
285   : SBase( orig )
286   , mRed(orig.mRed)
287   , mGreen(orig.mGreen)
288   , mBlue(orig.mBlue)
289   , mAlpha(orig.mAlpha)
290   , mValue(orig.mValue)
291 {
292 }
293 
294 
295 /*
296  * Assignment operator for ColorDefinition.
297  */
298 ColorDefinition&
operator =(const ColorDefinition & rhs)299 ColorDefinition::operator=(const ColorDefinition& rhs)
300 {
301   if (&rhs != this)
302   {
303     SBase::operator=(rhs);
304     mValue = rhs.mValue;
305     mRed = rhs.mRed;
306     mGreen = rhs.mGreen;
307     mBlue = rhs.mBlue;
308     mAlpha = rhs.mAlpha;
309   }
310 
311   return *this;
312 }
313 
314 
315 /*
316  * Creates and returns a deep copy of this ColorDefinition object.
317  */
318 ColorDefinition*
clone() const319 ColorDefinition::clone() const
320 {
321   return new ColorDefinition(*this);
322 }
323 
324 
325 /*
326  * Destructor for ColorDefinition.
327  */
~ColorDefinition()328 ColorDefinition::~ColorDefinition()
329 {
330 }
331 
332 
333 /*
334  * Returns the value of the "id" attribute of this ColorDefinition.
335  */
336 const std::string&
getId() const337 ColorDefinition::getId() const
338 {
339   return mId;
340 }
341 
342 
343 /*
344  * Returns the value of the "name" attribute of this ColorDefinition.
345  */
346 const std::string&
getName() const347 ColorDefinition::getName() const
348 {
349   return mName;
350 }
351 
352 
353 /*
354  * Returns the value of the "value" attribute of this ColorDefinition.
355  */
356 const std::string&
getValue() const357 ColorDefinition::getValue() const
358 {
359   return mValue;
360 }
361 
362 
363 /*
364  * Returns the red color component.
365  *
366  * @return the red color component for the ColorDefinition.
367  */
getRed() const368 unsigned char ColorDefinition::getRed() const
369 {
370     return mRed;
371 }
372 
373 
374 /*
375  * Returns the green color component.
376  *
377  * @return the green color component for the ColorDefinition.
378  */
getGreen() const379 unsigned char ColorDefinition::getGreen() const
380 {
381     return mGreen;
382 }
383 
384 
385 /*
386  * Returns the blue color component.
387  *
388  * @return the blue color component for the ColorDefinition.
389  */
getBlue() const390 unsigned char ColorDefinition::getBlue() const
391 {
392     return mBlue;
393 }
394 
395 
396 /*
397  * Returns the alpha color component.
398  *
399  * @return the alpha color component for the ColorDefinition.
400  */
getAlpha() const401 unsigned char ColorDefinition::getAlpha() const
402 {
403     return mAlpha;
404 }
405 
406 /*
407  * Predicate returning @c true if this ColorDefinition's "id" attribute is set.
408  */
409 bool
isSetId() const410 ColorDefinition::isSetId() const
411 {
412   return (mId.empty() == false);
413 }
414 
415 
416 /*
417  * Predicate returning @c true if this ColorDefinition's "name" attribute is
418  * set.
419  */
420 bool
isSetName() const421 ColorDefinition::isSetName() const
422 {
423   return (mName.empty() == false);
424 }
425 
426 
427 /*
428  * Predicate returning @c true if this ColorDefinition's "value" attribute is
429  * set.
430  */
431 bool
isSetValue() const432 ColorDefinition::isSetValue() const
433 {
434   return (mValue.empty() == false);
435 }
436 
437 
438 /*
439  * Sets the value of the "id" attribute of this ColorDefinition.
440  */
441 int
setId(const std::string & id)442 ColorDefinition::setId(const std::string& id)
443 {
444   return SyntaxChecker::checkAndSetSId(id, mId);
445 }
446 
447 
448 /*
449  * Sets the value of the "name" attribute of this ColorDefinition.
450  */
451 int
setName(const std::string & name)452 ColorDefinition::setName(const std::string& name)
453 {
454   mName = name;
455   return LIBSBML_OPERATION_SUCCESS;
456 }
457 
458 
459 /*
460  * Sets the value of the "value" attribute of this ColorDefinition.
461  */
462 int
setValue(const std::string & value)463 ColorDefinition::setValue(const std::string& value)
464 {
465   mValue = value;
466   setColorValue(value);
467   return LIBSBML_OPERATION_SUCCESS;
468 }
469 
470 
471 /*
472  * Sets the red color component.
473  *
474  * @param c the new red component value for the color definition.
475  */
setRed(unsigned char c)476 void ColorDefinition::setRed(unsigned char c)
477 {
478     this->mRed=c;
479     mValue = createValueString();
480 }
481 
482 
483 /*
484  * Sets the green color component.
485  *
486  * @param c the new green component value for the color definition.
487  */
setGreen(unsigned char c)488 void ColorDefinition::setGreen(unsigned char c)
489 {
490     this->mGreen=c;
491     mValue = createValueString();
492 }
493 
494 
495 /*
496  * Sets the blue color component.
497  *
498  * @param c the new blue component value for the color definition.
499  */
setBlue(unsigned char c)500 void ColorDefinition::setBlue(unsigned char c)
501 {
502     this->mBlue=c;
503     mValue = createValueString();
504 }
505 
506 
507 /*
508  * Sets alpha red color component.
509  *
510  * @param c the new alpha component value for the color definition.
511  */
setAlpha(unsigned char c)512 void ColorDefinition::setAlpha(unsigned char c)
513 {
514     this->mAlpha=c;
515     mValue = createValueString();
516 }
517 
518 
519 /*
520  * Sets the red green, blue and alpha color component.
521  * The alpha value is optional and defaults to 255 if not given.
522  * @param r Red component value. Has to be in the range of 0 to 255.
523  * @param g Green component value. Has to be in the range of 0 to 255.
524  * @param b Blue component value. Has to be in the range of 0 to 255.
525  * @param a Alpha component value. Has to be in the range of 0 to 255.
526  * The alpha component can be omitted. In that case it has a default value of 255.
527  */
setRGBA(unsigned char r,unsigned char g,unsigned char b,unsigned char a)528 void ColorDefinition::setRGBA(unsigned char r,unsigned char g,unsigned char b,unsigned char a)
529 {
530     this->mRed=r;
531     this->mGreen=g;
532     this->mBlue=b;
533     this->mAlpha=a;
534     mValue = createValueString();
535 }
536 
537 
538 /*
539  * Sets the color value from a given value string.
540  * If the string is not a valid value string, the color value is set to
541  * black and false is returned.
542  *
543  * @param valueString A const reference to a string that represents a valid color value,
544  * e.g. "#FFFFFFFF" for fully opaque white.
545  *
546  * @return @c true or false depending on whether setting the color value from the string
547  * was successfull.
548  */
setColorValue(const std::string & valueString)549 bool ColorDefinition::setColorValue(const std::string& valueString)
550 {
551     bool result=true;
552     size_t first_letter=valueString.find_first_not_of(std::string(" \t\r\n"));
553     if(first_letter == std::string::npos)
554     {
555         result=false;
556     }
557     else
558     {
559       std::string trimmed=valueString.substr(first_letter,valueString.find_last_not_of(" \t\r\n")-first_letter+1);
560       if(trimmed[0]=='#' && (trimmed.size() == 7 || trimmed.size() == 9) && trimmed.find_first_not_of("0123456789ABCDEFabcdef",1) == std::string::npos)
561       {
562         this->mRed=(unsigned char)strtol(trimmed.substr(1,2).c_str(),NULL,16);
563         this->mGreen=(unsigned char)strtol(trimmed.substr(3,2).c_str(),NULL,16);
564         this->mBlue=(unsigned char)strtol(trimmed.substr(5,2).c_str(),NULL,16);
565         if(trimmed.size() == 9)
566         {
567             // set the alpha value
568             this->mAlpha=(unsigned char)strtol(trimmed.substr(7,2).c_str(),NULL,16);
569         }
570         else
571         {
572             // if no alpha is given, the color is completely opaque
573             this->mAlpha=255;
574         }
575       }
576       else
577       {
578    result=false;
579       }
580     }
581     if(result==false)
582      {
583          this->mRed=0;
584          this->mGreen=0;
585          this->mBlue=0;
586          this->mAlpha=255;
587      }
588      return result;
589 }
590 
591 
592 /*
593  * Creates a string the represents the current color value.
594  *
595  * @return The string representation of the color value.
596  */
createValueString() const597 std::string ColorDefinition::createValueString() const
598 {
599    std::ostringstream os;
600    os << "#" << std::hex << std::setw(2) << std::setfill('0') <<  (unsigned int)mRed << std::hex << std::setw(2) << std::setfill('0') <<  (unsigned int)mGreen << std::hex << std::setw(2) << std::setfill('0') <<  (unsigned int)mBlue;
601    if(mAlpha!=255)
602    {
603      os  << std::hex << std::setw(2) << std::setfill('0') <<  (unsigned int)mAlpha;
604    }
605    return os.str();
606 }
607 
608 
609 /*
610  * Unsets the value of the "id" attribute of this ColorDefinition.
611  */
612 int
unsetId()613 ColorDefinition::unsetId()
614 {
615   mId.erase();
616 
617   if (mId.empty() == true)
618   {
619     return LIBSBML_OPERATION_SUCCESS;
620   }
621   else
622   {
623     return LIBSBML_OPERATION_FAILED;
624   }
625 }
626 
627 
628 /*
629  * Unsets the value of the "name" attribute of this ColorDefinition.
630  */
631 int
unsetName()632 ColorDefinition::unsetName()
633 {
634   mName.erase();
635 
636   if (mName.empty() == true)
637   {
638     return LIBSBML_OPERATION_SUCCESS;
639   }
640   else
641   {
642     return LIBSBML_OPERATION_FAILED;
643   }
644 }
645 
646 
647 /*
648  * Unsets the value of the "value" attribute of this ColorDefinition.
649  */
650 int
unsetValue()651 ColorDefinition::unsetValue()
652 {
653   mValue.erase();
654   setColorValue(mValue);
655 
656   if (mValue.empty() == true)
657   {
658     return LIBSBML_OPERATION_SUCCESS;
659   }
660   else
661   {
662     return LIBSBML_OPERATION_FAILED;
663   }
664 }
665 
666 
667 /*
668  * Returns the XML element name of this ColorDefinition object.
669  */
670 const std::string&
getElementName() const671 ColorDefinition::getElementName() const
672 {
673   static const string name = "colorDefinition";
674   return name;
675 }
676 
677 
678 /*
679  * Returns the libSBML type code for this ColorDefinition object.
680  */
681 int
getTypeCode() const682 ColorDefinition::getTypeCode() const
683 {
684   return SBML_RENDER_COLORDEFINITION;
685 }
686 
687 
688 /*
689 * Predicate returning @c true if all the required attributes but not defaults for this
690 * ColorDefinition object have been set.
691 */
692 bool
hasRequiredAttributes() const693 ColorDefinition::hasRequiredAttributes() const
694 {
695   bool allPresent = true;
696 
697   if (isSetId() == false)
698   {
699     allPresent = false;
700   }
701 
702   if (isSetValue() == false)
703   {
704     allPresent = false;
705   }
706 
707   return allPresent;
708 }
709 
710 
711 /** @cond doxygenLibsbmlInternal */
712 
713 /*
714 * Predicate returning @c true if all the required attributes but not defaults for this
715 * ColorDefinition object have been set.
716 */
717 bool
hasRequiredAttributesNoDefaults() const718 ColorDefinition::hasRequiredAttributesNoDefaults() const
719 {
720   bool allPresent = true;
721 
722   if (isSetId() == false)
723   {
724     allPresent = false;
725   }
726 
727   return allPresent;
728 }
729 
730 /** @endcond */
731 
732 
733 
734 /** @cond doxygenLibsbmlInternal */
735 
736 /*
737  * Write any contained elements
738  */
739 void
writeElements(XMLOutputStream & stream) const740 ColorDefinition::writeElements(XMLOutputStream& stream) const
741 {
742   SBase::writeElements(stream);
743 
744   SBase::writeExtensionElements(stream);
745 }
746 
747 /** @endcond */
748 
749 
750 
751 /** @cond doxygenLibsbmlInternal */
752 
753 /*
754  * Accepts the given SBMLVisitor
755  */
756 bool
accept(SBMLVisitor & v) const757 ColorDefinition::accept(SBMLVisitor& v) const
758 {
759   return v.visit(*this);
760 }
761 
762 /** @endcond */
763 
764 
765 
766 /** @cond doxygenLibsbmlInternal */
767 
768 /*
769  * Sets the parent SBMLDocument
770  */
771 void
setSBMLDocument(SBMLDocument * d)772 ColorDefinition::setSBMLDocument(SBMLDocument* d)
773 {
774   SBase::setSBMLDocument(d);
775 }
776 
777 /** @endcond */
778 
779 
780 
781 /** @cond doxygenLibsbmlInternal */
782 
783 /*
784  * Enables/disables the given package with this element
785  */
786 void
enablePackageInternal(const std::string & pkgURI,const std::string & pkgPrefix,bool flag)787 ColorDefinition::enablePackageInternal(const std::string& pkgURI,
788                                        const std::string& pkgPrefix,
789                                        bool flag)
790 {
791   SBase::enablePackageInternal(pkgURI, pkgPrefix, flag);
792 }
793 
794 /** @endcond */
795 
796 
797 
798 /** @cond doxygenLibsbmlInternal */
799 
800 /*
801  * Returns the value of the "attributeName" attribute of this ColorDefinition.
802  */
803 int
getAttribute(const std::string & attributeName,bool & value) const804 ColorDefinition::getAttribute(const std::string& attributeName,
805                               bool& value) const
806 {
807   int return_value = SBase::getAttribute(attributeName, value);
808 
809   return return_value;
810 }
811 
812 /** @endcond */
813 
814 
815 
816 /** @cond doxygenLibsbmlInternal */
817 
818 /*
819  * Returns the value of the "attributeName" attribute of this ColorDefinition.
820  */
821 int
getAttribute(const std::string & attributeName,int & value) const822 ColorDefinition::getAttribute(const std::string& attributeName,
823                               int& value) const
824 {
825   int return_value = SBase::getAttribute(attributeName, value);
826 
827   return return_value;
828 }
829 
830 /** @endcond */
831 
832 
833 
834 /** @cond doxygenLibsbmlInternal */
835 
836 /*
837  * Returns the value of the "attributeName" attribute of this ColorDefinition.
838  */
839 int
getAttribute(const std::string & attributeName,double & value) const840 ColorDefinition::getAttribute(const std::string& attributeName,
841                               double& value) const
842 {
843   int return_value = SBase::getAttribute(attributeName, value);
844 
845   return return_value;
846 }
847 
848 /** @endcond */
849 
850 
851 
852 /** @cond doxygenLibsbmlInternal */
853 
854 /*
855  * Returns the value of the "attributeName" attribute of this ColorDefinition.
856  */
857 int
getAttribute(const std::string & attributeName,unsigned int & value) const858 ColorDefinition::getAttribute(const std::string& attributeName,
859                               unsigned int& value) const
860 {
861   int return_value = SBase::getAttribute(attributeName, value);
862 
863   return return_value;
864 }
865 
866 /** @endcond */
867 
868 
869 
870 /** @cond doxygenLibsbmlInternal */
871 
872 /*
873  * Returns the value of the "attributeName" attribute of this ColorDefinition.
874  */
875 int
getAttribute(const std::string & attributeName,std::string & value) const876 ColorDefinition::getAttribute(const std::string& attributeName,
877                               std::string& value) const
878 {
879   int return_value = SBase::getAttribute(attributeName, value);
880 
881   if (return_value == LIBSBML_OPERATION_SUCCESS)
882   {
883     return return_value;
884   }
885 
886   if (attributeName == "id")
887   {
888     value = getId();
889     return_value = LIBSBML_OPERATION_SUCCESS;
890   }
891   else if (attributeName == "name")
892   {
893     value = getName();
894     return_value = LIBSBML_OPERATION_SUCCESS;
895   }
896   else if (attributeName == "value")
897   {
898     value = getValue();
899     return_value = LIBSBML_OPERATION_SUCCESS;
900   }
901 
902   return return_value;
903 }
904 
905 /** @endcond */
906 
907 
908 
909 /** @cond doxygenLibsbmlInternal */
910 
911 /*
912  * Predicate returning @c true if this ColorDefinition's attribute
913  * "attributeName" is set.
914  */
915 bool
isSetAttribute(const std::string & attributeName) const916 ColorDefinition::isSetAttribute(const std::string& attributeName) const
917 {
918   bool value = SBase::isSetAttribute(attributeName);
919 
920   if (attributeName == "id")
921   {
922     value = isSetId();
923   }
924   else if (attributeName == "name")
925   {
926     value = isSetName();
927   }
928   else if (attributeName == "value")
929   {
930     value = isSetValue();
931   }
932 
933   return value;
934 }
935 
936 /** @endcond */
937 
938 
939 
940 /** @cond doxygenLibsbmlInternal */
941 
942 /*
943  * Sets the value of the "attributeName" attribute of this ColorDefinition.
944  */
945 int
setAttribute(const std::string & attributeName,bool value)946 ColorDefinition::setAttribute(const std::string& attributeName, bool value)
947 {
948   int return_value = SBase::setAttribute(attributeName, value);
949 
950   return return_value;
951 }
952 
953 /** @endcond */
954 
955 
956 
957 /** @cond doxygenLibsbmlInternal */
958 
959 /*
960  * Sets the value of the "attributeName" attribute of this ColorDefinition.
961  */
962 int
setAttribute(const std::string & attributeName,int value)963 ColorDefinition::setAttribute(const std::string& attributeName, int value)
964 {
965   int return_value = SBase::setAttribute(attributeName, value);
966 
967   return return_value;
968 }
969 
970 /** @endcond */
971 
972 
973 
974 /** @cond doxygenLibsbmlInternal */
975 
976 /*
977  * Sets the value of the "attributeName" attribute of this ColorDefinition.
978  */
979 int
setAttribute(const std::string & attributeName,double value)980 ColorDefinition::setAttribute(const std::string& attributeName, double value)
981 {
982   int return_value = SBase::setAttribute(attributeName, value);
983 
984   return return_value;
985 }
986 
987 /** @endcond */
988 
989 
990 
991 /** @cond doxygenLibsbmlInternal */
992 
993 /*
994  * Sets the value of the "attributeName" attribute of this ColorDefinition.
995  */
996 int
setAttribute(const std::string & attributeName,unsigned int value)997 ColorDefinition::setAttribute(const std::string& attributeName,
998                               unsigned int value)
999 {
1000   int return_value = SBase::setAttribute(attributeName, value);
1001 
1002   return return_value;
1003 }
1004 
1005 /** @endcond */
1006 
1007 
1008 
1009 /** @cond doxygenLibsbmlInternal */
1010 
1011 /*
1012  * Sets the value of the "attributeName" attribute of this ColorDefinition.
1013  */
1014 int
setAttribute(const std::string & attributeName,const std::string & value)1015 ColorDefinition::setAttribute(const std::string& attributeName,
1016                               const std::string& value)
1017 {
1018   int return_value = SBase::setAttribute(attributeName, value);
1019 
1020   if (attributeName == "id")
1021   {
1022     return_value = setId(value);
1023   }
1024   else if (attributeName == "name")
1025   {
1026     return_value = setName(value);
1027   }
1028   else if (attributeName == "value")
1029   {
1030     return_value = setValue(value);
1031   }
1032 
1033   return return_value;
1034 }
1035 
1036 /** @endcond */
1037 
1038 
1039 
1040 /** @cond doxygenLibsbmlInternal */
1041 
1042 /*
1043  * Unsets the value of the "attributeName" attribute of this ColorDefinition.
1044  */
1045 int
unsetAttribute(const std::string & attributeName)1046 ColorDefinition::unsetAttribute(const std::string& attributeName)
1047 {
1048   int value = SBase::unsetAttribute(attributeName);
1049 
1050   if (attributeName == "id")
1051   {
1052     value = unsetId();
1053   }
1054   else if (attributeName == "name")
1055   {
1056     value = unsetName();
1057   }
1058   else if (attributeName == "value")
1059   {
1060     value = unsetValue();
1061   }
1062 
1063   return value;
1064 }
1065 
1066 /** @endcond */
1067 
1068 
1069 
1070 /** @cond doxygenLibsbmlInternal */
1071 /*
1072  * Creates an XMLNode object from this ListOfColorDefinitions object.
1073  *
1074  * @return the XMLNode with the XML representation for the
1075  * ColorDefinition object.
1076  */
toXML() const1077 XMLNode ColorDefinition::toXML() const
1078 {
1079   return getXmlNodeForSBase(this);
1080 }
1081 /** @endcond */
1082 
1083 
1084 
1085 /** @cond doxygenLibsbmlInternal */
1086 
1087 /*
1088  * Adds the expected attributes for this element
1089  */
1090 void
addExpectedAttributes(ExpectedAttributes & attributes)1091 ColorDefinition::addExpectedAttributes(ExpectedAttributes& attributes)
1092 {
1093   SBase::addExpectedAttributes(attributes);
1094 
1095   attributes.add("id");
1096 
1097   attributes.add("name");
1098 
1099   attributes.add("value");
1100 }
1101 
1102 /** @endcond */
1103 
1104 
1105 
1106 /** @cond doxygenLibsbmlInternal */
1107 
1108 /*
1109  * Reads the expected attributes into the member data variables
1110  */
1111 void
readAttributes(const XMLAttributes & attributes,const ExpectedAttributes & expectedAttributes)1112 ColorDefinition::readAttributes(const XMLAttributes& attributes,
1113                                 const ExpectedAttributes& expectedAttributes)
1114 {
1115   unsigned int level = getLevel();
1116   unsigned int version = getVersion();
1117   unsigned int pkgVersion = getPackageVersion();
1118   unsigned int numErrs;
1119   bool assigned = false;
1120   SBMLErrorLog* log = getErrorLog();
1121 
1122   if (log && getParentSBMLObject() &&
1123     static_cast<ListOfColorDefinitions*>(getParentSBMLObject())->size() < 2)
1124   {
1125     numErrs = log->getNumErrors();
1126     for (int n = numErrs-1; n >= 0; n--)
1127     {
1128       if (log->getError(n)->getErrorId() == UnknownPackageAttribute)
1129       {
1130         const std::string details = log->getError(n)->getMessage();
1131         log->remove(UnknownPackageAttribute);
1132         log->logPackageError("render", RenderColorDefinitionAllowedAttributes,
1133           pkgVersion, level, version, details, getLine(), getColumn());
1134       }
1135       else if (log->getError(n)->getErrorId() == UnknownCoreAttribute)
1136       {
1137         const std::string details = log->getError(n)->getMessage();
1138         log->remove(UnknownCoreAttribute);
1139         log->logPackageError("render",
1140           RenderRenderInformationBaseLOColorDefinitionsAllowedCoreAttributes,
1141             pkgVersion, level, version, details, getLine(), getColumn());
1142       }
1143     }
1144   }
1145 
1146   SBase::readAttributes(attributes, expectedAttributes);
1147 
1148   if (log)
1149   {
1150     numErrs = log->getNumErrors();
1151 
1152     for (int n = numErrs-1; n >= 0; n--)
1153     {
1154       if (log->getError(n)->getErrorId() == UnknownPackageAttribute)
1155       {
1156         const std::string details = log->getError(n)->getMessage();
1157         log->remove(UnknownPackageAttribute);
1158         log->logPackageError("render", RenderColorDefinitionAllowedAttributes,
1159           pkgVersion, level, version, details, getLine(), getColumn());
1160       }
1161       else if (log->getError(n)->getErrorId() == UnknownCoreAttribute)
1162       {
1163         const std::string details = log->getError(n)->getMessage();
1164         log->remove(UnknownCoreAttribute);
1165         log->logPackageError("render",
1166           RenderColorDefinitionAllowedCoreAttributes, pkgVersion, level, version,
1167             details, getLine(), getColumn());
1168       }
1169     }
1170   }
1171 
1172   //
1173   // id SId (use = "required" )
1174   //
1175 
1176   assigned = attributes.readInto("id", mId);
1177 
1178   if (assigned == true)
1179   {
1180     if (log && mId.empty() == true)
1181     {
1182       logEmptyString(mId, level, version, "<ColorDefinition>");
1183     }
1184     else if (log && SyntaxChecker::isValidSBMLSId(mId) == false)
1185     {
1186       log->logPackageError("render", RenderIdSyntaxRule, pkgVersion, level,
1187         version, "The id on the <" + getElementName() + "> is '" + mId + "', "
1188           "which does not conform to the syntax.", getLine(), getColumn());
1189     }
1190   }
1191   else
1192   {
1193     std::string message = "Render attribute 'id' is missing from the "
1194       "<ColorDefinition> element.";
1195     log->logPackageError("render", RenderColorDefinitionAllowedAttributes,
1196       pkgVersion, level, version, message, getLine(), getColumn());
1197   }
1198 
1199   //
1200   // name string (use = "optional" )
1201   //
1202 
1203   assigned = attributes.readInto("name", mName);
1204 
1205   if (assigned == true)
1206   {
1207     if (log && mName.empty() == true)
1208     {
1209       logEmptyString(mName, level, version, "<ColorDefinition>");
1210     }
1211   }
1212 
1213   //
1214   // value string (use = "required" )
1215   //
1216 
1217   assigned = attributes.readInto("value", mValue);
1218 
1219   if (assigned == true)
1220   {
1221     if (log && mValue.empty() == true)
1222     {
1223       logEmptyString(mValue, level, version, "<ColorDefinition>");
1224     }
1225     else
1226     {
1227       this->setColorValue(mValue);
1228     }
1229   }
1230   else
1231   {
1232     if (log)
1233     {
1234       std::string message = "Render attribute 'value' is missing from the "
1235         "<ColorDefinition> element.";
1236       log->logPackageError("render", RenderColorDefinitionAllowedAttributes,
1237         pkgVersion, level, version, message, getLine(), getColumn());
1238     }
1239   }
1240 }
1241 
1242 
1243 /** @endcond */
1244 
1245 
1246 
1247 /** @cond doxygenLibsbmlInternal */
1248 
1249 /*
1250  * Writes the attributes to the stream
1251  */
1252 void
writeAttributes(XMLOutputStream & stream) const1253 ColorDefinition::writeAttributes(XMLOutputStream& stream) const
1254 {
1255   SBase::writeAttributes(stream);
1256 
1257   if (isSetId() == true)
1258   {
1259     stream.writeAttribute("id", getPrefix(), mId);
1260   }
1261 
1262   if (isSetName() == true)
1263   {
1264     stream.writeAttribute("name", getPrefix(), mName);
1265   }
1266 
1267   if (isSetValue() == true)
1268   {
1269     stream.writeAttribute("value", getPrefix(), createValueString());
1270   }
1271 
1272   SBase::writeExtensionAttributes(stream);
1273 }
1274 
1275 /** @endcond */
1276 
1277 
1278 
1279 
1280 #endif /* __cplusplus */
1281 
1282 
1283 /*
1284  * Creates a new ColorDefinition_t using the given SBML Level, Version and
1285  * &ldquo;render&rdquo; package version.
1286  */
1287 LIBSBML_EXTERN
1288 ColorDefinition_t *
ColorDefinition_create(unsigned int level,unsigned int version,unsigned int pkgVersion)1289 ColorDefinition_create(unsigned int level,
1290                        unsigned int version,
1291                        unsigned int pkgVersion)
1292 {
1293   return new ColorDefinition(level, version, pkgVersion);
1294 }
1295 
1296 
1297 /*
1298  * Creates and returns a deep copy of this ColorDefinition_t object.
1299  */
1300 LIBSBML_EXTERN
1301 ColorDefinition_t*
ColorDefinition_clone(const ColorDefinition_t * cd)1302 ColorDefinition_clone(const ColorDefinition_t* cd)
1303 {
1304   if (cd != NULL)
1305   {
1306     return static_cast<ColorDefinition_t*>(cd->clone());
1307   }
1308   else
1309   {
1310     return NULL;
1311   }
1312 }
1313 
1314 
1315 /*
1316  * Frees this ColorDefinition_t object.
1317  */
1318 LIBSBML_EXTERN
1319 void
ColorDefinition_free(ColorDefinition_t * cd)1320 ColorDefinition_free(ColorDefinition_t* cd)
1321 {
1322   if (cd != NULL)
1323   {
1324     delete cd;
1325   }
1326 }
1327 
1328 
1329 /*
1330  * Returns the value of the "id" attribute of this ColorDefinition_t.
1331  */
1332 LIBSBML_EXTERN
1333 char *
ColorDefinition_getId(const ColorDefinition_t * cd)1334 ColorDefinition_getId(const ColorDefinition_t * cd)
1335 {
1336   if (cd == NULL)
1337   {
1338     return NULL;
1339   }
1340 
1341   return cd->getId().empty() ? NULL : safe_strdup(cd->getId().c_str());
1342 }
1343 
1344 
1345 /*
1346  * Returns the value of the "name" attribute of this ColorDefinition_t.
1347  */
1348 LIBSBML_EXTERN
1349 char *
ColorDefinition_getName(const ColorDefinition_t * cd)1350 ColorDefinition_getName(const ColorDefinition_t * cd)
1351 {
1352   if (cd == NULL)
1353   {
1354     return NULL;
1355   }
1356 
1357   return cd->getName().empty() ? NULL : safe_strdup(cd->getName().c_str());
1358 }
1359 
1360 
1361 /*
1362  * Returns the value of the "value" attribute of this ColorDefinition_t.
1363  */
1364 LIBSBML_EXTERN
1365 char *
ColorDefinition_getValue(const ColorDefinition_t * cd)1366 ColorDefinition_getValue(const ColorDefinition_t * cd)
1367 {
1368   if (cd == NULL)
1369   {
1370     return NULL;
1371   }
1372 
1373   return cd->getValue().empty() ? NULL : safe_strdup(cd->getValue().c_str());
1374 }
1375 
1376 
1377 /*
1378  * Predicate returning @c 1 (true) if this ColorDefinition_t's "id" attribute
1379  * is set.
1380  */
1381 LIBSBML_EXTERN
1382 int
ColorDefinition_isSetId(const ColorDefinition_t * cd)1383 ColorDefinition_isSetId(const ColorDefinition_t * cd)
1384 {
1385   return (cd != NULL) ? static_cast<int>(cd->isSetId()) : 0;
1386 }
1387 
1388 
1389 /*
1390  * Predicate returning @c 1 (true) if this ColorDefinition_t's "name" attribute
1391  * is set.
1392  */
1393 LIBSBML_EXTERN
1394 int
ColorDefinition_isSetName(const ColorDefinition_t * cd)1395 ColorDefinition_isSetName(const ColorDefinition_t * cd)
1396 {
1397   return (cd != NULL) ? static_cast<int>(cd->isSetName()) : 0;
1398 }
1399 
1400 
1401 /*
1402  * Predicate returning @c 1 (true) if this ColorDefinition_t's "value"
1403  * attribute is set.
1404  */
1405 LIBSBML_EXTERN
1406 int
ColorDefinition_isSetValue(const ColorDefinition_t * cd)1407 ColorDefinition_isSetValue(const ColorDefinition_t * cd)
1408 {
1409   return (cd != NULL) ? static_cast<int>(cd->isSetValue()) : 0;
1410 }
1411 
1412 
1413 /*
1414  * Sets the value of the "id" attribute of this ColorDefinition_t.
1415  */
1416 LIBSBML_EXTERN
1417 int
ColorDefinition_setId(ColorDefinition_t * cd,const char * id)1418 ColorDefinition_setId(ColorDefinition_t * cd, const char * id)
1419 {
1420   return (cd != NULL) ? cd->setId(id) : LIBSBML_INVALID_OBJECT;
1421 }
1422 
1423 
1424 /*
1425  * Sets the value of the "name" attribute of this ColorDefinition_t.
1426  */
1427 LIBSBML_EXTERN
1428 int
ColorDefinition_setName(ColorDefinition_t * cd,const char * name)1429 ColorDefinition_setName(ColorDefinition_t * cd, const char * name)
1430 {
1431   return (cd != NULL) ? cd->setName(name) : LIBSBML_INVALID_OBJECT;
1432 }
1433 
1434 
1435 /*
1436  * Sets the value of the "value" attribute of this ColorDefinition_t.
1437  */
1438 LIBSBML_EXTERN
1439 int
ColorDefinition_setValue(ColorDefinition_t * cd,const char * value)1440 ColorDefinition_setValue(ColorDefinition_t * cd, const char * value)
1441 {
1442   return (cd != NULL) ? cd->setValue(value) : LIBSBML_INVALID_OBJECT;
1443 }
1444 
1445 
1446 /*
1447  * Unsets the value of the "id" attribute of this ColorDefinition_t.
1448  */
1449 LIBSBML_EXTERN
1450 int
ColorDefinition_unsetId(ColorDefinition_t * cd)1451 ColorDefinition_unsetId(ColorDefinition_t * cd)
1452 {
1453   return (cd != NULL) ? cd->unsetId() : LIBSBML_INVALID_OBJECT;
1454 }
1455 
1456 
1457 /*
1458  * Unsets the value of the "name" attribute of this ColorDefinition_t.
1459  */
1460 LIBSBML_EXTERN
1461 int
ColorDefinition_unsetName(ColorDefinition_t * cd)1462 ColorDefinition_unsetName(ColorDefinition_t * cd)
1463 {
1464   return (cd != NULL) ? cd->unsetName() : LIBSBML_INVALID_OBJECT;
1465 }
1466 
1467 
1468 /*
1469  * Unsets the value of the "value" attribute of this ColorDefinition_t.
1470  */
1471 LIBSBML_EXTERN
1472 int
ColorDefinition_unsetValue(ColorDefinition_t * cd)1473 ColorDefinition_unsetValue(ColorDefinition_t * cd)
1474 {
1475   return (cd != NULL) ? cd->unsetValue() : LIBSBML_INVALID_OBJECT;
1476 }
1477 
1478 
1479 /*
1480  * Predicate returning @c 1 (true) if all the required attributes for this
1481  * ColorDefinition_t object have been set.
1482  */
1483 LIBSBML_EXTERN
1484 int
ColorDefinition_hasRequiredAttributes(const ColorDefinition_t * cd)1485 ColorDefinition_hasRequiredAttributes(const ColorDefinition_t * cd)
1486 {
1487   return (cd != NULL) ? static_cast<int>(cd->hasRequiredAttributes()) : 0;
1488 }
1489 
1490 
1491 LIBSBML_CPP_NAMESPACE_END
1492 
1493 
1494