COPASI API  4.16.103
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CCopasiXMLInterface Class Referenceabstract

#include <CCopasiXMLInterface.h>

Inheritance diagram for CCopasiXMLInterface:
Inheritance graph
[legend]
Collaboration diagram for CCopasiXMLInterface:
Collaboration graph
[legend]

Classes

class  DBL
 

Public Types

enum  EncodingType { none = 0, standard, attribute, character }
 

Public Member Functions

 CCopasiXMLInterface ()
 
virtual bool load (std::istream &is, const std::string &relativeTo)=0
 
bool load (const std::string &fileName, const std::string &relativeTo)
 
virtual bool save (std::ostream &os, const std::string &relativeTo)=0
 
bool save (const std::string &fileName, const std::string &relativeTo)
 
virtual ~CCopasiXMLInterface ()
 

Static Public Member Functions

static std::string encode (const std::string &str, const EncodingType &type=standard)
 
static std::string utf8 (const std::string &str)
 

Protected Member Functions

bool endSaveElement (const std::string &name)
 
bool saveData (const std::string &data)
 
bool saveElement (const std::string &name, CXMLAttributeList &attributeList)
 
bool saveParameter (const CCopasiParameter &parameter)
 
bool saveParameterGroup (const std::vector< CCopasiParameter * > &group)
 
bool saveXhtml (const std::string &xhtml)
 
bool startSaveElement (const std::string &name)
 
bool startSaveElement (const std::string &name, CXMLAttributeList &attributeList)
 

Protected Attributes

std::string mIndent
 
std::istream * mpIstream
 
std::ostream * mpOstream
 
std::string mPWD
 

Detailed Description

Definition at line 85 of file CCopasiXMLInterface.h.

Member Enumeration Documentation

Enumeration of encoding type.

Enumerator
none 
standard 
attribute 
character 

Definition at line 91 of file CCopasiXMLInterface.h.

Constructor & Destructor Documentation

CCopasiXMLInterface::CCopasiXMLInterface ( )

Constructor.

Definition at line 271 of file CCopasiXMLInterface.cpp.

271  :
272  mpIstream(NULL),
273  mpOstream(NULL),
274  mIndent()
275 {}
CCopasiXMLInterface::~CCopasiXMLInterface ( )
virtual

Destructor.

Definition at line 277 of file CCopasiXMLInterface.cpp.

277 {}

Member Function Documentation

std::string CCopasiXMLInterface::encode ( const std::string &  str,
const EncodingType type = standard 
)
static

Encode a given string to a valid XML string

Parameters
conststd::string & str
Returns
std::string encoded

Definition at line 158 of file CCopasiXMLInterface.cpp.

References attribute, character, encodeATTRIBUTE(), encodeCHARACTER(), encodeNONE(), encodeSTD(), none, standard, and void().

Referenced by CXMLAttributeList::add(), CCopasiXMLParser::UnsupportedAnnotationElement::end(), CCopasiXMLParser::MiriamAnnotationElement::end(), CMathMl::fixName(), CCopasiXMLParser::onCharacterData(), saveData(), CXMLAttributeList::set(), CXMLAttributeList::setValue(), CCopasiXMLParser::UnsupportedAnnotationElement::start(), CCopasiXMLParser::CommentElement::start(), and CCopasiXMLParser::MiriamAnnotationElement::start().

159 {
160  /* All COPASI std::strings and char are already UTF-8 encoded.*/
161  std::string tmp = str;
162  std::ostringstream xml;
163 
164  void (*encode)(const char & chr, std::ostringstream & xml) = encodeNONE;
165 
166  std::string::const_iterator it = str.begin();
167  std::string::const_iterator end = str.end();
168 
169  switch (type)
170  {
171  case none:
172  encode = encodeNONE;
173  break;
174 
175  case standard:
176  encode = encodeSTD;
177  break;
178 
179  case attribute:
181  break;
182 
183  case character:
185  break;
186  }
187 
188  for (; it != end; ++it)
189  encode(*it, xml);
190 
191  return xml.str();
192 }
void encodeATTRIBUTE(const char &chr, std::ostringstream &xml)
void encodeSTD(const char &chr, std::ostringstream &xml)
void encodeNONE(const char &chr, std::ostringstream &xml)
void encodeCHARACTER(const char &chr, std::ostringstream &xml)
static std::string encode(const std::string &str, const EncodingType &type=standard)
() void(yyvaluep))
bool CCopasiXMLInterface::endSaveElement ( const std::string &  name)
protected

End saving an XML element to the ostream started by startSaveElement.

Parameters
conststd::string & name
Returns
bool success

Definition at line 368 of file CCopasiXMLInterface.cpp.

References mIndent, and mpOstream.

Referenced by CCopasiXML::save(), CCopasiXML::saveAnnotation(), CCopasiXML::saveBoundingBox(), CCopasiXML::saveCurve(), CCopasiXML::saveCurveElements(), CCopasiXML::saveFunctionList(), CCopasiXML::saveGlobalRenderInformation(), CCopasiXML::saveGlobalStyle(), CCopasiXML::saveGroupElement(), CCopasiXML::saveGUI(), CCopasiXML::saveLayoutList(), CCopasiXML::saveLinearGradient(), CCopasiXML::saveLineEnding(), CCopasiXML::saveListOfGlobalRenderInformation(), CCopasiXML::saveListOfLocalRenderInformation(), CCopasiXML::saveLocalRenderInformation(), CCopasiXML::saveLocalStyle(), CCopasiXML::saveModel(), CCopasiXML::saveModelParameter(), saveParameter(), CCopasiXML::savePlotList(), CCopasiXML::savePolygonElement(), CCopasiXML::saveRadialGradient(), CCopasiXML::saveRenderCurveElement(), CCopasiXML::saveRenderInformationDefinitionElements(), CCopasiXML::saveRenderTextElement(), CCopasiXML::saveReportList(), CCopasiXML::saveReportSection(), CCopasiXML::saveSBMLReference(), and CCopasiXML::saveTaskList().

369 {
370  mIndent = mIndent.substr(0, mIndent.length() - 2);
371  *mpOstream << mIndent << "</" << name << ">" << std::endl;
372 
373  return true;
374 }
virtual bool CCopasiXMLInterface::load ( std::istream &  is,
const std::string &  relativeTo 
)
pure virtual

Load information from a given istream.

Parameters
std::istream& is
conststd::string & relativeTo
Returns
bool success

Implemented in CConfigurationFile::CXML, and CCopasiXML.

Referenced by load().

bool CCopasiXMLInterface::load ( const std::string &  fileName,
const std::string &  relativeTo 
)

Load information from a file.

Parameters
conststd::string & fileName
conststd::string & relativeTo
Returns
bool success

Definition at line 279 of file CCopasiXMLInterface.cpp.

References CLocaleString::fromUtf8(), load(), and mPWD.

281 {
282  mPWD = relativeTo;
283 
284  std::ifstream is(CLocaleString::fromUtf8(fileName).c_str());
285 
286  if (is.fail()) return false;
287 
288  return load(is, relativeTo);
289 }
virtual bool load(std::istream &is, const std::string &relativeTo)=0
static CLocaleString fromUtf8(const std::string &utf8)
virtual bool CCopasiXMLInterface::save ( std::ostream &  os,
const std::string &  relativeTo 
)
pure virtual

Save information to a given ostream.

Parameters
std::ostream& os
conststd::string & relativeTo
Returns
bool success

Implemented in CConfigurationFile::CXML, and CCopasiXML.

Referenced by save().

bool CCopasiXMLInterface::save ( const std::string &  fileName,
const std::string &  relativeTo 
)

Save information to a file.

Parameters
conststd::string & fileName
conststd::string & relativeTo
Returns
bool success

Definition at line 291 of file CCopasiXMLInterface.cpp.

References CLocaleString::fromUtf8(), mPWD, and save().

293 {
294  mPWD = relativeTo;
295 
296  std::ofstream os(CLocaleString::fromUtf8(fileName).c_str());
297 
298  if (os.fail()) return false;
299 
300  if (!save(os, relativeTo)) return false;
301 
302  return true;
303 }
static CLocaleString fromUtf8(const std::string &utf8)
virtual bool save(std::ostream &os, const std::string &relativeTo)=0
bool CCopasiXMLInterface::saveData ( const std::string &  data)
protected

