File indexing completed on 2024-04-21 16:32:04
0001 /** 0002 * SPDX-FileCopyrightText: (C) 2003 Sébastien Laoût <slaout@linux62.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "xmlwork.h" 0008 0009 #include <QtCore/QFile> 0010 #include <QtCore/QString> 0011 #include <QtCore/QStringList> 0012 #include <QtXml/QDomDocument> 0013 0014 QDomDocument *XMLWork::openFile(const QString &name, const QString &filePath) 0015 { 0016 QDomDocument *doc = new QDomDocument(name); 0017 QFile file(filePath); 0018 if (!file.open(QIODevice::ReadOnly)) { 0019 // QMessageBox::information(this, "Load an XML file", "Error : un-openable file"); 0020 delete doc; 0021 return nullptr; 0022 } 0023 if (!doc->setContent(&file)) { 0024 // QMessageBox::information(this, "Load an XML file", "Error : malformed content"); 0025 file.close(); 0026 delete doc; 0027 return nullptr; 0028 } 0029 file.close(); 0030 return doc; 0031 } 0032 0033 QDomElement XMLWork::getElement(const QDomElement &startElement, const QString &elementPath) 0034 { 0035 QStringList elements = elementPath.split('/'); 0036 QDomNode n = startElement.firstChild(); 0037 for (int i = 0; i < elements.count(); ++i) { // For each elements 0038 while (!n.isNull()) { // Browse their sub elements 0039 QDomElement e = n.toElement(); // and search the good one 0040 if ((!e.isNull()) && e.tagName() == elements.at(i)) { // If found 0041 if (i + 1 == elements.count()) // And if it is the asked element 0042 return e; // Return the first corresponding 0043 else { // Or if it is an intermediate element 0044 n = e.firstChild(); // Continue with the next sub element 0045 break; 0046 } 0047 } 0048 n = n.nextSibling(); 0049 } 0050 } 0051 return QDomElement(); // Not found ! 0052 } 0053 0054 QString XMLWork::getElementText(const QDomElement &startElement, const QString &elementPath, const QString &defaultTxt) 0055 { 0056 QDomElement e = getElement(startElement, elementPath); 0057 if (e.isNull()) 0058 return defaultTxt; 0059 else 0060 return e.text(); 0061 } 0062 0063 void XMLWork::addElement(QDomDocument &document, QDomElement &parent, const QString &name, const QString &text) 0064 { 0065 QDomElement tag = document.createElement(name); 0066 parent.appendChild(tag); 0067 QDomText content = document.createTextNode(text); 0068 tag.appendChild(content); 0069 } 0070 0071 bool XMLWork::trueOrFalse(const QString &value, bool defaultValue) 0072 { 0073 if (value == "true" || value == "1" || value == "on" || value == "yes") 0074 return true; 0075 if (value == "false" || value == "0" || value == "off" || value == "no") 0076 return false; 0077 return defaultValue; 0078 } 0079 0080 QString XMLWork::trueOrFalse(bool value) 0081 { 0082 return value ? "true" : "false"; 0083 } 0084 0085 QString XMLWork::innerXml(QDomElement &element) 0086 { 0087 QString inner; 0088 for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling()) 0089 if (n.isCharacterData()) 0090 inner += n.toCharacterData().data(); 0091 else if (n.isElement()) { 0092 QDomElement e = n.toElement(); 0093 inner += '<' + e.tagName() + '>' + innerXml(e) + "</" + e.tagName() + '>'; 0094 } 0095 return inner; 0096 } 0097 0098 void XMLWork::setupXmlStream(QXmlStreamWriter &stream, QString startElement) 0099 { 0100 stream.setAutoFormatting(true); 0101 stream.setAutoFormattingIndent(1); 0102 stream.writeStartDocument(); 0103 stream.writeDTD("<!DOCTYPE " + startElement + '>'); 0104 stream.writeStartElement(startElement); 0105 }