Loading...
Searching...
No Matches
xml.h
Go to the documentation of this file.
/**********************************************************************
xml.h Declaration of XMLConversion,
declaration and definition of XMLBaseFormat and XMLMoleculeFormat
Copyright (C) 2005-2006 by Chris Morley
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#ifndef OB_XML_H
#define OB_XML_H
#include <typeinfo>
#include <openbabel/obconversion.h>
#include <openbabel/obmolecformat.h>
#include <openbabel/mol.h>
#include <libxml/xmlreader.h>
#include <libxml/xmlwriter.h>
#include <typeinfo>
namespace OpenBabel
{
//forward declaration
class XMLBaseFormat;
//******************************************************
class XMLConversion : public OBConversion
{
public:
XMLConversion(OBConversion* pConv);
~XMLConversion();
bool SetupReader();
bool SetupWriter();
bool ReadXML(XMLBaseFormat* pFormat, OBBase* pOb);
int SkipXML(const char* ctag);
typedef std::map<std::string, XMLBaseFormat*> NsMapType;
static NsMapType& Namespaces()
{
static NsMapType ns;
return ns;
//static NsMapType* nsm = NULL;
//if (!nsm)
// nsm = new NsMapType;
//return *nsm;
};
static void RegisterXMLFormat(XMLBaseFormat* pFormat,
bool IsDefault=false, const char* uri=nullptr);
static XMLConversion* GetDerived(OBConversion* pConv, bool ForReading=true);
bool IsLast()
{ return _pConv->IsLast(); }
int GetOutputIndex()
{ return _pConv->GetOutputIndex(); }
xmlTextReaderPtr GetReader() const
{ return _reader; };
xmlTextWriterPtr GetWriter() const
{ return _writer; };
void OutputToStream()
{
xmlOutputBufferFlush(_buf);
}
static XMLBaseFormat* GetDefaultXMLClass() //TODO make dependent on object type
{ return _pDefault;};
void LookForNamespace()
{ _LookingForNamespace = true; };
static int ReadStream(void * context, char * buffer, int len);
static int WriteStream(void * context, const char * buffer, int len);
//static int CloseStream(void* context);
std::string GetAttribute(const char* attrname);
std::string GetContent();
bool GetContentInt(int& value);
bool GetContentDouble(double& value);
private:
static XMLBaseFormat* _pDefault;
OBConversion* _pConv;
std::streampos _requestedpos, _lastpos;
xmlTextReaderPtr _reader;
xmlTextWriterPtr _writer;
xmlOutputBufferPtr _buf;
// xmlBufferPtr _buf;
bool _LookingForNamespace;
public:
bool _SkipNextRead;
};
//*************************************************
class XMLBaseFormat : public OBFormat
{
protected:
XMLConversion* _pxmlConv;
//formating for output
std::string _prefix;
int baseindent, ind;
std::string nsdecl;
int _embedlevel;
public:
~XMLBaseFormat(){}
virtual const char* NamespaceURI()const=0;
virtual bool DoElement(const std::string& ElName){return false;};
virtual bool EndElement(const std::string& ElName){return false;};
virtual const char* EndTag(){return ">";};
protected:
xmlTextReaderPtr reader() const
{
return _pxmlConv->GetReader();
}
xmlTextWriterPtr writer() const
{
return _pxmlConv->GetWriter();
}
void OutputToStream()
{
_pxmlConv->OutputToStream();
}
virtual int SkipObjects(int n, OBConversion* pConv)
{
//don't implement on base class
if(*EndTag()=='>')
return 0;
//Set up XMLConversion class with reader
_pxmlConv = XMLConversion::GetDerived(pConv,true);
if(!_pxmlConv)
return -1;
//always find the end of at least 1 object
if(n==0)++n;
//Skip n objects, returning -1 if not successful
int i;
for(i=0; i<n; ++i)
if(_pxmlConv->SkipXML(EndTag())!=1)
return -1;
return 1;
}
};
//*************************************************
class XMLMoleculeFormat : public XMLBaseFormat
{
protected:
OBMol* _pmol;
public:
~XMLMoleculeFormat(){}
virtual bool ReadChemObject(OBConversion* pConv)
{
return OBMoleculeFormat::ReadChemObjectImpl(pConv, this);
};
virtual bool WriteChemObject(OBConversion* pConv)
{
return OBMoleculeFormat::WriteChemObjectImpl(pConv, this);
};
virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv)
{
_pmol = dynamic_cast<OBMol*>(pOb);
if(!_pmol)
return false;
_pxmlConv = XMLConversion::GetDerived(pConv,true);
if(!_pxmlConv)
return false;
_embedlevel = -1;
return _pxmlConv->ReadXML(this,pOb);
};
const std::type_info& GetType()
{
return typeid(OBMol*);
};
};
}//namespace
#endif
OpenBabel::OBBase
Base Class.
Definition base.h:240
OpenBabel::OBConversion
Class to convert from one format to another.
Definition obconversion.h:60
OpenBabel::OBConversion::OBConversion
OBConversion(std::istream *is=nullptr, std::ostream *os=nullptr)
Definition obconversion.cpp:230
OpenBabel::OBFormat::OBFormat
OBFormat()
Default constructor. Registration via RegisterFormat(), not via constructor as in other plugins.
Definition format.h:52
OpenBabel::OBMol
Molecule Class.
Definition mol.h:121
OpenBabel::OBMoleculeFormat::ReadChemObjectImpl
static bool ReadChemObjectImpl(OBConversion *pConv, OBFormat *)
Static routine, which can be called from elsewhere.
Definition obmolecformat.cpp:36
OpenBabel::OBMoleculeFormat::WriteChemObjectImpl
static bool WriteChemObjectImpl(OBConversion *pConv, OBFormat *)
Static routine, which can be called from elsewhere.
Definition obmolecformat.cpp:136
OpenBabel::XMLBaseFormat
Abstract class containing common functionality for XML formats.
Definition xml.h:151
OpenBabel::XMLBaseFormat::EndElement
virtual bool EndElement(const std::string &ElName)
Definition xml.h:165
OpenBabel::XMLBaseFormat::~XMLBaseFormat
~XMLBaseFormat()
Definition xml.h:162
OpenBabel::XMLBaseFormat::OutputToStream
void OutputToStream()
Definition xml.h:180
OpenBabel::XMLBaseFormat::ind
int ind
Definition xml.h:157
OpenBabel::XMLBaseFormat::reader
xmlTextReaderPtr reader() const
Definition xml.h:170
OpenBabel::XMLBaseFormat::baseindent
int baseindent
Definition xml.h:157
OpenBabel::XMLBaseFormat::_prefix
std::string _prefix
Definition xml.h:156
OpenBabel::XMLBaseFormat::DoElement
virtual bool DoElement(const std::string &ElName)
Definition xml.h:164
OpenBabel::XMLBaseFormat::nsdecl
std::string nsdecl
Definition xml.h:158
OpenBabel::XMLBaseFormat::writer
xmlTextWriterPtr writer() const
Definition xml.h:175
OpenBabel::XMLBaseFormat::NamespaceURI
virtual const char * NamespaceURI() const =0
OpenBabel::XMLBaseFormat::EndTag
virtual const char * EndTag()
The tag at the end of the chemical object e.g. "/molecule>".
Definition xml.h:167
OpenBabel::XMLBaseFormat::_pxmlConv
XMLConversion * _pxmlConv
Definition xml.h:153
OpenBabel::XMLBaseFormat::_embedlevel
int _embedlevel
Definition xml.h:159
OpenBabel::XMLBaseFormat::SkipObjects
virtual int SkipObjects(int n, OBConversion *pConv)
Definition xml.h:187
OpenBabel::XMLConversion
A subclass for conversion of XML formats.
Definition xml.h:54
OpenBabel::XMLConversion::IsLast
bool IsLast()
Definition xml.h:95
OpenBabel::XMLConversion::GetOutputIndex
int GetOutputIndex()
Definition xml.h:97
OpenBabel::XMLConversion::WriteStream
static int WriteStream(void *context, const char *buffer, int len)
OpenBabel::XMLConversion::GetDefaultXMLClass
static XMLBaseFormat * GetDefaultXMLClass()
Definition xml.h:112
OpenBabel::XMLConversion::OutputToStream
void OutputToStream()
Definition xml.h:107
OpenBabel::XMLConversion::ReadStream
static int ReadStream(void *context, char *buffer, int len)
Static callback functions for xmlReaderForIO().
OpenBabel::XMLConversion::GetReader
xmlTextReaderPtr GetReader() const
Definition xml.h:101
OpenBabel::XMLConversion::GetContentInt
bool GetContentInt(int &value)
Sets value to element content as an integer. Returns false if there is no content.
OpenBabel::XMLConversion::SetupReader
bool SetupReader()
opens libxml2 reader
OpenBabel::XMLConversion::NsMapType
std::map< std::string, XMLBaseFormat * > NsMapType
Definition xml.h:72
OpenBabel::XMLConversion::GetContentDouble
bool GetContentDouble(double &value)
Sets value to element content as an double. Returns false if there is no content.
OpenBabel::XMLConversion::GetContent
std::string GetContent()
Sets value to element content. Returns false if there is no content.
OpenBabel::XMLConversion::LookForNamespace
void LookForNamespace()
Definition xml.h:115
OpenBabel::XMLConversion::XMLConversion
XMLConversion(OBConversion *pConv)
Existing OBConversion instance copied.
OpenBabel::XMLConversion::RegisterXMLFormat
static void RegisterXMLFormat(XMLBaseFormat *pFormat, bool IsDefault=false, const char *uri=nullptr)
OpenBabel::XMLConversion::~XMLConversion
~XMLConversion()
Frees reader and writer if necessary.
OpenBabel::XMLConversion::GetDerived
static XMLConversion * GetDerived(OBConversion *pConv, bool ForReading=true)
Returns the extended OBConversion class, making it if necessary.
OpenBabel::XMLConversion::ReadXML
bool ReadXML(XMLBaseFormat *pFormat, OBBase *pOb)
Parses the input xml stream and sends each element to the format's callback routines.
OpenBabel::XMLConversion::Namespaces
static NsMapType & Namespaces()
Definition xml.h:76
OpenBabel::XMLConversion::GetAttribute
std::string GetAttribute(const char *attrname)
OpenBabel::XMLConversion::GetWriter
xmlTextWriterPtr GetWriter() const
Definition xml.h:104
OpenBabel::XMLConversion::SkipXML
int SkipXML(const char *ctag)
OpenBabel::XMLConversion::SetupWriter
bool SetupWriter()
opens libxml2 writer
OpenBabel::XMLConversion::_SkipNextRead
bool _SkipNextRead
Definition xml.h:144
OpenBabel::XMLMoleculeFormat
Abstract class for XML formats which represent molecules.
Definition xml.h:216
OpenBabel::XMLMoleculeFormat::WriteChemObject
virtual bool WriteChemObject(OBConversion *pConv)
The "Convert" interface Write function.
Definition xml.h:227
OpenBabel::XMLMoleculeFormat::ReadMolecule
virtual bool ReadMolecule(OBBase *pOb, OBConversion *pConv)
The "API" interface Read function.
Definition xml.h:232
OpenBabel::XMLMoleculeFormat::ReadChemObject
virtual bool ReadChemObject(OBConversion *pConv)
The "Convert" interface Read function.
Definition xml.h:222
OpenBabel::XMLMoleculeFormat::GetType
const std::type_info & GetType()
Definition xml.h:244
OpenBabel::XMLMoleculeFormat::~XMLMoleculeFormat
~XMLMoleculeFormat()
Definition xml.h:221
OpenBabel::XMLMoleculeFormat::_pmol
OBMol * _pmol
Definition xml.h:218
mol.h
Handle molecules. Declarations of OBMol, OBAtom, OBBond, OBResidue. (the main header for Open Babel).
OpenBabel
Global namespace for all Open Babel code.
Definition alias.h:23
obconversion.h
Handle file conversions. Declaration of OBFormat, OBConversion.
obmolecformat.h
Subclass of OBFormat for conversion of OBMol.