save work
This commit is contained in:
parent
07eb444d94
commit
b49b7f470b
|
@ -416,6 +416,8 @@ set(SOURCES
|
||||||
services/standard/parsers/atomparser.h
|
services/standard/parsers/atomparser.h
|
||||||
services/standard/parsers/feedparser.cpp
|
services/standard/parsers/feedparser.cpp
|
||||||
services/standard/parsers/feedparser.h
|
services/standard/parsers/feedparser.h
|
||||||
|
services/standard/parsers/icalparser.cpp
|
||||||
|
services/standard/parsers/icalparser.h
|
||||||
services/standard/parsers/jsonparser.cpp
|
services/standard/parsers/jsonparser.cpp
|
||||||
services/standard/parsers/jsonparser.h
|
services/standard/parsers/jsonparser.h
|
||||||
services/standard/parsers/rdfparser.cpp
|
services/standard/parsers/rdfparser.cpp
|
||||||
|
|
|
@ -13,13 +13,13 @@
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
FeedParser::FeedParser(QString data, bool is_xml)
|
FeedParser::FeedParser(QString data, DataType is_xml)
|
||||||
: m_isXml(is_xml), m_data(std::move(data)), m_mrssNamespace(QSL("http://search.yahoo.com/mrss/")) {
|
: m_dataType(is_xml), m_data(std::move(data)), m_mrssNamespace(QSL("http://search.yahoo.com/mrss/")) {
|
||||||
if (m_data.isEmpty()) {
|
if (m_data.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_isXml) {
|
if (m_dataType == DataType::Xml) {
|
||||||
// XML.
|
// XML.
|
||||||
QString error;
|
QString error;
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ FeedParser::FeedParser(QString data, bool is_xml)
|
||||||
throw FeedFetchException(Feed::Status::ParsingError, QObject::tr("XML problem: %1").arg(error));
|
throw FeedFetchException(Feed::Status::ParsingError, QObject::tr("XML problem: %1").arg(error));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else if (m_dataType == DataType::Json) {
|
||||||
// JSON.
|
// JSON.
|
||||||
QJsonParseError err;
|
QJsonParseError err;
|
||||||
|
|
||||||
|
@ -50,7 +50,6 @@ QList<StandardFeed*> FeedParser::discoverFeeds(ServiceRoot* root, const QUrl& ur
|
||||||
|
|
||||||
if (QFile::exists(file_path)) {
|
if (QFile::exists(file_path)) {
|
||||||
try {
|
try {
|
||||||
// 1.
|
|
||||||
auto guessed_feed = guessFeed(IOFactory::readFile(file_path), {});
|
auto guessed_feed = guessFeed(IOFactory::readFile(file_path), {});
|
||||||
|
|
||||||
guessed_feed.first->setSourceType(StandardFeed::SourceType::LocalFile);
|
guessed_feed.first->setSourceType(StandardFeed::SourceType::LocalFile);
|
||||||
|
@ -126,7 +125,7 @@ QList<Message> FeedParser::messages() {
|
||||||
QDateTime current_time = QDateTime::currentDateTimeUtc();
|
QDateTime current_time = QDateTime::currentDateTimeUtc();
|
||||||
|
|
||||||
// Pull out all messages.
|
// Pull out all messages.
|
||||||
if (m_isXml) {
|
if (m_dataType == DataType::Xml) {
|
||||||
QDomNodeList messages_in_xml = xmlMessageElements();
|
QDomNodeList messages_in_xml = xmlMessageElements();
|
||||||
|
|
||||||
for (int i = 0; i < messages_in_xml.size(); i++) {
|
for (int i = 0; i < messages_in_xml.size(); i++) {
|
||||||
|
@ -154,7 +153,7 @@ QList<Message> FeedParser::messages() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else if (m_dataType == DataType::Json) {
|
||||||
QJsonArray messages_in_json = jsonMessageElements();
|
QJsonArray messages_in_json = jsonMessageElements();
|
||||||
|
|
||||||
for (int i = 0; i < messages_in_json.size(); i++) {
|
for (int i = 0; i < messages_in_json.size(); i++) {
|
||||||
|
|
|
@ -16,7 +16,14 @@
|
||||||
// Base class for all XML-based feed parsers.
|
// Base class for all XML-based feed parsers.
|
||||||
class FeedParser {
|
class FeedParser {
|
||||||
public:
|
public:
|
||||||
explicit FeedParser(QString data, bool is_xml = true);
|
enum class DataType {
|
||||||
|
Xml,
|
||||||
|
Json,
|
||||||
|
Ical,
|
||||||
|
Other
|
||||||
|
};
|
||||||
|
|
||||||
|
explicit FeedParser(QString data, DataType is_xml = DataType::Xml);
|
||||||
virtual ~FeedParser();
|
virtual ~FeedParser();
|
||||||
|
|
||||||
// Returns list of absolute URLs of discovered feeds from provided base URL.
|
// Returns list of absolute URLs of discovered feeds from provided base URL.
|
||||||
|
@ -66,7 +73,7 @@ class FeedParser {
|
||||||
bool only_first) const;
|
bool only_first) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool m_isXml;
|
DataType m_dataType;
|
||||||
QString m_data;
|
QString m_data;
|
||||||
QDomDocument m_xml;
|
QDomDocument m_xml;
|
||||||
QJsonDocument m_json;
|
QJsonDocument m_json;
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
// For license of this file, see <project-root-folder>/LICENSE.md.
|
||||||
|
|
||||||
|
#include "services/standard/parsers/icalparser.h"
|
||||||
|
|
||||||
|
#include "definitions/definitions.h"
|
||||||
|
#include "exceptions/applicationexception.h"
|
||||||
|
#include "exceptions/feedrecognizedbutfailedexception.h"
|
||||||
|
#include "services/standard/definitions.h"
|
||||||
|
|
||||||
|
IcalParser::IcalParser(const QString& data) : FeedParser(data, DataType::Other) {}
|
||||||
|
|
||||||
|
IcalParser::~IcalParser() {}
|
||||||
|
|
||||||
|
QPair<StandardFeed*, QList<IconLocation>> IcalParser::guessFeed(const QByteArray& content,
|
||||||
|
const QString& content_type) const {
|
||||||
|
if (content_type == QSL("text/calendar") || content.contains('\n')) {
|
||||||
|
Icalendar calendar;
|
||||||
|
|
||||||
|
try {
|
||||||
|
calendar = Icalendar(content);
|
||||||
|
}
|
||||||
|
catch (const ApplicationException& ex) {
|
||||||
|
throw FeedRecognizedButFailedException(QObject::tr("iCalendar error '%1'").arg(ex.message()));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto* feed = new StandardFeed();
|
||||||
|
QList<IconLocation> icon_possible_locations;
|
||||||
|
|
||||||
|
feed->setEncoding(QSL(DEFAULT_FEED_ENCODING));
|
||||||
|
feed->setType(StandardFeed::Type::Atom10);
|
||||||
|
feed->setTitle(calendar.title());
|
||||||
|
|
||||||
|
return QPair<StandardFeed*, QList<IconLocation>>(feed, icon_possible_locations);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw ApplicationException(QObject::tr("not an iCalendar"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Icalendar::Icalendar() {}
|
||||||
|
|
||||||
|
Icalendar::Icalendar(const QByteArray& data) {
|
||||||
|
processLines(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString Icalendar::title() const {
|
||||||
|
return m_title;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Icalendar::setTitle(const QString& title) {
|
||||||
|
m_title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Icalendar::processLines(const QByteArray& data) {
|
||||||
|
QString str_data = QString::fromUtf8(data);
|
||||||
|
|
||||||
|
QStringList str_blocks =
|
||||||
|
str_data.remove(QRegularExpression("^END:\\w+$")).split(QRegularExpression(QSL("^BEGIN:\\w+$")));
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
// For license of this file, see <project-root-folder>/LICENSE.md.
|
||||||
|
|
||||||
|
#ifndef ICALPARSER_H
|
||||||
|
#define ICALPARSER_H
|
||||||
|
|
||||||
|
#include "services/standard/parsers/feedparser.h"
|
||||||
|
|
||||||
|
class IcalendarComponent {};
|
||||||
|
|
||||||
|
class EventComponent : public IcalendarComponent {};
|
||||||
|
|
||||||
|
class Icalendar {
|
||||||
|
public:
|
||||||
|
enum class ProcessingState {
|
||||||
|
BeginCalendar,
|
||||||
|
EndCalendar,
|
||||||
|
BeginEvent,
|
||||||
|
EndEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit Icalendar();
|
||||||
|
explicit Icalendar(const QByteArray& data);
|
||||||
|
|
||||||
|
QString title() const;
|
||||||
|
void setTitle(const QString& title);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void processLines(const QByteArray& data);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString m_title;
|
||||||
|
QList<IcalendarComponent> m_components;
|
||||||
|
};
|
||||||
|
|
||||||
|
class IcalParser : public FeedParser {
|
||||||
|
public:
|
||||||
|
explicit IcalParser(const QString& data);
|
||||||
|
virtual ~IcalParser();
|
||||||
|
|
||||||
|
virtual QPair<StandardFeed*, QList<IconLocation>> guessFeed(const QByteArray& content,
|
||||||
|
const QString& content_type) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ICALPARSER_H
|
|
@ -15,7 +15,7 @@
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
|
||||||
JsonParser::JsonParser(const QString& data) : FeedParser(data, false) {}
|
JsonParser::JsonParser(const QString& data) : FeedParser(data, DataType::Json) {}
|
||||||
|
|
||||||
JsonParser::~JsonParser() {}
|
JsonParser::~JsonParser() {}
|
||||||
|
|
||||||
|
|
|
@ -187,6 +187,9 @@ QString StandardFeed::typeToString(StandardFeed::Type type) {
|
||||||
case Type::Sitemap:
|
case Type::Sitemap:
|
||||||
return QSL("Sitemap");
|
return QSL("Sitemap");
|
||||||
|
|
||||||
|
case Type::iCalendar:
|
||||||
|
return QSL("iCalendar");
|
||||||
|
|
||||||
case Type::Rss2X:
|
case Type::Rss2X:
|
||||||
default:
|
default:
|
||||||
return QSL("RSS 2.0/2.0.1");
|
return QSL("RSS 2.0/2.0.1");
|
||||||
|
|
|
@ -37,7 +37,8 @@ class StandardFeed : public Feed {
|
||||||
Rdf = 2, // Sometimes denoted as RSS 1.0.
|
Rdf = 2, // Sometimes denoted as RSS 1.0.
|
||||||
Atom10 = 3,
|
Atom10 = 3,
|
||||||
Json = 4,
|
Json = 4,
|
||||||
Sitemap = 5
|
Sitemap = 5,
|
||||||
|
iCalendar = 6
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit StandardFeed(RootItem* parent_item = nullptr);
|
explicit StandardFeed(RootItem* parent_item = nullptr);
|
||||||
|
|
Loading…
Reference in New Issue