From 74bb9cd27242b9320f99ff4d2b50c3051576cc14 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 8 Feb 2022 16:53:51 +0800 Subject: ... --- include/cru/xml/Base.h | 11 ++++ include/cru/xml/Base.hpp | 11 ---- include/cru/xml/XmlNode.h | 135 ++++++++++++++++++++++++++++++++++++++++++ include/cru/xml/XmlNode.hpp | 135 ------------------------------------------ include/cru/xml/XmlParser.h | 47 +++++++++++++++ include/cru/xml/XmlParser.hpp | 47 --------------- 6 files changed, 193 insertions(+), 193 deletions(-) create mode 100644 include/cru/xml/Base.h delete mode 100644 include/cru/xml/Base.hpp create mode 100644 include/cru/xml/XmlNode.h delete mode 100644 include/cru/xml/XmlNode.hpp create mode 100644 include/cru/xml/XmlParser.h delete mode 100644 include/cru/xml/XmlParser.hpp (limited to 'include/cru/xml') diff --git a/include/cru/xml/Base.h b/include/cru/xml/Base.h new file mode 100644 index 00000000..5d6fe144 --- /dev/null +++ b/include/cru/xml/Base.h @@ -0,0 +1,11 @@ +#pragma once + +#ifdef CRU_PLATFORM_WINDOWS +#ifdef CRU_XML_EXPORT_API +#define CRU_XML_API __declspec(dllexport) +#else +#define CRU_XML_API __declspec(dllimport) +#endif +#else +#define CRU_XML_API +#endif diff --git a/include/cru/xml/Base.hpp b/include/cru/xml/Base.hpp deleted file mode 100644 index 5d6fe144..00000000 --- a/include/cru/xml/Base.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#ifdef CRU_PLATFORM_WINDOWS -#ifdef CRU_XML_EXPORT_API -#define CRU_XML_API __declspec(dllexport) -#else -#define CRU_XML_API __declspec(dllimport) -#endif -#else -#define CRU_XML_API -#endif diff --git a/include/cru/xml/XmlNode.h b/include/cru/xml/XmlNode.h new file mode 100644 index 00000000..737a2d87 --- /dev/null +++ b/include/cru/xml/XmlNode.h @@ -0,0 +1,135 @@ +#pragma once + +#include "Base.h" + +#include "cru/common/String.h" + +#include +#include +#include +#include + +namespace cru::xml { +class XmlElementNode; +class XmlTextNode; + +class CRU_XML_API XmlNode { + friend XmlElementNode; + + public: + enum class Type { Text, Element }; + + protected: + explicit XmlNode(Type type) : type_(type) {} + + public: + CRU_DELETE_COPY(XmlNode) + CRU_DELETE_MOVE(XmlNode) + + virtual ~XmlNode() = default; + + Type GetType() const { return type_; } + XmlElementNode* GetParent() const { return parent_; } + + virtual XmlNode* Clone() const = 0; + + bool IsTextNode() const { return type_ == Type::Text; } + bool IsElementNode() const { return type_ == Type::Element; } + + XmlElementNode* AsElement(); + XmlTextNode* AsText(); + const XmlElementNode* AsElement() const; + const XmlTextNode* AsText() const; + + private: + const Type type_; + XmlElementNode* parent_ = nullptr; +}; + +class CRU_XML_API XmlTextNode : public XmlNode { + public: + XmlTextNode() : XmlNode(Type::Text) {} + explicit XmlTextNode(String text) + : XmlNode(Type::Text), text_(std::move(text)) {} + + CRU_DELETE_COPY(XmlTextNode) + CRU_DELETE_MOVE(XmlTextNode) + + ~XmlTextNode() override = default; + + public: + String GetText() const { return text_; } + void SetText(String text) { text_ = std::move(text); } + + XmlNode* Clone() const override { return new XmlTextNode(text_); } + + private: + String text_; +}; + +class CRU_XML_API XmlElementNode : public XmlNode { + public: + XmlElementNode() : XmlNode(Type::Element) {} + explicit XmlElementNode(String tag, + std::unordered_map attributes = {}) + : XmlNode(Type::Element), + tag_(std::move(tag)), + attributes_(std::move(attributes)) {} + + CRU_DELETE_COPY(XmlElementNode) + CRU_DELETE_MOVE(XmlElementNode) + + ~XmlElementNode() override; + + public: + String GetTag() const { return tag_; } + void SetTag(String tag) { tag_ = std::move(tag); } + const std::unordered_map& GetAttributes() const { + return attributes_; + } + void SetAttributes(std::unordered_map attributes) { + attributes_ = std::move(attributes); + } + const std::vector GetChildren() const { return children_; } + + Index GetChildCount() const { return children_.size(); } + String GetAttribute(const String& key) const { return attributes_.at(key); } + String GetAttributeCaseInsensitive(const String& key) const { + return *GetOptionalAttributeCaseInsensitive(key); + } + std::optional GetOptionalAttribute(const String& key) const { + auto it = attributes_.find(key); + if (it == attributes_.end()) { + return std::nullopt; + } + + return it->second; + } + std::optional GetOptionalAttributeCaseInsensitive( + const String& key) const { + for (auto it = attributes_.begin(); it != attributes_.end(); ++it) { + if (it->first.CaseInsensitiveCompare(key) == 0) { + return it->second; + } + } + + return std::nullopt; + } + + XmlNode* GetChildAt(Index index) const { return children_[index]; } + + void AddAttribute(String key, String value); + void AddChild(XmlNode* child); + + Index GetChildElementCount() const; + XmlElementNode* GetFirstChildElement() const; + + XmlNode* Clone() const override; + + private: + String tag_; + std::unordered_map attributes_; + std::vector children_; +}; + +} // namespace cru::xml diff --git a/include/cru/xml/XmlNode.hpp b/include/cru/xml/XmlNode.hpp deleted file mode 100644 index cfe6e155..00000000 --- a/include/cru/xml/XmlNode.hpp +++ /dev/null @@ -1,135 +0,0 @@ -#pragma once - -#include "Base.hpp" - -#include "cru/common/String.hpp" - -#include -#include -#include -#include - -namespace cru::xml { -class XmlElementNode; -class XmlTextNode; - -class CRU_XML_API XmlNode { - friend XmlElementNode; - - public: - enum class Type { Text, Element }; - - protected: - explicit XmlNode(Type type) : type_(type) {} - - public: - CRU_DELETE_COPY(XmlNode) - CRU_DELETE_MOVE(XmlNode) - - virtual ~XmlNode() = default; - - Type GetType() const { return type_; } - XmlElementNode* GetParent() const { return parent_; } - - virtual XmlNode* Clone() const = 0; - - bool IsTextNode() const { return type_ == Type::Text; } - bool IsElementNode() const { return type_ == Type::Element; } - - XmlElementNode* AsElement(); - XmlTextNode* AsText(); - const XmlElementNode* AsElement() const; - const XmlTextNode* AsText() const; - - private: - const Type type_; - XmlElementNode* parent_ = nullptr; -}; - -class CRU_XML_API XmlTextNode : public XmlNode { - public: - XmlTextNode() : XmlNode(Type::Text) {} - explicit XmlTextNode(String text) - : XmlNode(Type::Text), text_(std::move(text)) {} - - CRU_DELETE_COPY(XmlTextNode) - CRU_DELETE_MOVE(XmlTextNode) - - ~XmlTextNode() override = default; - - public: - String GetText() const { return text_; } - void SetText(String text) { text_ = std::move(text); } - - XmlNode* Clone() const override { return new XmlTextNode(text_); } - - private: - String text_; -}; - -class CRU_XML_API XmlElementNode : public XmlNode { - public: - XmlElementNode() : XmlNode(Type::Element) {} - explicit XmlElementNode(String tag, - std::unordered_map attributes = {}) - : XmlNode(Type::Element), - tag_(std::move(tag)), - attributes_(std::move(attributes)) {} - - CRU_DELETE_COPY(XmlElementNode) - CRU_DELETE_MOVE(XmlElementNode) - - ~XmlElementNode() override; - - public: - String GetTag() const { return tag_; } - void SetTag(String tag) { tag_ = std::move(tag); } - const std::unordered_map& GetAttributes() const { - return attributes_; - } - void SetAttributes(std::unordered_map attributes) { - attributes_ = std::move(attributes); - } - const std::vector GetChildren() const { return children_; } - - Index GetChildCount() const { return children_.size(); } - String GetAttribute(const String& key) const { return attributes_.at(key); } - String GetAttributeCaseInsensitive(const String& key) const { - return *GetOptionalAttributeCaseInsensitive(key); - } - std::optional GetOptionalAttribute(const String& key) const { - auto it = attributes_.find(key); - if (it == attributes_.end()) { - return std::nullopt; - } - - return it->second; - } - std::optional GetOptionalAttributeCaseInsensitive( - const String& key) const { - for (auto it = attributes_.begin(); it != attributes_.end(); ++it) { - if (it->first.CaseInsensitiveCompare(key) == 0) { - return it->second; - } - } - - return std::nullopt; - } - - XmlNode* GetChildAt(Index index) const { return children_[index]; } - - void AddAttribute(String key, String value); - void AddChild(XmlNode* child); - - Index GetChildElementCount() const; - XmlElementNode* GetFirstChildElement() const; - - XmlNode* Clone() const override; - - private: - String tag_; - std::unordered_map attributes_; - std::vector children_; -}; - -} // namespace cru::xml diff --git a/include/cru/xml/XmlParser.h b/include/cru/xml/XmlParser.h new file mode 100644 index 00000000..75664ce3 --- /dev/null +++ b/include/cru/xml/XmlParser.h @@ -0,0 +1,47 @@ +#pragma once + +#include "XmlNode.h" + +#include "cru/common/Exception.h" +#include "cru/common/String.h" + +#include + +namespace cru::xml { +class CRU_XML_API XmlParsingException : public Exception { + public: + using Exception::Exception; +}; + +class CRU_XML_API XmlParser { + public: + explicit XmlParser(String xml); + + CRU_DELETE_COPY(XmlParser) + CRU_DELETE_MOVE(XmlParser) + + ~XmlParser(); + + XmlElementNode* Parse(); + + private: + XmlElementNode* DoParse(); + + char16_t Read1(); + String ReadWithoutAdvance(int count = 1); + void ReadSpacesAndDiscard(); + String ReadSpaces(); + String ReadIdenitifier(); + String ReadAttributeString(); + + private: + String xml_; + + XmlElementNode* cache_ = nullptr; + + // Consider the while file enclosed by a single tag called $root. + XmlElementNode* pseudo_root_node_ = new XmlElementNode(u"$root"); + XmlElementNode* current_ = pseudo_root_node_; + int current_position_ = 0; +}; +} // namespace cru::xml diff --git a/include/cru/xml/XmlParser.hpp b/include/cru/xml/XmlParser.hpp deleted file mode 100644 index e916cc53..00000000 --- a/include/cru/xml/XmlParser.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#include "XmlNode.hpp" - -#include "cru/common/Exception.hpp" -#include "cru/common/String.hpp" - -#include - -namespace cru::xml { -class CRU_XML_API XmlParsingException : public Exception { - public: - using Exception::Exception; -}; - -class CRU_XML_API XmlParser { - public: - explicit XmlParser(String xml); - - CRU_DELETE_COPY(XmlParser) - CRU_DELETE_MOVE(XmlParser) - - ~XmlParser(); - - XmlElementNode* Parse(); - - private: - XmlElementNode* DoParse(); - - char16_t Read1(); - String ReadWithoutAdvance(int count = 1); - void ReadSpacesAndDiscard(); - String ReadSpaces(); - String ReadIdenitifier(); - String ReadAttributeString(); - - private: - String xml_; - - XmlElementNode* cache_ = nullptr; - - // Consider the while file enclosed by a single tag called $root. - XmlElementNode* pseudo_root_node_ = new XmlElementNode(u"$root"); - XmlElementNode* current_ = pseudo_root_node_; - int current_position_ = 0; -}; -} // namespace cru::xml -- cgit v1.2.3