Alkimia API

alkdom.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Ralf Habacker ralf.habacker @freenet.de
3
4 This file is part of libalkimia.
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7*/
8
9#include "alkdom.h"
10
11AlkDomElement::AlkDomElement(const QString &tag)
12 : m_tag(tag)
13{
14}
15
16AlkDomElement::~AlkDomElement()
17{
18}
19
20void AlkDomElement::setComment(const QString &comment)
21{
22 m_comment = comment;
23}
24
25void AlkDomElement::setAttribute(const QString &name, const QString &value)
26{
27 m_attributes.append(QString("%1=\"%2\"").arg(name, value));
28}
29
30void AlkDomElement::setAttribute(const QString &name, double value)
31{
32 m_attributes.append(QString("%1=\"%2\"").arg(name).arg(value));
33}
34
35void AlkDomElement::appendChild(const AlkDomElement &element)
36{
37 m_childs.append(element);
38}
39
40QString AlkDomElement::toString(bool withIndentation, int level) const
41{
42 QString prefix = withIndentation ? QString(level, ' ') : QString();
43 QString s;
44 if (!m_comment.isEmpty()) {
45 if (m_comment.contains("\n")) {
46 QStringList lines = m_comment.split("\n");
47 QString delimiter = QString("\n%1 ").arg(prefix);
48 s.append(QString("%1<!--%2%3\n%1-->\n").arg(prefix, delimiter, lines.join(delimiter)));
49 } else
50 s.append(QString("%1<!-- %2 -->\n").arg(prefix, m_comment));
51 }
52
53 QStringList attributes(m_attributes);
54 attributes.sort();
55 if (m_childs.size() > 0) {
56 s.append(!m_tag.isEmpty()
57 ? attributes.size() > 0 ? QString("%1<%2 %3>\n").arg(prefix, m_tag, attributes.join(" ")) : QString("%1<%2>\n").arg(prefix, m_tag)
58 : "");
59 for (const AlkDomElement &child : m_childs) {
60 s += child.toString(withIndentation, level + 1);
61 }
62 s += !m_tag.isEmpty() ? QString("%1</%2>\n").arg(prefix, m_tag) : "";
63 } else {
64 s.append(!m_tag.isEmpty()
65 ? attributes.size() > 0 ? QString("%1<%2 %3 />\n").arg(prefix, m_tag, attributes.join(" ")) : QString("%1<%2 />\n").arg(prefix, m_tag)
66 : "");
67 }
68 return s;
69}
70
71AlkDomDocument::AlkDomDocument(const QString &type)
72 : AlkDomElement("")
73 , m_type(type)
74{
75}
76
77AlkDomDocument::~AlkDomDocument()
78{
79}
80
81AlkDomElement AlkDomDocument::createElement(const QString &name)
82{
83 return AlkDomElement(name);
84}
85
86QString AlkDomDocument::toString(bool withIndentation, int level) const
87{
88 QString s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
89 if (!m_type.isEmpty())
90 s.append(QString("<!DOCTYPE %1>\n").arg(m_type));
91 s.append(AlkDomElement::toString(withIndentation, level));
92 return s;
93}
The class AlkDomElement is a replacement for QDomElement with the possibility to store attributes sor...
Definition alkdom.h:28
Type type(const QSqlDatabase &db)
void append(QList< T > &&value)
QString & append(QChar ch)
QString arg(Args &&... args) const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.