[Pkg-owncloud-commits] [owncloud-client] 102/159: LsColJob: Create a XML parser object for better unit testability.

Sandro Knauß hefee-guest at moszumanska.debian.org
Fri May 1 13:05:31 UTC 2015


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit 4283ab3b44e8801a9707ff24baf2dd7b1d6e09b8
Author: Klaas Freitag <freitag at owncloud.com>
Date:   Tue Apr 14 08:36:17 2015 +0200

    LsColJob: Create a XML parser object for better unit testability.
---
 src/libsync/networkjobs.cpp | 235 ++++++++++++++++++++++++--------------------
 1 file changed, 131 insertions(+), 104 deletions(-)

diff --git a/src/libsync/networkjobs.cpp b/src/libsync/networkjobs.cpp
index e8a571a..18837ea 100644
--- a/src/libsync/networkjobs.cpp
+++ b/src/libsync/networkjobs.cpp
@@ -321,6 +321,124 @@ bool MkColJob::finished()
 }
 
 /*********************************************************************************************/
+// supposed to read <D:collection> when pointing to <D:resourcetype><D:collection></D:resourcetype>..
+static QString readContentsAsString(QXmlStreamReader &reader) {
+    QString result;
+    int level = 0;
+    do {
+        QXmlStreamReader::TokenType type = reader.readNext();
+        if (type == QXmlStreamReader::StartElement) {
+            level++;
+            result += "<" + reader.name().toString() + ">";
+        } else if (type == QXmlStreamReader::Characters) {
+            result += reader.text();
+        } else if (type == QXmlStreamReader::EndElement) {
+            level--;
+            if (level < 0) {
+                break;
+            }
+            result += "</" + reader.name().toString() + ">";
+        }
+
+    } while (!reader.atEnd());
+    return result;
+}
+
+
+LsColXMLParser::LsColXMLParser()
+{
+
+}
+
+bool LsColXMLParser::parse( const QByteArray& xml, QHash<QString, qint64> *sizes)
+{
+    // Parse DAV response
+    QXmlStreamReader reader(xml);
+    reader.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("d", "DAV:"));
+
+    QStringList folders;
+    QString currentHref;
+    QMap<QString, QString> currentTmpProperties;
+    QMap<QString, QString> currentHttp200Properties;
+    bool currentPropsHaveHttp200 = false;
+    bool insidePropstat = false;
+    bool insideProp = false;
+
+    while (!reader.atEnd()) {
+        QXmlStreamReader::TokenType type = reader.readNext();
+        QString name = reader.name().toString();
+        // Start elements with DAV:
+        if (type == QXmlStreamReader::StartElement && reader.namespaceUri() == QLatin1String("DAV:")) {
+            if (name == QLatin1String("href")) {
+                currentHref = QUrl::fromPercentEncoding(reader.readElementText().toUtf8());
+            } else if (name == QLatin1String("response")) {
+            } else if (name == QLatin1String("propstat")) {
+                insidePropstat = true;
+            } else if (name == QLatin1String("status") && insidePropstat) {
+                QString httpStatus = reader.readElementText();
+                if (httpStatus.startsWith("HTTP/1.1 200")) {
+                    currentPropsHaveHttp200 = true;
+                } else {
+                    currentPropsHaveHttp200 = false;
+                }
+            } else if (name == QLatin1String("prop")) {
+                insideProp = true;
+                continue;
+            }
+        }
+
+        if (type == QXmlStreamReader::StartElement && insidePropstat && insideProp) {
+            // All those elements are properties
+            QString propertyContent = readContentsAsString(reader);
+            if (name == QLatin1String("resourcetype") && propertyContent.contains("collection")) {
+                folders.append(currentHref);
+            } else if (name == QLatin1String("quota-used-bytes")) {
+                bool ok = false;
+                auto s = propertyContent.toLongLong(&ok);
+                if (ok && sizes) {
+                    sizes->insert(currentHref, s);
+                }
+            }
+            currentTmpProperties.insert(reader.name().toString(), propertyContent);
+        }
+
+        // End elements with DAV:
+        if (type == QXmlStreamReader::EndElement) {
+            if (reader.namespaceUri() == QLatin1String("DAV:")) {
+                if (reader.name() == "response") {
+                    if (currentHref.endsWith('/')) {
+                        currentHref.chop(1);
+                    }
+                    emit directoryListingIterated(currentHref, currentHttp200Properties);
+                    currentHref.clear();
+                    currentHttp200Properties.clear();
+                } else if (reader.name() == "propstat") {
+                    insidePropstat = false;
+                    if (currentPropsHaveHttp200) {
+                        currentHttp200Properties = QMap<QString,QString>(currentTmpProperties);
+                    }
+                    currentTmpProperties.clear();
+                    currentPropsHaveHttp200 = false;
+                } else if (reader.name() == "prop") {
+                    insideProp = false;
+                }
+            }
+        }
+    }
+
+    if (reader.hasError()) {
+        // XML Parser error? Whatever had been emitted before will come as directoryListingIterated
+        qDebug() << "ERROR" << reader.errorString();
+        return false;
+    } else {
+        emit directoryListingSubfolders(folders);
+        emit finishedWithoutError();
+    }
+    return true;
+
+}
+
+/*********************************************************************************************/
 
 LsColJob::LsColJob(AccountPtr account, const QString &path, QObject *parent)
     : AbstractNetworkJob(account, path, parent)
@@ -377,29 +495,6 @@ void LsColJob::start()
     AbstractNetworkJob::start();
 }
 
