Open Babel  3.0
xml.h
Go to the documentation of this file.
1 /**********************************************************************
2 xml.h Declaration of XMLConversion,
3 declaration and definition of XMLBaseFormat and XMLMoleculeFormat
4 Copyright (C) 2005-2006 by Chris Morley
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 ***********************************************************************/
15 
16 #ifndef OB_XML_H
17 #define OB_XML_H
18 
19 #include <typeinfo>
20 
21 #include <openbabel/obconversion.h>
23 #include <openbabel/mol.h>
24 
25 #include <libxml/xmlreader.h>
26 #include <libxml/xmlwriter.h>
27 #include <typeinfo>
28 
29 namespace OpenBabel
30 {
31 
32 
33  //forward declaration
34  class XMLBaseFormat;
35 
36  //******************************************************
53  class XMLConversion : public OBConversion
54  {
55  public:
58 
61 
62  bool SetupReader();
63  bool SetupWriter();
64 
66  bool ReadXML(XMLBaseFormat* pFormat, OBBase* pOb);
67 
70  int SkipXML(const char* ctag);
71 
72  typedef std::map<std::string, XMLBaseFormat*> NsMapType;
73 
76  static NsMapType& Namespaces()
77  {
78  static NsMapType ns;
79  return ns;
80 
81  //static NsMapType* nsm = NULL;
82  //if (!nsm)
83  // nsm = new NsMapType;
84  //return *nsm;
85  };
86 
87  static void RegisterXMLFormat(XMLBaseFormat* pFormat,
88  bool IsDefault=false, const char* uri=NULL);
89 
91  static XMLConversion* GetDerived(OBConversion* pConv, bool ForReading=true);
92 
95  bool IsLast()
96  { return _pConv->IsLast(); }
98  { return _pConv->GetOutputIndex(); }
99 
100 
101  xmlTextReaderPtr GetReader() const
102  { return _reader; };
103 
104  xmlTextWriterPtr GetWriter() const
105  { return _writer; };
106 
108  {
109  xmlOutputBufferFlush(_buf);
110  }
111 
112  static XMLBaseFormat* GetDefaultXMLClass() //TODO make dependent on object type
113  { return _pDefault;};
114 
116  { _LookingForNamespace = true; };
117 
119  static int ReadStream(void * context, char * buffer, int len);
120  static int WriteStream(void * context, const char * buffer, int len);
121  //static int CloseStream(void* context);
122 
123  std::string GetAttribute(const char* attrname);
124 
126  std::string GetContent();
127 
129  bool GetContentInt(int& value);
130 
132  bool GetContentDouble(double& value);
133 
134  private:
135  static XMLBaseFormat* _pDefault;
136  OBConversion* _pConv;
137  std::streampos _requestedpos, _lastpos;
138  xmlTextReaderPtr _reader;
139  xmlTextWriterPtr _writer;
140  xmlOutputBufferPtr _buf;
141  // xmlBufferPtr _buf;
142  bool _LookingForNamespace;
143  public:
145  };
146 
147  //*************************************************
150  class XMLBaseFormat : public OBFormat
151  {
152  protected:
154 
155  //formating for output
156  std::string _prefix;
157  int baseindent, ind;
158  std::string nsdecl;
160 
161  public:
163  virtual const char* NamespaceURI()const=0;
164  virtual bool DoElement(const std::string& ElName){return false;};
165  virtual bool EndElement(const std::string& ElName){return false;};
167  virtual const char* EndTag(){return ">";};
168 
169  protected:
170  xmlTextReaderPtr reader() const
171  {
172  return _pxmlConv->GetReader();
173  }
174 
175  xmlTextWriterPtr writer() const
176  {
177  return _pxmlConv->GetWriter();
178  }
179 
181  {
182  _pxmlConv->OutputToStream();
183  }
184 
187  virtual int SkipObjects(int n, OBConversion* pConv)
188  {
189  //don't implement on base class
190  if(*EndTag()=='>')
191  return 0;
192 
193  //Set up XMLConversion class with reader
194  _pxmlConv = XMLConversion::GetDerived(pConv,true);
195  if(!_pxmlConv)
196  return -1;
197 
198  //always find the end of at least 1 object
199  if(n==0)++n;
200 
201  //Skip n objects, returning -1 if not successful
202  int i;
203  for(i=0; i<n; ++i)
204  if(_pxmlConv->SkipXML(EndTag())!=1)
205  return -1;
206 
207  return 1;
208  }
209 
210  };
211 
212  //*************************************************
216  {
217  protected:
219 
220  public:
222  virtual bool ReadChemObject(OBConversion* pConv)
223  {
224  return OBMoleculeFormat::ReadChemObjectImpl(pConv, this);
225  };
226 
227  virtual bool WriteChemObject(OBConversion* pConv)
228  {
229  return OBMoleculeFormat::WriteChemObjectImpl(pConv, this);
230  };
231 
232  virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv)
233  {
234  _pmol = dynamic_cast<OBMol*>(pOb);
235  if(!_pmol)
236  return false;
237  _pxmlConv = XMLConversion::GetDerived(pConv,true);
238  if(!_pxmlConv)
239  return false;
240  _embedlevel = -1;
241  return _pxmlConv->ReadXML(this,pOb);
242  };
243 
244  const std::type_info& GetType()
245  {
246  return typeid(OBMol*);
247  };
248 
249  };
250 
251 
252 }//namespace
253 
257 
258 #endif
virtual bool ReadMolecule(OBBase *pOb, OBConversion *pConv)
The "API" interface Read function.
Definition: xml.h:232
std::string nsdecl
Definition: xml.h:158
OBMol * _pmol
Definition: xml.h:218
void OutputToStream()
Definition: xml.h:107
Abstract class containing common functionality for XML formats.
Definition: xml.h:150
xmlTextReaderPtr reader() const
Definition: xml.h:170
XMLConversion * _pxmlConv
Definition: xml.h:153
bool ReadXML(XMLBaseFormat *pFormat, OBBase *pOb)
Parses the input xml stream and sends each element to the format&#39;s callback routines.
Class to convert from one format to another.
Definition: obconversion.h:59
int SkipXML(const char *ctag)
static XMLConversion * GetDerived(OBConversion *pConv, bool ForReading=true)
Returns the extended OBConversion class, making it if necessary.
bool IsLast()
Definition: xml.h:95
virtual const char * EndTag()
The tag at the end of the chemical object e.g. "/molecule>".
Definition: xml.h:167
bool _SkipNextRead
Definition: xml.h:144
virtual int SkipObjects(int n, OBConversion *pConv)
Definition: xml.h:187
static int ReadStream(void *context, char *buffer, int len)
Static callback functions for xmlReaderForIO()
void LookForNamespace()
Definition: xml.h:115
virtual bool EndElement(const std::string &ElName)
Definition: xml.h:165
xmlTextWriterPtr writer() const
Definition: xml.h:175
bool SetupWriter()
opens libxml2 writer
static int WriteStream(void *context, const char *buffer, int len)
Molecule Class.
Definition: mol.h:118
Subclass of OBFormat for conversion of OBMol.
static bool ReadChemObjectImpl(OBConversion *pConv, OBFormat *)
Static routine, which can be called from elsewhere.
Definition: obmolecformat.cpp:36
Handle molecules. Declarations of OBMol, OBAtom, OBBond, OBResidue. (the main header for Open Babel) ...
virtual bool DoElement(const std::string &ElName)
Definition: xml.h:164
A subclass for conversion of XML formats.
Definition: xml.h:53
bool GetContentInt(int &value)
Sets value to element content as an integer. Returns false if there is no content.
~XMLBaseFormat()
Definition: xml.h:162
const std::type_info & GetType()
Definition: xml.h:244
bool GetContentDouble(double &value)
Sets value to element content as an double. Returns false if there is no content. ...
~XMLMoleculeFormat()
Definition: xml.h:221
static NsMapType & Namespaces()
Definition: xml.h:76
int GetOutputIndex()
Definition: xml.h:97
virtual bool ReadChemObject(OBConversion *pConv)
The "Convert" interface Read function.
Definition: xml.h:222
Handle file conversions. Declaration of OBFormat, OBConversion.
std::map< std::string, XMLBaseFormat * > NsMapType
Definition: xml.h:72
bool SetupReader()
opens libxml2 reader
int _embedlevel
Definition: xml.h:159
XMLConversion(OBConversion *pConv)
Existing OBConversion instance copied.
std::string _prefix
Definition: xml.h:156
Abstract class for XML formats which represent molecules.
Definition: xml.h:215
xmlTextReaderPtr GetReader() const
Definition: xml.h:101
int GetOutputIndex() const
Retrieves number of ChemObjects that have been actually output.
Definition: obconversion.cpp:738
xmlTextWriterPtr GetWriter() const
Definition: xml.h:104
~XMLConversion()
Frees reader and writer if necessary.
bool IsLast()
True if no more objects to be output.
Definition: obconversion.cpp:1143
std::string GetContent()
Sets value to element content. Returns false if there is no content.
virtual bool WriteChemObject(OBConversion *pConv)
The "Convert" interface Write function.
Definition: xml.h:227
static bool WriteChemObjectImpl(OBConversion *pConv, OBFormat *)
Static routine, which can be called from elsewhere.
Definition: obmolecformat.cpp:136
int ind
Definition: xml.h:157
static XMLBaseFormat * GetDefaultXMLClass()
Definition: xml.h:112
Base class for file formats.
Definition: format.h:44
Base Class.
Definition: base.h:239
void OutputToStream()
Definition: xml.h:180
std::string GetAttribute(const char *attrname)
static void RegisterXMLFormat(XMLBaseFormat *pFormat, bool IsDefault=false, const char *uri=NULL)
Global namespace for all Open Babel code.
Definition: alias.h:22