24#ifdef MUSX_USE_TINYXML2
31#include "XmlInterface.h"
51 const ::tinyxml2::XMLAttribute*
const m_attribute;
57 explicit Attribute(const ::tinyxml2::XMLAttribute* attr) : m_attribute(attr) {}
59 std::string
getName()
const override {
return m_attribute->Name(); }
60 std::string
getValue()
const override {
return m_attribute->Value(); }
63 const ::tinyxml2::XMLAttribute *next = m_attribute->Next();
64 return next ? std::make_shared<Attribute>(next) :
nullptr;
73 const ::tinyxml2::XMLElement*
const m_element;
75 static const char* tagPtr(
const std::string& tagName) {
76 return tagName.empty() ? nullptr : tagName.c_str();
83 explicit Element(const ::tinyxml2::XMLElement* elem) : m_element(elem) {}
85 std::string
getTagName()
const override {
return m_element->Name(); }
88 return m_element->GetText() ? m_element->GetText() :
"";
92 const ::tinyxml2::XMLAttribute *attr = m_element->FirstAttribute();
93 return attr ? std::make_shared<Attribute>(attr) :
nullptr;
96 std::shared_ptr<IXmlAttribute>
findAttribute(
const std::string& tagName)
const override {
97 const ::tinyxml2::XMLAttribute *attr = m_element->FindAttribute(tagName.c_str());
98 return attr ? std::make_shared<Attribute>(attr) :
nullptr;
102 const ::tinyxml2::XMLElement* child = m_element->FirstChildElement(tagPtr(tagName));
103 return child ? std::make_shared<Element>(child) : nullptr;
106 std::shared_ptr<IXmlElement>
getNextSibling(
const std::string& tagName = {})
const override {
107 const ::tinyxml2::XMLElement *sibling = m_element->NextSiblingElement(tagPtr(tagName));
108 return sibling ? std::make_shared<Element>(sibling) : nullptr;
112 const ::tinyxml2::XMLElement *sibling = m_element->PreviousSiblingElement(tagPtr(tagName));
113 return sibling ? std::make_shared<Element>(sibling) : nullptr;
116 std::shared_ptr<IXmlElement>
getParent()
const override {
117 if (!m_element->Parent() || !m_element->Parent()->ToElement())
return nullptr;
118 return std::make_shared<Element>(m_element->Parent()->ToElement());
127 ::tinyxml2::XMLDocument m_document;
132 if (m_document.Parse(xmlContent.c_str()) != ::tinyxml2::XML_SUCCESS) {
138 if (m_document.Parse(xmlContent.data(), xmlContent.size()) != ::tinyxml2::XML_SUCCESS) {
144 const ::tinyxml2::XMLElement* root = m_document.RootElement();
145 return root ? std::make_shared<Element>(root) :
nullptr;
Interface for an XML attribute.
Definition XmlInterface.h:65
Interface for an XML document.
Definition XmlInterface.h:230
Interface for an XML element.
Definition XmlInterface.h:133
Exception for load xml error.
Definition XmlInterface.h:46
Implementation of IXmlAttribute using tinyxml2.
Definition TinyXmlImpl.h:50
Attribute(const ::tinyxml2::XMLAttribute *attr)
Constructor.
Definition TinyXmlImpl.h:57
std::string getName() const override
Gets the name of the attribute.
Definition TinyXmlImpl.h:59
std::shared_ptr< IXmlAttribute > nextAttribute() const override
Advances to the next attribute.
Definition TinyXmlImpl.h:62
std::string getValue() const override
Gets the value of the attribute.
Definition TinyXmlImpl.h:60
Implementation of IXmlDocument using tinyxml2.
Definition TinyXmlImpl.h:126
void loadFromString(const std::string &xmlContent) override
Loads XML content from a string.
Definition TinyXmlImpl.h:130
void loadFromString(const std::vector< char > &xmlContent) override
Loads XML content from a vector of characters.
Definition TinyXmlImpl.h:137
std::shared_ptr< IXmlElement > getRootElement() const override
Gets the root element of the document.
Definition TinyXmlImpl.h:143
Implementation of IXmlElement using tinyxml2.
Definition TinyXmlImpl.h:72
std::string getTagName() const override
Gets the tag name of the element.
Definition TinyXmlImpl.h:85
std::shared_ptr< IXmlElement > getPreviousSibling(const std::string &tagName={}) const override
Gets the previous sibling element.
Definition TinyXmlImpl.h:111
std::shared_ptr< IXmlElement > getParent() const override
Gets the parent element.
Definition TinyXmlImpl.h:116
Element(const ::tinyxml2::XMLElement *elem)
Constructor.
Definition TinyXmlImpl.h:83
std::string getText() const override
Gets the text content of the element.
Definition TinyXmlImpl.h:87
std::shared_ptr< IXmlElement > getFirstChildElement(const std::string &tagName={}) const override
Finds the first child element.
Definition TinyXmlImpl.h:101
std::shared_ptr< IXmlElement > getNextSibling(const std::string &tagName={}) const override
Gets the next sibling element.
Definition TinyXmlImpl.h:106
std::shared_ptr< IXmlAttribute > findAttribute(const std::string &tagName) const override
Finds the first attribute.
Definition TinyXmlImpl.h:96
std::shared_ptr< IXmlAttribute > getFirstAttribute() const override
Gets the first attribute.
Definition TinyXmlImpl.h:91
object model for musx file (enigmaxml)
Definition BaseClasses.h:32