-// supposed to read <D:collection> when pointing to <D:resourcetype><D:collection></D:resourcetype>..
-static QString readContentsAsString(QXmlStreamReader &reader) {
-    QString result;
-    int level = 0;
-    do {
-        QXmlStreamReader::TokenType type = reader.readNext();
-        if (type == QXmlStreamReader::StartElement) {
-            level++;
-            result += "<" + reader.name().toString() + ">";
-        } else if (type == QXmlStreamReader::Characters) {
-            result += reader.text();
-        } else if (type == QXmlStreamReader::EndElement) {
-            level--;
-            if (level < 0) {
-                break;
-            }
-            result += "</" + reader.name().toString() + ">";
-        }
-
-    } while (!reader.atEnd());
-    return result;
-}
-
 // TODO: Instead of doing all in this slot, we should iteratively parse in readyRead(). This
 // would allow us to be more asynchronous in processing while data is coming from the network,
 // not in all in one big blobb at the end.
@@ -408,88 +503,19 @@ bool LsColJob::finished()
     QString contentType = reply()->header(QNetworkRequest::ContentTypeHeader).toString();
     int httpCode = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
     if (httpCode == 207 && contentType.contains("application/xml; charset=utf-8")) {
-        // Parse DAV response
-        QByteArray xml = reply()->readAll();
-        QXmlStreamReader reader(xml);
-        reader.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("d", "DAV:"));
-
-        QStringList folders;
-        QString currentHref;
-        QMap<QString, QString> currentTmpProperties;
-        QMap<QString, QString> currentHttp200Properties;
-        bool currentPropsHaveHttp200 = false;
-        bool insidePropstat = false;
-        bool insideProp = false;
-
-        while (!reader.atEnd()) {
-            QXmlStreamReader::TokenType type = reader.readNext();
-            QString name = reader.name().toString();
-            // Start elements with DAV:
-            if (type == QXmlStreamReader::StartElement && reader.namespaceUri() == QLatin1String("DAV:")) {
-                if (name == QLatin1String("href")) {
-                    currentHref = QUrl::fromPercentEncoding(reader.readElementText().toUtf8());
-                } else if (name == QLatin1String("response")) {
-                } else if (name == QLatin1String("propstat")) {
-                    insidePropstat = true;
-                } else if (name == QLatin1String("status") && insidePropstat) {
-                    QString httpStatus = reader.readElementText();
-                    if (httpStatus.startsWith("HTTP/1.1 200")) {
-                        currentPropsHaveHttp200 = true;
-                    } else {
-                        currentPropsHaveHttp200 = false;
-                    }
-                } else if (name == QLatin1String("prop")) {
-                    insideProp = true;
-                    continue;
-                }
-            }
-
-            if (type == QXmlStreamReader::StartElement && insidePropstat && insideProp) {
-                // All those elements are properties
-                QString propertyContent = readContentsAsString(reader);
-                if (name == QLatin1String("resourcetype") && propertyContent.contains("collection")) {
-                    folders.append(currentHref);
-                } else if (name == QLatin1String("quota-used-bytes")) {
-                    bool ok = false;
-                    auto s = propertyContent.toLongLong(&ok);
-                    if (ok) {
-                        _sizes[currentHref] = s;
-                    }
-                }
-                currentTmpProperties.insert(reader.name().toString(), propertyContent);
-            }
-
-            // End elements with DAV:
-            if (type == QXmlStreamReader::EndElement) {
-                if (reader.namespaceUri() == QLatin1String("DAV:")) {
-                    if (reader.name() == "response") {
-                        if (currentHref.endsWith('/')) {
-                            currentHref.chop(1);
-                        }
-                        emit directoryListingIterated(currentHref, currentHttp200Properties);
-                        currentHref.clear();                        
-                        currentHttp200Properties.clear();
-                    } else if (reader.name() == "propstat") {
-                        insidePropstat = false;
-                        if (currentPropsHaveHttp200) {
-                            currentHttp200Properties = QMap<QString,QString>(currentTmpProperties);
-                        }
-                        currentTmpProperties.clear();
-                        currentPropsHaveHttp200 = false;
-                    } else if (reader.name() == "prop") {
-                        insideProp = false;
-                    }
-                }
-            }
-        }
-
-        if (reader.hasError()) {
-            // XML Parser error? Whatever had been emitted before will come as directoryListingIterated
-            qDebug() << "ERROR" << reader.errorString();
+        LsColXMLParser parser;
+        connect( &parser, SIGNAL(directoryListingSubfolders(const QStringList&)),
+                 this, SIGNAL(directoryListingSubfolders(const QStringList&)) );
+        connect( &parser, SIGNAL(directoryListingIterated(const QString&, const QMap<QString,QString>&)),
+                 this, SIGNAL(directoryListingIterated(const QString&, const QMap<QString,QString>&)) );
+        connect( &parser, SIGNAL(finishedWithError(QNetworkReply *)),
+                 this, SIGNAL(finishedWithError(QNetworkReply *)) );
+        connect( &parser, SIGNAL(finishedWithoutError()),
+                 this, SIGNAL(finishedWithoutError()) );
+
+        if( !parser.parse( reply()->readAll(), &_sizes ) ) {
+            // XML parse error
             emit finishedWithError(reply());
-        } else {
-            emit directoryListingSubfolders(folders);
-            emit finishedWithoutError();
         }
     } else if (httpCode == 207) {
         // wrong content type
@@ -498,6 +524,7 @@ bool LsColJob::finished()
         // wrong HTTP code or any other network error
         emit finishedWithError(reply());
     }
+
     return true;
 }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list