libSBML C++ API  5.20.4
SBMLHttpResolverExample.cpp

Example of the definition and use of an SBMLHttpResolver for working with SBML “comp” files.

/**
* @file SBMLHttpResolverExample.cpp
* @brief Example of the definition of an SBMLHttpResolver, and how one would
* use it.
* @author Frank Bergmann
*
* <!--------------------------------------------------------------------------
* This sample program is distributed under a different license than the rest
* of libSBML. This program uses the open-source MIT license, as follows:
*
* Copyright (c) 2013-2018 by the California Institute of Technology
* (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
* and the University of Heidelberg (Germany), with support from the National
* Institutes of Health (USA) under grant R01GM070923. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Neither the name of the California Institute of Technology (Caltech), nor
* of the European Bioinformatics Institute (EMBL-EBI), nor of the University
* of Heidelberg, nor the names of any contributors, may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* ------------------------------------------------------------------------ -->
*
* @class SBMLHttpResolver
* @brief resolves HTTP documents, based on
* http://stackoverflow.com/questions/9786150/save-curl-content-result-into-a-string-in-c
*/
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <sbml/SBMLTypes.h>
LIBSBML_CPP_NAMESPACE_USE
using namespace std;
static size_t WriteCallback(void *contents, size_t size,
size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
class LIBSBML_EXTERN SBMLHttpResolver : public SBMLResolver
{
public:
/**
* Creates a new SBMLHttpResolver object.
*/
SBMLHttpResolver ()
{
}
/**
* Copy constructor; creates a copy of an SBMLHttpResolver object.
*
* @param c the SBMLHttpResolver object to copy.
*
* @throws @if python ValueError @else SBMLConstructorException @endif@~
* Thrown if the argument @p orig is @c NULL.
*/
SBMLHttpResolver(const SBMLHttpResolver& c)
{
}
/**
* Destroy this SBMLHttpResolver object.
*/
virtual ~SBMLHttpResolver ()
{
}
/**
* Assignment operator for SBMLHttpResolver.
*
* @param rhs The object whose values are used as the basis of the
* assignment.
*
* @throws @if python ValueError @else SBMLConstructorException @endif@~
* Thrown if the argument @p rhs is @c NULL.
*/
SBMLHttpResolver& operator=(const SBMLHttpResolver& rhs)
{
return *this;
}
/**
* Creates and returns a deep copy of this SBMLHttpResolver object.
*
* @return a (deep) copy of this SBMLHttpResolver object.
*/
virtual SBMLHttpResolver* clone() const
{
return new SBMLHttpResolver(*this);
}
/**
* resolve the document for the given uri
*
* @param uri the uri to the target document
* @param baseUri base uri, in case the uri is a relative one
*
* @return the document, if this resolver can resolve the document or NULL.
*/
virtual SBMLDocument* resolve(const std::string &uri,
const std::string& baseUri="") const
{
SBMLUri * resolved = resolveUri(uri, baseUri);
if (resolved == NULL)
return NULL;
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, resolved->getUri().c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK)
return readSBMLFromString(readBuffer.c_str());
// here is where errors could be handled, like
// as would occur whenever a network issue arrises.
}
// something went wrong ...
return NULL;
}
/**
* resolve the full uri for the given uri without actually reading the document
*
* @param uri the uri to the target document
* @param baseUri base uri, in case the uri is a relative one
*
* @return the full uri to the document, if this resolver can resolve the document or NULL.
*/
virtual SBMLUri* resolveUri(const std::string &sUri,
const std::string& sBaseUri="") const
{
SBMLUri uri(sUri);
if (uri.getScheme() != "http")
{
// this example just displays how to resolve http uris, so if we don't have one of those,
// bail
return NULL;
}
// here we could get fancy and construct relative uris ... but for
// now this is good enough :)
return new SBMLUri(uri);
}
protected:
private:
};
int resolveModelFromUri(const std::string& uri)
{
// create an external document to test
CompSBMLDocumentPlugin* plugin = (CompSBMLDocumentPlugin*)doc.getPlugin("comp");
// create an external model definition
external->setSource(uri);
Model* model = external->getReferencedModel();
if (model == NULL)
{
// bummer something went wrong ...
return -1;
}
// do something with the model
if (model->isSetId())
cout << "resolved: " << model->getId() << endl;
else
cout << "resolved: " << uri << endl;
if (model->isSetNotes())
cout << model->getNotesString() << endl << endl;
// all good
return 0;
}
int main(void)
{
// create http resolver
SBMLHttpResolver resolver;
// the resolver can of course already be used:
SBMLDocument* originalBM1 =
resolver.resolve("http://www.ebi.ac.uk/biomodels/models-main/publ/BIOMD0000000001/BIOMD0000000001.xml.origin");
if (originalBM1 != NULL)
{
if (originalBM1->getModel()->isSetId())
cout << "resolved: " << originalBM1->getModel()->getId() << endl;
else
cout << "resolved: " << "BIOMD0000000001.xml.origin" << endl;
}
else
{
cout << "an error occured while trying to resolve the document. " << endl;
}
// but usually what one would do would be to
// add it to the registry
// and then go from there ...
if (resolveModelFromUri("http://www.ebi.ac.uk/biomodels/models-main/publ/BIOMD0000000001/BIOMD0000000001.xml.origin") != 0 )
{
cout << "Comp V1 does not support L2V1 documents!" << endl;
}
if (resolveModelFromUri("http://www.ebi.ac.uk/biomodels-main/download?mid=BMID000000063853") != 0 )
{
cout << "something went wrong while getting the l3v1." << endl;
}
if (resolveModelFromUri("http://sbml.svn.sourceforge.net/viewvc/sbml/branches/libsbml-packages/comp/src/sbml/packages/comp/util/test/test-data/subdir/new_aggregate.xml?revision=16695") != 0 )
{
cout << "something went wrong while getting the l3v1 comp model." << endl;
}
cout << "done!";
return 0;
}
SBMLExtensionNamespaces< CompExtension > CompPkgNamespaces
Definition: CompExtension.h:326
Include all SBML types of Comp extension in a single header file.
SBMLDocument_t * readSBMLFromString(const char *xml)
Reads an SBML document from a text string.
Definition of SBMLResolver, the base class for resolving SBML documents.
Definition of SBMLResolverRegistry, a registry of available resolvers.
Include all SBML types in a single header file.
Definition of SBMLUri, the utility class for handling URIs.
Definition: CompSBMLDocumentPlugin.h:94
ExternalModelDefinition * createExternalModelDefinition()
Creates a ExternalModelDefinition object, adds it to the end of the ExternalModelDefinition objects l...
Definition: CompSBMLDocumentPlugin.cpp:536
Definition: ExternalModelDefinition.h:128
virtual int setSource(const std::string &source)
Sets the value of the "source" attribute of this ExternalModelDefinition.
Definition: ExternalModelDefinition.cpp:333
virtual Model * getReferencedModel()
Resolves and returns the referenced Model object of this ExternalModelDefinition.
Definition: ExternalModelDefinition.cpp:573
Definition: Model.h:485
virtual bool isSetId() const
Predicate returning true if this Model's "id" attribute is set.
Definition: Model.cpp:618
virtual const std::string & getId() const
Returns the value of the "id" attribute of this Model.
Definition: SBMLDocument.h:351
const Model * getModel() const
Returns the Model object stored in this SBMLDocument.
Definition: SBMLDocument.cpp:350
Definition: SBMLResolver.h:77
int addResolver(const SBMLResolver *resolver)
Adds the given resolver to the registry of SBML resolvers.
Definition: SBMLResolverRegistry.cpp:77
static SBMLResolverRegistry & getInstance()
Returns the singleton instance for the resolver registry.
Definition: SBMLResolverRegistry.cpp:66
Definition: SBMLUri.h:60
const std::string & getUri() const
Returns the full stored URI, after replacing backslashes with slashes.
Definition: SBMLUri.cpp:306
std::string getNotesString()
Returns the content of the "notes" subelement of this object as a string.
Definition: SBase.cpp:771
bool isSetNotes() const
Predicate returning true if this object's "notes" subelement exists and has content.
Definition: SBase.cpp:1143
#define LIBSBML_EXTERN
LIBSBML_EXTERN is used under Windows to simplify exporting functions from a DLL.
Definition: extern.h:102