From 114a878c64ce6f8223cfd22d76a20eb16d177e5e Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdevelop@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- lib/util/domutil.cpp | 367 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 367 insertions(+) create mode 100644 lib/util/domutil.cpp (limited to 'lib/util/domutil.cpp') diff --git a/lib/util/domutil.cpp b/lib/util/domutil.cpp new file mode 100644 index 00000000..b183717f --- /dev/null +++ b/lib/util/domutil.cpp @@ -0,0 +1,367 @@ +/*************************************************************************** + * Copyright (C) 2001-2002 by Bernd Gehrmann * + * bernd@kdevelop.org * + * default support: Eray Ozkural (exa) * + * additions: John Firebaugh * + * Jakob Simon-Gaarde * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "domutil.h" + +#include +#include +#include + + +void DomUtil::makeEmpty( QDomElement& e ) +{ + while( !e.firstChild().isNull() ) + e.removeChild( e.firstChild() ); +} + +QDomElement DomUtil::elementByPath(const QDomDocument &doc, const QString &path) +{ + QStringList l = QStringList::split('/', path); + + QDomElement el; + if(&doc) el = doc.documentElement(); + QStringList::ConstIterator it; + for (it = l.begin(); it != l.end(); ++it) { + el = el.namedItem(*it).toElement(); + } + + return el; +} + + +QString DomUtil::readEntry(const QDomDocument &doc, const QString &path, const QString &defaultEntry) +{ + QDomElement el = elementByPath(doc, path); + if (el.isNull()) + return defaultEntry; + else + return el.firstChild().toText().data(); +} + +/// @todo consider whether it's okay to accept empty string == default value +/// if not use the below type +///typedef pair EltInfo; + +QString DomUtil::readEntryAux(const QDomDocument &doc, const QString &path) +{ + QDomElement el = elementByPath(doc, path); + if (el.isNull()) + return QString::null; + else + return el.firstChild().toText().data(); +} + +int DomUtil::readIntEntry(const QDomDocument &doc, const QString &path, int defaultEntry) +{ + QString entry = readEntryAux(doc, path); + if (entry.isNull()) + return defaultEntry; + else + return entry.toInt(); +} + + +bool DomUtil::readBoolEntry(const QDomDocument &doc, const QString &path, bool defaultEntry) +{ + QString entry = readEntryAux(doc, path); + if (entry.isNull()) + return defaultEntry; + else + return entry == "TRUE" || entry == "true"; +} + + +QStringList DomUtil::readListEntry(const QDomDocument &doc, const QString &path, const QString &tag) +{ + QStringList list; + + QDomElement el = elementByPath(doc, path); + QDomElement subEl = el.firstChild().toElement(); + while (!subEl.isNull()) { + if (subEl.tagName() == tag) + list << subEl.firstChild().toText().data(); + subEl = subEl.nextSibling().toElement(); + } + + return list; +} + + +DomUtil::PairList DomUtil::readPairListEntry(const QDomDocument &doc, const QString &path, const QString &tag, + const QString &firstAttr, const QString &secondAttr) +{ + PairList list; + + QDomElement el = elementByPath(doc, path); + QDomElement subEl = el.firstChild().toElement(); + while (!subEl.isNull()) { + if (subEl.tagName() == tag) { + QString first = subEl.attribute(firstAttr); + QString second = subEl.attribute(secondAttr); + list << Pair(first, second); + } + subEl = subEl.nextSibling().toElement(); + } + + return list; +} + +QMap DomUtil::readMapEntry(const QDomDocument &doc, const QString& path) +{ + QMap map; + + QDomElement el = elementByPath(doc, path); + QDomElement subEl = el.firstChild().toElement(); + while (!subEl.isNull()) { + map[subEl.tagName()] = subEl.firstChild().toText().data(); + subEl = subEl.nextSibling().toElement(); + } + + return map; +} + +QDomElement DomUtil::namedChildElement( QDomElement& el, const QString& name ) +{ + QDomElement child = el.namedItem( name ).toElement(); + if (child.isNull()) { + child = el.ownerDocument().createElement( name ); + el.appendChild(child); + } + return child; +} + + +QDomElement DomUtil::createElementByPath(QDomDocument &doc, const QString &path) +{ + QStringList l = QStringList::split('/', path); + + QDomElement el; + if(&doc) el = doc.documentElement(); + QStringList::ConstIterator it; + for (it = l.begin(); it != l.end(); ++it) + el = DomUtil::namedChildElement( el, *it ); + + while (!el.firstChild().isNull()) + el.removeChild(el.firstChild()); + + return el; +} + + +void DomUtil::writeEntry(QDomDocument &doc, const QString &path, const QString &value) +{ + QDomElement el = createElementByPath(doc, path); + el.appendChild(doc.createTextNode(value)); +} + +void DomUtil::writeMapEntry(QDomDocument &doc, const QString &path, const QMap &map) +{ + QString basePath( path + "/" ); + QMap::ConstIterator it; + for (it = map.begin(); it != map.end(); ++it) + { + kdDebug( 9010 ) << "writing " << basePath << ";" << it.key() << ";" << it.data() << endl; + if( ! it.key().isEmpty() ) + writeEntry(doc, basePath + it.key(), it.data() ); + } +} + +void DomUtil::writeIntEntry(QDomDocument &doc, const QString &path, int value) +{ + writeEntry(doc, path, QString::number(value)); +} + + +void DomUtil::writeBoolEntry(QDomDocument &doc, const QString &path, bool value) +{ + writeEntry(doc, path, value? "true" : "false"); +} + + +void DomUtil::writeListEntry(QDomDocument &doc, const QString &path, const QString &tag, + const QStringList &value) +{ + QDomElement el = createElementByPath(doc, path); + + QStringList::ConstIterator it; + for (it = value.begin(); it != value.end(); ++it) { + QDomElement subEl = doc.createElement(tag); + subEl.appendChild(doc.createTextNode(*it)); + el.appendChild(subEl); + } +} + + +void DomUtil::writePairListEntry(QDomDocument &doc, const QString &path, const QString &tag, + const QString &firstAttr, const QString &secondAttr, + const PairList &value) +{ + QDomElement el = createElementByPath(doc, path); + + PairList::ConstIterator it; + for (it = value.begin(); it != value.end(); ++it) { + QDomElement subEl = doc.createElement(tag); + subEl.setAttribute(firstAttr, (*it).first); + subEl.setAttribute(secondAttr, (*it).second); + el.appendChild(subEl); + } +} + +DomPath DomUtil::resolvPathStringExt(const QString pathstring) +{ + // parse path + unsigned int i; + QStringList pathParts = QStringList::split('/',pathstring); + DomPath dompath; + for (i=0; i1) + { + // handle attributes + QStringList attrParts = QStringList::split(';',pathElemParts[1]); + for (unsigned int j=0; j2) + dompathelem.matchNumber = pathElemParts[2].toInt(); + else + dompathelem.matchNumber = 0; // or else the first + dompath.append(dompathelem); + } + return dompath; +} + + +#define rightchild !wrongchild + +QDomElement DomUtil::elementByPathExt(QDomDocument &doc, const QString &pathstring) +{ + DomPath dompath = resolvPathStringExt(pathstring); + QDomElement elem = doc.documentElement(); + QDomNodeList children; + QDomElement nextElem = elem; + for (unsigned int j=0; j