Attica

commentparser.cpp
1 /*
2  This file is part of KDE.
3 
4  SPDX-FileCopyrightText: 2010 Intel Corporation
5  SPDX-FileContributor: Mateu Batle Sastre <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 */
9 
10 #include "commentparser.h"
11 #include "atticautils.h"
12 #include <QDebug>
13 
14 using namespace Attica;
15 
16 Comment Comment::Parser::parseXml(QXmlStreamReader &xml)
17 {
18  Comment comment;
19 
20  while (!xml.atEnd()) {
21  xml.readNext();
22 
23  if (xml.isStartElement()) {
24  if (xml.name() == QLatin1String("id")) {
25  comment.setId(xml.readElementText());
26  } else if (xml.name() == QLatin1String("subject")) {
27  comment.setSubject(xml.readElementText());
28  } else if (xml.name() == QLatin1String("text")) {
29  comment.setText(xml.readElementText());
30  } else if (xml.name() == QLatin1String("childcount")) {
31  comment.setChildCount(xml.readElementText().toInt());
32  } else if (xml.name() == QLatin1String("user")) {
33  comment.setUser(xml.readElementText());
34  } else if (xml.name() == QLatin1String("date")) {
35  comment.setDate(Utils::parseQtDateTimeIso8601(xml.readElementText()));
36  } else if (xml.name() == QLatin1String("score")) {
37  comment.setScore(xml.readElementText().toInt());
38  } else if (xml.name() == QLatin1String("children")) {
39  // This may seem strange, however we are dealing with a situation where we may
40  // receive multiple children subsections (the standard accepts this, and certain
41  // server implementations do do this)
42  QList<Comment> children = comment.children();
43  children += parseXmlChildren(xml);
44  comment.setChildren(children);
45  }
46  } else if (xml.isEndElement() && xml.name() == QLatin1String("comment")) {
47  break;
48  }
49  }
50 
51  return comment;
52 }
53 
54 QList<Comment> Comment::Parser::parseXmlChildren(QXmlStreamReader &xml)
55 {
56  QList<Comment> children;
57 
58  while (!xml.atEnd()) {
59  xml.readNext();
60 
61  if (xml.isStartElement()) {
62  if (xml.name() == QLatin1String("comment")) {
63  Comment comment = parseXml(xml);
64  children.append(comment);
65  }
66  } else if (xml.isEndElement() && xml.name() == QLatin1String("children")) {
67  break;
68  }
69  }
70 
71  return children;
72 }
73 
74 QStringList Comment::Parser::xmlElement() const
75 {
76  return QStringList(QStringLiteral("comment"));
77 }
void append(const T &value)
bool isEndElement() const const
QStringRef name() const const
void setScore(const int score)
This is for internal usage,.
Definition: comment.cpp:132
QXmlStreamReader::TokenType readNext()
int toInt(bool *ok, int base) const const
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
The Attica namespace,.
bool isStartElement() const const
bool atEnd() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Dec 11 2023 04:05:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.