Updated the README file with some contributor tips.
[basket4.git] / src / xmlwork.cpp
blob1ca57b2a1541dbe455157d28e6c3de57e1fec2cc
1 /***************************************************************************
2 * Copyright (C) 2003 by Sébastien Laoût *
3 * slaout@linux62.org *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <qstring.h>
22 #include <qdom.h>
23 #include <qstringlist.h>
24 #include <qfile.h>
26 #include "xmlwork.h"
28 QDomDocument* XMLWork::openFile(const QString &name, const QString &filePath)
30 QDomDocument *doc = new QDomDocument(name);
31 QFile file(filePath);
32 if ( ! file.open(QIODevice::ReadOnly) ) {
33 // QMessageBox::information(this, "Load an XML file", "Error : un-openable file");
34 delete doc;
35 return 0;
37 if ( ! doc->setContent(&file) ) {
38 // QMessageBox::information(this, "Load an XML file", "Error : malformed content");
39 file.close();
40 delete doc;
41 return 0;
43 file.close();
44 return doc;
47 QDomElement XMLWork::getElement(const QDomElement &startElement, const QString &elementPath)
49 QStringList elements = QStringList::split("/", elementPath, false);
50 QDomNode n = startElement.firstChild();
51 for (unsigned int i = 0; i < elements.count(); ++i) { // For each elements
52 while ( ! n.isNull() ) { // Browse theire sub elements
53 QDomElement e = n.toElement(); // and search the good one
54 if ( (!e.isNull()) && e.tagName() == *elements.at(i) ) { // If found
55 if ( i + 1 == elements.count() ) // And if it is the asked element
56 return e; // Return the first corresponding
57 else { // Or if it is an intermediate element
58 n = e.firstChild(); // Continue with the next sub element
59 break;
62 n = n.nextSibling();
65 return QDomElement(); // Not found !
68 QString XMLWork::getElementText(const QDomElement &startElement, const QString &elementPath, const QString &defaultTxt)
70 QDomElement e = getElement(startElement, elementPath);
71 if (e.isNull())
72 return defaultTxt;
73 else
74 return e.text();
77 void XMLWork::addElement(QDomDocument &document, QDomElement &parent, const QString &name, const QString &text)
79 QDomElement tag = document.createElement(name);
80 parent.appendChild(tag);
81 QDomText content = document.createTextNode(text);
82 tag.appendChild(content);
85 bool XMLWork::trueOrFalse(const QString &value, bool defaultValue)
87 if ( value == "true" || value == "1" || value == "on" || value == "yes" )
88 return true;
89 if ( value == "false" || value == "0" || value == "off" || value == "no" )
90 return false;
91 return defaultValue;
94 QString XMLWork::trueOrFalse(bool value)
96 return value ? "true" : "false";
99 QString XMLWork::innerXml(QDomElement &element)
101 QString inner;
102 for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
103 if (n.isCharacterData())
104 inner += n.toCharacterData().data();
105 else if (n.isElement()) {
106 QDomElement e = n.toElement();
107 inner += "<" + e.tagName() + ">" + innerXml(e) + "</" + e.tagName() + ">";
109 return inner;