Git commit 55145bcd69797721b9b3c05623ab8eed99d25732 by Montel Laurent. Committed on 31/07/2017 at 20:16. Pushed by mlaurent into branch 'master'. Port to QXmlStreamReader M +23 -73 src/pimcommon/autocorrection/autocorrection.cpp M +5 -0 src/pimcommon/autocorrection/autotests/autocorrectiontest.cpp M +62 -2 src/pimcommon/autocorrection/import/importkmailautocorrectio= n.cpp https://commits.kde.org/pimcommon/55145bcd69797721b9b3c05623ab8eed99d25732 diff --git a/src/pimcommon/autocorrection/autocorrection.cpp b/src/pimcommo= n/autocorrection/autocorrection.cpp index b546e6a..9b70922 100644 --- a/src/pimcommon/autocorrection/autocorrection.cpp +++ b/src/pimcommon/autocorrection/autocorrection.cpp @@ -25,7 +25,6 @@ #include "pimcommon_debug.h" #include #include -#include #include #include #include @@ -1176,10 +1175,10 @@ void AutoCorrection::writeAutoCorrectionXmlFile(con= st QString &filename) QXmlStreamWriter streamWriter(&file); = streamWriter.setAutoFormatting(true); - streamWriter.setAutoFormattingIndent(2); + streamWriter.setAutoFormattingIndent(1); streamWriter.writeStartDocument(); = - streamWriter.writeStartElement(QStringLiteral("autocorrection")); + streamWriter.writeDTD(QStringLiteral("")); = streamWriter.writeStartElement(QStringLiteral("Word")); = @@ -1203,83 +1202,34 @@ void AutoCorrection::writeAutoCorrectionXmlFile(con= st QString &filename) ++upper; streamWriter.writeEndElement(); } - streamWriter.writeEndElement(); = - streamWriter.writeEndDocument(); - -#if 0 - //TODO port to QXmlStreamWriter + add Autotests. - QDomDocument root(QStringLiteral("autocorrection")); - root.appendChild(root.createProcessingInstruction(QStringLiteral("xml"= ), QStringLiteral("version=3D\"1.0\" encoding=3D\"UTF-8\""))); - - QDomElement word =3D root.createElement(QStringLiteral("Word")); - root.appendChild(word); - QDomElement items =3D root.createElement(QStringLiteral("items")); = - QHashIterator i(mAutocorrectEntries); - while (i.hasNext()) { - i.next(); - QDomElement item =3D root.createElement(QStringLiteral("item")); - item.setAttribute(QStringLiteral("find"), i.key()); - item.setAttribute(QStringLiteral("replace"), i.value()); - items.appendChild(item); - } - word.appendChild(items); - - QDomElement upperCaseExceptions =3D root.createElement(QStringLiteral(= "UpperCaseExceptions")); - QSet::const_iterator upper =3D mUpperCaseExceptions.constBegi= n(); - while (upper !=3D mUpperCaseExceptions.constEnd()) { - QDomElement item =3D root.createElement(QStringLiteral("word")); - item.setAttribute(QStringLiteral("exception"), *upper); - upperCaseExceptions.appendChild(item); - ++upper; - } - word.appendChild(upperCaseExceptions); - - QDomElement twoUpperLetterExceptions =3D root.createElement(QStringLit= eral("TwoUpperLetterExceptions")); + streamWriter.writeStartElement(QStringLiteral("TwoUpperLetterException= s")); QSet::const_iterator twoUpper =3D mTwoUpperLetterExceptions.c= onstBegin(); while (twoUpper !=3D mTwoUpperLetterExceptions.constEnd()) { - QDomElement item =3D root.createElement(QStringLiteral("word")); - item.setAttribute(QStringLiteral("exception"), *twoUpper); - twoUpperLetterExceptions.appendChild(item); + streamWriter.writeStartElement(QStringLiteral("word")); + streamWriter.writeAttribute(QStringLiteral("exception"), *twoUpper= ); ++twoUpper; + streamWriter.writeEndElement(); } - word.appendChild(twoUpperLetterExceptions); - - //Don't save it as discussed with Calligra dev - /* - QDomElement supperscript =3D root.createElement(QStringLiteral( "Super= Script" )); - QHashIterator j(mSuperScriptEntries); - while (j.hasNext()) { - j.next(); - QDomElement item =3D root.createElement(QStringLiteral( "superscri= pt" )); - item.setAttribute(QStringLiteral("find"), j.key()); - item.setAttribute(QStringLiteral("super"), j.value()); - supperscript.appendChild(item); - } - word.appendChild(supperscript); - */ - - QDomElement doubleQuote =3D root.createElement(QStringLiteral("DoubleQ= uote")); - QDomElement item =3D root.createElement(QStringLiteral("doublequote")); - item.setAttribute(QStringLiteral("begin"), mTypographicDoubleQuotes.be= gin); - item.setAttribute(QStringLiteral("end"), mTypographicDoubleQuotes.end); - doubleQuote.appendChild(item); - word.appendChild(doubleQuote); - - QDomElement singleQuote =3D root.createElement(QStringLiteral("SimpleQ= uote")); - item =3D root.createElement(QStringLiteral("simplequote")); - item.setAttribute(QStringLiteral("begin"), mTypographicSingleQuotes.be= gin); - item.setAttribute(QStringLiteral("end"), mTypographicSingleQuotes.end); - singleQuote.appendChild(item); - word.appendChild(singleQuote); - - QTextStream ts(&file); - ts.setCodec("UTF-8"); - ts << root.toString(); - file.close(); -#endif + streamWriter.writeEndElement(); + + streamWriter.writeStartElement(QStringLiteral("DoubleQuote")); + streamWriter.writeStartElement(QStringLiteral("doublequote")); + streamWriter.writeAttribute(QStringLiteral("begin"), mTypographicDoubl= eQuotes.begin); + streamWriter.writeAttribute(QStringLiteral("end"), mTypographicDoubleQ= uotes.end); + streamWriter.writeEndElement(); + streamWriter.writeEndElement(); + + streamWriter.writeStartElement(QStringLiteral("SimpleQuote")); + streamWriter.writeStartElement(QStringLiteral("simplequote")); + streamWriter.writeAttribute(QStringLiteral("begin"), mTypographicSingl= eQuotes.begin); + streamWriter.writeAttribute(QStringLiteral("end"), mTypographicSingleQ= uotes.end); + streamWriter.writeEndElement(); + streamWriter.writeEndElement(); + + streamWriter.writeEndDocument(); } = QString AutoCorrection::language() const diff --git a/src/pimcommon/autocorrection/autotests/autocorrectiontest.cpp = b/src/pimcommon/autocorrection/autotests/autocorrectiontest.cpp index bed7d44..eaf5f72 100644 --- a/src/pimcommon/autocorrection/autotests/autocorrectiontest.cpp +++ b/src/pimcommon/autocorrection/autotests/autocorrectiontest.cpp @@ -695,9 +695,14 @@ void AutoCorrectionTest::shouldLoadSaveAutocorrection() const QString refFile =3D QLatin1String(AUTOCORRECTION_DATA_DIR) + QLa= tin1Char('/') + filename + QStringLiteral("-ref.xml"); const QString generatedFile =3D QLatin1String(AUTOCORRECTION_DATA_DIR)= + QLatin1Char('/') + filename + QStringLiteral("-generated.xml"); = + //First autocorrection.loadGlobalFileName(originalFile, true); autocorrection.writeAutoCorrectionXmlFile(generatedFile); = + //Second + autocorrection.loadGlobalFileName(generatedFile, true); + autocorrection.writeAutoCorrectionXmlFile(generatedFile); + QStringList args =3D QStringList() << QStringLiteral("-u") << refFile diff --git a/src/pimcommon/autocorrection/import/importkmailautocorrection.= cpp b/src/pimcommon/autocorrection/import/importkmailautocorrection.cpp index c24b045..4ca4aac 100644 --- a/src/pimcommon/autocorrection/import/importkmailautocorrection.cpp +++ b/src/pimcommon/autocorrection/import/importkmailautocorrection.cpp @@ -19,8 +19,10 @@ = #include #include +#include #include "pimcommon_debug.h" using namespace PimCommon; +//#define USE_XMLSTREAMREADER 1 = ImportKMailAutocorrection::ImportKMailAutocorrection(QWidget *parent) : ImportAbstractAutocorrection(parent) @@ -38,7 +40,65 @@ bool ImportKMailAutocorrection::import(const QString &fi= leName, LoadAttribute lo if (!xmlFile.open(QIODevice::ReadOnly)) { return false; } - +#ifdef USE_XMLSTREAMREADER + QXmlStreamReader xml(&xmlFile); + if (xml.readNextStartElement()) { + while (xml.readNextStartElement()) { + qDebug() << "xml.name() "<