• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • sources
  • kde-4.14
  • kdelibs
  • khtml
  • xpath
interpreter_tester.cpp
Go to the documentation of this file.
1 /*
2  * interpreter_tester.cpp - Copyright 2005 Frerich Raabe <raabe@kde.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "expression.h"
26 #include "parsedstatement.h"
27 
28 #include "XPathExceptionImpl.h"
29 
30 #include "DocumentImpl.h"
31 #include "DOMStringImpl.h"
32 #include "KDOMParser.h"
33 #include "KDOMParserFactory.h"
34 #include "NamedAttrMapImpl.h"
35 
36 #include "kdom.h"
37 
38 #include <kaboutdata.h>
39 #include <kapplication.h>
40 #include <kcmdlineargs.h>
41 
42 #include <QBuffer>
43 #include <QtDebug>
44 
45 using namespace KDOM;
46 
47 void check( DocumentImpl *doc, const QString &statement, const QString &expected )
48 {
49  ParsedStatement s( statement );
50  Value result = s.evaluate( doc );
51  if ( !result.isString() ) {
52  qDebug( "ERROR: Query '%s' did not return a string!", statement.latin1() );
53  exit( 1 );
54  }
55 
56  QString string = result.toString();
57  if ( string != expected ) {
58  qDebug( "ERROR: Failed to interprete '%s' correctly!", statement.latin1() );
59  qDebug( "Expected to get: %s", expected.latin1() );
60  qDebug( "Got : %s", string.latin1() );
61  exit( 1 );
62  }
63 }
64 
65 
66 void check( DocumentImpl *doc, const QString &statement, double expected )
67 {
68  ParsedStatement s( statement );
69  Value result = s.evaluate( doc );
70  if ( !result.isNumber() ) {
71  qDebug( "ERROR: Query '%s' did not return a number!", statement.latin1() );
72  exit( 1 );
73  }
74 
75  double number = result.toNumber();
76  if ( number != expected ) {
77  qDebug( "ERROR: Failed to interprete '%s' correctly!", statement.latin1() );
78  qDebug( "Expected to get: %f", expected );
79  qDebug( "Got : %f", number );
80  exit( 1 );
81  }
82 }
83 
84 void check( DocumentImpl *doc, const QString &statement,
85  const QStringList &idsOfExpectedMatches )
86 {
87  ParsedStatement s( statement );
88  Value result = s.evaluate( doc );
89  if ( !result.isNodeset() ) {
90  qDebug( "ERROR: Query '%s' did not return a nodeset!", statement.latin1() );
91  exit( 1 );
92  }
93 
94  QStringList idsOfResultMatches;
95 
96  DomNodeList nodes = result.toNodeset();
97  foreach( NodeImpl *node, nodes ) {
98  if ( node->nodeType() != ELEMENT_NODE ) {
99  continue;
100  }
101  NodeImpl *idNode = 0;
102  NamedAttrMapImpl *attrs = node->attributes( true /*read-only*/ );
103  for ( unsigned long i = 0; i < attrs->length(); ++i ) {
104  idNode = attrs->item( i );
105  if ( idNode->nodeName()->string() == "id" ) {
106  break;
107  }
108  }
109  if ( !idNode ) {
110  qDebug( "ERROR: Found match without id attribute!" );
111  exit( 1 );
112  }
113  idsOfResultMatches.append( idNode->nodeValue()->string() );
114  }
115 
116  bool failure = false;
117 
118  foreach( QString id, idsOfExpectedMatches ) {
119  if ( !idsOfResultMatches.contains( id ) ) {
120  failure = true;
121  break;
122  }
123  }
124 
125  if ( !failure ) {
126  foreach( QString id, idsOfResultMatches ) {
127  if ( !idsOfExpectedMatches.contains( id ) ) {
128  failure = true;
129  break;
130  }
131  }
132  }
133 
134  if ( failure ) {
135  qDebug() << "ERROR: Failed to interprete '" << statement << "' correctly!";
136  qDebug() << "Expected to match: " << idsOfExpectedMatches.join( "," );
137  qDebug() << "Got matches : " << idsOfResultMatches.join( "," );
138  exit( 1 );
139  }
140 }
141 
142 int main( int argc, char **argv )
143 {
144  QString bookMarkup =
145  "<book id=\"1\">"
146  "<abstract id=\"2\">"
147  "</abstract>"
148  "<chapter id=\"3\" title=\"Introduction\">"
149  "<para id=\"4\">Blah blah blah</para>"
150  "<para id=\"5\">Foo bar yoyodyne</para>"
151  "</chapter>"
152  "<chapter id=\"6\" title=\"TEST\" type=\"x\">"
153  "<para id=\"7\">My sister got bitten by a yak.</para>"
154  "</chapter>"
155  "<chapter id=\"8\" title=\"note\" type=\"y\">"
156  "<para id=\"9\">141,000</para>"
157  "</chapter>"
158  "<chapter id=\"10\" title=\"note\">"
159  "<para id=\"11\">Hell yeah, yaks are nice.</para>"
160  "</chapter>"
161  "</book>";
162 
163  KAboutData about( "interpreter_tester", "interpreter_tester", "0.1", "Tests KDOM's XPath 1.0 Interpreter" );
164  KCmdLineArgs::init( argc, argv, &about );
165 
166  KApplication::disableAutoDcopRegistration();
167  KApplication app( false, false );
168 
169  // Parser::syncParse called blow deletes the given buffer, so we
170  // cannot use QBuffer objects which live on the stack.
171  QBuffer *buf = new QBuffer;
172  buf->open( QBuffer::ReadWrite );
173  buf->write( bookMarkup.toUtf8() );
174 
175  // I can't believe that I have to go through all these hoops to get
176  // a DocumentImpl* out of a string with markup.
177  Parser *parser = ParserFactory::self()->request( KURL(), 0, "qxml" );
178  DocumentImpl *doc = parser->syncParse( buf );
179 
180  try {
181  check( doc, "/book", QStringList() << "1" );
182  check( doc, "/book/chapter", QStringList() << "3" << "6" << "8" );
183  check( doc, "/book/chapter[@title=\"TEST\"]", QStringList() << "6" );
184  check( doc, "/book/chapter[last()-1]", QStringList() << "8" );
185  check( doc, "/book/chapter[contains(string(@title), \"tro\")]", QStringList() << "3" );
186  check( doc, "//para", QStringList() << "4" << "5" << "7" << "9" );
187  check( doc, "/book/chapter[position()=2]/following::*", QStringList() << "8" << "9" );
188  check( doc, "//chapter[@title and @type]", QStringList() << "6" << "8" );
189  check( doc, "/book/chapter[attribute::title = \"note\"][position() = 2]", QStringList() << "10" );
190  check( doc, "count(//para)", 5.0 );
191  check( doc, "string(/book/chapter/para[text() = \"Foo bar yoyodyne\" ])", QLatin1String( "Foo bar yoyodyne" ) );
192  check( doc, "substring-before(/book/chapter/para[@id=\"9\" ], ',' )", QLatin1String( "141" ) );
193  } catch ( XPath::XPathExceptionImpl *e ) {
194  qDebug( "Caught XPath exception '%s'.", e->codeAsString().latin1() );
195  delete e;
196  return 1;
197  }
198 
199  qDebug( "All OK!" );
200 }
201 
KApplication
main
int main(int argc, char **argv)
Definition: interpreter_tester.cpp:142
parsedstatement.h
kapplication.h
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QBuffer
QStringList::join
QString join(const QString &separator) const
khtml::XPath::DomNodeList
SharedPtr< DOM::StaticNodeListImpl > DomNodeList
Definition: util.h:41
QList::append
void append(const T &value)
expression.h
kcmdlineargs.h
QString::latin1
const char * latin1() const
KAboutData
QBuffer::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > flags)
QString
QStringList
self
static KJavaAppletServer * self
Definition: kjavaappletserver.cpp:133
QLatin1String
check
void check(DocumentImpl *doc, const QString &statement, const QString &expected)
Definition: interpreter_tester.cpp:47
KCmdLineArgs::init
static void init(int argc, char **argv, const QByteArray &appname, const QByteArray &catalog, const KLocalizedString &programName, const QByteArray &version, const KLocalizedString &description=KLocalizedString(), StdCmdLineArgs stdargs=StdCmdLineArgs(CmdLineArgQt|CmdLineArgKDE))
QIODevice::write
qint64 write(const char *data, qint64 maxSize)
kaboutdata.h
number
QString number(KIO::filesize_t size)
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:26:18 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal