File: rssfeed.cpp

package info (click to toggle)
grantlee5 5.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,004 kB
  • sloc: cpp: 25,617; javascript: 6,043; python: 299; sh: 97; perl: 37; ruby: 24; makefile: 20
file content (224 lines) | stat: -rw-r--r-- 6,902 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
  This file is part of the Grantlee template system.

  Copyright (c) 2011 Stephen Kelly <steveire@gmail.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either version
  2.1 of the Licence, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "rssfeed.h"

#include <grantlee_templates.h>

#include <QAbstractXmlNodeModel>
#include <QXmlNodeModelIndex>
#include <QXmlQuery>
#include <QXmlResultItems>
#include <QtCore/QBuffer>
#include <QtCore/QDebug>
#include <QtCore/QEventLoop>
#include <QtCore/QFile>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>

Q_DECLARE_METATYPE(QXmlQuery)

RssFeedNodeFactory::RssFeedNodeFactory(QObject *parent)
    : Grantlee::AbstractNodeFactory(parent)
{
}

Grantlee::Node *RssFeedNodeFactory::getNode(const QString &tagContent,
                                            Grantlee::Parser *p) const
{
  QStringList expr = smartSplit(tagContent);
  Grantlee::FilterExpression url(expr.at(1), p);
  Grantlee::FilterExpression query(expr.at(2), p);

  RssFeedNode *n = new RssFeedNode(url, query);

  QList<Grantlee::Node *> nodes = p->parse(n, "endrssfeed");
  p->takeNextToken();

  n->setChildNodes(nodes);

  return n;
}

RssFeedNode::RssFeedNode(const Grantlee::FilterExpression &url,
                         const Grantlee::FilterExpression &query,
                         QObject *parent)
    : Grantlee::Node(parent), m_url(url), m_query(query)
{
}

void RssFeedNode::setChildNodes(QList<Grantlee::Node *> childNodes)
{
  m_childNodes = childNodes;
}

void RssFeedNode::render(Grantlee::OutputStream *stream,
                         Grantlee::Context *c) const
{
  QNetworkAccessManager *mgr
      = new QNetworkAccessManager(const_cast<RssFeedNode *>(this));
  QUrl url(Grantlee::getSafeString(m_url.resolve(c)));
  QNetworkReply *reply = mgr->get(QNetworkRequest(url));
  QEventLoop eLoop;
  connect(mgr, SIGNAL(finished(QNetworkReply *)), &eLoop, SLOT(quit()));
  eLoop.exec(QEventLoop::ExcludeUserInputEvents);

  c->push();
  foreach (Grantlee::Node *n, m_childNodes) {
    if (!n->inherits(XmlNamespaceNode::staticMetaObject.className()))
      continue;
    Grantlee::OutputStream _dummy;
    n->render(&_dummy, c);
  }

  QXmlQuery query;
  QByteArray ba = reply->readAll();

  QBuffer buffer;
  buffer.setData(ba);
  buffer.open(QIODevice::ReadOnly);
  query.bindVariable("inputDocument", &buffer);
  QString ns;
  QHash<QString, QVariant> h = c->lookup("_ns").toHash();
  QHash<QString, QVariant>::const_iterator it = h.constBegin();
  const QHash<QString, QVariant>::const_iterator end = h.constEnd();
  for (; it != end; ++it) {
    if (it.key().isEmpty()) {
      ns += QStringLiteral("declare default element namespace ")
            + QLatin1String(" \"") + it.value().toString()
            + QLatin1String("\";\n");
    } else {
      ns += QStringLiteral("declare namespace ") + it.key()
            + QLatin1String(" = \"") + it.value().toString()
            + QLatin1String("\";\n");
    }
  }
  query.setQuery(ns + "doc($inputDocument)"
                 + Grantlee::getSafeString(m_query.resolve(c)).get());

  QXmlResultItems result;
  query.evaluateTo(&result);

  QXmlItem item(result.next());
  int count = 0;
  while (!item.isNull()) {
    if (count++ > 20)
      break;
    query.setFocus(item);
    c->push();
    foreach (Grantlee::Node *n, m_childNodes) {
      if (n->inherits(XmlNamespaceNode::staticMetaObject.className()))
        continue;
      c->insert("_q", QVariant::fromValue(query));
      n->render(stream, c);
    }
    c->pop();
    item = result.next();
  }
  c->pop();
}

XmlRoleNodeFactory::XmlRoleNodeFactory(QObject *parent) {}

Grantlee::Node *XmlRoleNodeFactory::getNode(const QString &tagContent,
                                            Grantlee::Parser *p) const
{
  QStringList expr = smartSplit(tagContent);
  Grantlee::FilterExpression query(expr.at(1), p);
  return new XmlRoleNode(query);
}

XmlRoleNode::XmlRoleNode(const Grantlee::FilterExpression &query,
                         QObject *parent)
    : m_query(query), m_count(0)
{
}

static QString unescape(const QString &_input)
{
  QString input = _input;
  input.replace("&lt;", "<");
  input.replace("&gt;", ">");
  input.replace("&quot;", "\"");
  input.replace("&amp;", "&");
  return input;
}

void XmlRoleNode::render(Grantlee::OutputStream *stream,
                         Grantlee::Context *c) const
{
  QXmlQuery q = c->lookup("_q").value<QXmlQuery>();
  QHash<QString, QVariant> h = c->lookup("_ns").toHash();
  QString ns;
  QHash<QString, QVariant>::const_iterator it = h.constBegin();
  const QHash<QString, QVariant>::const_iterator end = h.constEnd();
  for (; it != end; ++it) {
    if (it.key().isEmpty()) {
      ns += QStringLiteral("declare default element namespace ")
            + QLatin1String(" \"") + it.value().toString()
            + QLatin1String("\";\n");
    } else {
      ns += QStringLiteral("declare namespace ") + it.key()
            + QLatin1String(" = \"") + it.value().toString()
            + QLatin1String("\";\n");
    }
  }
  q.setQuery(ns + Grantlee::getSafeString(m_query.resolve(c)));
  QString s;
  q.evaluateTo(&s);
  (*stream) << unescape(s);
}

XmlNamespaceNodeFactory::XmlNamespaceNodeFactory(QObject *parent) {}

Grantlee::Node *XmlNamespaceNodeFactory::getNode(const QString &tagContent,
                                                 Grantlee::Parser *p) const
{
  QStringList expr = smartSplit(tagContent);
  Grantlee::FilterExpression query(expr.at(1), p);
  QString name;
  if (expr.size() == 4)
    name = expr.at(3);
  return new XmlNamespaceNode(query, name);
}

XmlNamespaceNode::XmlNamespaceNode(const Grantlee::FilterExpression &query,
                                   const QString &name, QObject *parent)
    : m_query(query), m_name(name)
{
}

void XmlNamespaceNode::render(Grantlee::OutputStream *stream,
                              Grantlee::Context *c) const
{
  QString q = Grantlee::getSafeString(m_query.resolve(c));
  QHash<QString, QVariant> h = c->lookup("_ns").toHash();
  h.insert(m_name, q);
  c->insert("_ns", h);
}

QVariant ResizeFilter::doFilter(const QVariant &input, const QVariant &argument,
                                bool autoescape) const
{
  QString url = Grantlee::getSafeString(input);
  url.replace("_s", "_z");
  return url;
}