File indexing completed on 2024-03-24 04:03:44

0001 /**
0002  * backgroundtest.cpp
0003  *
0004  * SPDX-FileCopyrightText: 2004 Zack Rusin <zack@kde.org>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-or-later
0007  */
0008 #include "backgroundtest.h"
0009 
0010 #include "speller.h"
0011 using namespace Sonnet;
0012 
0013 #include <QApplication>
0014 #include <QDebug>
0015 
0016 const char *text =
0017     "Rationale \
0018 ========= \
0019  \
0020 This code is intended to provide an implementation of the W3C's XPath \
0021 specification for KHTML. XPath isn't particularly useful on its own, however\
0022 it is an essential building block for the implementation of other standards \
0023 like XSLT and XQuery. XPath is supported to a greater or lesser extent by both\
0024 IE and Mozilla so it is likely to become increasingly important over the next\
0025 few years.\
0026  \
0027 Why write another XPath implementation? \
0028 ======================================= \
0029  \
0030 The are already a number of XPath implementations available under free \
0031 licenses including Mozilla's, libxml2, Xerces and probably others, so it is \
0032 reasonable to ask why there should be another one. \
0033  \
0034 It would certainly be possible to integrate one of these implementations into\
0035 KHTML, but it would actually be quite a lot of work. I looked at all of the\
0036 implementations mentioned with a view to using this approach before I decided\
0037 to start from scratch.\
0038  \
0039 Mozilla XPath\
0040 -------------\
0041  \
0042 Seems to be incomplete, and though the code was originally standalone it now\
0043 seems to be tied to the mozilla codebase quite tightly. This makes porting it\
0044 a lot of work, and makes working with the mozilla team on fixing bugs\
0045 etc. hard as the code bases would have diverged quite a lot.\
0046  \
0047 Xerces XPath (C++ version)\
0048 --------------------------\
0049  \
0050 The Xerces code seemed pretty clean and was reasonably understandable, however\
0051 it doesn't seem to be used very much which greatly reduces the utility. As\
0052 with the mozilla code, porting it to use KHTML's DOM would take a fair bit of \
0053 work. The main issue here being that Xerces is based around pointers to Nodes\
0054 rather than implicitly shared Node objects.\
0055  \
0056 libxml2 \
0057 ------- \
0058  \
0059 This is the most obvious library to reuse as it is currently used to generate\
0060 the KDE documentation, and is also a very complete and fast\
0061 implementation. The down side of using this code is that it would either need\
0062 "
0063     "a new DOM implementation in KHTML (which used the libxml2 structures), a \
0064 wrapper library that made on of the DOM trees support the API of the other, or\
0065 binding layer that parsed the XML twice and somehow maintained a mapping\
0066 between the two DOM trees. Unfortunately the documentation of this library is\
0067 less than great, which would add to the problems.\
0068  \
0069 The C++ wrappers to libxml2 are considerably closer to what I was looking\
0070 for. They are well documented and have a well structured API. Unfortunately\
0071 using this library still requires some mechanism to integrate the two\
0072 underlying DOM implementations.\
0073  \
0074 KHTML XPath\
0075 ----------- \
0076  \
0077 There are some advantages to the XPath implementation Zack and I are working\
0078 on, namely: \
0079  \
0080 - Ease of integration with the rest of kjs/khtml.\
0081 - Use of dom2 traversal (which will also be available to use directly).\
0082 - C++ rather than C and a wrapper (reducing the overheads).\
0083 - The code is clean and uses familiar types and idioms. \
0084  \
0085 We intend the code to be build on top of the DOM api rather than tying it\
0086 directly to the Qt or KHTML XML implementations. This will allow us to take\
0087 advantage of any improvements that might be made to the underlying parser\
0088 etc. The DOM2 traversal APIs provide a set of classes that map almost directly \
0089 to the XPath location steps, since we need to implement these facilities\
0090 anyway writing the relatively small amount of code needed to support XPath\
0091 seems sensible.\
0092  \
0093 Building \
0094 ========\
0095  \
0096 This code needs to live in a subdir off the khtml directory in kdelibs. The\
0097 subdir should be called 'xpath'. The easiest way to regenerate the makefiles\
0098 is to go to the root of the kdelibs tree and run: \
0099   create_makefiles khtml/xpath\
0100  \
0101 This code is intended to compile, but not to work.\
0102  \
0103 Usage \
0104 ===== \
0105  \
0106 ./test_xpath simple.xml\
0107 ./test_values\
0108 ./test_functions \
0109  \
0110 Notes\
0111 ===== \
0112  \
0113 apidoc                  Duh! It's the docs \
0114 working                 Stuff that I'm mining for ideas\
0115 \
0116 Discussion\
0117 ========== \
0118  \
0119 If you want to talk about this code feel free to mail us.";
0120 
0121 BackgroundTest::BackgroundTest()
0122     : QObject(0)
0123     , m_speller("en")
0124 {
0125     m_checker = new BackgroundChecker(m_speller, this);
0126     connect(m_checker, SIGNAL(done()), SLOT(slotDone()));
0127     connect(m_checker, SIGNAL(misspelling(QString, int)), SLOT(slotMisspelling(QString, int)));
0128     m_len = strlen(text);
0129     m_checker->setText(text);
0130     m_checker->speller().setLanguage("en");
0131     m_timer.start();
0132 }
0133 
0134 void BackgroundTest::slotDone()
0135 {
0136     qDebug() << "Text of length" << m_len << "checked in" << m_timer.elapsed() << "msec.";
0137     QApplication::exit();
0138 }
0139 
0140 void BackgroundTest::slotMisspelling(const QString &word, int start)
0141 {
0142     qDebug() << "Misspelling" << word << "at" << start;
0143     m_checker->continueChecking();
0144 }
0145 
0146 int main(int argc, char **argv)
0147 {
0148     QApplication app(argc, argv);
0149 
0150     BackgroundTest test;
0151 
0152     return app.exec();
0153 }
0154 
0155 #include "moc_backgroundtest.cpp"