Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

domutil.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2001-2002 by Bernd Gehrmann                             *
00003  *   bernd@kdevelop.org                                                    *
00004  *   default support: Eray Ozkural (exa)                                   *
00005  *   additions: John Firebaugh <jfirebaugh@kde.org>                        *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  ***************************************************************************/
00013 
00014 #include "domutil.h"
00015 #include <qstringlist.h>
00016 
00017 
00018 void DomUtil::makeEmpty( QDomElement& e )
00019 {
00020     while( !e.firstChild().isNull() )
00021         e.removeChild( e.firstChild() );
00022 }
00023 
00024 QDomElement DomUtil::elementByPath(const QDomDocument &doc, const QString &path)
00025 {
00026     QStringList l = QStringList::split('/', path);
00027 
00028     QDomElement el = doc.documentElement();
00029     QStringList::ConstIterator it;
00030     for (it = l.begin(); it != l.end(); ++it) {
00031         el = el.namedItem(*it).toElement();
00032     }
00033 
00034     return el;
00035 }
00036 
00037 
00038 QString DomUtil::readEntry(const QDomDocument &doc, const QString &path, const QString &defaultEntry)
00039 {
00040     QDomElement el = elementByPath(doc, path);
00041     if (el.isNull())
00042         return defaultEntry;
00043     else
00044         return el.firstChild().toText().data();
00045 }
00046 
00047 // TODO: consider whether it's okay to accept empty string == default value
00048 // if not use the below type
00049 //typedef pair<bool,QString> EltInfo;
00050 
00051 QString DomUtil::readEntryAux(const QDomDocument &doc, const QString &path)
00052 {
00053     QDomElement el = elementByPath(doc, path);
00054     if (el.isNull())
00055         return QString::null;
00056     else
00057         return el.firstChild().toText().data();
00058 }
00059 
00060 int DomUtil::readIntEntry(const QDomDocument &doc, const QString &path, int defaultEntry)
00061 {
00062     QString entry = readEntryAux(doc, path);
00063     if (entry==QString::null)
00064       return defaultEntry;
00065     else
00066       return entry.toInt();
00067 }
00068 
00069 
00070 bool DomUtil::readBoolEntry(const QDomDocument &doc, const QString &path, bool defaultEntry)
00071 {
00072     QString entry = readEntryAux(doc, path);
00073     if (entry==QString::null)
00074       return defaultEntry;
00075     else
00076       return entry == "TRUE" || entry == "true";
00077 }
00078 
00079 
00080 QStringList DomUtil::readListEntry(const QDomDocument &doc, const QString &path, const QString &tag)
00081 {
00082     QStringList list;
00083     
00084     QDomElement el = elementByPath(doc, path);
00085     QDomElement subEl = el.firstChild().toElement();
00086     while (!subEl.isNull()) {
00087         if (subEl.tagName() == tag)
00088             list << subEl.firstChild().toText().data();
00089         subEl = subEl.nextSibling().toElement();
00090     }
00091 
00092     return list;
00093 }
00094 
00095 
00096 DomUtil::PairList DomUtil::readPairListEntry(const QDomDocument &doc, const QString &path, const QString &tag,
00097                                              const QString &firstAttr, const QString &secondAttr)
00098 {
00099     PairList list;
00100     
00101     QDomElement el = elementByPath(doc, path);
00102     QDomElement subEl = el.firstChild().toElement();
00103     while (!subEl.isNull()) {
00104         if (subEl.tagName() == tag) {
00105             QString first = subEl.attribute(firstAttr);
00106             QString second = subEl.attribute(secondAttr);
00107             list << Pair(first, second);
00108         }
00109         subEl = subEl.nextSibling().toElement();
00110     }
00111     
00112     return list;
00113 }
00114  
00115 DomUtil::TupleList DomUtil::readTupleListEntry(const QDomDocument &doc, const QString &path, const QString &tag,
00116                                         const QStringList &attrList)
00117 {
00118     TupleList list;
00119     QDomElement el = elementByPath(doc, path);
00120     QDomElement subEl = el.firstChild().toElement();
00121     while (!subEl.isNull()) {
00122         if (subEl.tagName() == tag) {
00123             QStringList l;
00124             for(QValueListConstIterator<QString> it = attrList.begin(); it != attrList.end(); ++it)
00125                 l << subEl.attribute(*it);
00126             list << l;
00127         }
00128         subEl = subEl.nextSibling().toElement();
00129     }
00130     return list;
00131 }
00132 
00133 QDomElement DomUtil::namedChildElement( QDomElement& el, const QString& name )
00134 {
00135     QDomElement child = el.namedItem( name ).toElement();
00136     if (child.isNull()) {
00137         child = el.ownerDocument().createElement( name );
00138         el.appendChild(child);
00139     }
00140     return child;
00141 }
00142 
00143 
00144 QDomElement DomUtil::createElementByPath(QDomDocument &doc, const QString &path)
00145 {
00146     QStringList l = QStringList::split('/', path);
00147 
00148     QDomElement el = doc.documentElement();
00149     QStringList::ConstIterator it;
00150     for (it = l.begin(); it != l.end(); ++it)
00151         el = DomUtil::namedChildElement( el, *it );
00152         
00153     while (!el.firstChild().isNull())
00154         el.removeChild(el.firstChild());
00155 
00156     return el;
00157 }
00158 
00159 
00160 void DomUtil::writeEntry(QDomDocument &doc, const QString &path, const QString &value)
00161 {
00162     QDomElement el = DomUtil::createElementByPath(doc, path);
00163     el.appendChild(doc.createTextNode(value));
00164 }
00165     
00166 
00167 void DomUtil::writeIntEntry(QDomDocument &doc, const QString &path, int value)
00168 {
00169     writeEntry(doc, path, QString::number(value));
00170 }
00171 
00172 
00173 void DomUtil::writeBoolEntry(QDomDocument &doc, const QString &path, bool value)
00174 {
00175     writeEntry(doc, path, value? "true" : "false");
00176 }
00177 
00178 
00179 void DomUtil::writeListEntry(QDomDocument &doc, const QString &path, const QString &tag,
00180                              const QStringList &value)
00181 {
00182     QDomElement el = createElementByPath(doc, path);
00183 
00184     QStringList::ConstIterator it;
00185     for (it = value.begin(); it != value.end(); ++it) {
00186         QDomElement subEl = doc.createElement(tag);
00187         subEl.appendChild(doc.createTextNode(*it));
00188         el.appendChild(subEl);
00189     }
00190 }
00191 
00192 
00193 void DomUtil::writePairListEntry(QDomDocument &doc, const QString &path, const QString &tag,
00194                                  const QString &firstAttr, const QString &secondAttr,
00195                                  const PairList &value)
00196 {
00197     QDomElement el = createElementByPath(doc, path);
00198 
00199     PairList::ConstIterator it;
00200     for (it = value.begin(); it != value.end(); ++it) {
00201         QDomElement subEl = doc.createElement(tag);
00202         subEl.setAttribute(firstAttr, (*it).first);
00203         subEl.setAttribute(secondAttr, (*it).second);
00204         el.appendChild(subEl);
00205     }
00206 }
00207 
00208 void DomUtil::writeTupleListEntry(QDomDocument &doc, const QString &path, const QString &tag,
00209                                   const QStringList &attrList, const TupleList &value)
00210 {
00211 QDomElement el = createElementByPath(doc, path);
00212 
00213     TupleList::ConstIterator it;
00214     for (it = value.begin(); it != value.end(); ++it) {
00215         QDomElement subEl = doc.createElement(tag);
00216         int i=0;
00217         for(QValueListConstIterator<QString> iter = attrList.begin(); iter != attrList.end(); ++iter) {
00218             subEl.setAttribute(*iter,(*it)[i++]);
00219             }
00220         el.appendChild(subEl);
00221     }
00222 }
00223 

Generated on Sat May 10 15:09:26 2003 for qnet by doxygen1.3