00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KoXmlReader.h"
00021
00022 static const char* const KoXmlNS_office = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
00023 static const char* const KoXmlNS_text = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
00024
00025 #include <QApplication>
00026
00027 #include <QByteArray>
00028 #include <assert.h>
00029
00030
00031
00032
00033
00034
00035 void testQDom( const KoXmlDocument& doc )
00036 {
00037 KoXmlElement docElem = doc.documentElement();
00038
00039 assert( docElem.nodeName() == "o:document-content" );
00040 assert( docElem.tagName() == "document-content" );
00041 assert( docElem.localName() == "document-content" );
00042 assert( docElem.prefix() == "o" );
00043 assert( docElem.namespaceURI() == KoXmlNS_office );
00044
00045 KoXmlElement elem = KoXml::namedItemNS( docElem, KoXmlNS_office, "body" );
00046
00047
00048 assert( elem.tagName() == "body" );
00049 assert( elem.localName() == "body" );
00050 assert( elem.prefix() == "o" );
00051 assert( elem.namespaceURI() == KoXmlNS_office );
00052
00053 KoXmlNode n = elem.firstChild();
00054 for ( ; !n.isNull() ; n = n.nextSibling() ) {
00055 if ( !n.isElement() )
00056 continue;
00057 KoXmlElement e = n.toElement();
00058
00059 assert( e.tagName() == "p" );
00060 assert( e.localName() == "p" );
00061 assert( e.prefix().isEmpty() );
00062 assert( e.namespaceURI() == KoXmlNS_text );
00063 }
00064
00065 qDebug("testQDom... ok");
00066 }
00067
00068 void testKoDom( const KoXmlDocument& doc )
00069 {
00070 KoXmlElement docElem = KoXml::namedItemNS( doc, KoXmlNS_office, "document-content" );
00071 assert( !docElem.isNull() );
00072 assert( docElem.localName() == "document-content" );
00073 assert( docElem.namespaceURI() == KoXmlNS_office );
00074
00075 KoXmlElement body = KoXml::namedItemNS( docElem, KoXmlNS_office, "body" );
00076 assert( !body.isNull() );
00077 assert( body.localName() == "body" );
00078 assert( body.namespaceURI() == KoXmlNS_office );
00079
00080 KoXmlElement p = KoXml::namedItemNS( body, KoXmlNS_text, "p" );
00081 assert( !p.isNull() );
00082 assert( p.localName() == "p" );
00083 assert( p.namespaceURI() == KoXmlNS_text );
00084
00085 const KoXmlElement officeStyle = KoXml::namedItemNS( docElem, KoXmlNS_office, "styles" );
00086 assert( !officeStyle.isNull() );
00087
00088
00089 KoXmlElement notexist = KoXml::namedItemNS( body, KoXmlNS_text, "notexist" );
00090 assert( notexist.isNull() );
00091
00092 int count = 0;
00093 KoXmlElement elem;
00094 forEachElement( elem, body ) {
00095 assert( elem.localName() == "p" );
00096 assert( elem.namespaceURI() == KoXmlNS_text );
00097 ++count;
00098 }
00099 assert( count == 2 );
00100
00101
00102
00103 const QString styleName = p.attributeNS( KoXmlNS_text, "style-name", QString::null );
00104 qDebug( "%s", qPrintable( styleName ) );
00105 assert( styleName == "L1" );
00106
00107 qDebug("testKoDom... ok");
00108 }
00109
00110 int main( int argc, char** argv ) {
00111 QApplication app( argc, argv, QApplication::Tty );
00112
00113 const QByteArray xml = QByteArray( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
00114 "<o:document-content xmlns:o=\"" )
00115 + KoXmlNS_office
00116 + "\" xmlns=\"" + KoXmlNS_text
00117 + "\" xmlns:text=\"" + KoXmlNS_text
00118 + "\">\n"
00119 "<o:body><p text:style-name=\"L1\">foobar</p><p>2nd</p></o:body><o:styles></o:styles>\n"
00120 "</o:document-content>\n";
00121 KoXmlDocument doc;
00122
00123
00124
00125 bool ok = doc.setContent( xml, true );
00126 assert( ok );
00127
00128 testQDom(doc);
00129 testKoDom(doc);
00130 }