MUSX Document Model
Loading...
Searching...
No Matches
TinyXmlImpl.h
1/*
2 * Copyright (C) 2025, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22#pragma once
23
24#ifdef MUSX_USE_TINYXML2
25
26#include <string>
27#include <memory>
28#include <exception>
29
30#include "tinyxml2.h"
31#include "XmlInterface.h"
32
33namespace musx {
34namespace xml {
35
44namespace tinyxml2 {
45
50{
51 const ::tinyxml2::XMLAttribute* const m_attribute;
52
53public:
57 explicit Attribute(const ::tinyxml2::XMLAttribute* attr) : m_attribute(attr) {}
58
59 std::string getName() const override { return m_attribute->Name(); }
60 std::string getValue() const override { return m_attribute->Value(); }
61
62 std::shared_ptr<IXmlAttribute> nextAttribute() const override {
63 const ::tinyxml2::XMLAttribute *next = m_attribute->Next();
64 return next ? std::make_shared<Attribute>(next) : nullptr;
65 }
66};
67
72{
73 const ::tinyxml2::XMLElement* const m_element;
74
75 static const char* tagPtr(const std::string& tagName) {
76 return tagName.empty() ? nullptr : tagName.c_str();
77 }
78
79public:
83 explicit Element(const ::tinyxml2::XMLElement* elem) : m_element(elem) {}
84
85 std::string getTagName() const override { return m_element->Name(); }
86
87 std::string getText() const override {
88 return m_element->GetText() ? m_element->GetText() : "";
89 }
90
91 std::shared_ptr<IXmlAttribute> getFirstAttribute() const override {
92 const ::tinyxml2::XMLAttribute *attr = m_element->FirstAttribute();
93 return attr ? std::make_shared<Attribute>(attr) : nullptr;
94 }
95
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;
99 }
100
101 std::shared_ptr<IXmlElement> getFirstChildElement(const std::string& tagName = {}) const override {
102 const ::tinyxml2::XMLElement* child = m_element->FirstChildElement(tagPtr(tagName));
103 return child ? std::make_shared<Element>(child) : nullptr;
104 }
105
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;
109 }
110
111 std::shared_ptr<IXmlElement> getPreviousSibling(const std::string& tagName = {}) const override {
112 const ::tinyxml2::XMLElement *sibling = m_element->PreviousSiblingElement(tagPtr(tagName));
113 return sibling ? std::make_shared<Element>(sibling) : nullptr;
114 }
115
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());
119 }
120};
121
126{
127 ::tinyxml2::XMLDocument m_document;
128
129public:
130 void loadFromString(const std::string& xmlContent) override
131 {
132 if (m_document.Parse(xmlContent.c_str()) != ::tinyxml2::XML_SUCCESS) {
133 throw musx::xml::load_error(m_document.ErrorStr());
134 }
135 }
136
137 void loadFromString(const std::vector<char>& xmlContent) override {
138 if (m_document.Parse(xmlContent.data(), xmlContent.size()) != ::tinyxml2::XML_SUCCESS) {
139 throw musx::xml::load_error(m_document.ErrorStr());
140 }
141 }
142
143 std::shared_ptr<IXmlElement> getRootElement() const override {
144 const ::tinyxml2::XMLElement* root = m_document.RootElement();
145 return root ? std::make_shared<Element>(root) : nullptr;
146 }
147};
148
149} // namespace tinyxml2
150} // namespace xml
151} // namespace musx
152
153#endif // MUSX_USE_TINYXML2
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