Example of creating a RELAX NG (RNG) validator to be called during validation.
#include <iostream>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/relaxng.h>
#include <stdarg.h>
using namespace std;
LIBSBML_CPP_NAMESPACE_USE
std::vector<std::string> warnings;
std::vector<std::string> errors;
void logWarning(void *ctx, const char *msg, ...)
{
char buffer[1000];
va_list args;
va_start (args, msg);
vsprintf (buffer,msg, args);
va_end (args);
warnings.push_back(buffer);
}
void logError(
void *ctx,
const char *msg, ...)
{
char buffer[1000];
va_list args;
va_start (args, msg);
vsprintf (buffer,msg, args);
va_end (args);
errors.push_back(buffer);
}
{
private:
std::string mSchema;
public:
void setSchema(const std::string& schemaFile)
{
mSchema =schemaFile;
}
const std::string& getSchema() const { return mSchema; }
RNGValidator(
const std::string& schema) :
SBMLValidator(), mSchema(schema) {}
RNGValidator(
const RNGValidator& orig) :
SBMLValidator(orig), mSchema(orig.mSchema) {
}
virtual ~RNGValidator() {}
virtual SBMLValidator* clone()
const {
return new RNGValidator(*
this); }
virtual unsigned int validate() {
if (getDocument() == NULL || getModel() == NULL)
return 0;
if (mSchema.empty())
return 0;
if (getModel()->getNumReactions() == 0 && getModel()->getNumRules() == 0)
return 0;
warnings.clear();
errors.clear();
xmlDoc *doc;
xmlRelaxNGPtr schema;
xmlRelaxNGValidCtxtPtr validctxt;
xmlRelaxNGParserCtxtPtr rngparser;
doc = xmlParseMemory(sbmlDoc.c_str(), (int)(sbmlDoc.length()));
rngparser = xmlRelaxNGNewParserCtxt(mSchema.c_str());
schema = xmlRelaxNGParse(rngparser);
validctxt = xmlRelaxNGNewValidCtxt(schema);
xmlRelaxNGSetParserErrors (rngparser,
&logWarning,
(void *) getErrorLog());
xmlRelaxNGSetValidErrors (validctxt,
&logWarning,
(void *) getErrorLog());
xmlRelaxNGValidateDoc(validctxt, doc);
xmlRelaxNGFree(schema);
xmlRelaxNGFreeValidCtxt(validctxt);
xmlRelaxNGFreeParserCtxt(rngparser);
xmlFreeDoc(doc);
unsigned int numErrors = 0;
for (size_t i = 0; i < warnings.size(); ++i)
{
warnings[i],
0, 0,
));
}
for (size_t i = 0; i < errors.size(); ++i)
{
errors[i],
0, 0,
));
numErrors ++;
}
return numErrors;
}
};
int
main (int argc, char *argv[])
{
if (argc < 2)
{
cout << endl << "Usage: rngvalidator filename [schema]" << endl << endl;
return 1;
}
const char* filename = argv[1];
const char* schema = argc > 2 ? argv[2] : NULL;
if (schema != NULL)
document->addValidator(new RNGValidator(schema));
unsigned long long start, stop;
start = getCurrentMillis();
stop = getCurrentMillis();
cout << " validation time (ms): " << stop - start << endl<< endl;
return numErrors;
}
static void logError(XMLInputStream *stream, const XMLToken &element, SBMLErrorCode_t code, const std::string &msg="")
logs the given erroron the error log of the stream.
Definition: RDFAnnotationParser.cpp:71
Represents SBML errors and other diagnostics.
@ LIBSBML_CAT_SBML
Definition: SBMLError.h:940
SBMLDocument_t * readSBML(const char *filename)
Reads an SBML document from the given file.
Include all SBML types in a single header file.
Definition of SBMLValidator, the base class for user callable SBML validators.
char * writeSBMLToString(const SBMLDocument_t *d)
Writes the given SBML document d to an in-memory string and returns a pointer to it.
@ LIBSBML_SEV_WARNING
Definition: XMLError.h:529
@ LIBSBML_SEV_ERROR
Definition: XMLError.h:534
Definition: SBMLDocument.h:351
unsigned int checkConsistency()
Performs consistency checking and validation on this SBML document.
Definition: SBMLDocument.cpp:699
void printErrors(std::ostream &stream=std::cerr) const
Prints all the errors or warnings encountered trying to parse, check, or translate this SBML document...
Definition: SBMLDocument.cpp:1192
Definition: SBMLError.h:1100
Definition: SBMLValidator.h:96