Commit 0dcd3150 authored by Jeremy Whiting's avatar Jeremy Whiting
Browse files

Also port WordList away from QXmlSimpleReader.

parent 9f97092c
......@@ -37,86 +37,37 @@ namespace WordList
{
void addWords(WordMap &map, const QString &line);
XMLParser::XMLParser()
XMLReader::XMLReader()
{
}
XMLParser::~XMLParser()
XMLReader::~XMLReader()
{
}
bool XMLParser::warning(const QXmlParseException &)
bool XMLReader::read(QIODevice *device)
{
return false;
}
bool XMLParser::error(const QXmlParseException &)
{
return false;
}
bool XMLParser::fatalError(const QXmlParseException &)
{
return false;
}
QString XMLParser::errorString() const
{
return QLatin1String("");
}
bool XMLParser::startDocument()
{
text.clear();
return true;
}
bool XMLParser::startElement(const QString &, const QString &,
const QString &,
const QXmlAttributes &)
{
if (!text.isNull() && !text.isEmpty()) {
addWords(list, text);
text.clear();
}
return true;
}
xml.setDevice(device);
bool XMLParser::characters(const QString &ch)
{
text += ch;
return true;
}
list.clear();
bool XMLParser::ignorableWhitespace(const QString &)
{
if (!text.isNull() && !text.isEmpty()) {
addWords(list, text);
text.clear();
while (xml.readNextStartElement()) {
const QString *word = xml.name().string();
addWords(list, *word);
}
return true;
}
bool XMLParser::endElement(const QString &, const QString &,
const QString &)
{
if (!text.isNull() && !text.isEmpty()) {
addWords(list, text);
text.clear();
}
return true;
return !xml.error();
}
bool XMLParser::endDocument()
QString XMLReader::errorString() const
{
if (!text.isNull() && !text.isEmpty()) {
addWords(list, text);
text.clear();
}
return true;
return QLatin1String("%1\nLine %2, column %3")
.arg(xml.errorString())
.arg(xml.lineNumber())
.arg(xml.columnNumber());
}
WordMap XMLParser::getList()
WordMap XMLReader::getList()
{
return list;
}
......@@ -180,15 +131,11 @@ void addWords(WordMap &map, const WordMap &add)
void addWordsFromFile(WordMap &map, const QString &filename, QTextCodec *codec)
{
QFile xmlfile(filename);
QXmlInputSource source(&xmlfile);
XMLParser parser;
QXmlSimpleReader reader;
reader.setFeature(QStringLiteral("http://qt-project.org/xml/features/report-start-end-entity"), true);
reader.setContentHandler(&parser);
XMLReader reader;
WordMap words;
if (reader.parse(source)) // try to load the file as an xml-file
addWords(map, parser.getList());
if (reader.read(&xmlfile)) // try to load the file as an xml-file
addWords(map, reader.getList());
else {
QFile wpdfile(filename);
if (wpdfile.open(QIODevice::ReadOnly)) {
......
......@@ -22,9 +22,7 @@
#define WORDLIST_H
#include <QMap>
#include <QXmlAttributes>
#include <QXmlDefaultHandler>
#include <QXmlParseException>
#include <QXmlStreamReader>
class QTextCodec;
class QProgressDialog;
......@@ -47,39 +45,18 @@ bool saveWordList(const WordMap &map, const QString &filename);
/**
* This class implements a parser for reading docbooks and generating word
* lists. It is intended to be used together with the Qt SAX2 framework.
* @author Gunnar Schmi Dt
* lists.
*/
class XMLParser : public QXmlDefaultHandler
class XMLReader
{
public:
XMLParser();
~XMLParser() override;
XMLReader();
~XMLReader();
bool warning(const QXmlParseException &exception) override;
bool error(const QXmlParseException &exception) override;
bool fatalError(const QXmlParseException &exception) override;
QString errorString() const override;
bool read(QIODevice *device);
/** Processes the start of the document. */
bool startDocument() override;
/** Processes the start tag of an element. */
bool startElement(const QString &, const QString &, const QString &name,
const QXmlAttributes &attributes) override;
/** Processes a chunk of normal character data. */
bool characters(const QString &ch) override;
/** Processes whitespace. */
bool ignorableWhitespace(const QString &ch) override;
/** Processes the end tag of an element. */
bool endElement(const QString &, const QString &, const QString &name) override;
/** Processes the end of the document. */
bool endDocument() override;
QString errorString() const;
/** returns a list of words */
WordMap getList();
......@@ -87,6 +64,8 @@ public:
private:
WordMap list;
QString text;
QXmlStreamReader xml;
};
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment