Skip to content

Commit

Permalink
[DLG] Minichanges to document.h and Xml_document.h
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolodares committed Jul 30, 2024
1 parent ae0ef51 commit 7ba4f91
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
95 changes: 49 additions & 46 deletions lion/io/Xml_document.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifndef __XML_DOCUMENT_H__
#define __XML_DOCUMENT_H__


#include "tinyxml2.h"

#include "Xml_element.h"
#include "document.h"


class Xml_document : public Document
{
public:
Expand All @@ -14,7 +16,7 @@ class Xml_document : public Document

//! Constructor
//! @param[in] name: name of the file
Xml_document(const std::string& name = "", bool load_file = false) : _name(name) { if (load_file) load();}
Xml_document(const std::string& name = "", bool load_file = false) : base_type(name) { if (load_file) load();}

//! Destructor
~Xml_document() { _doc.Clear(); }
Expand All @@ -25,7 +27,7 @@ class Xml_document : public Document

//! Save an XML document to file
//! @return true if OK, false if NOT OK
bool save() override { return _doc.SaveFile(_name.c_str()) == tinyxml2::XML_SUCCESS; }
bool save() override { return _doc.SaveFile(get_file_name().c_str()) == tinyxml2::XML_SUCCESS; }

//! Parse an XML document from a string
void parse(const char *contents) override { _doc.Parse(contents); }
Expand All @@ -50,8 +52,9 @@ class Xml_document : public Document
//! Print
void print(std::ostream&) const;


private:
std::string _name; //! Name of the file on disk

tinyxml2::XMLDocument _doc; //! Tinyxml2 document object
};

Expand Down Expand Up @@ -139,92 +142,92 @@ inline Xml_element Xml_document::add_element(const std::string& full_path)

inline void Xml_document::load()
{
switch (_doc.LoadFile(_name.c_str()))
{
case(tinyxml2::XML_SUCCESS):
switch (_doc.LoadFile(get_file_name().c_str())) {
case tinyxml2::XML_SUCCESS:
break;

case(tinyxml2::XML_NO_ATTRIBUTE):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_NO_ATTRIBUTE");
case tinyxml2::XML_NO_ATTRIBUTE:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_NO_ATTRIBUTE");
break;

case(tinyxml2::XML_WRONG_ATTRIBUTE_TYPE):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_WRONG_ATTRIBUTE_TYPE");
case tinyxml2::XML_WRONG_ATTRIBUTE_TYPE:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_WRONG_ATTRIBUTE_TYPE");
break;

case(tinyxml2::XML_ERROR_FILE_NOT_FOUND):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_FILE_NOT_FOUND");
case tinyxml2::XML_ERROR_FILE_NOT_FOUND:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_FILE_NOT_FOUND");
break;

case(tinyxml2::XML_ERROR_FILE_COULD_NOT_BE_OPENED):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_FILE_COULD_NOT_BE_OPENED");
case tinyxml2::XML_ERROR_FILE_COULD_NOT_BE_OPENED:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_FILE_COULD_NOT_BE_OPENED");
break;

case(tinyxml2::XML_ERROR_FILE_READ_ERROR):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_FILE_READ_ERROR");
case tinyxml2::XML_ERROR_FILE_READ_ERROR:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_FILE_READ_ERROR");
break;

case(tinyxml2::XML_ERROR_PARSING_ELEMENT):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_PARSING_ELEMENT");
case tinyxml2::XML_ERROR_PARSING_ELEMENT:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_PARSING_ELEMENT");
break;

case(tinyxml2::XML_ERROR_PARSING_ATTRIBUTE):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_PARSING_ATTRIBUTE");
case tinyxml2::XML_ERROR_PARSING_ATTRIBUTE:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_PARSING_ATTRIBUTE");
break;

case(tinyxml2::XML_ERROR_PARSING_TEXT):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_PARSING_TEXT");
case tinyxml2::XML_ERROR_PARSING_TEXT:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_PARSING_TEXT");
break;

case(tinyxml2::XML_ERROR_PARSING_CDATA):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_PARSING_CDATA");
case tinyxml2::XML_ERROR_PARSING_CDATA:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_PARSING_CDATA");
break;

case(tinyxml2::XML_ERROR_PARSING_COMMENT):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_PARSING_COMMENT");
case tinyxml2::XML_ERROR_PARSING_COMMENT:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_PARSING_COMMENT");
break;

case(tinyxml2::XML_ERROR_PARSING_DECLARATION):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_PARSING_DECLARATION");
case tinyxml2::XML_ERROR_PARSING_DECLARATION:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_PARSING_DECLARATION");
break;

case(tinyxml2::XML_ERROR_PARSING_UNKNOWN):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_PARSING_UNKNOWN");
case tinyxml2::XML_ERROR_PARSING_UNKNOWN:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_PARSING_UNKNOWN");
break;

case(tinyxml2::XML_ERROR_EMPTY_DOCUMENT):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_EMPTY_DOCUMENT");
case tinyxml2::XML_ERROR_EMPTY_DOCUMENT:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_EMPTY_DOCUMENT");
break;

case(tinyxml2::XML_ERROR_MISMATCHED_ELEMENT):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_MISMATCHED_ELEMENT");
case tinyxml2::XML_ERROR_MISMATCHED_ELEMENT:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_MISMATCHED_ELEMENT");
break;

case(tinyxml2::XML_ERROR_PARSING):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_PARSING");
case tinyxml2::XML_ERROR_PARSING:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_PARSING");
break;

case(tinyxml2::XML_CAN_NOT_CONVERT_TEXT):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_CAN_NOT_CONVERT_TEXT");
case tinyxml2::XML_CAN_NOT_CONVERT_TEXT:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_CAN_NOT_CONVERT_TEXT");
break;

case(tinyxml2::XML_NO_TEXT_NODE):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_NO_TEXT_NODE");
case tinyxml2::XML_NO_TEXT_NODE:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_NO_TEXT_NODE");
break;

case(tinyxml2::XML_ELEMENT_DEPTH_EXCEEDED):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ELEMENT_DEPTH_EXCEEDED");
case tinyxml2::XML_ELEMENT_DEPTH_EXCEEDED:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ELEMENT_DEPTH_EXCEEDED");
break;

case(tinyxml2::XML_ERROR_COUNT):
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": XML_ERROR_COUNT");
case tinyxml2::XML_ERROR_COUNT:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": XML_ERROR_COUNT");
break;

default:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + _name + "\": error code was not recognized");
default:
throw lion_exception("[ERROR] Xml_document::load() -> Tinyxml2 error loading file \"" + get_file_name() + "\": error code was not recognized");
}
}


inline bool Xml_document::has_element(const std::string& name)
{
// Divide the string in root_name/the_rest
Expand Down
2 changes: 1 addition & 1 deletion lion/io/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Document
return save();
}

const auto& get_file_name() const { return _name; }
const std::string& get_file_name() const { return _name; }

virtual void parse(const char* contents) = 0;

Expand Down

0 comments on commit 7ba4f91

Please sign in to comment.