Save CDATA to the ostream

Parameters
conststd::string & data
Returns
bool success

Definition at line 305 of file CCopasiXMLInterface.cpp.

References encode(), mIndent, and mpOstream.

Referenced by CCopasiXML::saveFunctionList(), CCopasiXML::saveModel(), CCopasiXML::saveModelParameter(), saveParameter(), CCopasiXML::saveRenderTextElement(), CCopasiXML::saveReportSection(), and saveXhtml().

306 {
307  *mpOstream << mIndent << CCopasiXMLInterface::encode(data) << std::endl;
308 
309  return true;
310 }
static std::string encode(const std::string &str, const EncodingType &type=standard)
bool CCopasiXMLInterface::saveElement ( const std::string &  name,
CXMLAttributeList attributeList 
)
protected
bool CCopasiXMLInterface::saveParameter ( const CCopasiParameter parameter)
protected

Save a Parameter.

Parameters
constCCopasiParameter & parameter
Returns
bool success

Definition at line 376 of file CCopasiXMLInterface.cpp.

References CXMLAttributeList::add(), CCopasiParameter::BOOL, CCopasiParameter::CN, CCopasiParameter::DOUBLE, endSaveElement(), CCopasiParameter::EXPRESSION, CCopasiParameter::FILE, CDirEntry::fileName(), CCopasiObject::getObjectName(), CCopasiParameter::getType(), CCopasiParameter::getValue(), CCopasiParameter::GROUP, CCopasiParameter::INT, CCopasiParameter::INVALID, CDirEntry::isRelativePath(), CCopasiParameter::KEY, CDirEntry::makePathRelative(), mPWD, CCopasiParameter::Value::pBOOL, CCopasiParameter::Value::pCN, CCopasiParameter::Value::pDOUBLE, CCopasiParameter::Value::pEXPRESSION, CCopasiParameter::Value::pFILE, CCopasiParameter::Value::pGROUP, CCopasiParameter::Value::pINT, CCopasiParameter::Value::pKEY, CCopasiParameter::Value::pSTRING, CCopasiParameter::Value::pUDOUBLE, CCopasiParameter::Value::pUINT, saveData(), saveElement(), saveParameterGroup(), CXMLAttributeList::skip(), startSaveElement(), CCopasiParameter::STRING, CCopasiParameter::UDOUBLE, CCopasiParameter::UINT, and CCopasiParameter::XMLType.

Referenced by saveParameterGroup().

