1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/***************************************************************************
qdom_add.cpp - description
-------------------
begin : Wed Nov 21 2001
copyright : (C) 2001, 2002, 2003 by The KXMLEditor Team
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
/** This file contains useful datatypes and functions in addition to the Qt DOM classes. */
#include "qdom_add.h"
#include <kiconloader.h>
#include <kdebug.h>
#include <qtextstream.h>
#include "kxmleditorfactory.h"
#include "kxesearchdialog.h"
QPixmap g_iconElement( UserIcon("xml_element",KXMLEditorFactory::instance()) );
QPixmap g_iconText( UserIcon("xml_text",KXMLEditorFactory::instance()) );
QPixmap g_iconComment( UserIcon("xml_comment",KXMLEditorFactory::instance()) );
QPixmap g_iconCDATASection( UserIcon("xml_cdata",KXMLEditorFactory::instance()) );
QPixmap g_iconProcessingInstruction( UserIcon("xml_procinstr",KXMLEditorFactory::instance()) );
QPixmap g_iconElement_b( UserIcon("xml_element_b",KXMLEditorFactory::instance()) );
QPixmap g_iconText_b( UserIcon("xml_text_b",KXMLEditorFactory::instance()) );
QPixmap g_iconComment_b( UserIcon("xml_comment_b",KXMLEditorFactory::instance()) );
QPixmap g_iconCDATASection_b( UserIcon("xml_cdata_b",KXMLEditorFactory::instance()) );
QPixmap g_iconProcessingInstruction_b( UserIcon("xml_procinstr_b",KXMLEditorFactory::instance()) );
QPixmap g_iconUnknown;
const QPixmap & domTool_getIconForNodeType( QDomNode::NodeType type, bool bBookmarked )
{
if(!bBookmarked)
{ switch(type)
{ case QDomNode::ElementNode: return g_iconElement; break;
case QDomNode::TextNode: return g_iconText; break;
case QDomNode::CDATASectionNode: return g_iconCDATASection; break;
case QDomNode::CommentNode: return g_iconComment; break;
case QDomNode::ProcessingInstructionNode: return g_iconProcessingInstruction; break;
default:
kdDebug() << "domTool_getIconForNodeType: unknown node type (" << type << ")" << endl;
}
}
else
{ switch(type)
{ case QDomNode::ElementNode: return g_iconElement_b; break;
case QDomNode::TextNode: return g_iconText_b; break;
case QDomNode::CDATASectionNode: return g_iconCDATASection_b; break;
case QDomNode::CommentNode: return g_iconComment_b; break;
case QDomNode::ProcessingInstructionNode: return g_iconProcessingInstruction_b; break;
default:
kdDebug() << "domTool_getIconForNodeType: unknown node type (" << type << ")" << endl;
}
}
return g_iconUnknown;
}
// Obtain XPath for all nodes, instead of elements
QString domTool_getPath( const QDomNode & node )
{
if ( node.isNull() )
{
kdDebug() << "domTool_getPath: elelent given" << endl;
return QString();
}
if(node.isElement())
{
kdDebug() << "use domTool_getPath( const QDomElement & domElement ) for elements" << endl;
}
QString strReturn;
QDomNode parentNode = node.parentNode();
if ( (!parentNode.isNull()) && (!parentNode.isDocument()) )
{
strReturn = domTool_getPath( parentNode.toElement() ); // get the parent's path
strReturn += "/"; // append slash
strReturn += node.nodeName(); // append the given node's name
}
else
strReturn = node.nodeName(); // set the given node's name (must be root element)
return strReturn;
}
// Obtain XPath for elements
QString domTool_getPath( const QDomElement & domElement )
{
if ( domElement.isNull() )
{
kdDebug() << "domTool_getPath: no node given" << endl;
return QString();
}
QString strReturn;
QDomNode parentNode = domElement.parentNode();
if ( (!parentNode.isNull()) && (!parentNode.isDocument()) )
{
// calculate index - only for elements with the same name
int i = 0;
bool bUseIndex = false; // index is used only when exist sibling(s) with the same name
// traverse previous sibling elements with same name and calculate index
QDomNode tmpNode = domElement.previousSibling();
while ( ! tmpNode.isNull() )
{
if(tmpNode.isElement())
{
QDomElement domSiblingElement = tmpNode.toElement();
if(domElement.tagName() == domSiblingElement.tagName())
{ i++; bUseIndex = true;
}
}
tmpNode = tmpNode.previousSibling();
}
if(bUseIndex == false)
{
// traverse next sibling elements with same name
// and decide, if index is necessary
QDomNode tmpNode = domElement.nextSibling();
while ( ! tmpNode.isNull() )
{
if(tmpNode.isElement())
{
QDomElement domSiblingElement = tmpNode.toElement();
if(domElement.tagName() == domSiblingElement.tagName())
bUseIndex = true;
}
tmpNode = tmpNode.nextSibling();
}
}
strReturn = domTool_getPath( parentNode.toElement() ); // get the parent's path
strReturn += "/"; // append slash
strReturn += domElement.nodeName(); // append the given node's name
if(bUseIndex)
{
QString strIndex;
strIndex.setNum(i+1);
strReturn += "[" + strIndex + "]"; // append the index
}
}
else
strReturn = domElement.nodeName(); // set the given node's name (must be root element)
return strReturn;
}
unsigned int domTool_getLevel( const QDomNode & node )
{
if ( node.isNull() )
{
kdDebug() << "domTool_getLevel: internal implementation error - the given node is an empty one" << endl;
return 0;
}
unsigned int iLevel = 0;
QDomNode parentNode = node.parentNode();
while ( ! parentNode.isNull() )
{
iLevel++;
parentNode = parentNode.parentNode();
}
return iLevel - 1;
}
QString domTool_save( const QDomNode & node, int iIndent )
{
QString strXML;
QTextStream ts( & strXML, IO_WriteOnly );
node.save(ts, iIndent);
return strXML;
}
QDomNode domTool_prevNode( const QDomNode & node )
{
if ( node.isNull() )
{
kdDebug() << "domTool_prevNode: internal implementation error - the given node is an empty one" << endl;
return QDomNode();
}
if ( ! node.previousSibling().isNull() ) // if there is a prev. sibling
{ // return its last grand child (if there is any)
QDomNode prevNode = node.previousSibling();
while ( ! prevNode.lastChild().isNull() )
prevNode = prevNode.lastChild();
return prevNode;
}
else // if there is no prev. sibling, return
return node.parentNode(); // the nodes parent (if there is any)
}
QDomNode domTool_nextNode( const QDomNode & node )
{
if ( node.isNull() )
{
kdDebug() << "domTool_nextNode: internal implementation error - the given node is an empty one" << endl;
return QDomNode();
}
// checking for a child
if ( ! node.firstChild().isNull() )
return node.firstChild();
// there is no child -> checking for the next sibling
if ( ! node.nextSibling().isNull() )
return node.nextSibling();
// there is no next sibling -> checking for parents' next sibling(s)
QDomNode nodeParent = node.parentNode();
while ( ! nodeParent.isNull() )
{
if ( ! nodeParent.nextSibling().isNull() )
return nodeParent.nextSibling();
nodeParent = nodeParent.parentNode(); // parent has no sibling - try its parent
}
// parent has no parents anymore
return QDomNode(); // return empty node
}
QDomNode domTool_matchingNode( const QDomNode & node, const QString & szPath )
{
if(szPath.length() == 0)
return QDomNode(); // return void node
QString szNodePath = node.isDocument() ? QString("") : domTool_getPath(node);
if ( szPath == szNodePath ) // test if the strings match
return node;
/* L.V. those optimalizations disallow find proc. instr. at doc. level
// we will find any node in root element subtree
if ( szPath.length() <= szNodePath.length() ) // the given string must be longer
return QDomNode(); // otherwise we don't need to check the childs
if ( szPath.left(szNodePath.length()) != szNodePath ) // the nodes path must be left part of the given path
return QDomNode(); // otherwise we don't need to check the childs
*/
// recursively check the childs
QDomNode nodeChild = node.firstChild();
QDomNode nodeTmp;
while ( ! nodeChild.isNull() )
{
nodeTmp = domTool_matchingNode( nodeChild, szPath );
if ( ! nodeTmp.isNull() )
return nodeTmp;
nodeChild = nodeChild.nextSibling();
}
return QDomNode(); // nothing found -> return empty node
}
bool domTool_match( QDomNode node, const KXESearchDialog * const pConditions )
{
if ( node.isNull() )
{
kdDebug() << "domTool_match: internal implementation error - the given node is an empty one" << endl;
return false;
}
if ( ! pConditions )
{
kdDebug() << "domTool_match: internal implementation error - the given pointer is a null pointer" << endl;
return false;
}
switch ( node.nodeType() )
{
case QDomNode::ElementNode: // ----------------------------------------
{
if ( pConditions->getInElementNames() )
{
if ( node.toElement().tagName().find( pConditions->getSearchString(), 0, pConditions->getMatchCase() ) >= 0 )
return true;
}
if ( ( pConditions->getInAttributeNames() ) || ( pConditions->getInAttributeValues() ) )
{
QDomNamedNodeMap list = node.toElement().attributes();
unsigned int iLength = list.length();
if ( iLength <= 0 )
return false; // no attributes
for ( unsigned int iRow = 0; iRow < iLength; iRow++ )
{
if ( pConditions->getInAttributeNames() )
if ( list.item(iRow).toAttr().name().find( pConditions->getSearchString(), 0, pConditions->getMatchCase() ) >= 0 )
return true;
if ( pConditions->getInAttributeValues() )
if ( list.item(iRow).toAttr().value().find( pConditions->getSearchString(), 0, pConditions->getMatchCase() ) >= 0 )
return true;
}
return false;
}
return false;
break;
}
case QDomNode::TextNode: // ----------------------------------------
case QDomNode::CDATASectionNode:
case QDomNode::CommentNode:
{
if ( pConditions->getInContents() )
{
if ( node.toCharacterData().data().find( pConditions->getSearchString(), 0, pConditions->getMatchCase() ) >= 0 )
return true;
else
return false;
}
else
return false;
break;
}
// TODO implement this for the other node types (eg proc.instr.)
default:
kdDebug() << "domTool_match: unknown node type (" << node.nodeType() << ")" << endl;
}
return true;
}
|