1 /**
2  * @file    XMLTriple.cpp
3  * @brief   Stores an XML namespace triple.
4  * @author  Ben Bornstein
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 and
39  * also available online as http://sbml.org/software/libsbml/license.html
40  * ---------------------------------------------------------------------- -->*/
41 
42 #include <sbml/xml/XMLTriple.h>
43 #include <sbml/util/util.h>
44 #include <sbml/xml/XMLAttributes.h>
45 #include <sbml/xml/XMLConstructorException.h>
46 
47 /** @cond doxygenIgnored */
48 using namespace std;
49 /** @endcond */
50 
51 LIBSBML_CPP_NAMESPACE_BEGIN
52 #ifdef __cplusplus
53 
54 /*
55  * Creates a new empty XMLTriple.
56  */
XMLTriple()57 XMLTriple::XMLTriple ()
58 {
59 }
60 
61 
62 /*
63  * Creates a new XMLTriple.
64  */
XMLTriple(const std::string & name,const std::string & uri,const std::string & prefix)65 XMLTriple::XMLTriple (  const std::string&  name
66                       , const std::string&  uri
67                       , const std::string&  prefix )
68  : mName   ( name )
69  , mURI    ( uri  )
70  , mPrefix ( prefix )
71 {
72 }
73 
74 
75 /*
76  * Creates a new XMLTriple by splitting triplet on sepchar.  Triplet
77  * may be in one of the following formats:
78  *
79  *   name
80  *   uri sepchar name
81  *   uri sepchar name sepchar prefix
82  */
XMLTriple(const std::string & triplet,const char sepchar)83 XMLTriple::XMLTriple (const std::string& triplet, const char sepchar)
84 {
85 
86   string::size_type start = 0;
87   string::size_type pos   = triplet.find(sepchar, start);
88 
89 
90   if (pos != string::npos)
91   {
92     mURI = triplet.substr(start, pos);
93 
94     start = pos + 1;
95     pos   = triplet.find(sepchar, start);
96 
97     if (pos != string::npos)
98     {
99       mName   = triplet.substr(start, pos - start);
100       mPrefix = triplet.substr(pos + 1);
101     }
102     else
103     {
104       mName = triplet.substr(start);
105     }
106   }
107   else
108   {
109     mName = triplet;
110   }
111 }
112 
113 
114 /*
115  * Copy constructor; creates a copy of this XMLTriple set.
116  */
XMLTriple(const XMLTriple & orig)117 XMLTriple::XMLTriple(const XMLTriple& orig)
118   : mName   ( orig.mName )
119   , mURI    ( orig.mURI )
120   , mPrefix ( orig.mPrefix )
121 {
122 }
123 
124 
125 /*
126  * Assignment operator for XMLTriple.
127  */
128 XMLTriple&
operator =(const XMLTriple & rhs)129 XMLTriple::operator=(const XMLTriple& rhs)
130 {
131   if(&rhs!=this)
132   {
133     mName   = rhs.mName;
134     mURI    = rhs.mURI;
135     mPrefix = rhs.mPrefix;
136   }
137 
138   return *this;
139 }
140 
141 
~XMLTriple()142 XMLTriple::~XMLTriple()
143 {
144 }
145 
146 /*
147  * Creates and returns a deep copy of this XMLTriple set.
148  *
149  * @return a (deep) copy of this XMLTriple set.
150  */
151 XMLTriple*
clone() const152 XMLTriple::clone () const
153 {
154   return new XMLTriple(*this);
155 }
156 
157 
158 /*
159  * @return a string, the name from this XMLTriple.
160  */
161 const std::string&
getName() const162 XMLTriple::getName () const
163 {
164   return mName;
165 }
166 
167 
168 /*
169  * @return a string, the @em prefix portion of this XMLTriple.
170  */
171 const std::string&
getPrefix() const172 XMLTriple::getPrefix () const
173 {
174   return mPrefix;
175 }
176 
177 
178 /*
179  * @return URI a string, the @em prefix portion of this XMLTriple.
180  */
181 const std::string&
getURI() const182 XMLTriple::getURI () const
183 {
184   return mURI;
185 }
186 
187 
188 /*
189  * @return prefixed name from this XMLTriple.
190  */
191 const std::string
getPrefixedName() const192 XMLTriple::getPrefixedName () const
193 {
194   return mPrefix + ((mPrefix != "") ? ":" : "") + mName;
195 }
196 
197 
198 /*
199  * @return @c true if this XMLTriple set is empty, false otherwise.
200  */
201 bool
isEmpty() const202 XMLTriple::isEmpty () const
203 {
204   return ( getName().size() == 0
205         && getURI().size() == 0
206         && getPrefix().size() == 0);
207 }
208 
209 
210 /*
211  * Comparison (equal-to) operator for XMLTriple.
212  *
213  * @return @c 1 (true) if the combination of name, URI, and
214  * prefix of lhs is equal to that of rhs @c 0 (false) otherwise.
215  */
operator ==(const XMLTriple & lhs,const XMLTriple & rhs)216 bool operator==(const XMLTriple& lhs, const XMLTriple& rhs)
217 {
218   if (lhs.getName()   != rhs.getName()  ) return false;
219   if (lhs.getURI()    != rhs.getURI()   ) return false;
220   if (lhs.getPrefix() != rhs.getPrefix()) return false;
221 
222   return true;
223 }
224 
225 
226 /*
227  * Comparison (not equal-to) operator for XMLTriple.
228  *
229  * @return @c 1 (true) if the combination of name, URI, and
230  * prefix of lhs is not equal to that of rhs @c 0 (false) otherwise.
231  */
operator !=(const XMLTriple & lhs,const XMLTriple & rhs)232 bool operator!=(const XMLTriple& lhs, const XMLTriple& rhs)
233 {
234   return !(lhs == rhs);
235 }
236 
237 #endif /* __cplusplus */
238 /** @cond doxygenIgnored */
239 LIBLAX_EXTERN
240 XMLTriple_t *
XMLTriple_create(void)241 XMLTriple_create (void)
242 {
243   return new(nothrow) XMLTriple;
244 }
245 
246 
247 LIBLAX_EXTERN
248 XMLTriple_t *
XMLTriple_createWith(const char * name,const char * uri,const char * prefix)249 XMLTriple_createWith (const char *name, const char *uri, const char *prefix)
250 {
251   if (name == NULL || uri == NULL || prefix == NULL) return NULL;
252   return new(nothrow) XMLTriple(name, uri, prefix);
253 }
254 
255 
256 LIBLAX_EXTERN
257 void
XMLTriple_free(XMLTriple_t * triple)258 XMLTriple_free (XMLTriple_t *triple)
259 {
260   if (triple == NULL) return;
261   delete static_cast<XMLTriple*>( triple );
262 }
263 
264 
265 LIBLAX_EXTERN
266 XMLTriple_t *
XMLTriple_clone(const XMLTriple_t * t)267 XMLTriple_clone (const XMLTriple_t* t)
268 {
269   if (t == NULL) return NULL;
270   return static_cast<XMLTriple*>( t->clone() );
271 }
272 
273 
274 LIBLAX_EXTERN
275 const char *
XMLTriple_getName(const XMLTriple_t * triple)276 XMLTriple_getName (const XMLTriple_t *triple)
277 {
278   if (triple == NULL) return NULL;
279   return triple->getName().empty() ? NULL : triple->getName().c_str();
280 }
281 
282 
283 LIBLAX_EXTERN
284 const char *
XMLTriple_getPrefix(const XMLTriple_t * triple)285 XMLTriple_getPrefix (const XMLTriple_t *triple)
286 {
287   if (triple == NULL) return NULL;
288   return triple->getPrefix().empty() ? NULL : triple->getPrefix().c_str();
289 }
290 
291 
292 LIBLAX_EXTERN
293 const char *
XMLTriple_getURI(const XMLTriple_t * triple)294 XMLTriple_getURI (const XMLTriple_t *triple)
295 {
296   if (triple == NULL) return NULL;
297   return triple->getURI().empty() ? NULL : triple->getURI().c_str();
298 }
299 
300 
301 LIBLAX_EXTERN
302 const char *
XMLTriple_getPrefixedName(const XMLTriple_t * triple)303 XMLTriple_getPrefixedName (const XMLTriple_t *triple)
304 {
305   if (triple == NULL) return NULL;
306   return triple->getPrefixedName().empty() ? NULL : safe_strdup(triple->getPrefixedName().c_str());
307 }
308 
309 
310 LIBLAX_EXTERN
311 int
XMLTriple_isEmpty(const XMLTriple_t * triple)312 XMLTriple_isEmpty (const XMLTriple_t *triple)
313 {
314   if (triple == NULL) return (int)true;
315   return static_cast<int> (triple->isEmpty());
316 }
317 
318 
319 LIBLAX_EXTERN
320 int
XMLTriple_equalTo(const XMLTriple_t * lhs,const XMLTriple_t * rhs)321 XMLTriple_equalTo(const XMLTriple_t *lhs, const XMLTriple_t* rhs)
322 {
323   if (lhs == NULL && rhs == NULL) return (int) true;
324   if (lhs == NULL || rhs == NULL) return (int) false;
325   return (*lhs == *rhs);
326 }
327 
328 
329 LIBLAX_EXTERN
330 int
XMLTriple_notEqualTo(const XMLTriple_t * lhs,const XMLTriple_t * rhs)331 XMLTriple_notEqualTo(const XMLTriple_t *lhs, const XMLTriple_t* rhs)
332 {
333   return (int) !((bool)XMLTriple_equalTo(lhs, rhs));
334 }
335 /** @endcond */
336 
337 LIBSBML_CPP_NAMESPACE_END
338