27#include <QDomDocument>
32#include "XmlInterface.h"
51 Attribute(
const QDomAttr& attr,
int index)
52 : m_attr(attr), m_index(index) {}
54 std::string getName()
const override {
55 return m_attr.name().toStdString();
58 std::string getValue()
const override {
59 return m_attr.value().toStdString();
62 std::shared_ptr<IXmlAttribute> nextAttribute()
const override {
63 QDomNamedNodeMap attributes = m_attr.parentNode().attributes();
64 if (m_index + 1 >= attributes.count()) {
67 QDomAttr nextAttr = attributes.item(m_index + 1).toAttr();
68 if (nextAttr.isNull()) {
71 return std::make_shared<Attribute>(nextAttr, m_index + 1);
79 const QDomElement m_element;
82 explicit Element(
const QDomElement& elem) : m_element(elem) {}
84 std::string getTagName()
const override {
return m_element.tagName().toStdString(); }
86 std::string getText()
const override {
return m_element.text().toStdString(); }
88 std::shared_ptr<IXmlAttribute> getFirstAttribute()
const override {
89 QDomNamedNodeMap attributes = m_element.attributes();
90 if (attributes.isEmpty()) {
93 QDomAttr attr = attributes.item(0).toAttr();
97 return std::make_shared<Attribute>(attr, 0);
100 std::shared_ptr<IXmlAttribute> findAttribute(
const std::string& name)
const override {
102 QDomElement& nonConstElement =
const_cast<QDomElement&
>(m_element);
103 QDomAttr attr = nonConstElement.attributeNode(QString::fromStdString(name));
104 return attr.isNull() ? nullptr : std::make_shared<Attribute>(attr, 0);
107 std::shared_ptr<IXmlElement> getFirstChildElement(
const std::string& tagName = {})
const override {
108 QDomNode child = tagName.empty()
109 ? m_element.firstChildElement()
110 : m_element.firstChildElement(QString::fromStdString(tagName));
111 return child.isNull() ? nullptr : std::make_shared<Element>(child.toElement());
114 std::shared_ptr<IXmlElement> getNextSibling(
const std::string& tagName = {})
const override {
115 QDomNode sibling = m_element.nextSiblingElement(QString::fromStdString(tagName));
116 if (tagName.empty() && sibling.isNull()) {
117 sibling = m_element.nextSibling();
118 while (!sibling.isNull() && !sibling.isElement()) {
119 sibling = sibling.nextSibling();
122 return sibling.isNull() ? nullptr : std::make_shared<Element>(sibling.toElement());
125 std::shared_ptr<IXmlElement> getPreviousSibling(
const std::string& tagName = {})
const override {
126 QDomNode sibling = m_element.previousSiblingElement(QString::fromStdString(tagName));
127 if (tagName.empty() && sibling.isNull()) {
128 sibling = m_element.previousSibling();
129 while (!sibling.isNull() && !sibling.isElement()) {
130 sibling = sibling.previousSibling();
133 return sibling.isNull() ? nullptr : std::make_shared<Element>(sibling.toElement());
136 std::shared_ptr<IXmlElement> getParent()
const override {
137 QDomNode parent = m_element.parentNode();
138 return parent.isNull() || !parent.isElement() ? nullptr : std::make_shared<Element>(parent.toElement());
146 QDomDocument m_document;
149 void loadFromString(
const std::string& xmlContent)
override {
151 int errorLine = 0, errorColumn = 0;
152 if (!m_document.setContent(QString::fromUtf8(xmlContent.c_str()), &errorMsg, &errorLine, &errorColumn)) {
154 ", column " + std::to_string(errorColumn) +
": " + errorMsg.toStdString());
158 void loadFromBuffer(
const char * data,
size_t size)
override {
160 int errorLine = 0, errorColumn = 0;
161 if (!m_document.setContent(QByteArray(data, size), &errorMsg, &errorLine, &errorColumn)) {
163 ", column " + std::to_string(errorColumn) +
": " + errorMsg.toStdString());
167 std::shared_ptr<IXmlElement> getRootElement()
const override {
168 QDomElement root = m_document.documentElement();
169 return root.isNull() ? nullptr : std::make_shared<Element>(root);
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
object model for musx file (enigmaxml)
Definition BaseClasses.h:32