Quellcode durchsuchen

Add indenting for toXML() method

Patrick vor 4 Jahren
Ursprung
Commit
2b5506a7fc

+ 5 - 1
source/io/xml/XMLDocument.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-30
- * Changed:         2020-09-30
+ * Changed:         2020-10-26
  *
  * */
 
@@ -38,6 +38,10 @@ std::string ls_std::XMLDocument::toXML()
 
   if(this->declaration != nullptr) {
     xmlString = this->declaration->toXML();
+
+    if(!xmlString.empty()) {
+      xmlString += "\n";
+    }
   }
 
   return xmlString + this->rootElement->toXML();

+ 29 - 6
source/io/xml/XMLNode.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-24
- * Changed:         2020-10-22
+ * Changed:         2020-10-26
  *
  * */
 
@@ -234,19 +234,37 @@ void ls_std::XMLNode::setValue(std::string _value)
 }
 
 std::string ls_std::XMLNode::toXML()
+{
+  return this->_toXML_(0);
+}
+
+std::string ls_std::XMLNode::_toXML_(uint8_t _tabSize)
 {
   std::string xmlStream {};
 
+  xmlStream += ls_std::XMLNode::_getTab(_tabSize);
   xmlStream += this->_toXMLOpenTag();
   xmlStream += this->_toXMLAttributes();
   xmlStream += this->_toXMLOpenTagClose();
-  xmlStream += this->value;
-  xmlStream += this->_toXMLChildren();
-  xmlStream += this->_toXMLCloseTag();
+  xmlStream += this->_toXMLValue();
+  xmlStream += this->_toXMLChildren(_tabSize + TAB_SIZE);
+  xmlStream += this->value.empty() ? ls_std::XMLNode::_getTab(_tabSize) : "";
+  xmlStream += this->_toXMLCloseTag() + "\n";
 
   return xmlStream;
 }
 
+std::string ls_std::XMLNode::_getTab(uint8_t _tabSize)
+{
+  std::string tab {};
+
+  for(uint8_t index = 0 ; index < _tabSize ; index++) {
+    tab += " ";
+  }
+
+  return tab;
+}
+
 bool ls_std::XMLNode::_hasAttribute(const std::string &_name)
 {
   bool exists {};
@@ -291,13 +309,13 @@ std::string ls_std::XMLNode::_toXMLAttributes()
   return stream;
 }
 
-std::string ls_std::XMLNode::_toXMLChildren()
+std::string ls_std::XMLNode::_toXMLChildren(uint8_t _tabSize)
 {
   std::string stream {};
 
   if(this->value.empty()) {
     for(const auto& _child : this->children) {
-      stream += _child->toXML();
+      stream += _child->_toXML_(_tabSize);
     }
   }
 
@@ -332,3 +350,8 @@ std::string ls_std::XMLNode::_toXMLOpenTagClose()
 
   return stream;
 }
+
+std::string ls_std::XMLNode::_toXMLValue()
+{
+  return this->value.empty() ? "\n" : this->value;
+}

+ 9 - 3
source/io/xml/XMLNode.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-24
- * Changed:         2020-10-25
+ * Changed:         2020-10-26
  *
  * */
 
@@ -47,22 +47,28 @@ namespace ls_std {
       void setValue(std::string _value);
       std::string toXML();
 
+    protected:
+
+      std::string _toXML_(uint8_t _tabSize);
+
     private:
 
       std::list<std::shared_ptr<XMLAttribute>> attributes {};
       std::list<std::shared_ptr<XMLNode>> children {};
       std::string name {};
-      const static uint8_t tabSize {4};
+      const static uint8_t TAB_SIZE {4};
       std::string value {};
 
+      static std::string _getTab(uint8_t _tabSize);
       bool _hasAttribute(const std::string& _name);
       bool _hasChild(const std::shared_ptr<XMLNode>& _child);
       bool _hasChild(const std::string& _name);
       std::string _toXMLAttributes();
-      std::string _toXMLChildren();
+      std::string _toXMLChildren(uint8_t _tabSize);
       std::string _toXMLCloseTag();
       std::string _toXMLOpenTag();
       std::string _toXMLOpenTagClose();
+      std::string _toXMLValue();
   };
 }
 

+ 14 - 2
test/cases/io/xml/XMLDocumentTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-30
- * Changed:         2020-09-30
+ * Changed:         2020-10-26
  *
  * */
 
@@ -69,7 +69,19 @@ namespace {
     std::string xmlStream = document.toXML();
 
     ASSERT_TRUE(!xmlStream.empty());
-    std::string expectedXMLContent = R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><dialog name="dungeon_001"><dialogUnit id="001"><text>Hello!</text></dialogUnit><dialogUnit id="002"><text>Hello again!</text></dialogUnit></dialog>)";
+
+    std::string expectedXMLContent =
+        R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
+<dialog name="dungeon_001">
+    <dialogUnit id="001">
+        <text>Hello!</text>
+    </dialogUnit>
+    <dialogUnit id="002">
+        <text>Hello again!</text>
+    </dialogUnit>
+</dialog>
+)";
+
     ASSERT_STREQ(expectedXMLContent.c_str(), xmlStream.c_str());
   }
 }