ソースを参照

Implemented XMLReader class (incoomplete)

- implemented XMLReader functionality,
read implementation is missing
- added tests for XMLReader class
Patrick 3 年 前
コミット
029ecee587

+ 4 - 2
CMakeLists.txt

@@ -121,7 +121,8 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/base/Version.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLDeclaration.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLDocument.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLReader.hpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLReader.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLReader.cpp)
 
 set(LIBRARY_SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/lib/tiny_xml_2/include/tinyxml2.h
@@ -168,7 +169,8 @@ set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLNodeTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/base/VersionTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLDeclarationTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLDocumentTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLDocumentTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLReaderTest.cpp)
 
 ##########################################################
 # Build

+ 81 - 0
source/io/xml/XMLReader.cpp

@@ -0,0 +1,81 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-10-10
+ * Changed:         2020-10-10
+ *
+ * */
+
+#include "XMLReader.hpp"
+#include "../../exception/IllegalArgumentException.hpp"
+#include "../FileReader.hpp"
+
+ls_std::XMLReader::XMLReader(const std::shared_ptr<ls_std::XMLDocument>& _document, const std::string& _absolutePath) :
+Class("XMLReader"),
+xmlFile(ls_std::File {_absolutePath}),
+document(_document)
+{
+  XMLReader::_checkDocumentExistence(_document);
+  XMLReader::_checkFileExistence(this->xmlFile);
+}
+
+ls_std::byte_field ls_std::XMLReader::read()
+{
+  ls_std::byte_field data = ls_std::FileReader {this->xmlFile}.read();
+  this->_read(data);
+
+  return data;
+}
+
+std::shared_ptr<ls_std::XMLDocument> ls_std::XMLReader::getDocument()
+{
+  return this->document;
+}
+
+void ls_std::XMLReader::setFile(const ls_std::File &_xmlFile)
+{
+  XMLReader::_checkFileExistence(_xmlFile);
+  this->xmlFile = _xmlFile;
+}
+
+void ls_std::XMLReader::_checkDocumentExistence(const std::shared_ptr<ls_std::XMLDocument>& _document)
+{
+  if(_document == nullptr) {
+    throw ls_std::IllegalArgumentException {};
+  }
+}
+
+void ls_std::XMLReader::_checkFileExistence(ls_std::File _xmlFile)
+{
+  if(!_xmlFile.exists()) {
+    throw ls_std::IllegalArgumentException {};
+  }
+}
+
+void ls_std::XMLReader::_read(const ls_std::byte_field &_data)
+{
+  uint8_t mode {};
+
+  for(std::string::size_type i = 0 ; i < _data.size() ; i++) {
+    switch(mode)
+    {
+      case 1: // xml declaration
+      {
+
+      } break;
+      case 2: // xml opening tag
+      {
+
+      } break;
+      case 3: // xml closing tag
+      {
+
+      } break;
+      case 4: // xml attribute
+      {
+
+      } break;
+    }
+  }
+}

+ 13 - 3
source/io/xml/XMLReader.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-10-08
- * Changed:         2020-10-08
+ * Changed:         2020-10-10
  *
  * */
 
@@ -13,22 +13,32 @@
 #include "../../base/Class.hpp"
 #include "XMLDocument.hpp"
 #include "../IReader.hpp"
+#include "../File.hpp"
 
 namespace ls_std {
   class XMLReader : public Class, IReader {
     public:
 
-      explicit XMLReader(std::shared_ptr<ls_std::XMLDocument> _document, std::string _path);
+      explicit XMLReader(const std::shared_ptr<ls_std::XMLDocument>& _document, const std::string& _absolutePath);
       ~XMLReader() = default;
 
       // implementation
 
       ls_std::byte_field read() override;
 
+      // additional functionality
+
+      std::shared_ptr<ls_std::XMLDocument> getDocument();
+      void setFile(const ls_std::File& _xmlFile);
+
     private:
 
       std::shared_ptr<ls_std::XMLDocument> document {};
-      std::string path {};
+      ls_std::File xmlFile;
+
+      static void _checkDocumentExistence(const std::shared_ptr<ls_std::XMLDocument>& _document);
+      static void _checkFileExistence(ls_std::File _xmlFile);
+      void _read(const ls_std::byte_field& _data);
   };
 }
 

+ 50 - 0
test/cases/io/xml/XMLReaderTest.cpp

@@ -0,0 +1,50 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-10-10
+ * Changed:         2020-10-10
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../../../source/io/xml/XMLReader.hpp"
+#include "../../../TestHelper.hpp"
+
+namespace {
+  class XMLReaderTest : public ::testing::Test {
+    protected:
+
+      XMLReaderTest() = default;
+      ~XMLReaderTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(XMLReaderTest, read)
+  {
+    std::string xmlPath = TestHelper::getResourcesFolderLocation() + "state_machine_test.xml";
+    ls_std::XMLReader xmlReader {std::make_shared<ls_std::XMLDocument>(), xmlPath};
+
+    ASSERT_TRUE(!xmlReader.read().empty());
+  }
+
+  TEST_F(XMLReaderTest, getDocument)
+  {
+    std::string xmlPath = TestHelper::getResourcesFolderLocation() + "state_machine_test.xml";
+    ls_std::XMLReader xmlReader {std::make_shared<ls_std::XMLDocument>(), xmlPath};
+
+    ASSERT_TRUE(xmlReader.getDocument() != nullptr);
+  }
+
+  TEST_F(XMLReaderTest, setFile)
+  {
+    std::string xmlPath = TestHelper::getResourcesFolderLocation() + "state_machine_test.xml";
+    ls_std::XMLReader xmlReader {std::make_shared<ls_std::XMLDocument>(), xmlPath};
+    ls_std::File xmlFile {xmlPath};
+    xmlReader.setFile(xmlFile);
+
+    ASSERT_TRUE(true);
+  }
+}