[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [pimcommon] src/pimcommon/autocorrection: Port to QXmlStreamReader
From:       Montel Laurent <null () kde ! org>
Date:       2017-07-31 20:17:07
Message-ID: E1dcH7j-0003de-TM () code ! kde ! org
[Download RAW message or body]

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/importkmailautocorrection.cpp

https://commits.kde.org/pimcommon/55145bcd69797721b9b3c05623ab8eed99d25732

diff --git a/src/pimcommon/autocorrection/autocorrection.cpp \
b/src/pimcommon/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 <QTextBlock>
 #include <QTextDocument>
-#include <QDomDocument>
 #include <QFile>
 #include <QDir>
 #include <QStandardPaths>
@@ -1176,10 +1175,10 @@ void AutoCorrection::writeAutoCorrectionXmlFile(const QString \
&filename)  QXmlStreamWriter streamWriter(&file);
 
     streamWriter.setAutoFormatting(true);
-    streamWriter.setAutoFormattingIndent(2);
+    streamWriter.setAutoFormattingIndent(1);
     streamWriter.writeStartDocument();
 
-    streamWriter.writeStartElement(QStringLiteral("autocorrection"));
+    streamWriter.writeDTD(QStringLiteral("<!DOCTYPE autocorrection>"));
 
     streamWriter.writeStartElement(QStringLiteral("Word"));
 
@@ -1203,83 +1202,34 @@ void AutoCorrection::writeAutoCorrectionXmlFile(const 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=\"1.0\" encoding=\"UTF-8\"")));
-
-    QDomElement word = root.createElement(QStringLiteral("Word"));
-    root.appendChild(word);
-    QDomElement items = root.createElement(QStringLiteral("items"));
 
-    QHashIterator<QString, QString> i(mAutocorrectEntries);
-    while (i.hasNext()) {
-        i.next();
-        QDomElement item = root.createElement(QStringLiteral("item"));
-        item.setAttribute(QStringLiteral("find"), i.key());
-        item.setAttribute(QStringLiteral("replace"), i.value());
-        items.appendChild(item);
-    }
-    word.appendChild(items);
-
-    QDomElement upperCaseExceptions = \
                root.createElement(QStringLiteral("UpperCaseExceptions"));
