1 /**
2 * @file SBMLValidator.cpp
3 * @brief Implementation of SBMLValidator, the base class for user callable SBML validators.
4 * @author Frank Bergmann
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/validator/SBMLValidator.h>
44 #include <sbml/SBMLDocument.h>
45 #include <sbml/SBMLError.h>
46 #include <sbml/SBMLReader.h>
47
48 #ifdef __cplusplus
49
50 #include <algorithm>
51 #include <string>
52
53 using namespace std;
54 LIBSBML_CPP_NAMESPACE_BEGIN
55
SBMLValidator()56 SBMLValidator::SBMLValidator () :
57 mDocument (NULL)
58 {
59 }
60
61
62 /*
63 * Copy constructor.
64 */
SBMLValidator(const SBMLValidator & orig)65 SBMLValidator::SBMLValidator(const SBMLValidator& orig)
66 : mDocument (orig.mDocument)
67 {
68 }
69
70
71 /*
72 * Destroy this object.
73 */
~SBMLValidator()74 SBMLValidator::~SBMLValidator ()
75 {
76
77 }
78
79
80 /*
81 * Assignment operator for SBMLConverter.
82 */
83 SBMLValidator&
operator =(const SBMLValidator & rhs)84 SBMLValidator::operator=(const SBMLValidator& rhs)
85 {
86 if(&rhs!=this)
87 {
88 mDocument = rhs.mDocument;
89 }
90
91 return *this;
92 }
93
94
95 SBMLValidator*
clone() const96 SBMLValidator::clone () const
97 {
98 return new SBMLValidator(*this);
99 }
100
101
102 SBMLDocument*
getDocument()103 SBMLValidator::getDocument()
104 {
105 return mDocument;
106 }
107
108
109 const SBMLDocument*
getDocument() const110 SBMLValidator::getDocument() const
111 {
112 return mDocument;
113 }
114
115 int
setDocument(const SBMLDocument * doc)116 SBMLValidator::setDocument(const SBMLDocument* doc)
117 {
118 if (mDocument == doc)
119 {
120 return LIBSBML_OPERATION_SUCCESS;
121 }
122
123 mDocument = const_cast<SBMLDocument *> (doc);
124 return LIBSBML_OPERATION_SUCCESS;
125 }
126
127
128
129
130 /*
131 * Clears the Validator's list of failures.
132 *
133 * If you are validating multiple SBML documents with the same Validator,
134 * call this method after you have processed the list of failures from the
135 * last Validation run and before validating the next document.
136 */
137 void
clearFailures()138 SBMLValidator::clearFailures ()
139 {
140 mFailures.clear();
141 }
142
143
144 /*
145 * @return a list of failures logged during validation.
146 */
147 const std::vector<SBMLError>&
getFailures() const148 SBMLValidator::getFailures () const
149 {
150 return mFailures;
151 }
152
153
154
155 /*
156 * Adds the given failure to this list of Validators failures.
157 */
158 void
logFailure(const SBMLError & msg)159 SBMLValidator::logFailure (const SBMLError& msg)
160 {
161 mFailures.push_back(msg);
162 }
163
164
165 /*
166 * Validates the given SBMLDocument. Failures logged during
167 * validation may be retrieved via <code>getFailures()</code>.
168 *
169 * @return the number of validation errors that occurred.
170 */
171 unsigned int
validate(const std::string & filename)172 SBMLValidator::validate (const std::string& filename)
173 {
174 SBMLReader reader;
175 SBMLDocument* d = reader.readSBML(filename);
176
177
178 for (unsigned int n = 0; n < d->getNumErrors(); ++n)
179 {
180 logFailure( *d->getError(n) );
181 }
182
183 unsigned int ret = validate(*d);
184 delete d;
185 return ret;
186 }
187
188
189 unsigned int
validate(const SBMLDocument & d)190 SBMLValidator::validate(const SBMLDocument& d)
191 {
192 setDocument(&d);
193 return validate();
194 }
195
196
197 /*
198 * @return the SBMLErrorLog used to log errors while reading and
199 * validating SBML.
200 */
201 SBMLErrorLog*
getErrorLog()202 SBMLValidator::getErrorLog ()
203 {
204 if (mDocument == NULL) return NULL;
205 return mDocument->getErrorLog();
206 }
207
208 /*
209 * @return the Model contained in this SBMLDocument.
210 */
211 const Model*
getModel() const212 SBMLValidator::getModel () const
213 {
214 if (mDocument == NULL) return NULL;
215 return mDocument->getModel();
216 }
217
218
219 /*
220 * @return the Model contained in this SBMLDocument.
221 */
222 Model*
getModel()223 SBMLValidator::getModel ()
224 {
225 if (mDocument == NULL) return NULL;
226 return mDocument->getModel();
227 }
228
229 unsigned int
validate()230 SBMLValidator::validate()
231 {
232 return 0;
233 }
234
235
236 SBMLError*
getFailure(unsigned int n) const237 SBMLValidator::getFailure (unsigned int n) const
238 {
239 return (n < mFailures.size()) ? mFailures[n].clone() : NULL;
240 }
241
242 unsigned int
getNumFailures() const243 SBMLValidator::getNumFailures() const
244 {
245 return (unsigned int) mFailures.size();
246 }
247
248 /** @cond doxygenIgnored */
249 /** @endcond */
250
251 LIBSBML_CPP_NAMESPACE_END
252
253 #endif /* __cplusplus */
254
255
256