Added initial support for RDF.

This commit is contained in:
Martin Rotter 2013-12-27 11:32:03 +01:00
parent 3739cdb1f4
commit b4d60e01f1
4 changed files with 75 additions and 13 deletions

View File

@ -54,10 +54,10 @@ QString FeedsModelFeed::typeToString(FeedsModelFeed::Type type) {
return QObject::tr("ATOM 1.0");
case StandardRdf:
return QObject::tr("RDF 1.0");
return QObject::tr("RDF");
case StandardRss0X:
return QObject::tr("RSS 0.90/0.91/0.92");
return QObject::tr("RSS 0.91/0.92/0.93");
case StandardRss2X:
default:

View File

@ -186,6 +186,10 @@ void FeedsModelStandardFeed::update() {
messages = ParsingFactory::parseAsRSS20(formatted_feed_contents);
break;
case FeedsModelFeed::StandardRdf:
messages = ParsingFactory::parseAsRDF(formatted_feed_contents);
break;
// TODO: Add support for other standard formats.
default:

View File

@ -14,6 +14,70 @@ QList<Message> ParsingFactory::parseAsATOM10(const QString &data) {
return QList<Message>();
}
QList<Message> ParsingFactory::parseAsRDF(const QString &data) {
QList<Message> messages;
QDomDocument xml_file;
QDateTime current_time = QDateTime::currentDateTime();
xml_file.setContent(data, true);
// Pull out all messages.
QDomNodeList messages_in_xml = xml_file.elementsByTagName("item");
for (int i = 0; i < messages_in_xml.size(); i++) {
QDomNode message_item = messages_in_xml.item(i);
Message new_message;
// Deal with title and description.
QString elem_title = message_item.namedItem("title").toElement().text().simplified();
QString elem_description = message_item.namedItem("description").toElement().text();
// Now we obtained maximum of informations for title & description.
if (elem_title.isEmpty()) {
if (elem_description.isEmpty()) {
// BOTH title and description are empty, skip this message.
continue;
}
else {
// Title is empty but description is not.
new_message.m_title = TextFactory::stripTags(elem_description.simplified());
new_message.m_contents = elem_description;
}
}
else {
// Title is really not empty, description does not matter.
new_message.m_title = TextFactory::stripTags(elem_title);
new_message.m_contents = elem_description;
}
// Deal with link and author.
new_message.m_url = message_item.namedItem("link").toElement().text();
new_message.m_author = message_item.namedItem("creator").toElement().text();
// Deal with creation date.
QString elem_updated = message_item.namedItem("date").toElement().text();
if (elem_updated.isEmpty()) {
elem_updated = message_item.namedItem("dc:date").toElement().text();
}
// Deal with creation date.
new_message.m_created = TextFactory::parseDateTime(elem_updated);
new_message.m_createdFromFeed = !new_message.m_created.isNull();
if (!new_message.m_createdFromFeed) {
// Date was NOT obtained from the feed,
// set current date as creation date for the message.
new_message.m_created = current_time;
}
messages.append(new_message);
}
return messages;
}
QList<Message> ParsingFactory::parseAsRSS20(const QString &data) {
QList<Message> messages;
QDomDocument xml_file;
@ -44,22 +108,15 @@ QList<Message> ParsingFactory::parseAsRSS20(const QString &data) {
}
else {
// Title is empty but description is not.
new_message.m_title = TextFactory::stripTags(elem_description);
new_message.m_title = TextFactory::stripTags(elem_description.simplified());
new_message.m_contents = elem_description;
}
}
else {
if (elem_description.isEmpty()) {
// Title is not empty but description is empty.
new_message.m_title = TextFactory::stripTags(elem_title);
new_message.m_contents = QObject::tr("This message has not contents.");
}
else {
// Both description and title are not empty.
// Title is really not empty, description does not matter.
new_message.m_title = TextFactory::stripTags(elem_title);
new_message.m_contents = elem_description;
}
}
// Deal with link and author.
new_message.m_url = message_item.namedItem("link").toElement().text();

View File

@ -23,6 +23,7 @@ class ParsingFactory {
// Parses input textual data into Message objects.
// NOTE: Input is correctly encoded in Unicode.
static QList<Message> parseAsATOM10(const QString &data);
static QList<Message> parseAsRDF(const QString &data);
static QList<Message> parseAsRSS20(const QString &data);
};