• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • KDevelop Utility Library
 

KDevelop Utility Library

  • lib
  • util
domutil.cpp
1/***************************************************************************
2 * Copyright (C) 2001-2002 by Bernd Gehrmann *
3 * bernd@kdevelop.org *
4 * default support: Eray Ozkural (exa) *
5 * additions: John Firebaugh <jfirebaugh@kde.org> *
6 * Jakob Simon-Gaarde <jakob@simon-gaarde.dk> *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 ***************************************************************************/
14
15#include "domutil.h"
16
17#include <kdebug.h>
18#include <tqstringlist.h>
19#include <tqfile.h>
20
21
22void DomUtil::makeEmpty( TQDomElement& e )
23{
24 while( !e.firstChild().isNull() )
25 e.removeChild( e.firstChild() );
26}
27
28TQDomElement DomUtil::elementByPath(const TQDomDocument &doc, const TQString &path)
29{
30 TQStringList l = TQStringList::split('/', path);
31
32 TQDomElement el;
33 if(&doc) el = doc.documentElement();
34 TQStringList::ConstIterator it;
35 for (it = l.begin(); it != l.end(); ++it) {
36 el = el.namedItem(*it).toElement();
37 }
38
39 return el;
40}
41
42
43TQString DomUtil::readEntry(const TQDomDocument &doc, const TQString &path, const TQString &defaultEntry)
44{
45 TQDomElement el = elementByPath(doc, path);
46 if (el.isNull())
47 return defaultEntry;
48 else
49 return el.firstChild().toText().data();
50}
51
55
56TQString DomUtil::readEntryAux(const TQDomDocument &doc, const TQString &path)
57{
58 TQDomElement el = elementByPath(doc, path);
59 if (el.isNull())
60 return TQString();
61 else
62 return el.firstChild().toText().data();
63}
64
65int DomUtil::readIntEntry(const TQDomDocument &doc, const TQString &path, int defaultEntry)
66{
67 TQString entry = readEntryAux(doc, path);
68 if (entry.isNull())
69 return defaultEntry;
70 else
71 return entry.toInt();
72}
73
74
75bool DomUtil::readBoolEntry(const TQDomDocument &doc, const TQString &path, bool defaultEntry)
76{
77 TQString entry = readEntryAux(doc, path);
78 if (entry.isNull())
79 return defaultEntry;
80 else
81 return entry == "TRUE" || entry == "true";
82}
83
84
85TQStringList DomUtil::readListEntry(const TQDomDocument &doc, const TQString &path, const TQString &tag)
86{
87 TQStringList list;
88
89 TQDomElement el = elementByPath(doc, path);
90 TQDomElement subEl = el.firstChild().toElement();
91 while (!subEl.isNull()) {
92 if (subEl.tagName() == tag)
93 list << subEl.firstChild().toText().data();
94 subEl = subEl.nextSibling().toElement();
95 }
96
97 return list;
98}
99
100
101DomUtil::PairList DomUtil::readPairListEntry(const TQDomDocument &doc, const TQString &path, const TQString &tag,
102 const TQString &firstAttr, const TQString &secondAttr)
103{
104 PairList list;
105
106 TQDomElement el = elementByPath(doc, path);
107 TQDomElement subEl = el.firstChild().toElement();
108 while (!subEl.isNull()) {
109 if (subEl.tagName() == tag) {
110 TQString first = subEl.attribute(firstAttr);
111 TQString second = subEl.attribute(secondAttr);
112 list << Pair(first, second);
113 }
114 subEl = subEl.nextSibling().toElement();
115 }
116
117 return list;
118}
119
120TQMap<TQString, TQString> DomUtil::readMapEntry(const TQDomDocument &doc, const TQString& path)
121{
122 TQMap<TQString, TQString> map;
123
124 TQDomElement el = elementByPath(doc, path);
125 TQDomElement subEl = el.firstChild().toElement();
126 while (!subEl.isNull()) {
127 map[subEl.tagName()] = subEl.firstChild().toText().data();
128 subEl = subEl.nextSibling().toElement();
129 }
130
131 return map;
132}
133
134TQDomElement DomUtil::namedChildElement( TQDomElement& el, const TQString& name )
135{
136 TQDomElement child = el.namedItem( name ).toElement();
137 if (child.isNull()) {
138 child = el.ownerDocument().createElement( name );
139 el.appendChild(child);
140 }
141 return child;
142}
143
144
145TQDomElement DomUtil::createElementByPath(TQDomDocument &doc, const TQString &path)
146{
147 TQStringList l = TQStringList::split('/', path);
148
149 TQDomElement el;
150 if(&doc) el = doc.documentElement();
151 TQStringList::ConstIterator it;
152 for (it = l.begin(); it != l.end(); ++it)
153 el = DomUtil::namedChildElement( el, *it );
154
155 while (!el.firstChild().isNull())
156 el.removeChild(el.firstChild());
157
158 return el;
159}
160
161
162void DomUtil::writeEntry(TQDomDocument &doc, const TQString &path, const TQString &value)
163{
164 TQDomElement el = createElementByPath(doc, path);
165 el.appendChild(doc.createTextNode(value));
166}
167
168void DomUtil::writeMapEntry(TQDomDocument &doc, const TQString &path, const TQMap<TQString, TQString> &map)
169{
170 TQString basePath( path + "/" );
171 TQMap<TQString,TQString>::ConstIterator it;
172 for (it = map.begin(); it != map.end(); ++it)
173 {
174 kdDebug( 9010 ) << "writing " << basePath << ";" << it.key() << ";" << it.data() << endl;
175 if( ! it.key().isEmpty() )
176 writeEntry(doc, basePath + it.key(), it.data() );
177 }
178}
179
180void DomUtil::writeIntEntry(TQDomDocument &doc, const TQString &path, int value)
181{
182 writeEntry(doc, path, TQString::number(value));
183}
184
185
186void DomUtil::writeBoolEntry(TQDomDocument &doc, const TQString &path, bool value)
187{
188 writeEntry(doc, path, value? "true" : "false");
189}
190
191
192void DomUtil::writeListEntry(TQDomDocument &doc, const TQString &path, const TQString &tag,
193 const TQStringList &value)
194{
195 TQDomElement el = createElementByPath(doc, path);
196
197 TQStringList::ConstIterator it;
198 for (it = value.begin(); it != value.end(); ++it) {
199 TQDomElement subEl = doc.createElement(tag);
200 subEl.appendChild(doc.createTextNode(*it));
201 el.appendChild(subEl);
202 }
203}
204
205
206void DomUtil::writePairListEntry(TQDomDocument &doc, const TQString &path, const TQString &tag,
207 const TQString &firstAttr, const TQString &secondAttr,
208 const PairList &value)
209{
210 TQDomElement el = createElementByPath(doc, path);
211
212 PairList::ConstIterator it;
213 for (it = value.begin(); it != value.end(); ++it) {
214 TQDomElement subEl = doc.createElement(tag);
215 subEl.setAttribute(firstAttr, (*it).first);
216 subEl.setAttribute(secondAttr, (*it).second);
217 el.appendChild(subEl);
218 }
219}
220
221DomPath DomUtil::resolvPathStringExt(const TQString pathstring)
222{
223 // parse path
224 unsigned int i;
225 TQStringList pathParts = TQStringList::split('/',pathstring);
226 DomPath dompath;
227 for (i=0; i<pathParts.count(); i++)
228 {
229 TQStringList pathElemParts = TQStringList::split('|',pathParts[i],true);
230 DomPathElement dompathelem;
231 dompathelem.tagName = pathElemParts[0].simplifyWhiteSpace();
232 if (pathElemParts.count()>1)
233 {
234 // handle attributes
235 TQStringList attrParts = TQStringList::split(';',pathElemParts[1]);
236 for (unsigned int j=0; j<attrParts.count(); j++)
237 {
238 TQStringList attribSet = TQStringList::split('=',attrParts[j]);
239 if (attribSet.count()<2)
240 continue;
241 DomAttribute domattribute;
242 domattribute.name = attribSet[0].simplifyWhiteSpace();
243 domattribute.value = attribSet[1].simplifyWhiteSpace();
244 dompathelem.attribute.append(domattribute);
245 }
246 }
247 if (pathElemParts.count()>2)
248 dompathelem.matchNumber = pathElemParts[2].toInt();
249 else
250 dompathelem.matchNumber = 0; // or else the first
251 dompath.append(dompathelem);
252 }
253 return dompath;
254}
255
256
257#define rightchild !wrongchild
258
259TQDomElement DomUtil::elementByPathExt(TQDomDocument &doc, const TQString &pathstring)
260{
261 DomPath dompath = resolvPathStringExt(pathstring);
262 TQDomElement elem = doc.documentElement();
263 TQDomNodeList children;
264 TQDomElement nextElem = elem;
265 for (unsigned int j=0; j<dompath.count(); j++)
266 {
267 children = nextElem.childNodes();
268 DomPathElement dompathelement= dompath[j];
269 bool wrongchild = false;
270 int matchCount = 0;
271 for (unsigned int i=0; i<children.count(); i++)
272 {
273 wrongchild = false;
274 TQDomElement child = children.item(i).toElement();
275 TQString tag = child.tagName();
276 tag = dompathelement.tagName;
277 if (child.tagName() == dompathelement.tagName)
278 {
279 for (unsigned int k=0; k<dompathelement.attribute.count(); k++)
280 {
281 DomAttribute domattribute = dompathelement.attribute[k];
282 TQDomAttr domattr = child.attributeNode(domattribute.name);
283 if (domattr.isNull() ||
284 domattr.value() != domattribute.value)
285 {
286 wrongchild = true;
287 break;
288 }
289 }
290 }
291 else
292 wrongchild=true;
293 if (rightchild)
294 {
295 if (dompathelement.matchNumber == matchCount++)
296 {
297 nextElem = child;
298 break;
299 }
300 }
301 }
302 if (wrongchild)
303 {
304 TQDomElement nullDummy;
305 nullDummy.clear();
306 return nullDummy;
307 }
308 }
309 return nextElem;
310}
311
312
313bool DomUtil::openDOMFile(TQDomDocument &doc, TQString filename)
314{
315 TQFile file( filename );
316 if ( !file.open( IO_ReadOnly ) )
317 return false;
318 if ( !doc.setContent( &file ) ) {
319 file.close();
320 return false;
321 }
322 file.close();
323 return true;
324}
325
326bool DomUtil::saveDOMFile(TQDomDocument &doc, TQString filename)
327{
328 TQFile file( filename );
329 if ( !file.open( IO_ReadWrite | IO_Truncate ) )
330 return false;
331 TQTextStream t( &file );
332 t << doc.toString();
333 file.close();
334 return true;
335}
336
337bool DomUtil::removeTextNodes(TQDomDocument doc,TQString pathExt)
338{
339 TQDomElement elem = elementByPathExt(doc,pathExt);
340 if (elem.isNull())
341 return false;
342 TQDomNodeList children = elem.childNodes();
343 for (unsigned int i=0;i<children.count();i++)
344 if (children.item(i).isText())
345 elem.removeChild(children.item(i));
346 return true;
347}
348
349
350bool DomUtil::appendText(TQDomDocument doc, TQString pathExt, TQString text)
351{
352 TQDomElement elem = elementByPathExt(doc,pathExt);
353 if (elem.isNull())
354 return false;
355 elem.appendChild(doc.createTextNode(text));
356 return true;
357}
358
359
360bool DomUtil::replaceText(TQDomDocument doc, TQString pathExt, TQString text)
361{
362 if (removeTextNodes(doc,pathExt) &&
363 appendText(doc,pathExt,text))
364 return true;
365 else
366 return false;
367}
DomUtil::replaceText
static bool replaceText(TQDomDocument doc, TQString pathExt, TQString text)
Replace all chilt text nodes of parent described in pathExt with one new.
Definition domutil.cpp:360
DomUtil::readMapEntry
static TQMap< TQString, TQString > readMapEntry(const TQDomDocument &doc, const TQString &path)
Reads a string to string map.
Definition domutil.cpp:120
DomUtil::writeBoolEntry
static void writeBoolEntry(TQDomDocument &doc, const TQString &path, bool value)
Writes a boolean entry.
Definition domutil.cpp:186
DomUtil::elementByPathExt
static TQDomElement elementByPathExt(TQDomDocument &doc, const TQString &pathstring)
Retrieve an element specified with extended path examples:
Definition domutil.cpp:259
DomUtil::appendText
static bool appendText(TQDomDocument doc, TQString pathExt, TQString text)
Add child text node to parent described in pathExt.
Definition domutil.cpp:350
DomUtil::saveDOMFile
static bool saveDOMFile(TQDomDocument &doc, TQString filename)
Store contents of doc in file - filename.
Definition domutil.cpp:326
DomUtil::createElementByPath
static TQDomElement createElementByPath(TQDomDocument &doc, const TQString &path)
Retrieves an element by path, creating the necessary nodes.
Definition domutil.cpp:145
DomUtil::removeTextNodes
static bool removeTextNodes(TQDomDocument doc, TQString pathExt)
Remove all child text nodes of parent described in pathExt.
Definition domutil.cpp:337
DomUtil::readEntry
static TQString readEntry(const TQDomDocument &doc, const TQString &path, const TQString &defaultEntry=TQString())
Reads a string entry.
Definition domutil.cpp:43
DomUtil::openDOMFile
static bool openDOMFile(TQDomDocument &doc, TQString filename)
Open file - filename - and set setContents of doc.
Definition domutil.cpp:313
DomUtil::namedChildElement
static TQDomElement namedChildElement(TQDomElement &el, const TQString &name)
Retrieves a child element, creating it if it does not exist.
Definition domutil.cpp:134
DomUtil::writeEntry
static void writeEntry(TQDomDocument &doc, const TQString &path, const TQString &value)
Writes a string entry.
Definition domutil.cpp:162
DomUtil::writePairListEntry
static void writePairListEntry(TQDomDocument &doc, const TQString &path, const TQString &tag, const TQString &firstAttr, const TQString &secondAttr, const PairList &value)
Writes a list of string pairs.
Definition domutil.cpp:206
DomUtil::writeMapEntry
static void writeMapEntry(TQDomDocument &doc, const TQString &path, const TQMap< TQString, TQString > &map)
Writes a string to string map.
Definition domutil.cpp:168
DomUtil::readPairListEntry
static PairList readPairListEntry(const TQDomDocument &doc, const TQString &path, const TQString &tag, const TQString &firstAttr, const TQString &secondAttr)
Reads a list of string pairs.
Definition domutil.cpp:101
DomUtil::readIntEntry
static int readIntEntry(const TQDomDocument &doc, const TQString &path, int defaultEntry=0)
Reads a number entry.
Definition domutil.cpp:65
DomUtil::makeEmpty
static void makeEmpty(TQDomElement &)
Remove all child elements from a given element.
Definition domutil.cpp:22
DomUtil::elementByPath
static TQDomElement elementByPath(const TQDomDocument &doc, const TQString &path)
Retrieves an element by path, return null if any item along the path does not exist.
Definition domutil.cpp:28
DomUtil::resolvPathStringExt
static DomPath resolvPathStringExt(const TQString pathstring)
Resolves an extended path Extended path format: pathpart: tag[|attr1=value[;attr2=value;....
Definition domutil.cpp:221
DomUtil::writeIntEntry
static void writeIntEntry(TQDomDocument &doc, const TQString &path, int value)
Writes a number entry.
Definition domutil.cpp:180
DomUtil::readListEntry
static TQStringList readListEntry(const TQDomDocument &doc, const TQString &path, const TQString &tag)
Reads a list entry.
Definition domutil.cpp:85
DomUtil::readBoolEntry
static bool readBoolEntry(const TQDomDocument &doc, const TQString &path, bool defaultEntry=false)
Reads a boolean entry.
Definition domutil.cpp:75
DomUtil::writeListEntry
static void writeListEntry(TQDomDocument &doc, const TQString &path, const TQString &tag, const TQStringList &value)
Writes a string list element.
Definition domutil.cpp:192
domutil.h
Utility functions to operate on DOM.

KDevelop Utility Library

Skip menu "KDevelop Utility Library"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

KDevelop Utility Library

Skip menu "KDevelop Utility Library"
  • buildtools
  •   lib
  •     base
  •     parsers
  •       autotools
  •       qmake
  •     widgets
  •   api
  • languages
  •   lib
  •     debugger
  •     designer_integration
  •     interfaces
  • lib
  •   catalog
  •   interfaces
  •     extensions
  •     external
  •     extras
  •   util
  •   widgets
  •     propeditor
  • parts
  •   documentation
  •     interfaces
  • src
  •   profileengine
  •     lib
Generated for KDevelop Utility Library by doxygen 1.9.8
This website is maintained by Timothy Pearson.