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
00121 QDomElement DomUtil::namedChildElement(
QDomElement& el,
const QString& name )
00122 {
00123
QDomElement child = el.namedItem( name ).toElement();
00124
if (child.isNull()) {
00125 child = el.ownerDocument().createElement( name );
00126 el.appendChild(child);
00127 }
00128
return child;
00129 }
00130
00131
00132 QDomElement DomUtil::createElementByPath(
QDomDocument &doc,
const QString &path)
00133 {
00134
QStringList l = QStringList::split(
'/', path);
00135
00136
QDomElement el;
00137
if(&doc) el = doc.documentElement();
00138 QStringList::ConstIterator it;
00139
for (it = l.begin(); it != l.end(); ++it)
00140 el =
DomUtil::namedChildElement( el, *it );
00141
00142
while (!el.firstChild().isNull())
00143 el.removeChild(el.firstChild());
00144
00145
return el;
00146 }
00147
00148
00149 void DomUtil::writeEntry(
QDomDocument &doc,
const QString &path,
const QString &value)
00150 {
00151
QDomElement el =
createElementByPath(doc, path);
00152 el.appendChild(doc.createTextNode(value));
00153 }
00154
00155
00156 void DomUtil::writeIntEntry(
QDomDocument &doc,
const QString &path,
int value)
00157 {
00158
writeEntry(doc, path, QString::number(value));
00159 }
00160
00161
00162 void DomUtil::writeBoolEntry(
QDomDocument &doc,
const QString &path,
bool value)
00163 {
00164
writeEntry(doc, path, value?
"true" :
"false");
00165 }
00166
00167
00168 void DomUtil::writeListEntry(
QDomDocument &doc,
const QString &path,
const QString &tag,
00169
const QStringList &value)
00170 {
00171
QDomElement el =
createElementByPath(doc, path);
00172
00173 QStringList::ConstIterator it;
00174
for (it = value.begin(); it != value.end(); ++it) {
00175
QDomElement subEl = doc.createElement(tag);
00176 subEl.appendChild(doc.createTextNode(*it));
00177 el.appendChild(subEl);
00178 }
00179 }
00180
00181
00182 void DomUtil::writePairListEntry(
QDomDocument &doc,
const QString &path,
const QString &tag,
00183
const QString &firstAttr,
const QString &secondAttr,
00184
const PairList &value)
00185 {
00186
QDomElement el =
createElementByPath(doc, path);
00187
00188 PairList::ConstIterator it;
00189
for (it = value.begin(); it != value.end(); ++it) {
00190
QDomElement subEl = doc.createElement(tag);
00191 subEl.setAttribute(firstAttr, (*it).first);
00192 subEl.setAttribute(secondAttr, (*it).second);
00193 el.appendChild(subEl);
00194 }
00195 }
00196
00197 DomPath DomUtil::resolvPathStringExt(
const QString pathstring)
00198 {
00199
00200
unsigned int i;
00201
QStringList pathParts = QStringList::split(
'/',pathstring);
00202
DomPath dompath;
00203
for (i=0; i<pathParts.count(); i++)
00204 {
00205
QStringList pathElemParts = QStringList::split(
'|',pathParts[i],TRUE);
00206
DomPathElement dompathelem;
00207 dompathelem.
tagName = pathElemParts[0].simplifyWhiteSpace();
00208
if (pathElemParts.count()>1)
00209 {
00210
00211
QStringList attrParts = QStringList::split(
';',pathElemParts[1]);
00212
for (
unsigned int j=0; j<attrParts.count(); j++)
00213 {
00214
QStringList attribSet = QStringList::split(
'=',attrParts[j]);
00215
if (attribSet.count()<2)
00216
continue;
00217
DomAttribute domattribute;
00218 domattribute.
name = attribSet[0].simplifyWhiteSpace();
00219 domattribute.
value = attribSet[1].simplifyWhiteSpace();
00220 dompathelem.
attribute.append(domattribute);
00221 }
00222 }
00223
if (pathElemParts.count()>2)
00224 dompathelem.
matchNumber = pathElemParts[2].toInt();
00225
else
00226 dompathelem.
matchNumber = 0;
00227 dompath.append(dompathelem);
00228 }
00229
return dompath;
00230 }
00231
00232
00233 #define rightchild !wrongchild
00234
00235 QDomElement DomUtil::elementByPathExt(
QDomDocument &doc,
const QString &pathstring)
00236 {
00237
DomPath dompath =
resolvPathStringExt(pathstring);
00238
QDomElement elem = doc.documentElement();
00239
QDomNodeList children;
00240
QDomElement nextElem = elem;
00241
for (
unsigned int j=0; j<dompath.count(); j++)
00242 {
00243 children = nextElem.childNodes();
00244
DomPathElement dompathelement= dompath[j];
00245
bool wrongchild =
false;
00246
int matchCount = 0;
00247
for (
unsigned int i=0; i<children.count(); i++)
00248 {
00249 wrongchild =
false;
00250
QDomElement child = children.item(i).toElement();
00251
QString tag = child.tagName();
00252 tag = dompathelement.
tagName;
00253
if (child.tagName() == dompathelement.
tagName)
00254 {
00255
for (
unsigned int k=0; k<dompathelement.
attribute.count(); k++)
00256 {
00257
DomAttribute domattribute = dompathelement.
attribute[k];
00258
QDomAttr domattr = child.attributeNode(domattribute.
name);
00259
if (domattr.isNull() ||
00260 domattr.value() != domattribute.
value)
00261 {
00262 wrongchild =
true;
00263
break;
00264 }
00265 }
00266 }
00267
else
00268 wrongchild=
true;
00269
if (
rightchild)
00270 {
00271
if (dompathelement.
matchNumber == matchCount++)
00272 {
00273 nextElem = child;
00274
break;
00275 }
00276 }
00277 }
00278
if (wrongchild)
00279 {
00280
QDomElement nullDummy;
00281 nullDummy.clear();
00282
return nullDummy;
00283 }
00284 }
00285
return nextElem;
00286 }
00287
00288
00289 bool DomUtil::openDOMFile(
QDomDocument &doc,
QString filename)
00290 {
00291
QFile file( filename );
00292
if ( !
file.open( IO_ReadOnly ) )
00293
return false;
00294
if ( !doc.setContent( &
file ) ) {
00295
file.close();
00296
return false;
00297 }
00298
file.close();
00299
return true;
00300 }
00301
00302 bool DomUtil::saveDOMFile(
QDomDocument &doc,
QString filename)
00303 {
00304
QFile file( filename );
00305
if ( !
file.open( IO_ReadWrite | IO_Truncate ) )
00306
return false;
00307
QTextStream t( &
file );
00308 t << doc.toString();
00309
file.close();
00310
return true;
00311 }
00312
00313 bool DomUtil::removeTextNodes(
QDomDocument doc,
QString pathExt)
00314 {
00315
QDomElement elem =
elementByPathExt(doc,pathExt);
00316
if (elem.isNull())
00317
return false;
00318
QDomNodeList children = elem.childNodes();
00319
for (
unsigned int i=0;i<children.count();i++)
00320
if (children.item(i).isText())
00321 elem.removeChild(children.item(i));
00322
return true;
00323 }
00324
00325
00326 bool DomUtil::appendText(
QDomDocument doc,
QString pathExt,
QString text)
00327 {
00328
QDomElement elem =
elementByPathExt(doc,pathExt);
00329
if (elem.isNull())
00330
return false;
00331 elem.appendChild(doc.createTextNode(
text));
00332
return true;
00333 }
00334
00335
00336 bool DomUtil::replaceText(
QDomDocument doc,
QString pathExt,
QString text)
00337 {
00338
if (
removeTextNodes(doc,pathExt) &&
00339
appendText(doc,pathExt,
text))
00340
return true;
00341
else
00342
return false;
00343 }