From 4e92e8709b30c385e1a88d7d4f76c50ee4a3d736 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 31 Dec 2021 00:26:55 +0800 Subject: ... --- include/cru/xml/XmlNode.hpp | 67 +++++++++++++++++++++++++++++++++++++++++++ include/cru/xml/XmlParser.hpp | 29 +++++++++++++++++++ src/xml/CMakeLists.txt | 5 ++++ src/xml/XmlNode.cpp | 14 +++++++++ src/xml/XmlParser.cpp | 27 +++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 include/cru/xml/XmlNode.hpp create mode 100644 include/cru/xml/XmlParser.hpp create mode 100644 src/xml/XmlNode.cpp create mode 100644 src/xml/XmlParser.cpp diff --git a/include/cru/xml/XmlNode.hpp b/include/cru/xml/XmlNode.hpp new file mode 100644 index 00000000..1787d6a0 --- /dev/null +++ b/include/cru/xml/XmlNode.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include "cru/common/String.hpp" + +#include +#include +#include + +namespace cru::xml { +class XmlNode { + public: + enum class Type { Text, Element }; + + static XmlNode CreateText(String text) { + XmlNode node(Type::Text); + node.SetText(std::move(text)); + return node; + } + + static XmlNode CreateElement( + String tag, std::unordered_map attributes = {}, + std::vector children = {}) { + XmlNode node(Type::Element); + node.SetTag(std::move(tag)); + node.SetAttributes(std::move(attributes)); + node.SetChildren(std::move(children)); + return node; + } + + explicit XmlNode(Type type) : type_(type) {} + + CRU_DEFAULT_COPY(XmlNode) + CRU_DEFAULT_MOVE(XmlNode) + + ~XmlNode() = default; + + Type GetType() const { return type_; } + String GetText() const { return text_; } + String GetTag() const { return tag_; } + std::unordered_map GetAttributes() { return attributes_; } + const std::unordered_map& GetAttributes() const { + return attributes_; + } + std::vector& GetChildren() { return children_; } + const std::vector& GetChildren() const { return children_; } + + void SetType(Type type) { type_ = type; } + void SetText(String text) { text_ = std::move(text); } + void SetTag(String tag) { tag_ = std::move(tag); } + void SetAttributes(std::unordered_map attributes) { + attributes_ = std::move(attributes); + } + void SetChildren(std::vector children) { + children_ = std::move(children); + } + + private: + Type type_; + String text_; + String tag_; + std::unordered_map attributes_; + std::vector children_; +}; + +bool operator==(const XmlNode& lhs, const XmlNode& rhs); +bool operator!=(const XmlNode& lhs, const XmlNode& rhs); +} // namespace cru::xml diff --git a/include/cru/xml/XmlParser.hpp b/include/cru/xml/XmlParser.hpp new file mode 100644 index 00000000..19f569d1 --- /dev/null +++ b/include/cru/xml/XmlParser.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "XmlNode.hpp" + +#include "cru/common/String.hpp" + +#include + +namespace cru::xml { +class XmlParser { + public: + explicit XmlParser(String xml); + + CRU_DELETE_COPY(XmlParser) + CRU_DELETE_MOVE(XmlParser) + + ~XmlParser(); + + XmlNode Parse(); + + private: + XmlNode DoParse(); + + private: + String xml_; + + std::optional root_node_; +}; +} // namespace cru::xml diff --git a/src/xml/CMakeLists.txt b/src/xml/CMakeLists.txt index e69de29b..20e889ec 100644 --- a/src/xml/CMakeLists.txt +++ b/src/xml/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(cru_xml SHARED + XmlNode.cpp + XmlParser.cpp +) +target_link_libraries(cru_xml PUBLIC cru_base) diff --git a/src/xml/XmlNode.cpp b/src/xml/XmlNode.cpp new file mode 100644 index 00000000..cfeb5cf9 --- /dev/null +++ b/src/xml/XmlNode.cpp @@ -0,0 +1,14 @@ +#include "cru/xml/XmlNode.hpp" + +namespace cru::xml { +bool operator==(const XmlNode& lhs, const XmlNode& rhs) { + return lhs.GetType() == rhs.GetType() && lhs.GetText() == rhs.GetText() && + lhs.GetTag() == rhs.GetTag() && + lhs.GetAttributes() == rhs.GetAttributes() && + lhs.GetChildren() == rhs.GetChildren(); +} + +bool operator!=(const XmlNode& lhs, const XmlNode& rhs) { + return !(lhs == rhs); +} +} // namespace cru::xml diff --git a/src/xml/XmlParser.cpp b/src/xml/XmlParser.cpp new file mode 100644 index 00000000..23407d11 --- /dev/null +++ b/src/xml/XmlParser.cpp @@ -0,0 +1,27 @@ +#include "cru/xml/XmlParser.hpp" + +namespace cru::xml { +XmlNode XmlParser::Parse() { + if (!root_node_) { + root_node_ = DoParse(); + } + return *root_node_; +} + +XmlNode XmlParser::DoParse() { + XmlNode root(XmlNode::Type::Element); + XmlNode* current = &root; + int current_position = 0; + + // TODO: Implement this. + while (current_position < xml_.size()) { + switch (xml_[current_position]) { + case '<': { + break; + } + } + } + + return root; +} +} // namespace cru::xml -- cgit v1.2.3