COPASI API  4.16.103
CCopasiXMLInterface.h
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2003 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 /**
16  * CCopasiXMLInterface class.
17  * The class CCopasiXMLInterface is the interface to various XML document
18  * containing COPASI relevant information.
19  *
20  * Created for COPASI by Stefan Hoops 2003
21  */
22 
23 #ifndef COPASI_CCopasiXMLInterface
24 #define COPASI_CCopasiXMLInterface
25 
26 #include <vector>
27 #include <string>
28 #include <sstream>
29 
30 #include "copasi.h"
32 
33 template <class CType> class CCopasiVector;
34 template <class CType> class CCopasiVectorN;
35 class CXMLAttributeList;
36 class CSlider;
37 class CCopasiParameter;
38 
39 /**
40  * The class CCopasiXMLInterface specifies an interface to various XML formats
41  * related to COPASI relevant information.
42  * Derived classes have to implement the following two methods:
43  * @see bool load(std::istream & is)
44  * @see bool save(std::ostream & os)
45  *
46  * Note: the responsibility to free allocated memory during load is handed to the
47  * user.
48  */
50 {
51  // Operations
52 public:
53  /**
54  * Constructor
55  */
56  SCopasiXMLGUI(const std::string & name,
57  const CCopasiContainer * pParent = NULL,
58  const std::string & type = "CN",
59  const unsigned C_INT32 & flag = CCopasiObject::Container);
60 
61  /**
62  * Constructor
63  */
65 
66  /**
67  * Returns a pointer to the slider list.
68  */
70 
71  /**
72  * Returns a const pointer to the slider list.
73  */
75 
76  // Attributes
77 protected:
78  /**
79  * Pointer to a vector of sliders which has been loaded or is to be saved.
80  * The ownership is handed to the user.
81  */
83 };
84 
86 {
87 public:
88  /**
89  * Enumeration of encoding type.
90  */
92  {
93  none = 0,
97  };
98 
99  class DBL
100  {
101  // Operations
102  public:
103  DBL(const C_FLOAT64 & value = 0.0);
104 
105  DBL(const char * value);
106 
107  ~DBL();
108 
109  operator const C_FLOAT64 & () const;
110 
111  friend std::ostream & operator << (std::ostream & os, const DBL & dbl);
112 
113  // Attributes
114  private:
116  };
117 
118  // Attributes
119 protected:
120  /**
121  * A pointer to the input stream
122  */
123  std::istream *mpIstream;
124 
125  /**
126  * A pointer to the output stream
127  */
128  std::ostream *mpOstream;
129 
130  /**
131  * Indent
132  */
133  std::string mIndent;
134 
135  /**
136  * The filename if available
137  */
138  std::string mPWD;
139 
140  // Operations
141 public:
142  /**
143  * Constructor.
144  */
146 
147  /**
148  * Destructor.
149  */
150  virtual ~CCopasiXMLInterface();
151 
152  /**
153  * Load information from a given istream.
154  * @param std::istream & is
155  * @param const std::string & relativeTo
156  * @return bool success
157  */
158  virtual bool load(std::istream & is,
159  const std::string & relativeTo) = 0;
160 
161  /**
162  * Load information from a file.
163  * @param const std::string & fileName
164  * @param const std::string & relativeTo
165  * @return bool success
166  */
167  bool load(const std::string & fileName,
168  const std::string & relativeTo);
169 
170  /**
171  * Save information to a given ostream.
172  * @param std::ostream & os
173  * @param const std::string & relativeTo
174  * @return bool success
175  */
176  virtual bool save(std::ostream & os,
177  const std::string & relativeTo) = 0;
178 
179  /**
180  * Save information to a file.
181  * @param const std::string & fileName
182  * @param const std::string & relativeTo
183  * @return bool success
184  */
185  bool save(const std::string & fileName,
186  const std::string & relativeTo);
187 
188  /**
189  * Encode a given string to a valid XML string
190  * @param const std::string & str
191  * @return std::string encoded
192  */
193  static std::string encode(const std::string & str, const EncodingType & type = standard);
194 
195  /**
196  * Encode a given string to a valid XML string
197  * @param const C_FLOAT64 & dbl
198  * @return std::string encoded
199  */
200  // static std::string encodeDBL(const C_FLOAT64 & dbl);
201 
202  /**
203  * Encode a given string to a utf-8 string
204  * @param const std::string & str
205  * @return std::string encoded
206  */
207  static std::string utf8(const std::string & str);
208 
209 protected:
210  /**
211  * Save CDATA to the ostream
212  * @param const std::string & data
213  * @return bool success
214  */
215  bool saveData(const std::string & data);
216 
217  /**
218  * Save xhtml to the ostream
219  * @param const std::string & xhtml
220  * @return bool success
221  */
222  bool saveXhtml(const std::string & xhtml);
223 
224  /**
225  * Save an XML element to the ostream
226  * @param const std::string & name
227  * @param CXMLAttributeList & attributeList
228  * @return bool success
229  */
230  bool saveElement(const std::string & name,
231  CXMLAttributeList & attributeList);
232 
233  /**
234  * Start saving an XML element to the ostream. Call endSaveElement to
235  * conclude.
236  * @param const std::string & name
237  * @return bool success
238  */
239  bool startSaveElement(const std::string & name);
240 
241  /**
242  * Start saving an XML element to the ostream. Call endSaveElement to
243  * conclude.
244  * @param const std::string & name
245  * @param CXMLAttributeList & attributeList
246  * @return bool success
247  */
248  bool startSaveElement(const std::string & name,
249  CXMLAttributeList & attributeList);
250 
251  /**
252  * End saving an XML element to the ostream started by startSaveElement.
253  * @param const std::string & name
254  * @return bool success
255  */
256  bool endSaveElement(const std::string & name);
257 
258  /**
259  * Save a Parameter.
260  * @param const CCopasiParameter & parameter
261  * @return bool success
262  */
263  bool saveParameter(const CCopasiParameter & parameter);
264 
265  /**
266  * Save a Parameter Group.
267  * @param const CCopasiParameterGroup::parameterGroup & group
268  * @return bool success
269  */
270  bool saveParameterGroup(const std::vector< CCopasiParameter * > & group);
271 };
272 
273 /**
274  * A list of XML attributes used by saveElement and startSaveElement.
275  */
277 {
278  // Attributes
279 private:
280  /**
281  * The attribute list.
282  */
283  std::vector< std::string > mAttributeList;
284 
285  /**
286  * The list whether an attribute is to be saved.
287  */
288  std::vector< bool > mSaveList;
289 
290  // Operations
291 public:
292  /**
293  * Constructor.
294  */
296 
297  /**
298  * Copy constructor.
299  * @param const CXMLAttributeList & src
300  */
302 
303  /**
304  * Destructor.
305  */
307 
308  /**
309  * Erase the content of the attribute list.
310  * @return bool success
311  */
312  bool erase();
313 
314  /**
315  * Add an attribute to the end of the list.
316  * Note: the value will be XML encoded
317  * @param const std::string & name
318  * @param const CType & value
319  * @return bool success
320  */
321  template <class CType> inline bool add(const std::string & name,
322  const CType & value)
323  {return add(name, value, CCopasiXMLInterface::attribute);}
324 
325  /**
326  * Add an attribute to the end of the list.
327  * Note: the value will be XML encoded
328  * @param const std::string & name
329  * @param const C_FLOAT64 & value
330  * @return bool success
331  */
332  bool add(const std::string & name, const C_FLOAT64 & value);
333 
334  /**
335  * Add an attribute to the end of the list.
336  * Note: the value will be XML encoded
337  * @param const std::string & name
338  * @param const CType & value
339  * @param const CCopasiXMLInterface::EncodingType & encodingType
340  * @return bool success
341  */
342  template <class CType> bool add(const std::string & name,
343  const CType & value,
344  const CCopasiXMLInterface::EncodingType & encodingType)
345  {
346  std::ostringstream Value;
347 
348  try
349  {
350  Value << value;
351  }
352 
353  catch (...)
354  {
355  Value << "";
356  }
357 
358  mAttributeList.push_back(name);
359  mAttributeList.push_back(CCopasiXMLInterface::encode(Value.str(),
360  encodingType));
361 
362  mSaveList.push_back(true);
363 
364  return true;
365  }
366 
367  /**
368  * Retrieve the size of the list.
369  * @return size_t size
370  */
371  size_t size();
372 
373  /**
374  * Set the name of the indexed attribute
375  * @param const size_t & index
376  * @param const std::string & name
377  * @return bool success
378  */
379  bool setName(const size_t & index,
380  const std::string & name);
381 
382  /**
383  * Retrieve the name of the indexed attribute.
384  * @param const size_t & index
385  * @return const std::string & name
386  */
387  const std::string & getName(const size_t & index) const;
388 
389  /**
390  * Set the name and value of the indexed attribute
391  * Note: the value will be XML encoded
392  * @param const size_t & index
393  * @param const std::string & name
394  * @param const CType & value
395  * @return bool success
396  */
397  template <class CType> inline bool set(const size_t & index,
398  const std::string & name,
399  const CType & value)
400  {return set(index, name, value, CCopasiXMLInterface::attribute);}
401  /**
402  * Set the name and value of the indexed attribute
403  * Note: the value will be XML encoded
404  * @param const size_t & index
405  * @param const std::string & name
406  * @param const CType & value
407  * @param const CCopasiXMLInterface::EncodingType & encodingType
408  * @return bool success
409  */
410  template <class CType> bool set(const size_t & index,
411  const std::string & name,
412  const CType & value,
413  const CCopasiXMLInterface::EncodingType & encodingType)
414  {
415  mAttributeList[2 * index] = name;
416 
417  std::ostringstream Value;
418 
419  try
420  {
421  Value << value;
422  }
423 
424  catch (...)
425  {
426  Value << "";
427  }
428 
429  mAttributeList[2 * index + 1] =
430  CCopasiXMLInterface::encode(Value.str(),
431  encodingType);
432  mSaveList[index] = true;
433 
434  return true;
435  }
436 
437  /**
438  * Set the value of the indexed attribute
439  * Note: the value will be XML encoded
440  * @param const size_t & index
441  * @param const CType & value
442  * @return bool success
443  */
444  template <class CType> inline bool setValue(const size_t & index,
445  const CType & value)
446  {return setValue(index, value, CCopasiXMLInterface::attribute);}
447 
448  /**
449  * Set the value of the indexed attribute
450  * Note: the value will be XML encoded
451  * @param const size_t & index
452  * @param const CType & value
453  * @param const CCopasiXMLInterface::EncodingType & encodingType
454  * @return bool success
455  */
456  template <class CType> bool setValue(const size_t & index,
457  const CType & value,
458  const CCopasiXMLInterface::EncodingType & encodingType)
459  {
460  std::ostringstream Value;
461 
462  try
463  {
464  Value << value;
465  }
466 
467  catch (...)
468  {
469  Value << "";
470  }
471 
472  mAttributeList[2 * index + 1] =
473  CCopasiXMLInterface::encode(Value.str(),
474  encodingType);
475  mSaveList[index] = true;
476 
477  return true;
478  }
479 
480  /**
481  * Retrieve the value of the indexed attribute.
482  * @param const size_t & index
483  * @return const std::string & value
484  */
485  const std::string & getValue(const size_t & index) const;
486 
487  /**
488  * Set whether the indexed attribute shall be skipped during save.
489  * @param const size_t & index
490  * @return bool success
491  */
492  bool skip(const size_t & index);
493 
494  /**
495  * Retrieve the indexed attribute (' name="value"' or '' if skipped).
496  * @param const size_t & index
497  * @return std::string attribute
498  */
499  std::string getAttribute(const size_t & index) const;
500 
501  friend std::ostream &operator<<(std::ostream &os, const CXMLAttributeList & attr);
502 };
503 
504 #endif // COPASI_CCopasiXMLInterface
CCopasiVector< CSlider > * getSliderList()
SCopasiXMLGUI(const std::string &name, const CCopasiContainer *pParent=NULL, const std::string &type="CN", const unsigned C_INT32 &flag=CCopasiObject::Container)
friend std::ostream & operator<<(std::ostream &os, const CXMLAttributeList &attr)
bool set(const size_t &index, const std::string &name, const CType &value)
bool setValue(const size_t &index, const CType &value, const CCopasiXMLInterface::EncodingType &encodingType)
friend std::ostream & operator<<(std::ostream &os, const DBL &dbl)
bool saveElement(const std::string &name, CXMLAttributeList &attributeList)
CCopasiVector< CSlider > * mpSliderList
bool startSaveElement(const std::string &name)
bool saveParameterGroup(const std::vector< CCopasiParameter * > &group)
bool saveData(const std::string &data)
const std::string & getValue(const size_t &index) const
bool saveXhtml(const std::string &xhtml)
#define C_INT32
Definition: copasi.h:90
DBL(const C_FLOAT64 &value=0.0)
const std::string & getName(const size_t &index) const
bool skip(const size_t &index)
bool setValue(const size_t &index, const CType &value)
bool setName(const size_t &index, const std::string &name)
bool set(const size_t &index, const std::string &name, const CType &value, const CCopasiXMLInterface::EncodingType &encodingType)
virtual bool load(std::istream &is, const std::string &relativeTo)=0
std::string getAttribute(const size_t &index) const
Header file of class CCopasiContainer.
bool add(const std::string &name, const CType &value, const CCopasiXMLInterface::EncodingType &encodingType)
std::vector< std::string > mAttributeList
long int flag
Definition: f2c.h:52
#define C_FLOAT64
Definition: copasi.h:92
static std::string encode(const std::string &str, const EncodingType &type=standard)
bool endSaveElement(const std::string &name)
std::vector< bool > mSaveList
static std::string utf8(const std::string &str)
virtual bool save(std::ostream &os, const std::string &relativeTo)=0
bool saveParameter(const CCopasiParameter &parameter)
bool add(const std::string &name, const CType &value)