F:/KPlato/koffice/libs/store/tests/kodomtest.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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 //Added by qt3to4:
00027 #include <QByteArray>
00028 #include <assert.h>
00029 
00030 //static void debugElemNS( const QDomElement& elem )
00031 //{
00032 //    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() );
00033 //}
00034 
00035 void testQDom( const KoXmlDocument& doc )
00036 {
00037     KoXmlElement docElem = doc.documentElement();
00038     //debugElemNS( docElem );
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     //debugElemNS( elem );
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         //debugElemNS( e );
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     // Look for a non-existing element
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     // Attributes
00102     // ### Qt bug: it doesn't work if using style-name instead of text:style-name in the XML
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     //QXmlSimpleReader reader;
00123     //reader.setFeature( "http://xml.org/sax/features/namespaces", TRUE );
00124     //reader.setFeature( "http://xml.org/sax/features/namespace-prefixes", FALSE );
00125     bool ok = doc.setContent( xml, true /* namespace processing */ );
00126     assert( ok );
00127 
00128     testQDom(doc);
00129     testKoDom(doc);
00130 }

Généré le Wed Nov 22 23:41:14 2006 pour KPlato par  doxygen 1.5.1-p1