diff options
author | crupest <crupest@outlook.com> | 2022-03-04 20:07:50 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-03-04 20:07:50 +0800 |
commit | 14e1b0f188d302b69816ddf12f5ac591fd76b91d (patch) | |
tree | 8481328cfa20a43b67abd5455c2e34fd42639e0b /src/xml | |
parent | 57353bd3acd97957cb5f970016fec52977cc6e95 (diff) | |
download | cru-14e1b0f188d302b69816ddf12f5ac591fd76b91d.tar.gz cru-14e1b0f188d302b69816ddf12f5ac591fd76b91d.tar.bz2 cru-14e1b0f188d302b69816ddf12f5ac591fd76b91d.zip |
...
Diffstat (limited to 'src/xml')
-rw-r--r-- | src/xml/XmlNode.cpp | 16 | ||||
-rw-r--r-- | src/xml/XmlParser.cpp | 21 |
2 files changed, 32 insertions, 5 deletions
diff --git a/src/xml/XmlNode.cpp b/src/xml/XmlNode.cpp index cfa87dd6..41bbed4d 100644 --- a/src/xml/XmlNode.cpp +++ b/src/xml/XmlNode.cpp @@ -9,6 +9,10 @@ XmlElementNode* XmlNode::AsElement() { XmlTextNode* XmlNode::AsText() { return static_cast<XmlTextNode*>(this); } +XmlCommentNode* XmlNode::AsComment() { + return static_cast<XmlCommentNode*>(this); +} + const XmlElementNode* XmlNode::AsElement() const { return static_cast<const XmlElementNode*>(this); } @@ -17,6 +21,10 @@ const XmlTextNode* XmlNode::AsText() const { return static_cast<const XmlTextNode*>(this); } +const XmlCommentNode* XmlNode::AsComment() const { + return static_cast<const XmlCommentNode*>(this); +} + XmlElementNode::~XmlElementNode() { for (auto child : children_) { delete child; @@ -57,4 +65,12 @@ XmlNode* XmlElementNode::Clone() const { return node; } + +XmlCommentNode::~XmlCommentNode() {} + +XmlNode* XmlCommentNode::Clone() const { + XmlCommentNode* node = new XmlCommentNode(text_); + + return node; +} } // namespace cru::xml diff --git a/src/xml/XmlParser.cpp b/src/xml/XmlParser.cpp index babb6b00..313015d5 100644 --- a/src/xml/XmlParser.cpp +++ b/src/xml/XmlParser.cpp @@ -22,7 +22,7 @@ char16_t XmlParser::Read1() { String XmlParser::ReadWithoutAdvance(int count) { if (current_position_ + count > xml_.size()) { - return u""; + count = xml_.size() - current_position_; } return xml_.substr(current_position_, count); } @@ -108,6 +108,19 @@ XmlElementNode* XmlParser::DoParse() { } current_ = current_->GetParent(); + } else if (ReadWithoutAdvance(3) == u"!--") { + current_position_ += 3; + + String text; + while (true) { + auto str = ReadWithoutAdvance(3); + if (str == u"-->") break; + if (str.empty()) throw XmlParsingException(u"Unexpected end of xml"); + text += Read1(); + } + + current_position_ += 3; + current_->AddChild(new XmlCommentNode(text.Trim())); } else { ReadSpacesAndDiscard(); @@ -173,10 +186,8 @@ XmlElementNode* XmlParser::DoParse() { throw XmlParsingException(u"Unexpected end of xml"); } - if (pseudo_root_node_->GetChildren().size() != 1 || - pseudo_root_node_->GetChildren()[0]->GetType() != - XmlNode::Type::Element) { - throw XmlParsingException(u"Expected 1 element node as root."); + if (pseudo_root_node_->GetChildren().size() != 1) { + throw XmlParsingException(u"Expected 1 node as root."); } return static_cast<XmlElementNode*>(pseudo_root_node_->GetChildren()[0]); |