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
|
/* This file is part of the KDE project
Copyright (C) 2004 David Faure <faure@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KoDom.h"
#include "KoXmlNS.h"
#include <tqapplication.h>
#include <assert.h>
//static void debugElemNS( const TQDomElement& elem )
//{
// qDebug( "nodeName=%s tagName=%s localName=%s prefix=%s namespaceURI=%s", elem.nodeName().latin1(), elem.tagName().latin1(), elem.localName().latin1(), elem.prefix().latin1(), elem.namespaceURI().latin1() );
//}
void testTQDom( const TQDomDocument& doc )
{
TQDomElement docElem = doc.documentElement();
//debugElemNS( docElem );
assert( docElem.nodeName() == "o:document-content" );
assert( docElem.tagName() == "document-content" );
assert( docElem.localName() == "document-content" );
assert( docElem.prefix() == "o" );
assert( docElem.namespaceURI() == KoXmlNS::office );
// WARNING, elementsByTagNameNS is recursive!!!
TQDomNodeList docElemChildren = docElem.elementsByTagNameNS( KoXmlNS::office, "body" );
assert( docElemChildren.length() == 1 );
TQDomElement elem = docElemChildren.item( 0 ).toElement();
//debugElemNS( elem );
assert( elem.tagName() == "body" );
assert( elem.localName() == "body" );
assert( elem.prefix() == "o" );
assert( elem.namespaceURI() == KoXmlNS::office );
TQDomNode n = elem.firstChild();
for ( ; !n.isNull() ; n = n.nextSibling() ) {
if ( !n.isElement() )
continue;
TQDomElement e = n.toElement();
//debugElemNS( e );
assert( e.tagName() == "p" );
assert( e.localName() == "p" );
assert( e.prefix().isEmpty() );
assert( e.namespaceURI() == KoXmlNS::text );
}
qDebug("testTQDom... ok");
}
void testKoDom( const TQDomDocument& doc )
{
TQDomElement docElem = KoDom::namedItemNS( doc, KoXmlNS::office, "document-content" );
assert( !docElem.isNull() );
assert( docElem.localName() == "document-content" );
assert( docElem.namespaceURI() == KoXmlNS::office );
TQDomElement body = KoDom::namedItemNS( docElem, KoXmlNS::office, "body" );
assert( !body.isNull() );
assert( body.localName() == "body" );
assert( body.namespaceURI() == KoXmlNS::office );
TQDomElement p = KoDom::namedItemNS( body, KoXmlNS::text, "p" );
assert( !p.isNull() );
assert( p.localName() == "p" );
assert( p.namespaceURI() == KoXmlNS::text );
const TQDomElement officeStyle = KoDom::namedItemNS( docElem, KoXmlNS::office, "styles" );
assert( !officeStyle.isNull() );
// Look for a non-existing element
TQDomElement notexist = KoDom::namedItemNS( body, KoXmlNS::text, "notexist" );
assert( notexist.isNull() );
int count = 0;
TQDomElement elem;
forEachElement( elem, body ) {
assert( elem.localName() == "p" );
assert( elem.namespaceURI() == KoXmlNS::text );
++count;
}
assert( count == 2 );
// Attributes
// ### TQt bug: it doesn't work if using style-name instead of text:style-name in the XML
const TQString styleName = p.attributeNS( KoXmlNS::text, "style-name", TQString() );
qDebug( "%s", styleName.latin1() );
assert( styleName == "L1" );
qDebug("testKoDom... ok");
}
int main( int argc, char** argv ) {
TQApplication app( argc, argv, TQApplication::Tty );
const TQCString xml = TQCString( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<o:document-content xmlns:o=\"" )
+ KoXmlNS::office
+ "\" xmlns=\"" + KoXmlNS::text
+ "\" xmlns:text=\"" + KoXmlNS::text
+ "\">\n"
"<o:body><p text:style-name=\"L1\">foobar</p><p>2nd</p></o:body><o:styles></o:styles>\n"
"</o:document-content>\n";
TQDomDocument doc;
//TQXmlSimpleReader reader;
//reader.setFeature( "http://xml.org/sax/features/namespaces", TRUE );
//reader.setFeature( "http://xml.org/sax/features/namespace-prefixes", FALSE );
bool ok = doc.setContent( xml, true /* namespace processing */ );
assert( ok );
testTQDom(doc);
testKoDom(doc);
}
|