-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Adamska1008/3-feature-support-for-parsing-…
…xml-document 3 feature support for parsing xml document
- Loading branch information
Showing
11 changed files
with
438 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include <set> | ||
#include "document.hpp" | ||
#include "parser.hpp" | ||
|
||
namespace myxml | ||
{ | ||
void Document::SetDeclaration(const Declaration &declaration) | ||
{ | ||
this->declaration = declaration; | ||
} | ||
|
||
void Document::SetRoot(std::shared_ptr<Element> root) | ||
{ | ||
this->root = root; | ||
} | ||
|
||
const Declaration &Document::GetDeclartion() const | ||
{ | ||
return this->declaration; | ||
} | ||
|
||
Declaration &Document::GetDeclartion() | ||
{ | ||
return this->declaration; | ||
} | ||
|
||
const std::shared_ptr<Element> &Document::GetRoot() const | ||
{ | ||
return this->root; | ||
} | ||
|
||
std::shared_ptr<Element> Document::GetRoot() | ||
{ | ||
return this->root; | ||
} | ||
|
||
std::optional<Document> Document::Parse(std::string input) | ||
{ | ||
return Parser(input).ParseDocument(); | ||
} | ||
|
||
std::optional<Declaration> Declaration::BuildFromAttrs(std::map<std::string, std::string> attrs) | ||
{ | ||
if (!attrs.count("version") || !util::isValidXmlVersion(attrs["version"])) | ||
{ | ||
return std::nullopt; | ||
} | ||
Declaration declaration; | ||
declaration.version = attrs["version"]; | ||
if (attrs.count("encoding")) | ||
{ | ||
auto encoding = attrs["encoding"]; | ||
if (!util::isValidXmlEncoding(encoding)) | ||
{ | ||
return std::nullopt; | ||
} | ||
declaration.encoding = encoding; | ||
} | ||
if (attrs.count("standalone")) | ||
{ | ||
auto standalone = attrs["standalone"]; | ||
if (!util::isValidXmlStandalone(standalone)) | ||
{ | ||
return std::nullopt; | ||
} | ||
declaration.standalone = standalone; | ||
} | ||
return declaration; | ||
} | ||
|
||
namespace util | ||
{ | ||
bool isValidXmlVersion(std::string_view version) | ||
{ | ||
return version == "1.0" || version == "1.1"; | ||
} | ||
|
||
bool isValidXmlEncoding(std::string_view encoding) | ||
{ | ||
// FIXME: not cover all valid encoding | ||
static std::set<std::string, std::less<>> valid{ | ||
"UTF-8", | ||
"UTF-16", | ||
"UTF-32", | ||
"GBK", | ||
}; | ||
return valid.count(encoding); | ||
} | ||
|
||
bool isValidXmlStandalone(std::string_view standalone) | ||
{ | ||
return standalone == "yes" || standalone == "no"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,45 @@ | ||
#pragma once | ||
#include <map> | ||
#include "node.hpp" | ||
|
||
class XMLDocument | ||
// Declaration and Documant are both NOT Node | ||
namespace myxml | ||
{ | ||
}; | ||
struct Declaration | ||
{ | ||
std::string version; | ||
std::optional<std::string> encoding; | ||
std::optional<std::string> standalone; | ||
|
||
// return `std::nullopt` if declartion is in bad format | ||
// TODO: use exception to distinguish each of bad format | ||
static std::optional<Declaration> BuildFromAttrs(std::map<std::string, std::string> attrs); | ||
}; | ||
|
||
class Document | ||
{ | ||
private: | ||
Declaration declaration; | ||
std::shared_ptr<Element> root; | ||
|
||
public: | ||
/* Manipulate */ | ||
void SetDeclaration(const Declaration &); | ||
void SetRoot(std::shared_ptr<Element> root); | ||
|
||
/* Query */ | ||
const Declaration &GetDeclartion() const; | ||
Declaration &GetDeclartion(); | ||
const std::shared_ptr<Element> &GetRoot() const; | ||
std::shared_ptr<Element> GetRoot(); | ||
|
||
static std::optional<Document> Parse(std::string); | ||
}; | ||
|
||
namespace util | ||
{ | ||
bool isValidXmlVersion(std::string_view); | ||
bool isValidXmlEncoding(std::string_view); | ||
bool isValidXmlStandalone(std::string_view); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.