00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "domutil.h"
00016
00017 #include <kdebug.h>
00018 #include <qstringlist.h>
00019 #include <qfile.h>
00020
00021
00022 void DomUtil::makeEmpty( QDomElement& e )
00023 {
00024 while( !e.firstChild().isNull() )
00025 e.removeChild( e.firstChild() );
00026 }
00027
00028 QDomElement DomUtil::elementByPath(const QDomDocument &doc, const QString &path)
00029 {
00030 QStringList l = QStringList::split('/', path);
00031
00032 QDomElement el;
00033 if(&doc) el = doc.documentElement();
00034 QStringList::ConstIterator it;
00035 for (it = l.begin(); it != l.end(); ++it) {
00036 el = el.namedItem(*it).toElement();
00037 }
00038
00039 return el;
00040 }
00041
00042
00043 QString DomUtil::readEntry(const QDomDocument &doc, const QString &path, const QString &defaultEntry)
00044 {
00045 QDomElement el = elementByPath(doc, path);
00046 if (el.isNull())
00047 return defaultEntry;
00048 else
00049 return el.firstChild().toText().data();
00050 }
00051
00055
00056 QString DomUtil::readEntryAux(const QDomDocument &doc, const QString &path)
00057 {
00058 QDomElement el = elementByPath(doc, path);
00059 if (el.isNull())
00060 return QString::null;
00061 else
00062 return el.firstChild().toText().data();
00063 }
00064
00065 int DomUtil::readIntEntry(const QDomDocument &doc, const QString &path, int defaultEntry)
00066 {
00067 QString entry = readEntryAux(doc, path);
00068 if (entry.isNull())
00069 return defaultEntry;
00070 else
00071 return entry.toInt();
00072 }
00073
00074
00075 bool DomUtil::readBoolEntry(const QDomDocument &doc, const QString &path, bool defaultEntry)
00076 {
00077 QString entry = readEntryAux(doc, path);
00078 if (entry.isNull())
00079 return defaultEntry;
00080 else
00081 return entry == "TRUE" || entry == "true";
00082 }
00083
00084
00085 QStringList DomUtil::readListEntry(const QDomDocument &doc, const QString &path, const QString &tag)
00086 {
00087 QStringList list;
00088
00089 QDomElement el = elementByPath(doc, path);
00090 QDomElement subEl = el.firstChild().toElement();
00091 while (!subEl.isNull()) {
00092 if (subEl.tagName() == tag)
00093 list << subEl.firstChild().toText().data();
00094 subEl = subEl.nextSibling().toElement();
00095 }
00096
00097 return list;
00098 }
00099
00100
00101 DomUtil::PairList DomUtil::readPairListEntry(const QDomDocument &doc, const QString &path, const QString &tag,
00102 const QString &firstAttr, const QString &secondAttr)
00103 {
00104 PairList list;
00105
00106 QDomElement el = elementByPath(doc, path);
00107 QDomElement subEl = el.firstChild().toElement();
00108 while (!subEl.isNull()) {
00109 if (subEl.tagName() == tag) {
00110 QString first = subEl.attribute(firstAttr);
00111 QString second = subEl.attribute(secondAttr);
00112 list << Pair(first, second);
00113 }
00114 subEl = subEl.nextSibling().toElement();
00115 }
00116
00117 return list;
00118 }
00119
00120 QMap<QString, QString> DomUtil::readMapEntry(const QDomDocument &doc, const QString& path)
00121 {
00122 QMap<QString, QString> map;
00123
00124 QDomElement el = elementByPath(doc, path);
00125 QDomElement subEl = el.firstChild().toElement();
00126 while (!subEl.isNull()) {
00127 map[subEl.tagName()] = subEl.firstChild().toText().data();
00128 subEl = subEl.nextSibling().toElement();
00129 }
00130
00131 return map;
00132 }
00133
00134 QDomElement DomUtil::namedChildElement( QDomElement& el, const QString& name )
00135 {
00136 QDomElement child = el.namedItem( name ).toElement();
00137 if (child.isNull()) {
00138 child = el.ownerDocument().createElement( name );
00139 el.appendChild(child);
00140 }
00141 return child;
00142 }
00143
00144
00145 QDomElement DomUtil::createElementByPath(QDomDocument &doc, const QString &path)
00146 {
00147 QStringList l = QStringList::split('/', path);
00148
00149 QDomElement el;
00150 if(&doc) el = doc.documentElement();
00151 QStringList::ConstIterator it;
00152 for (it = l.begin(); it != l.end(); ++it)
00153 el = DomUtil::namedChildElement( el, *it );
00154
00155 while (!el.firstChild().isNull())
00156 el.removeChild(el.firstChild());
00157
00158 return el;
00159 }
00160
00161
00162 void DomUtil::writeEntry(QDomDocument &doc, const QString &path, const QString &value)
00163 {
00164 QDomElement el = createElementByPath(doc, path);
00165 el.appendChild(doc.createTextNode(value));
00166 }
00167
00168 void DomUtil::writeMapEntry(QDomDocument &doc, const QString &path, const QMap<QString, QString> &map)
00169 {
00170 QString basePath( path + "/" );
00171 QMap<QString,QString>::ConstIterator it;
00172 for (it = map.begin(); it != map.end(); ++it)
00173 {
00174 kdDebug( 9010 ) << "writing " << basePath << ";" << it.key() << ";" << it.data() << endl;
00175 if( ! it.key().isEmpty() )
00176 writeEntry(doc, basePath + it.key(), it.data() );
00177 }
00178 }
00179
00180 void DomUtil::writeIntEntry(QDomDocument &doc, const QString &path, int value)
00181 {
00182 writeEntry(doc, path, QString::number(value));
00183 }
00184
00185
00186 void DomUtil::writeBoolEntry(QDomDocument &doc, const QString &path, bool value)
00187 {
00188 writeEntry(doc, path, value? "true" : "false");
00189 }
00190
00191
00192 void DomUtil::writeListEntry(QDomDocument &doc, const QString &path, const QString &tag,
00193 const QStringList &value)
00194 {
00195 QDomElement el = createElementByPath(doc, path);
00196
00197 QStringList::ConstIterator it;
00198 for (it = value.begin(); it != value.end(); ++it) {
00199 QDomElement subEl = doc.createElement(tag);
00200 subEl.appendChild(doc.createTextNode(*it));
00201 el.appendChild(subEl);
00202 }
00203 }
00204
00205
00206 void DomUtil::writePairListEntry(QDomDocument &doc, const QString &path, const QString &tag,
00207 const QString &firstAttr, const QString &secondAttr,
00208 const PairList &value)
00209 {
00210 QDomElement el = createElementByPath(doc, path);
00211
00212 PairList::ConstIterator it;
00213 for (it = value.begin(); it != value.end(); ++it) {
00214 QDomElement subEl = doc.createElement(tag);
00215 subEl.setAttribute(firstAttr, (*it).first);
00216 subEl.setAttribute(secondAttr, (*it).second);
00217 el.appendChild(subEl);
00218 }
00219 }
00220
00221 DomPath DomUtil::resolvPathStringExt(const QString pathstring)
00222 {
00223
00224 unsigned int i;
00225 QStringList pathParts = QStringList::split('/',pathstring);
00226 DomPath dompath;
00227 for (i=0; i<pathParts.count(); i++)
00228 {
00229 QStringList pathElemParts = QStringList::split('|',pathParts[i],TRUE);
00230 DomPathElement dompathelem;
00231 dompathelem.tagName = pathElemParts[0].simplifyWhiteSpace();
00232 if (pathElemParts.count()>1)
00233 {
00234
00235 QStringList attrParts = QStringList::split(';',pathElemParts[1]);
00236 for (unsigned int j=0; j<attrParts.count(); j++)
00237 {
00238 QStringList attribSet = QStringList::split('=',attrParts[j]);
00239 if (attribSet.count()<2)
00240 continue;
00241 DomAttribute domattribute;
00242 domattribute.name = attribSet[0].simplifyWhiteSpace();
00243 domattribute.value = attribSet[1].simplifyWhiteSpace();
00244 dompathelem.attribute.append(domattribute);
00245 }
00246 }
00247 if (pathElemParts.count()>2)
00248 dompathelem.matchNumber = pathElemParts[2].toInt();
00249 else
00250 dompathelem.matchNumber = 0;
00251 dompath.append(dompathelem);
00252 }
00253 return dompath;
00254 }
00255
00256
00257 #define rightchild !wrongchild
00258
00259 QDomElement DomUtil::elementByPathExt(QDomDocument &doc, const QString &pathstring)
00260 {
00261 DomPath dompath = resolvPathStringExt(pathstring);
00262 QDomElement elem = doc.documentElement();
00263 QDomNodeList children;
00264 QDomElement nextElem = elem;
00265 for (unsigned int j=0; j<dompath.count(); j++)
00266 {
00267 children = nextElem.childNodes();
00268 DomPathElement dompathelement= dompath[j];
00269 bool wrongchild = false;
00270 int matchCount = 0;
00271 for (unsigned int i=0; i<children.count(); i++)
00272 {
00273 wrongchild = false;
00274 QDomElement child = children.item(i).toElement();
00275 QString tag = child.tagName();
00276 tag = dompathelement.tagName;
00277 if (child.tagName() == dompathelement.tagName)
00278 {
00279 for (unsigned int k=0; k<dompathelement.attribute.count(); k++)
00280 {
00281 DomAttribute domattribute = dompathelement.attribute[k];
00282 QDomAttr domattr = child.attributeNode(domattribute.name);
00283 if (domattr.isNull() ||
00284 domattr.value() != domattribute.value)
00285 {
00286 wrongchild = true;
00287 break;
00288 }
00289 }
00290 }
00291 else
00292 wrongchild=true;
00293 if (rightchild)
00294 {
00295 if (dompathelement.matchNumber == matchCount++)
00296 {
00297 nextElem = child;
00298 break;
00299 }
00300 }
00301 }
00302 if (wrongchild)
00303 {
00304 QDomElement nullDummy;
00305 nullDummy.clear();
00306 return nullDummy;
00307 }
00308 }
00309 return nextElem;
00310 }
00311
00312
00313 bool DomUtil::openDOMFile(QDomDocument &doc, QString filename)
00314 {
00315 QFile file( filename );
00316 if ( !file.open( IO_ReadOnly ) )
00317 return false;
00318 if ( !doc.setContent( &file ) ) {
00319 file.close();
00320 return false;
00321 }
00322 file.close();
00323 return true;
00324 }
00325
00326 bool DomUtil::saveDOMFile(QDomDocument &doc, QString filename)
00327 {
00328 QFile file( filename );
00329 if ( !file.open( IO_ReadWrite | IO_Truncate ) )
00330 return false;
00331 QTextStream t( &file );
00332 t << doc.toString();
00333 file.close();
00334 return true;
00335 }
00336
00337 bool DomUtil::removeTextNodes(QDomDocument doc,QString pathExt)
00338 {
00339 QDomElement elem = elementByPathExt(doc,pathExt);
00340 if (elem.isNull())
00341 return false;
00342 QDomNodeList children = elem.childNodes();
00343 for (unsigned int i=0;i<children.count();i++)
00344 if (children.item(i).isText())
00345 elem.removeChild(children.item(i));
00346 return true;
00347 }
00348
00349
00350 bool DomUtil::appendText(QDomDocument doc, QString pathExt, QString text)
00351 {
00352 QDomElement elem = elementByPathExt(doc,pathExt);
00353 if (elem.isNull())
00354 return false;
00355 elem.appendChild(doc.createTextNode(text));
00356 return true;
00357 }
00358
00359
00360 bool DomUtil::replaceText(QDomDocument doc, QString pathExt, QString text)
00361 {
00362 if (removeTextNodes(doc,pathExt) &&
00363 appendText(doc,pathExt,text))
00364 return true;
00365 else
00366 return false;
00367 }