|
@@ -3,48 +3,48 @@
|
|
|
* Company: Lynar Studios
|
|
|
* E-Mail: webmaster@lynarstudios.com
|
|
|
* Created: 2020-11-26
|
|
|
- * Changed: 2021-04-23
|
|
|
+ * Changed: 2021-05-02
|
|
|
*
|
|
|
* */
|
|
|
|
|
|
-#include <ls_std/io/xml/XMLParser.hpp>
|
|
|
+#include <ls_std/io/xml/XmlParser.hpp>
|
|
|
#include <ls_std/exception/IllegalArgumentException.hpp>
|
|
|
#include <ls_std/boxing/String.hpp>
|
|
|
|
|
|
-ls_std::XMLParser::XMLParser(const std::shared_ptr<ls_std::XMLDocument> &_document) : ls_std::Class("XMLParser")
|
|
|
+ls_std::XmlParser::XmlParser(const std::shared_ptr<ls_std::XmlDocument> &_document) : ls_std::Class("XmlParser")
|
|
|
{
|
|
|
this->_assignDocument(_document);
|
|
|
this->_reset();
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<ls_std::XMLDocument> ls_std::XMLParser::getDocument()
|
|
|
+std::shared_ptr<ls_std::XmlDocument> ls_std::XmlParser::getDocument()
|
|
|
{
|
|
|
return this->document;
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::parse(const ls_std::byte_field &_data)
|
|
|
+void ls_std::XmlParser::parse(const ls_std::byte_field &_data)
|
|
|
{
|
|
|
this->_parse(_data);
|
|
|
this->_mergeNodes();
|
|
|
this->_reset();
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::setDocument(const std::shared_ptr<ls_std::XMLDocument> &_document)
|
|
|
+void ls_std::XmlParser::setDocument(const std::shared_ptr<ls_std::XmlDocument> &_document)
|
|
|
{
|
|
|
this->_assignDocument(_document);
|
|
|
}
|
|
|
|
|
|
-std::pair<std::string, std::string> ls_std::XMLParser::_readAttribute_(const ls_std::byte_field &_data)
|
|
|
+std::pair<std::string, std::string> ls_std::XmlParser::_readAttribute_(const ls_std::byte_field &_data)
|
|
|
{
|
|
|
- return ls_std::XMLParser::_parseAttribute(_data);
|
|
|
+ return ls_std::XmlParser::_parseAttribute(_data);
|
|
|
}
|
|
|
|
|
|
-std::list<std::pair<std::string, std::string>> ls_std::XMLParser::_readAttributes_(ls_std::byte_field _data)
|
|
|
+std::list<std::pair<std::string, std::string>> ls_std::XmlParser::_readAttributes_(ls_std::byte_field _data)
|
|
|
{
|
|
|
- return ls_std::XMLParser::_parseAttributes(std::move(_data));
|
|
|
+ return ls_std::XmlParser::_parseAttributes(std::move(_data));
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_analyze(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
+void ls_std::XmlParser::_analyze(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
{
|
|
|
this->_isDeclaration(_data, _index);
|
|
|
this->_isClosingTag(_data, _index);
|
|
@@ -52,7 +52,7 @@ void ls_std::XMLParser::_analyze(const ls_std::byte_field &_data, std::string::s
|
|
|
this->_isValue(_data, _index);
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_assignDocument(const std::shared_ptr<ls_std::XMLDocument> &_document)
|
|
|
+void ls_std::XmlParser::_assignDocument(const std::shared_ptr<ls_std::XmlDocument> &_document)
|
|
|
{
|
|
|
if (_document == nullptr)
|
|
|
{
|
|
@@ -62,24 +62,24 @@ void ls_std::XMLParser::_assignDocument(const std::shared_ptr<ls_std::XMLDocumen
|
|
|
this->document = _document;
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<ls_std::XMLDeclaration> ls_std::XMLParser::_createDeclaration(const std::list<std::pair<std::string, std::string>> &_attributes)
|
|
|
+std::shared_ptr<ls_std::XmlDeclaration> ls_std::XmlParser::_createDeclaration(const std::list<std::pair<std::string, std::string>> &_attributes)
|
|
|
{
|
|
|
- std::shared_ptr<ls_std::XMLDeclaration> declaration = std::make_shared<ls_std::XMLDeclaration>("1.0");
|
|
|
- std::pair<std::string, std::string> attribute = ls_std::XMLParser::_findAttribute(_attributes, "version");
|
|
|
+ std::shared_ptr<ls_std::XmlDeclaration> declaration = std::make_shared<ls_std::XmlDeclaration>("1.0");
|
|
|
+ std::pair<std::string, std::string> attribute = ls_std::XmlParser::_findAttribute(_attributes, "version");
|
|
|
|
|
|
if (!attribute.first.empty())
|
|
|
{
|
|
|
declaration->setVersion(attribute.second);
|
|
|
}
|
|
|
|
|
|
- attribute = ls_std::XMLParser::_findAttribute(_attributes, "encoding");
|
|
|
+ attribute = ls_std::XmlParser::_findAttribute(_attributes, "encoding");
|
|
|
|
|
|
if (!attribute.first.empty())
|
|
|
{
|
|
|
declaration->setEncoding(attribute.second);
|
|
|
}
|
|
|
|
|
|
- attribute = ls_std::XMLParser::_findAttribute(_attributes, "standalone");
|
|
|
+ attribute = ls_std::XmlParser::_findAttribute(_attributes, "standalone");
|
|
|
|
|
|
if (!attribute.first.empty())
|
|
|
{
|
|
@@ -89,14 +89,14 @@ std::shared_ptr<ls_std::XMLDeclaration> ls_std::XMLParser::_createDeclaration(co
|
|
|
return declaration;
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<ls_std::XMLNode> ls_std::XMLParser::_createNode(const std::list<std::pair<std::string, std::string>> &_attributes, const std::string &_name)
|
|
|
+std::shared_ptr<ls_std::XmlNode> ls_std::XmlParser::_createNode(const std::list<std::pair<std::string, std::string>> &_attributes, const std::string &_name)
|
|
|
{
|
|
|
- std::shared_ptr<ls_std::XMLNode> node = std::make_shared<ls_std::XMLNode>(_name);
|
|
|
- std::shared_ptr<ls_std::XMLAttribute> attribute{};
|
|
|
+ std::shared_ptr<ls_std::XmlNode> node = std::make_shared<ls_std::XmlNode>(_name);
|
|
|
+ std::shared_ptr<ls_std::XmlAttribute> attribute{};
|
|
|
|
|
|
for (const auto &parsedAttribute : _attributes)
|
|
|
{
|
|
|
- attribute = std::make_shared<ls_std::XMLAttribute>(parsedAttribute.first);
|
|
|
+ attribute = std::make_shared<ls_std::XmlAttribute>(parsedAttribute.first);
|
|
|
attribute->setValue(parsedAttribute.second);
|
|
|
node->addAttributeToEnd(attribute);
|
|
|
}
|
|
@@ -104,7 +104,7 @@ std::shared_ptr<ls_std::XMLNode> ls_std::XMLParser::_createNode(const std::list<
|
|
|
return node;
|
|
|
}
|
|
|
|
|
|
-std::pair<std::string, std::string> ls_std::XMLParser::_findAttribute(const std::list<std::pair<std::string, std::string>> &_attributes, const std::string &_name)
|
|
|
+std::pair<std::string, std::string> ls_std::XmlParser::_findAttribute(const std::list<std::pair<std::string, std::string>> &_attributes, const std::string &_name)
|
|
|
{
|
|
|
std::pair<std::string, std::string> attribute{};
|
|
|
|
|
@@ -120,7 +120,7 @@ std::pair<std::string, std::string> ls_std::XMLParser::_findAttribute(const std:
|
|
|
return attribute;
|
|
|
}
|
|
|
|
|
|
-size_t ls_std::XMLParser::_findAttributeEndPosition(const ls_std::byte_field &_data)
|
|
|
+size_t ls_std::XmlParser::_findAttributeEndPosition(const ls_std::byte_field &_data)
|
|
|
{
|
|
|
std::string::size_type position = std::string::npos;
|
|
|
std::string::size_type counter{};
|
|
@@ -143,7 +143,7 @@ size_t ls_std::XMLParser::_findAttributeEndPosition(const ls_std::byte_field &_d
|
|
|
return position;
|
|
|
}
|
|
|
|
|
|
-ls_std::byte_field ls_std::XMLParser::_getNextTagString(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
+ls_std::byte_field ls_std::XmlParser::_getNextTagString(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
{
|
|
|
ls_std::byte_field tag{};
|
|
|
size_t closingCharacterPosition = _index + _data.substr(_index).find('>');
|
|
@@ -156,7 +156,7 @@ ls_std::byte_field ls_std::XMLParser::_getNextTagString(const ls_std::byte_field
|
|
|
return tag;
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_isClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
+void ls_std::XmlParser::_isClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
{
|
|
|
if (this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 2) == "</")
|
|
|
{
|
|
@@ -164,7 +164,7 @@ void ls_std::XMLParser::_isClosingTag(const ls_std::byte_field &_data, std::stri
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_isDeclaration(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
+void ls_std::XmlParser::_isDeclaration(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
{
|
|
|
if (_data.substr(_index, 5) == "<?xml")
|
|
|
{
|
|
@@ -172,7 +172,7 @@ void ls_std::XMLParser::_isDeclaration(const ls_std::byte_field &_data, std::str
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_isOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
+void ls_std::XmlParser::_isOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
{
|
|
|
if (this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 1) == "<")
|
|
|
{
|
|
@@ -180,7 +180,7 @@ void ls_std::XMLParser::_isOpeningTag(const ls_std::byte_field &_data, std::stri
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_isValue(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
+void ls_std::XmlParser::_isValue(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
{
|
|
|
if (this->mode == XML_PARSE_MODE_ANALYZE)
|
|
|
{
|
|
@@ -199,7 +199,7 @@ void ls_std::XMLParser::_isValue(const ls_std::byte_field &_data, std::string::s
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_mergeNodes()
|
|
|
+void ls_std::XmlParser::_mergeNodes()
|
|
|
{
|
|
|
while (this->maxLevel > 1)
|
|
|
{
|
|
@@ -207,16 +207,16 @@ void ls_std::XMLParser::_mergeNodes()
|
|
|
this->maxLevel -= 1;
|
|
|
}
|
|
|
|
|
|
- this->document->setRootElement(this->parseData.front().node);
|
|
|
+ this->document->setRootElement(this->parseParameters.front().node);
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_mergeChildrenToParentNode(const std::shared_ptr<ls_std::XMLNode> &_parent, std::list<ls_std::XMLParseData>::iterator &_iterator, uint8_t _parentLevel)
|
|
|
+void ls_std::XmlParser::_mergeChildrenToParentNode(const std::shared_ptr<ls_std::XmlNode> &_parent, std::list<ls_std::XmlParseParameter>::iterator &_iterator, uint8_t _parentLevel)
|
|
|
{
|
|
|
do
|
|
|
{
|
|
|
_iterator++;
|
|
|
|
|
|
- if (_iterator == this->parseData.end())
|
|
|
+ if (_iterator == this->parseParameters.end())
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
@@ -230,12 +230,12 @@ void ls_std::XMLParser::_mergeChildrenToParentNode(const std::shared_ptr<ls_std:
|
|
|
} while (_iterator->level > _parentLevel);
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_mergeNodesOnCurrentLevel()
|
|
|
+void ls_std::XmlParser::_mergeNodesOnCurrentLevel()
|
|
|
{
|
|
|
- auto iterator = this->parseData.begin();
|
|
|
+ auto iterator = this->parseParameters.begin();
|
|
|
uint8_t parentLevel = this->maxLevel - 1;
|
|
|
|
|
|
- while (iterator != this->parseData.end())
|
|
|
+ while (iterator != this->parseParameters.end())
|
|
|
{
|
|
|
if (iterator->level == parentLevel)
|
|
|
{
|
|
@@ -248,7 +248,7 @@ void ls_std::XMLParser::_mergeNodesOnCurrentLevel()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_parse(const ls_std::byte_field &_data)
|
|
|
+void ls_std::XmlParser::_parse(const ls_std::byte_field &_data)
|
|
|
{
|
|
|
for (std::string::size_type index = 0; index < _data.size(); index++)
|
|
|
{
|
|
@@ -269,21 +269,21 @@ void ls_std::XMLParser::_parse(const ls_std::byte_field &_data)
|
|
|
case XML_PARSE_MODE_OPENING_TAG:
|
|
|
{
|
|
|
--index;
|
|
|
- index = ls_std::XMLParser::_parseOpeningTag(_data, index);
|
|
|
+ index = ls_std::XmlParser::_parseOpeningTag(_data, index);
|
|
|
this->mode = XML_PARSE_MODE_ANALYZE;
|
|
|
}
|
|
|
break;
|
|
|
case XML_PARSE_MODE_VALUE:
|
|
|
{
|
|
|
--index;
|
|
|
- index = ls_std::XMLParser::_parseValue(_data, index);
|
|
|
+ index = ls_std::XmlParser::_parseValue(_data, index);
|
|
|
this->mode = XML_PARSE_MODE_ANALYZE;
|
|
|
}
|
|
|
break;
|
|
|
case XML_PARSE_MODE_CLOSING_TAG:
|
|
|
{
|
|
|
--index;
|
|
|
- index = ls_std::XMLParser::_parseClosingTag(_data, index);
|
|
|
+ index = ls_std::XmlParser::_parseClosingTag(_data, index);
|
|
|
this->mode = XML_PARSE_MODE_ANALYZE;
|
|
|
}
|
|
|
break;
|
|
@@ -291,7 +291,7 @@ void ls_std::XMLParser::_parse(const ls_std::byte_field &_data)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-std::pair<std::string, std::string> ls_std::XMLParser::_parseAttribute(const ls_std::byte_field &_data)
|
|
|
+std::pair<std::string, std::string> ls_std::XmlParser::_parseAttribute(const ls_std::byte_field &_data)
|
|
|
{
|
|
|
std::pair<std::string, std::string> parsedAttribute{};
|
|
|
parsedAttribute.first = _data.substr(0, _data.find('='));
|
|
@@ -301,7 +301,7 @@ std::pair<std::string, std::string> ls_std::XMLParser::_parseAttribute(const ls_
|
|
|
return parsedAttribute;
|
|
|
}
|
|
|
|
|
|
-std::list<std::pair<std::string, std::string>> ls_std::XMLParser::_parseAttributes(ls_std::byte_field _data)
|
|
|
+std::list<std::pair<std::string, std::string>> ls_std::XmlParser::_parseAttributes(ls_std::byte_field _data)
|
|
|
{
|
|
|
std::list<std::pair<std::string, std::string>> attributes{};
|
|
|
size_t position = _data.find(' ');
|
|
@@ -319,48 +319,48 @@ std::list<std::pair<std::string, std::string>> ls_std::XMLParser::_parseAttribut
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- std::string attributeString = _data.substr(position, ls_std::XMLParser::_findAttributeEndPosition(_data) + 1);
|
|
|
- attributes.push_back(ls_std::XMLParser::_parseAttribute(attributeString));
|
|
|
+ std::string attributeString = _data.substr(position, ls_std::XmlParser::_findAttributeEndPosition(_data) + 1);
|
|
|
+ attributes.push_back(ls_std::XmlParser::_parseAttribute(attributeString));
|
|
|
_data = _data.substr(position + attributeString.size());
|
|
|
}
|
|
|
|
|
|
return attributes;
|
|
|
}
|
|
|
|
|
|
-size_t ls_std::XMLParser::_parseClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
+size_t ls_std::XmlParser::_parseClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
{
|
|
|
- std::string tagString = ls_std::XMLParser::_getNextTagString(_data, _index);
|
|
|
+ std::string tagString = ls_std::XmlParser::_getNextTagString(_data, _index);
|
|
|
this->currentLevel -= 1;
|
|
|
return tagString.empty() ? _index : _index + (tagString.size() - 1);
|
|
|
}
|
|
|
|
|
|
-size_t ls_std::XMLParser::_parseDeclaration(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
+size_t ls_std::XmlParser::_parseDeclaration(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
{
|
|
|
- std::string tagString = ls_std::XMLParser::_getNextTagString(_data, _index);
|
|
|
+ std::string tagString = ls_std::XmlParser::_getNextTagString(_data, _index);
|
|
|
bool isValidTagString = !tagString.empty();
|
|
|
|
|
|
if (isValidTagString)
|
|
|
{
|
|
|
- std::shared_ptr<ls_std::XMLDeclaration> declaration = this->_createDeclaration(ls_std::XMLParser::_parseAttributes(tagString));
|
|
|
+ std::shared_ptr<ls_std::XmlDeclaration> declaration = this->_createDeclaration(ls_std::XmlParser::_parseAttributes(tagString));
|
|
|
this->document->setDeclaration(declaration);
|
|
|
}
|
|
|
|
|
|
return !isValidTagString ? _index : _index + (tagString.size() - 1);
|
|
|
}
|
|
|
|
|
|
-size_t ls_std::XMLParser::_parseOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
+size_t ls_std::XmlParser::_parseOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
{
|
|
|
- ls_std::String tagString{ls_std::XMLParser::_getNextTagString(_data, _index)};
|
|
|
+ ls_std::String tagString{ls_std::XmlParser::_getNextTagString(_data, _index)};
|
|
|
bool isValidTagString = !tagString.toString().empty();
|
|
|
- ls_std::XMLParseData singleParseData{};
|
|
|
+ ls_std::XmlParseParameter singleParseParameter{};
|
|
|
|
|
|
if (isValidTagString)
|
|
|
{
|
|
|
- std::shared_ptr<ls_std::XMLNode> node = ls_std::XMLParser::_createNode(ls_std::XMLParser::_parseAttributes(tagString), ls_std::XMLParser::_parseTagName(tagString));
|
|
|
+ std::shared_ptr<ls_std::XmlNode> node = ls_std::XmlParser::_createNode(ls_std::XmlParser::_parseAttributes(tagString), ls_std::XmlParser::_parseTagName(tagString));
|
|
|
|
|
|
- singleParseData.level = this->currentLevel;
|
|
|
- singleParseData.node = node;
|
|
|
- this->parseData.push_back(singleParseData);
|
|
|
+ singleParseParameter.level = this->currentLevel;
|
|
|
+ singleParseParameter.node = node;
|
|
|
+ this->parseParameters.push_back(singleParseParameter);
|
|
|
|
|
|
if (!tagString.endsWith("/>"))
|
|
|
{
|
|
@@ -372,7 +372,7 @@ size_t ls_std::XMLParser::_parseOpeningTag(const ls_std::byte_field &_data, std:
|
|
|
return !isValidTagString ? _index : _index + (tagString.toString().size() - 1);
|
|
|
}
|
|
|
|
|
|
-ls_std::byte_field ls_std::XMLParser::_parseTagName(const ls_std::byte_field &_data)
|
|
|
+ls_std::byte_field ls_std::XmlParser::_parseTagName(const ls_std::byte_field &_data)
|
|
|
{
|
|
|
std::string::size_type position = _data.find(' ');
|
|
|
|
|
@@ -384,23 +384,23 @@ ls_std::byte_field ls_std::XMLParser::_parseTagName(const ls_std::byte_field &_d
|
|
|
return _data.substr(1, position - 1);
|
|
|
}
|
|
|
|
|
|
-size_t ls_std::XMLParser::_parseValue(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
+size_t ls_std::XmlParser::_parseValue(const ls_std::byte_field &_data, std::string::size_type _index)
|
|
|
{
|
|
|
ls_std::byte_field value = _data.substr(_index, _data.substr(_index).find('<'));
|
|
|
- this->parseData.back().node->setValue(value);
|
|
|
+ this->parseParameters.back().node->setValue(value);
|
|
|
|
|
|
return _index + (value.size() - 1);
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_reset()
|
|
|
+void ls_std::XmlParser::_reset()
|
|
|
{
|
|
|
this->currentLevel = 1;
|
|
|
this->maxLevel = 1;
|
|
|
this->mode = XML_PARSE_MODE_ANALYZE;
|
|
|
- this->parseData.clear();
|
|
|
+ this->parseParameters.clear();
|
|
|
}
|
|
|
|
|
|
-void ls_std::XMLParser::_setMaxLevel()
|
|
|
+void ls_std::XmlParser::_setMaxLevel()
|
|
|
{
|
|
|
if (this->currentLevel > this->maxLevel)
|
|
|
{
|