1 /*
2 * ****************************************************************************
3 * This file is part of libNUML. Please visit http://code.google.com/p/numl/for more
4 * information about NUML, and the latest version of libNUML.
5 * Copyright (c) 2013 The University of Manchester.
6 *
7 * This library is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation. A copy of the license agreement is
10 * provided in the file named "LICENSE.txt" included with this software
11 * distribution and also available online as http://www.gnu.org/licenses/lgpl.html
12 *
13 * Contributors:
14 * Joseph O. Dada, The University of Manchester - initial API and implementation
15 * ****************************************************************************
16 **/
17
18 #include <numl/NUMLNamespaces.h>
19
20 using namespace std;
21
22 LIBNUML_CPP_NAMESPACE_BEGIN
23
NUMLNamespaces(unsigned int level,unsigned int version)24 NUMLNamespaces::NUMLNamespaces(unsigned int level, unsigned int version)
25 {
26 mLevel = level;
27 mVersion = version;
28 mNamespaces = new LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces();
29
30 mNamespaces->add(NUML_XMLNS_L1);
31 }
32
~NUMLNamespaces()33 NUMLNamespaces::~NUMLNamespaces()
34 {
35 if (mNamespaces)
36 delete mNamespaces;
37 }
38
39
40 /*
41 * Copy constructor; creates a copy of a NUMLNamespaces.
42 */
NUMLNamespaces(const NUMLNamespaces & orig)43 NUMLNamespaces::NUMLNamespaces(const NUMLNamespaces& orig) :
44 mLevel (orig.mLevel)
45 , mVersion (orig.mVersion)
46 {
47 if(orig.mNamespaces)
48 this->mNamespaces =
49 new LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces(*const_cast<NUMLNamespaces&>(orig).mNamespaces);
50 else
51 this->mNamespaces = 0;
52 }
53
54
55
56 /*
57 * Assignment operator for NUMLNamespaces.
58 */
59 NUMLNamespaces&
operator =(const NUMLNamespaces & orig)60 NUMLNamespaces::operator=(const NUMLNamespaces& orig)
61 {
62 if (&orig != this)
63 {
64 mLevel = orig.mLevel;
65 mVersion = orig.mVersion;
66 delete this->mNamespaces;
67 if(orig.mNamespaces)
68 this->mNamespaces =
69 new LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces(*const_cast<NUMLNamespaces&>(orig).mNamespaces);
70 else
71 this->mNamespaces = 0;
72 }
73
74 return *this;
75 }
76
77
78
79 /*
80 * Creates and returns a deep copy of this NUMLNamespaces.
81 */
82 NUMLNamespaces *
clone() const83 NUMLNamespaces::clone () const
84 {
85 return new NUMLNamespaces(*this);
86 }
87
88
89 std::string
getNUMLNamespaceURI(unsigned int level,unsigned int version)90 NUMLNamespaces::getNUMLNamespaceURI(unsigned int level,
91 unsigned int version)
92 {
93 return NUML_XMLNS_L1;
94 }
95
96
97 unsigned int
getLevel()98 NUMLNamespaces::getLevel()
99 {
100 return mLevel;
101 }
102
103
104 unsigned int
getVersion()105 NUMLNamespaces::getVersion()
106 {
107 return mVersion;
108 }
109
110
111 LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces *
getNamespaces()112 NUMLNamespaces::getNamespaces()
113 {
114 return mNamespaces;
115 }
116
117 void
addNamespaces(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces * xmlns)118 NUMLNamespaces::addNamespaces(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces * xmlns)
119 {
120 if (xmlns == NULL)
121 return;
122
123 /* check whether the namespace already exists
124 * add if it does not
125 */
126 for (int i = 0; i < xmlns->getLength(); i++)
127 {
128 if (!(mNamespaces->hasNS(xmlns->getURI(i), xmlns->getPrefix(i))))
129 {
130 mNamespaces->add(xmlns->getURI(i), xmlns->getPrefix(i));
131 }
132 }
133 }
134
135 void
setLevel(unsigned int level)136 NUMLNamespaces::setLevel(unsigned int level)
137 {
138 mLevel = level;
139 }
140
141
142 void
setVersion(unsigned int version)143 NUMLNamespaces::setVersion(unsigned int version)
144 {
145 mVersion = version;
146 }
147
148
149 void
setNamespaces(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces * xmlns)150 NUMLNamespaces::setNamespaces(LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces * xmlns)
151 {
152 delete mNamespaces;
153 if (xmlns)
154 mNamespaces = xmlns->clone();
155 else
156 mNamespaces = NULL;
157 }
158
159 /**
160 * Creates a new NUMLNamespaces_t structure corresponding to the given NUML
161 * @p level and @p version.
162 *
163 * NUMLNamespaces objects are used in libNUML to communicate NUML Level
164 * and Version data between constructors and other methods. The
165 * NUMLNamespaces object class tracks 3-tuples (triples) consisting of
166 * NUML Level, Version, and the corresponding NUML XML namespace. Most
167 * constructors for NUML objects in libNUML take a NUMLNamespaces object
168 * as an argument, thereby allowing the constructor to produce the proper
169 * combination of attributes and other internal data structures for the
170 * given NUML Level and Version.
171 *
172 * The plural name "NUMLNamespaces" is not a mistake, because in NUML
173 * Level 3, objects may have extensions added by Level 3
174 * packages used by a given model; however, until the introduction of
175 * NUML Level 3, the NUMLNamespaces object only records one NUML
176 * Level/Version/namespace combination at a time.
177 *
178 * @param level the NUML level
179 * @param version the NUML version
180 *
181 * @return NUMLNamespaces_t structure created
182 *
183 * @docnote The native C++ implementation of this method defines a
184 * default argument value. In the documentation generated for different
185 * libNUML language bindings, you may or may not see corresponding
186 * arguments in the method declarations. For example, in Java, a default
187 * argument is handled by declaring two separate methods, with one of
188 * them having the argument and the other one lacking the argument.
189 * However, the libNUML documentation will be @em identical for both
190 * methods. Consequently, if you are reading this and do not see an
191 * argument even though one is described, please look for descriptions of
192 * other variants of this method near where this one appears in the
193 * documentation.
194 */
195
196 LIBNUML_EXTERN
197 NUMLNamespaces_t *
NUMLNamespaces_create(unsigned int level,unsigned int version)198 NUMLNamespaces_create(unsigned int level, unsigned int version)
199 {
200 return new NUMLNamespaces(level, version);
201 }
202
203
204 /**
205 * Get the NUML Level of this NUMLNamespaces_t structure.
206 *
207 * @param numlns the NUMLNamespaces_t structure to query
208 *
209 * @return the NUML Level of this NUMLNamespaces_t structure.
210 */
211 LIBNUML_EXTERN
212 unsigned int
NUMLNamespaces_getLevel(NUMLNamespaces_t * numlns)213 NUMLNamespaces_getLevel(NUMLNamespaces_t *numlns)
214 {
215 return numlns->getLevel();
216 }
217
218
219 /**
220 * Get the NUML Version of this NUMLNamespaces_t structure.
221 *
222 * @param numlns the NUMLNamespaces_t structure to query
223 *
224 * @return the NUML Version of this NUMLNamespaces_t structure.
225 */
226 LIBNUML_EXTERN
227 unsigned int
NUMLNamespaces_getVersion(NUMLNamespaces_t * numlns)228 NUMLNamespaces_getVersion(NUMLNamespaces_t *numlns)
229 {
230 return numlns->getVersion();
231 }
232
233
234 /**
235 * Get the NUML Version of this NUMLNamespaces_t structure.
236 *
237 * @param numlns the NUMLNamespaces_t structure to query
238 *
239 * @return the XMLNamespaces_t structure of this NUMLNamespaces_t structure.
240 */
241 LIBNUML_EXTERN
242 LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces_t *
NUMLNamespaces_getNamespaces(NUMLNamespaces_t * numlns)243 NUMLNamespaces_getNamespaces(NUMLNamespaces_t *numlns)
244 {
245 return numlns->getNamespaces();
246 }
247
248
249 /**
250 * Returns a string representing the NUML XML namespace for the
251 * given @p level and @p version of NUML.
252 *
253 * @param level the NUML level
254 * @param version the NUML version
255 *
256 * @return a string representing the NUML namespace that reflects the
257 * NUML Level and Version specified.
258 */
259 LIBNUML_EXTERN
260 const char *
NUMLNamespaces_getNUMLNamespaceURI(unsigned int level,unsigned int version)261 NUMLNamespaces_getNUMLNamespaceURI(unsigned int level, unsigned int version)
262 {
263 return NUMLNamespaces::getNUMLNamespaceURI(level, version).c_str();
264 }
265
266
267 /**
268 * Add the XML namespaces list to the set of namespaces
269 * within this NUMLNamespaces_t structure.
270 *
271 * @param numlns the NUMLNamespaces_t structure to add to
272 * @param xmlns the XML namespaces to be added.
273 */
274 LIBNUML_EXTERN
275 void
NUMLNamespaces_addNamespaces(NUMLNamespaces_t * numlns,LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces_t * xmlns)276 NUMLNamespaces_addNamespaces(NUMLNamespaces_t *numlns,
277 LIBSBML_CPP_NAMESPACE_QUALIFIER XMLNamespaces_t * xmlns)
278 {
279 numlns->addNamespaces(xmlns);
280 }
281
282
283 LIBNUML_CPP_NAMESPACE_END
284
285