377 {
378  bool success = true;
379 
380  CXMLAttributeList Attributes;
381  std::string File;
382 
383  Attributes.add("name", parameter.getObjectName());
384 
385  CCopasiParameter::Type Type = parameter.getType();
386  Attributes.add("type", CCopasiParameter::XMLType[Type]);
387 
388  switch (parameter.getType())
389  {
391  Attributes.add("value", * parameter.getValue().pDOUBLE);
392 
393  if (!saveElement("Parameter", Attributes)) success = false;
394 
395  break;
396 
398  Attributes.add("value", * parameter.getValue().pUDOUBLE);
399 
400  if (!saveElement("Parameter", Attributes)) success = false;
401 
402  break;
403 
405  Attributes.add("value", * parameter.getValue().pINT);
406 
407  if (!saveElement("Parameter", Attributes)) success = false;
408 
409  break;
410 
412  Attributes.add("value", * parameter.getValue().pUINT);
413 
414  if (!saveElement("Parameter", Attributes)) success = false;
415 
416  break;
417 
419  Attributes.add("value", * parameter.getValue().pBOOL);
420 
421  if (!saveElement("Parameter", Attributes)) success = false;
422 
423  break;
424 
426  Attributes.add("value", * parameter.getValue().pSTRING);
427 
428  if (!saveElement("Parameter", Attributes)) success = false;
429 
430  break;
431 
433  Attributes.add("value", * parameter.getValue().pKEY);
434 
435  if (!saveElement("Parameter", Attributes)) success = false;
436 
437  break;
438 
440  File = * parameter.getValue().pFILE;
441 
442  if (!CDirEntry::isRelativePath(File) &&
444  File = CDirEntry::fileName(File);
445 
446  Attributes.add("value", File);
447 
448  if (!saveElement("Parameter", Attributes)) success = false;
449 
450  break;
451 
453  Attributes.add("value", * parameter.getValue().pCN);
454 
455  if (!saveElement("Parameter", Attributes)) success = false;
456 
457  break;
458 
460 
461  if (!startSaveElement("ParameterText", Attributes)) success = false;
462 
463  if (!saveData(*parameter.getValue().pEXPRESSION)) success = false;
464 
465  if (!endSaveElement("ParameterText")) success = false;
466 
467  break;
468 
470  Attributes.skip(1);
471 
472  if (!startSaveElement("ParameterGroup", Attributes)) success = false;
473 
474  if (!saveParameterGroup(* parameter.getValue().pGROUP)) success = false;
475 
476  if (!endSaveElement("ParameterGroup")) success = false;
477 
478  break;
479 
481  default:
482  success = false;
483  break;
484  }
485 
486  return success;
487 }
bool saveElement(const std::string &name, CXMLAttributeList &attributeList)
const std::string & getObjectName() const
bool startSaveElement(const std::string &name)
static bool isRelativePath(const std::string &path)
Definition: CDirEntry.cpp:414
bool saveParameterGroup(const std::vector< CCopasiParameter * > &group)
bool saveData(const std::string &data)
std::vector< CCopasiParameter * > * pGROUP
static std::string fileName(const std::string &path)
Definition: CDirEntry.cpp:119
CRegisteredObjectName * pCN
bool skip(const size_t &index)
const CCopasiParameter::Type & getType() const
const Value & getValue() const
unsigned C_INT32 * pUINT
bool endSaveElement(const std::string &name)
static const char * XMLType[]
static bool makePathRelative(std::string &absolutePath, const std::string &relativeTo)
Definition: CDirEntry.cpp:434
bool add(const std::string &name, const CType &value)
bool CCopasiXMLInterface::saveParameterGroup ( const std::vector< CCopasiParameter * > &  group)
protected

Save a Parameter Group.

Parameters
constCCopasiParameterGroup::parameterGroup & group
Returns
bool success

Definition at line 489 of file CCopasiXMLInterface.cpp.

References saveParameter().

Referenced by saveParameter(), CCopasiXML::savePlotList(), and CCopasiXML::saveTaskList().

490 {
491  bool success = true;
492 
493  std::vector< CCopasiParameter * >::const_iterator it = group.begin();
494  std::vector< CCopasiParameter * >::const_iterator end = group.end();
495 
496  for (; it != end; ++it)
497  if (!saveParameter(**it)) success = false;
498 
499  return success;
500 }
bool saveParameter(const CCopasiParameter &parameter)
bool CCopasiXMLInterface::saveXhtml ( const std::string &  xhtml)
protected

Save xhtml to the ostream

Parameters
conststd::string & xhtml
Returns
bool success

Definition at line 312 of file CCopasiXMLInterface.cpp.

References mIndent, mpOstream, and saveData().

Referenced by CCopasiXML::saveAnnotation(), and CCopasiXML::saveReportList().

313 {
314  // if there is nothing to save bail
315  if (xhtml.empty())
316  return true;
317 
318  std::string::size_type start = xhtml.find_first_not_of("\x0a\x0d\t ");
319 
320  if (start != std::string::npos && xhtml[start] == '<')
321  {
322  std::string::size_type pos = xhtml.find('>');
323  std::string FirstElement = xhtml.substr(0, pos);
324 
325  if (FirstElement.find("xmlns=\"http://www.w3.org/1999/xhtml\"") == std::string::npos
326  && FirstElement.find("xmlns='http://www.w3.org/1999/xhtml'") == std::string::npos)
327  FirstElement += " xmlns=\"http://www.w3.org/1999/xhtml\"";
328 
329  *mpOstream << mIndent << FirstElement << xhtml.substr(pos) << std::endl;
330  }
331  else
332  {
333  saveData(xhtml);
334  }
335 
336  return true;
337 }
bool saveData(const std::string &data)
bool CCopasiXMLInterface::startSaveElement ( const std::string &  name)
protected

Start saving an XML element to the ostream. Call endSaveElement to conclude.

Parameters
conststd::string & name
Returns
bool success

Definition at line 349 of file CCopasiXMLInterface.cpp.

References mIndent, and mpOstream.

Referenced by CCopasiXML::save(), CCopasiXML::saveAnnotation(), CCopasiXML::saveBoundingBox(), CCopasiXML::saveCurve(), CCopasiXML::saveCurveElements(), CCopasiXML::saveFunctionList(), CCopasiXML::saveGlobalRenderInformation(), CCopasiXML::saveGlobalStyle(), CCopasiXML::saveGroupElement(), CCopasiXML::saveGUI(), CCopasiXML::saveLayoutList(), CCopasiXML::saveLinearGradient(), CCopasiXML::saveLineEnding(), CCopasiXML::saveListOfGlobalRenderInformation(), CCopasiXML::saveListOfLocalRenderInformation(), CCopasiXML::saveLocalRenderInformation(), CCopasiXML::saveLocalStyle(), CCopasiXML::saveModel(), CCopasiXML::saveModelParameter(), saveParameter(), CCopasiXML::savePlotList(), CCopasiXML::savePolygonElement(), CCopasiXML::saveRadialGradient(), CCopasiXML::saveRenderCurveElement(), CCopasiXML::saveRenderInformationDefinitionElements(), CCopasiXML::saveRenderTextElement(), CCopasiXML::saveReportList(), CCopasiXML::saveReportSection(), CCopasiXML::saveSBMLReference(), and CCopasiXML::saveTaskList().

350 {
351  *mpOstream << mIndent << "<" << name << ">" << std::endl;
352 
353  mIndent += " ";
354  return true;
355 }
bool CCopasiXMLInterface::startSaveElement ( const std::string &  name,
CXMLAttributeList attributeList 
)
protected

Start saving an XML element to the ostream. Call endSaveElement to conclude.

Parameters
conststd::string & name
CXMLAttributeList& attributeList
Returns
bool success

Definition at line 357 of file CCopasiXMLInterface.cpp.

References mIndent, and mpOstream.

359 {
360  *mpOstream << mIndent << "<" << name;
361  *mpOstream << attributeList;
362  *mpOstream << ">" << std::endl;
363 
364  mIndent += " ";
365  return true;
366 }
std::string CCopasiXMLInterface::utf8 ( const std::string &  str)
static

Encode a given string to a valid XML string

Parameters
constC_FLOAT64 & dbl
Returns
std::string encoded Encode a given string to a utf-8 string
Parameters
conststd::string & str
Returns
std::string encoded

Definition at line 245 of file CCopasiXMLInterface.cpp.

Referenced by CMIRIAMResourceObject::unescapeId().

246 {
247  std::ostringstream utf8;
248 
249  /* Based on RFC 2279.
250  Since every string within COPASI is treated as latin1 and input
251  is only obtained through QT and Expat which will provide latin1
252  encoded strings the below should suffice. */
253  size_t i, imax;
254 
255  for (i = 0, imax = str.length(); i < imax; i++)
256  {
257  const unsigned char Char = str[i];
258 
259  if (Char < 0x80)
260  utf8 << Char;
261  else
262  {
263  utf8 << (unsigned char)(0xc0 + ((Char >> 6) & 0x03));
264  utf8 << (unsigned char)(0x80 + (Char & 0x3f));
265  }
266  }
267 
268  return utf8.str();
269 }
static std::string utf8(const std::string &str)

Member Data Documentation

std::string CCopasiXMLInterface::mIndent
protected
std::istream* CCopasiXMLInterface::mpIstream
protected

A pointer to the input stream

Definition at line 123 of file CCopasiXMLInterface.h.

Referenced by CCopasiXML::load().

std::ostream* CCopasiXMLInterface::mpOstream
protected
std::string CCopasiXMLInterface::mPWD
protected

The documentation for this class was generated from the following files: