summaryrefslogtreecommitdiffstats
path: root/part/qdom_add.cpp
blob: ccca6bc4e455d8c84d4da30daf13b07d8c65b5bf (plain)
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 TQt DOM classes. */

#include "qdom_add.h"

#include <kiconloader.h>
#include <kdebug.h>

#include <tqtextstream.h>

#include "kxmleditorfactory.h"
#include "kxesearchdialog.h"

TQPixmap g_iconElement( UserIcon("xml_element",KXMLEditorFactory::instance()) );
TQPixmap g_iconText( UserIcon("xml_text",KXMLEditorFactory::instance()) );
TQPixmap g_iconComment( UserIcon("xml_comment",KXMLEditorFactory::instance()) );
TQPixmap g_iconCDATASection( UserIcon("xml_cdata",KXMLEditorFactory::instance()) );
TQPixmap g_iconProcessingInstruction( UserIcon("xml_procinstr",KXMLEditorFactory::instance()) );
TQPixmap g_iconElement_b( UserIcon("xml_element_b",KXMLEditorFactory::instance()) );
TQPixmap g_iconText_b( UserIcon("xml_text_b",KXMLEditorFactory::instance()) );
TQPixmap g_iconComment_b( UserIcon("xml_comment_b",KXMLEditorFactory::instance()) );
TQPixmap g_iconCDATASection_b( UserIcon("xml_cdata_b",KXMLEditorFactory::instance()) );
TQPixmap g_iconProcessingInstruction_b( UserIcon("xml_procinstr_b",KXMLEditorFactory::instance()) );
TQPixmap g_iconUnknown;

const TQPixmap & domTool_getIconForNodeType( TQDomNode::NodeType type, bool bBookmarked )
{
	if(!bBookmarked)
    { switch(type)
	      { case  TQDomNode::ElementNode: return g_iconElement; break;
		      case	TQDomNode::TextNode: return g_iconText; break;
		      case	TQDomNode::CDATASectionNode: return g_iconCDATASection; break;
		      case	TQDomNode::CommentNode: return g_iconComment; break;
		      case	TQDomNode::ProcessingInstructionNode: return g_iconProcessingInstruction; break;

		      default:
			      kdDebug() << "domTool_getIconForNodeType: unknown node type (" << type << ")" << endl;
	      }
    }
  else
    { switch(type)
	      { case  TQDomNode::ElementNode: return g_iconElement_b; break;
		      case	TQDomNode::TextNode: return g_iconText_b; break;
		      case	TQDomNode::CDATASectionNode: return g_iconCDATASection_b; break;
		      case	TQDomNode::CommentNode: return g_iconComment_b; break;
		      case	TQDomNode::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
TQString domTool_getPath( const TQDomNode & node )
{
	if ( node.isNull() )
	{
		kdDebug() << "domTool_getPath: elelent given" << endl;
		return TQString();
	}

  if(node.isElement())
		{
      kdDebug() << "use domTool_getPath( const TQDomElement & domElement ) for elements" << endl;
    }
    
	TQString strReturn;

  TQDomNode 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
TQString domTool_getPath( const TQDomElement & domElement )
{
	if ( domElement.isNull() )
	{
		kdDebug() << "domTool_getPath: no node given" << endl;
		return TQString();
	}

	TQString strReturn;
	TQDomNode 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
    TQDomNode tmpNode = domElement.previousSibling();
    while ( ! tmpNode.isNull() )
    {
      if(tmpNode.isElement())
      {
        TQDomElement 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
      TQDomNode tmpNode = domElement.nextSibling();
      while ( ! tmpNode.isNull() )
      {
        if(tmpNode.isElement())
        {
          TQDomElement 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)
      {
        TQString 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 TQDomNode & node )
{
	if ( node.isNull() )
	{
		kdDebug() << "domTool_getLevel: internal implementation error - the given node is an empty one" << endl;
		return 0;
	}

	unsigned int iLevel = 0;
	TQDomNode parentNode = node.parentNode();
	while ( ! parentNode.isNull() )
	{
		iLevel++;
		parentNode = parentNode.parentNode();
	}

	return iLevel - 1;
}

TQString domTool_save( const TQDomNode & node, int iIndent )
{
	TQString strXML;
	TQTextStream ts( & strXML, IO_WriteOnly );

  node.save(ts, iIndent);
	return strXML;
}

TQDomNode domTool_prevNode( const TQDomNode & node )
{
	if ( node.isNull() )
	{
		kdDebug() << "domTool_prevNode: internal implementation error - the given node is an empty one" << endl;
		return TQDomNode();
	}

	if ( ! node.previousSibling().isNull() ) // if there is a prev. sibling
	{                                    // return its last grand child (if there is any)
		TQDomNode 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)

}

TQDomNode domTool_nextNode( const TQDomNode & node )
{
	if ( node.isNull() )
	{
		kdDebug() << "domTool_nextNode: internal implementation error - the given node is an empty one" << endl;
		return TQDomNode();
	}

	// 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)
	TQDomNode 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 TQDomNode(); // return empty node
}

TQDomNode domTool_matchingNode( const TQDomNode & node, const TQString & szPath )
{
  if(szPath.length() == 0)
    return TQDomNode(); // return void node

  TQString szNodePath = node.isDocument() ? TQString("") : 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 TQDomNode(); // 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 TQDomNode(); // otherwise we don't need to check the childs
  */   
     
	// recursively check the childs
	TQDomNode nodeChild = node.firstChild();
	TQDomNode nodeTmp;
	while ( ! nodeChild.isNull() )
	{
		nodeTmp = domTool_matchingNode( nodeChild, szPath );
		if ( ! nodeTmp.isNull() )
			return nodeTmp;
		nodeChild = nodeChild.nextSibling();
	}

	return TQDomNode(); // nothing found -> return empty node
}

bool domTool_match( TQDomNode 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 TQDomNode::ElementNode: // ----------------------------------------
		{
			if ( pConditions->getInElementNames() )
			{
				if ( node.toElement().tagName().find( pConditions->getSearchString(), 0, pConditions->getMatchCase() ) >= 0 )
					return true;
			}

			if ( ( pConditions->getInAttributeNames() ) || ( pConditions->getInAttributeValues() ) )
			{
				TQDomNamedNodeMap 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 TQDomNode::TextNode: // ----------------------------------------
		case TQDomNode::CDATASectionNode:
		case TQDomNode::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;
}