-    QSet<QString>::const_iterator upper = mUpperCaseExceptions.constBegin();
-    while (upper != mUpperCaseExceptions.constEnd()) {
-        QDomElement item = root.createElement(QStringLiteral("word"));
-        item.setAttribute(QStringLiteral("exception"), *upper);
-        upperCaseExceptions.appendChild(item);
-        ++upper;
-    }
-    word.appendChild(upperCaseExceptions);
-
-    QDomElement twoUpperLetterExceptions = \
root.createElement(QStringLiteral("TwoUpperLetterExceptions")); +    \
                streamWriter.writeStartElement(QStringLiteral("TwoUpperLetterExceptions"));
                
     QSet<QString>::const_iterator twoUpper = mTwoUpperLetterExceptions.constBegin();
     while (twoUpper != mTwoUpperLetterExceptions.constEnd()) {
-        QDomElement item = 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 = root.createElement(QStringLiteral( "SuperScript" ));
-    QHashIterator<QString, QString> j(mSuperScriptEntries);
-    while (j.hasNext()) {
-        j.next();
-        QDomElement item = root.createElement(QStringLiteral( "superscript" ));
-        item.setAttribute(QStringLiteral("find"), j.key());
-        item.setAttribute(QStringLiteral("super"), j.value());
-        supperscript.appendChild(item);
-    }
-    word.appendChild(supperscript);
-    */
-
-    QDomElement doubleQuote = root.createElement(QStringLiteral("DoubleQuote"));
-    QDomElement item = root.createElement(QStringLiteral("doublequote"));
-    item.setAttribute(QStringLiteral("begin"), mTypographicDoubleQuotes.begin);
-    item.setAttribute(QStringLiteral("end"), mTypographicDoubleQuotes.end);
-    doubleQuote.appendChild(item);
-    word.appendChild(doubleQuote);
-
-    QDomElement singleQuote = root.createElement(QStringLiteral("SimpleQuote"));
-    item = root.createElement(QStringLiteral("simplequote"));
-    item.setAttribute(QStringLiteral("begin"), mTypographicSingleQuotes.begin);
-    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"), \
mTypographicDoubleQuotes.begin); +    \
streamWriter.writeAttribute(QStringLiteral("end"), mTypographicDoubleQuotes.end); +   \
streamWriter.writeEndElement(); +    streamWriter.writeEndElement();
+
+    streamWriter.writeStartElement(QStringLiteral("SimpleQuote"));
+    streamWriter.writeStartElement(QStringLiteral("simplequote"));
+    streamWriter.writeAttribute(QStringLiteral("begin"), \
mTypographicSingleQuotes.begin); +    \
streamWriter.writeAttribute(QStringLiteral("end"), mTypographicSingleQuotes.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 = QLatin1String(AUTOCORRECTION_DATA_DIR) + \
                QLatin1Char('/') + filename + QStringLiteral("-ref.xml");
     const QString generatedFile = 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 = 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 <QFile>
 #include <QDomDocument>
+#include <QXmlStreamReader>
 #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 &fileName, \
LoadAttribute lo  if (!xmlFile.open(QIODevice::ReadOnly)) {
         return false;
     }
-
+#ifdef USE_XMLSTREAMREADER
+    QXmlStreamReader xml(&xmlFile);
+    if (xml.readNextStartElement()) {
+        while (xml.readNextStartElement()) {
+            qDebug() << "xml.name() "<<xml.name();
+            if (xml.name() == QLatin1String("UpperCaseExceptions")) {
+                if (loadAttribute == All) {
+                    while (xml.readNextStartElement()) {
+                        const QStringRef tagname = xml.name();
+                        if (tagname == QLatin1String("word")) {
+                            if \
(xml.attributes().hasAttribute(QStringLiteral("exception"))) { +                      \
const QString exception = \
xml.attributes().value(QStringLiteral("exception")).toString(); +                     \
mUpperCaseExceptions += exception; +                                \
xml.skipCurrentElement(); +                            }
+                        }
+                    }
+                } else {
+                    xml.skipCurrentElement();
+                }
+            } else if(xml.name() == QLatin1String("TwoUpperLetterExceptions")) {
+                if (loadAttribute == All) {
+                    while (xml.readNextStartElement()) {
+                        const QStringRef tagname = xml.name();
+                        if (tagname == QLatin1String("word")) {
+                            if \
(xml.attributes().hasAttribute(QStringLiteral("exception"))) { +                      \
const QString exception = \
xml.attributes().value(QStringLiteral("exception")).toString(); +                     \
mTwoUpperLetterExceptions += exception; +                                \
xml.skipCurrentElement(); +                            }
+                        } else {
+                            xml.skipCurrentElement();
+                        }
+                    }
+                } else {
+                    xml.skipCurrentElement();
+                }
+            } else if(xml.name() == QLatin1String("DoubleQuote")) {
+                if (loadAttribute == All) {
+                } else {
+                    xml.skipCurrentElement();
+                }
+            } else if(xml.name() == QLatin1String("SimpleQuote")) {
+                if (loadAttribute == All) {
+                } else {
+                    xml.skipCurrentElement();
+                }
+            } else if(xml.name() == QLatin1String("SuperScript")) {
+                if (loadAttribute == All || loadAttribute == SuperScript) {
+                } else {
+                    xml.skipCurrentElement();
+                }
+            } else {
+                //TODO verify
+                xml.skipCurrentElement();
+            }
+        }
+    }
+#else
     QDomDocument doc;
     if (!doc.setContent(&xmlFile)) {
         return false;
@@ -120,7 +180,7 @@ bool ImportKMailAutocorrection::import(const QString &fileName, \
LoadAttribute lo  }
         }
     }
-
+#endif
     return true;
 }
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic