From 1a6111e3f02b0a9cff0f81fb524b4dfb7d69854b Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Tue, 4 Nov 2025 21:59:42 +0800 Subject: Move xml to base. --- include/cru/base/xml/XmlNode.h | 138 ++++++++++++++++++++ include/cru/base/xml/XmlParser.h | 43 ++++++ include/cru/ui/ThemeResourceDictionary.h | 2 +- include/cru/ui/mapper/BorderStyleMapper.h | 2 +- include/cru/ui/mapper/BrushMapper.h | 2 +- include/cru/ui/mapper/ColorMapper.h | 2 +- include/cru/ui/mapper/CursorMapper.h | 2 +- include/cru/ui/mapper/Mapper.h | 2 +- include/cru/ui/mapper/ThicknessMapper.h | 2 +- include/cru/ui/mapper/style/BorderStylerMapper.h | 2 +- include/cru/ui/mapper/style/IConditionMapper.h | 2 +- include/cru/ui/mapper/style/IStylerMapper.h | 2 +- include/cru/ui/mapper/style/NoConditionMapper.h | 2 +- include/cru/ui/mapper/style/StyleRuleMapper.h | 2 +- include/cru/xml/Base.h | 11 -- include/cru/xml/XmlNode.h | 158 ----------------------- include/cru/xml/XmlParser.h | 44 ------- 17 files changed, 193 insertions(+), 225 deletions(-) create mode 100644 include/cru/base/xml/XmlNode.h create mode 100644 include/cru/base/xml/XmlParser.h delete mode 100644 include/cru/xml/Base.h delete mode 100644 include/cru/xml/XmlNode.h delete mode 100644 include/cru/xml/XmlParser.h (limited to 'include') diff --git a/include/cru/base/xml/XmlNode.h b/include/cru/base/xml/XmlNode.h new file mode 100644 index 00000000..e9090b40 --- /dev/null +++ b/include/cru/base/xml/XmlNode.h @@ -0,0 +1,138 @@ +#pragma once + +#include "../Base.h" +#include "../StringUtil.h" + +namespace cru::xml { +class XmlElementNode; +class XmlTextNode; +class XmlCommentNode; + +class CRU_BASE_API XmlNode : public Object { + friend XmlElementNode; + + public: + enum class Type { Text, Element, Comment }; + + protected: + explicit XmlNode(Type type) : type_(type) {} + + public: + 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; } + bool IsCommentNode() const { return type_ == Type::Comment; } + + XmlElementNode* AsElement(); + XmlTextNode* AsText(); + XmlCommentNode* AsComment(); + const XmlElementNode* AsElement() const; + const XmlTextNode* AsText() const; + const XmlCommentNode* AsComment() const; + + private: + const Type type_; + XmlElementNode* parent_ = nullptr; +}; + +class CRU_BASE_API XmlTextNode : public XmlNode { + public: + XmlTextNode() : XmlNode(Type::Text) {} + explicit XmlTextNode(std::string text) + : XmlNode(Type::Text), text_(std::move(text)) {} + + public: + std::string GetText() const { return text_; } + void SetText(std::string text) { text_ = std::move(text); } + + XmlNode* Clone() const override { return new XmlTextNode(text_); } + + private: + std::string text_; +}; + +class CRU_BASE_API XmlElementNode : public XmlNode { + public: + XmlElementNode() : XmlNode(Type::Element) {} + explicit XmlElementNode( + std::string tag, + std::unordered_map attributes = {}) + : XmlNode(Type::Element), + tag_(std::move(tag)), + attributes_(std::move(attributes)) {} + + ~XmlElementNode() override; + + public: + std::string GetTag() const { return tag_; } + void SetTag(std::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(); } + std::string GetAttributeValue(const std::string& key) const { + return attributes_.at(key); + } + std::string GetAttributeValueCaseInsensitive(const std::string& key) const { + return *GetOptionalAttributeValueCaseInsensitive(key); + } + std::optional GetOptionalAttributeValue( + const std::string& key) const { + auto it = attributes_.find(key); + if (it == attributes_.end()) { + return std::nullopt; + } + + return it->second; + } + std::optional GetOptionalAttributeValueCaseInsensitive( + const std::string& key) const { + for (auto it = attributes_.begin(); it != attributes_.end(); ++it) { + if (cru::string::CaseInsensitiveCompare(it->first, key) == 0) { + return it->second; + } + } + + return std::nullopt; + } + + XmlNode* GetChildAt(Index index) const { return children_[index]; } + + void AddAttribute(std::string key, std::string value); + void AddChild(XmlNode* child); + + Index GetChildElementCount() const; + XmlElementNode* GetFirstChildElement() const; + + XmlNode* Clone() const override; + + private: + std::string tag_; + std::unordered_map attributes_; + std::vector children_; +}; + +class CRU_BASE_API XmlCommentNode : public XmlNode { + public: + XmlCommentNode() : XmlNode(Type::Comment) {} + explicit XmlCommentNode(std::string text) + : XmlNode(Type::Comment), text_(std::move(text)) {} + + std::string GetText() const { return text_; } + void SetText(std::string text) { text_ = std::move(text); } + + XmlNode* Clone() const override; + + private: + std::string text_; +}; +} // namespace cru::xml diff --git a/include/cru/base/xml/XmlParser.h b/include/cru/base/xml/XmlParser.h new file mode 100644 index 00000000..22b529d7 --- /dev/null +++ b/include/cru/base/xml/XmlParser.h @@ -0,0 +1,43 @@ +#pragma once + +#include "../Exception.h" +#include "XmlNode.h" + +namespace cru::xml { +class CRU_BASE_API XmlParsingException : public Exception { + public: + using Exception::Exception; +}; + +class CRU_BASE_API XmlParser { + public: + explicit XmlParser(std::string xml); + + CRU_DELETE_COPY(XmlParser) + CRU_DELETE_MOVE(XmlParser) + + ~XmlParser(); + + XmlElementNode* Parse(); + + private: + XmlElementNode* DoParse(); + + char16_t Read1(); + std::string ReadWithoutAdvance(int count = 1); + void ReadSpacesAndDiscard(); + std::string ReadSpaces(); + std::string ReadIdenitifier(); + std::string ReadAttributeString(); + + private: + std::string xml_; + + XmlElementNode* cache_ = nullptr; + + // Consider the while file enclosed by a single tag called $root. + XmlElementNode* pseudo_root_node_ = new XmlElementNode("$root"); + XmlElementNode* current_ = pseudo_root_node_; + int current_position_ = 0; +}; +} // namespace cru::xml diff --git a/include/cru/ui/ThemeResourceDictionary.h b/include/cru/ui/ThemeResourceDictionary.h index f28a61db..3995fe25 100644 --- a/include/cru/ui/ThemeResourceDictionary.h +++ b/include/cru/ui/ThemeResourceDictionary.h @@ -2,7 +2,7 @@ #include "Base.h" #include "cru/base/Base.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" #include "mapper/MapperRegistry.h" #include "style/StyleRuleSet.h" diff --git a/include/cru/ui/mapper/BorderStyleMapper.h b/include/cru/ui/mapper/BorderStyleMapper.h index d585639f..fd84b0b0 100644 --- a/include/cru/ui/mapper/BorderStyleMapper.h +++ b/include/cru/ui/mapper/BorderStyleMapper.h @@ -1,7 +1,7 @@ #pragma once #include "Mapper.h" #include "cru/ui/style/ApplyBorderStyleInfo.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" namespace cru::ui::mapper { class CRU_UI_API BorderStyleMapper diff --git a/include/cru/ui/mapper/BrushMapper.h b/include/cru/ui/mapper/BrushMapper.h index 3b918dc7..3972d6ff 100644 --- a/include/cru/ui/mapper/BrushMapper.h +++ b/include/cru/ui/mapper/BrushMapper.h @@ -2,7 +2,7 @@ #include "Mapper.h" #include "cru/base/Base.h" #include "cru/platform/graphics/Brush.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" namespace cru::ui::mapper { class BrushMapper : public BasicSharedPtrMapper { diff --git a/include/cru/ui/mapper/ColorMapper.h b/include/cru/ui/mapper/ColorMapper.h index e3846712..9555bf5a 100644 --- a/include/cru/ui/mapper/ColorMapper.h +++ b/include/cru/ui/mapper/ColorMapper.h @@ -1,6 +1,6 @@ #pragma once #include "Mapper.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" namespace cru::ui::mapper { class CRU_UI_API ColorMapper : public BasicMapper { diff --git a/include/cru/ui/mapper/CursorMapper.h b/include/cru/ui/mapper/CursorMapper.h index 951291c1..4d55f849 100644 --- a/include/cru/ui/mapper/CursorMapper.h +++ b/include/cru/ui/mapper/CursorMapper.h @@ -1,7 +1,7 @@ #pragma once #include "Mapper.h" #include "cru/platform/gui/Cursor.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" namespace cru::ui::mapper { class CRU_UI_API CursorMapper : public BasicSharedPtrMapper { diff --git a/include/cru/ui/mapper/Mapper.h b/include/cru/ui/mapper/Mapper.h index a4507fc9..e4a4c528 100644 --- a/include/cru/ui/mapper/Mapper.h +++ b/include/cru/ui/mapper/Mapper.h @@ -3,7 +3,7 @@ #include "cru/base/ClonablePtr.h" #include "cru/base/Exception.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" #include #include diff --git a/include/cru/ui/mapper/ThicknessMapper.h b/include/cru/ui/mapper/ThicknessMapper.h index 84b6b036..25885cbf 100644 --- a/include/cru/ui/mapper/ThicknessMapper.h +++ b/include/cru/ui/mapper/ThicknessMapper.h @@ -3,7 +3,7 @@ #include "../Base.h" #include "cru/base/Base.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" namespace cru::ui::mapper { class CRU_UI_API ThicknessMapper : public BasicMapper { diff --git a/include/cru/ui/mapper/style/BorderStylerMapper.h b/include/cru/ui/mapper/style/BorderStylerMapper.h index 57b5dce4..8cb4d392 100644 --- a/include/cru/ui/mapper/style/BorderStylerMapper.h +++ b/include/cru/ui/mapper/style/BorderStylerMapper.h @@ -3,7 +3,7 @@ #include "cru/base/ClonablePtr.h" #include "cru/ui/mapper/style/IStylerMapper.h" #include "cru/ui/style/Styler.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" namespace cru::ui::mapper::style { class CRU_UI_API BorderStylerMapper diff --git a/include/cru/ui/mapper/style/IConditionMapper.h b/include/cru/ui/mapper/style/IConditionMapper.h index bfe24224..7bfd1427 100644 --- a/include/cru/ui/mapper/style/IConditionMapper.h +++ b/include/cru/ui/mapper/style/IConditionMapper.h @@ -3,7 +3,7 @@ #include "cru/base/ClonablePtr.h" #include "cru/ui/mapper/Mapper.h" #include "cru/ui/style/Condition.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" namespace cru::ui::mapper::style { struct CRU_UI_API IConditionMapper : virtual Interface { diff --git a/include/cru/ui/mapper/style/IStylerMapper.h b/include/cru/ui/mapper/style/IStylerMapper.h index adf9b6fb..4aa43665 100644 --- a/include/cru/ui/mapper/style/IStylerMapper.h +++ b/include/cru/ui/mapper/style/IStylerMapper.h @@ -3,7 +3,7 @@ #include "cru/base/ClonablePtr.h" #include "cru/ui/mapper/Mapper.h" #include "cru/ui/style/Styler.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" namespace cru::ui::mapper::style { struct CRU_UI_API IStylerMapper : virtual Interface { diff --git a/include/cru/ui/mapper/style/NoConditionMapper.h b/include/cru/ui/mapper/style/NoConditionMapper.h index 06106514..bd2adf14 100644 --- a/include/cru/ui/mapper/style/NoConditionMapper.h +++ b/include/cru/ui/mapper/style/NoConditionMapper.h @@ -4,7 +4,7 @@ #include "cru/base/Base.h" #include "cru/base/ClonablePtr.h" #include "cru/ui/style/Condition.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" namespace cru::ui::mapper::style { class CRU_UI_API NoConditionMapper diff --git a/include/cru/ui/mapper/style/StyleRuleMapper.h b/include/cru/ui/mapper/style/StyleRuleMapper.h index 12fcb85b..b4cb95e7 100644 --- a/include/cru/ui/mapper/style/StyleRuleMapper.h +++ b/include/cru/ui/mapper/style/StyleRuleMapper.h @@ -3,7 +3,7 @@ #include "cru/base/Base.h" #include "cru/base/ClonablePtr.h" #include "cru/ui/style/StyleRule.h" -#include "cru/xml/XmlNode.h" +#include "cru/base/xml/XmlNode.h" namespace cru::ui::mapper::style { class CRU_UI_API StyleRuleMapper : public BasicClonablePtrMapper { diff --git a/include/cru/xml/Base.h b/include/cru/xml/Base.h deleted file mode 100644 index aa3aeaa2..00000000 --- a/include/cru/xml/Base.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#ifdef CRU_IS_DLL -#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 deleted file mode 100644 index 1574f6fa..00000000 --- a/include/cru/xml/XmlNode.h +++ /dev/null @@ -1,158 +0,0 @@ -#pragma once - -#include "Base.h" - -#include -#include -#include -#include - -namespace cru::xml { -class XmlElementNode; -class XmlTextNode; -class XmlCommentNode; - -class CRU_XML_API XmlNode { - friend XmlElementNode; - - public: - enum class Type { Text, Element, Comment }; - - 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; } - bool IsCommentNode() const { return type_ == Type::Comment; } - - XmlElementNode* AsElement(); - XmlTextNode* AsText(); - XmlCommentNode* AsComment(); - const XmlElementNode* AsElement() const; - const XmlTextNode* AsText() const; - const XmlCommentNode* AsComment() const; - - private: - const Type type_; - XmlElementNode* parent_ = nullptr; -}; - -class CRU_XML_API XmlTextNode : public XmlNode { - public: - XmlTextNode() : XmlNode(Type::Text) {} - explicit XmlTextNode(std::string text) - : XmlNode(Type::Text), text_(std::move(text)) {} - - CRU_DELETE_COPY(XmlTextNode) - CRU_DELETE_MOVE(XmlTextNode) - - ~XmlTextNode() override = default; - - public: - std::string GetText() const { return text_; } - void SetText(std::string text) { text_ = std::move(text); } - - XmlNode* Clone() const override { return new XmlTextNode(text_); } - - private: - std::string text_; -}; - -class CRU_XML_API XmlElementNode : public XmlNode { - public: - XmlElementNode() : XmlNode(Type::Element) {} - explicit XmlElementNode(std::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: - std::string GetTag() const { return tag_; } - void SetTag(std::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(); } - std::string GetAttributeValue(const std::string& key) const { - return attributes_.at(key); - } - std::string GetAttributeValueCaseInsensitive(const std::string& key) const { - return *GetOptionalAttributeValueCaseInsensitive(key); - } - std::optional GetOptionalAttributeValue(const std::string& key) const { - auto it = attributes_.find(key); - if (it == attributes_.end()) { - return std::nullopt; - } - - return it->second; - } - std::optional GetOptionalAttributeValueCaseInsensitive( - const std::string& key) const { - for (auto it = attributes_.begin(); it != attributes_.end(); ++it) { - if (cru::string::CaseInsensitiveCompare(it->first, key) == 0) { - return it->second; - } - } - - return std::nullopt; - } - - XmlNode* GetChildAt(Index index) const { return children_[index]; } - - void AddAttribute(std::string key, std::string value); - void AddChild(XmlNode* child); - - Index GetChildElementCount() const; - XmlElementNode* GetFirstChildElement() const; - - XmlNode* Clone() const override; - - private: - std::string tag_; - std::unordered_map attributes_; - std::vector children_; -}; - -class CRU_XML_API XmlCommentNode : public XmlNode { - public: - XmlCommentNode() : XmlNode(Type::Comment) {} - explicit XmlCommentNode(std::string text) - : XmlNode(Type::Comment), text_(std::move(text)) {} - - CRU_DELETE_COPY(XmlCommentNode) - CRU_DELETE_MOVE(XmlCommentNode) - - ~XmlCommentNode() override; - - std::string GetText() const { return text_; } - void SetText(std::string text) { text_ = std::move(text); } - - XmlNode* Clone() const override; - - private: - std::string text_; -}; -} // namespace cru::xml diff --git a/include/cru/xml/XmlParser.h b/include/cru/xml/XmlParser.h deleted file mode 100644 index b6589f1b..00000000 --- a/include/cru/xml/XmlParser.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include "XmlNode.h" - -#include "cru/base/Exception.h" - -namespace cru::xml { -class CRU_XML_API XmlParsingException : public Exception { - public: - using Exception::Exception; -}; - -class CRU_XML_API XmlParser { - public: - explicit XmlParser(std::string xml); - - CRU_DELETE_COPY(XmlParser) - CRU_DELETE_MOVE(XmlParser) - - ~XmlParser(); - - XmlElementNode* Parse(); - - private: - XmlElementNode* DoParse(); - - char16_t Read1(); - std::string ReadWithoutAdvance(int count = 1); - void ReadSpacesAndDiscard(); - std::string ReadSpaces(); - std::string ReadIdenitifier(); - std::string ReadAttributeString(); - - private: - std::string xml_; - - XmlElementNode* cache_ = nullptr; - - // Consider the while file enclosed by a single tag called $root. - XmlElementNode* pseudo_root_node_ = new XmlElementNode("$root"); - XmlElementNode* current_ = pseudo_root_node_; - int current_position_ = 0; -}; -} // namespace cru::xml -- cgit v1.2.3