• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

syndication/atom

  • syndication
  • atom
parser.cpp
1/*
2 * This file is part of the syndication library
3 *
4 * Copyright (C) 2006 Frank Osterfeld <osterfeld@kde.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "parser.h"
24#include "constants.h"
25#include "content.h"
26#include "document.h"
27
28#include <documentsource.h>
29
30#include <QtXml/QDomAttr>
31#include <QtXml/QDomDocument>
32#include <QtXml/QDomElement>
33#include <QtXml/QDomNamedNodeMap>
34#include <QtXml/QDomNode>
35#include <QtXml/QDomNodeList>
36
37#include <QtCore/QHash>
38#include <QtCore/QRegExp>
39#include <QtCore/QString>
40
41namespace Syndication {
42namespace Atom {
43
44class Parser::ParserPrivate
45{
46 public:
47 static QDomDocument convertAtom0_3(const QDomDocument& document);
48 static QDomNode convertNode(QDomDocument& doc, const QDomNode& node, const QHash<QString, QString>& nameMapper);
49};
50
51bool Parser::accept(const Syndication::DocumentSource& source) const
52{
53 QDomElement root = source.asDomDocument().documentElement();
54 return !root.isNull() && (root.namespaceURI() == atom1Namespace() || root.namespaceURI() == atom0_3Namespace());
55}
56
57Syndication::SpecificDocumentPtr Parser::parse(const Syndication::DocumentSource& source) const
58{
59 QDomDocument doc = source.asDomDocument();
60
61 if (doc.isNull())
62 {
63 // if this is not atom, return an invalid feed document
64 return FeedDocumentPtr(new FeedDocument());
65 }
66
67 QDomElement feed = doc.namedItem(QLatin1String("feed")).toElement();
68
69 bool feedValid = !feed.isNull();
70
71 if (feedValid && feed.attribute(QLatin1String("version"))
72 == QLatin1String("0.3"))
73 {
74 doc = ParserPrivate::convertAtom0_3(doc);
75 feed = doc.namedItem(QLatin1String("feed")).toElement();
76
77 }
78
79 feedValid = !feed.isNull() && feed.namespaceURI() == atom1Namespace();
80
81 if (feedValid)
82 {
83 return FeedDocumentPtr(new FeedDocument(feed));
84 }
85
86 QDomElement entry = doc.namedItem(QLatin1String("entry")).toElement();
87 bool entryValid = !entry.isNull() && entry.namespaceURI() == atom1Namespace();
88
89 if (entryValid)
90 {
91 return EntryDocumentPtr(new EntryDocument(feed));
92 }
93
94 // if this is not atom, return an invalid feed document
95 return FeedDocumentPtr(new FeedDocument());
96}
97
98QString Parser::format() const
99{
100 return QLatin1String("atom");
101}
102
103QDomNode Parser::ParserPrivate::convertNode(QDomDocument& doc, const QDomNode& node, const QHash<QString, QString>& nameMapper)
104{
105 if (!node.isElement())
106 return node.cloneNode(true);
107
108 bool isAtom03Element = node.namespaceURI() == atom0_3Namespace();
109 QDomElement oldEl = node.toElement();
110
111 // use new namespace
112 QString newNS = isAtom03Element ? atom1Namespace() : node.namespaceURI();
113
114 QString newName = node.localName();
115
116 // rename tags that are listed in the nameMapper
117 if (isAtom03Element && nameMapper.contains(node.localName()))
118 newName = nameMapper[node.localName()];
119
120 QDomElement newEl = doc.createElementNS(newNS, newName);
121
122 QDomNamedNodeMap attributes = oldEl.attributes();
123
124 // copy over attributes
125 for (int i = 0; i < attributes.count(); ++i)
126 {
127 QDomAttr attr = attributes.item(i).toAttr();
128 if (attr.namespaceURI().isEmpty())
129 newEl.setAttribute(attr.name(), attr.value());
130 else
131 newEl.setAttributeNS(attr.namespaceURI(), attr.name(), attr.value());
132 }
133
134 bool isTextConstruct = newNS == atom1Namespace()
135 && (newName == QLatin1String("title")
136 || newName == QLatin1String("rights")
137 || newName == QLatin1String("subtitle")
138 || newName == QLatin1String("summary"));
139
140 // for atom text constructs, map to new type schema (which only allows text, type, xhtml)
141
142 if (isTextConstruct)
143 {
144 QString oldType = newEl.attribute(QLatin1String("type"), QLatin1String("text/plain") );
145 QString newType;
146
147 Content::Format format = Content::mapTypeToFormat(oldType);
148 switch (format)
149 {
150 case Content::XML:
151 newType = QLatin1String("xhtml");
152 break;
153 case Content::EscapedHTML:
154 newType = QLatin1String("html");
155 break;
156 case Content::PlainText:
157 case Content::Binary:
158 default:
159 newType = QLatin1String("text");
160
161 }
162
163 newEl.setAttribute(QLatin1String("type"), newType);
164 }
165 else
166 {
167 // for generator, rename the "url" attribute to "uri"
168
169 bool isGenerator = newNS == atom1Namespace() && newName == QLatin1String("generator");
170 if (isGenerator && newEl.hasAttribute(QLatin1String("url")))
171 newEl.setAttribute(QLatin1String("uri"), newEl.attribute(QLatin1String("url")));
172 }
173
174 // process child nodes recursively and append them
175 QDomNodeList children = node.childNodes();
176 for (int i = 0; i < children.count(); ++i)
177 {
178 newEl.appendChild(convertNode(doc, children.item(i), nameMapper));
179 }
180
181 return newEl;
182}
183
184QDomDocument Parser::ParserPrivate::convertAtom0_3(const QDomDocument& doc03)
185{
186 QDomDocument doc = doc03.cloneNode(false).toDocument();
187
188 // these are the tags that were renamed in 1.0
189 QHash<QString, QString> nameMapper;
190 nameMapper.insert(QLatin1String("issued"), QLatin1String("published"));
191 nameMapper.insert(QLatin1String("modified"), QLatin1String("updated"));
192 nameMapper.insert(QLatin1String("url"), QLatin1String("uri"));
193 nameMapper.insert(QLatin1String("copyright"), QLatin1String("rights"));
194 nameMapper.insert(QLatin1String("tagline"), QLatin1String("subtitle"));
195
196 QDomNodeList children = doc03.childNodes();
197
198 for (int i = 0; i < children.count(); ++i)
199 {
200 doc.appendChild(convertNode(doc, children.item(i), nameMapper));
201 }
202
203 return doc;
204}
205
206Parser::Parser() : d(0) {}
207Parser::~Parser() {}
208Parser::Parser(const Parser& other) : AbstractParser(other), d(0) {}
209Parser& Parser::operator=(const Parser& /*other*/) { return *this; }
210
211} // namespace Atom
212} // namespace Syndication
Syndication::Atom::Content::mapTypeToFormat
static Format mapTypeToFormat(const QString &type, const QString &src=QString())
maps a mimetype to Format enum according to the Atom 1.0 specification
Definition content.cpp:90
Syndication::Atom::Content::Format
Format
format of the content.
Definition content.h:54
Syndication::Atom::Content::XML
@ XML
the content is embedded XML
Definition content.h:60
Syndication::Atom::Content::EscapedHTML
@ EscapedHTML
the content is escaped HTML, (i.e., "<", ">" etc.
Definition content.h:58
Syndication::Atom::Content::PlainText
@ PlainText
the content is plain text (i.e.
Definition content.h:55
Syndication::Atom::Content::Binary
@ Binary
the content is base64-encoded binary content
Definition content.h:61
Syndication::Atom::EntryDocument
An Atom 1.0 Entry Document, containing a single Atom entry outside of the context of a feed.
Definition document.h:203
Syndication::Atom::FeedDocument
An Atom 1.0 Feed Document, containing metadata describing the feed and a number of entries.
Definition document.h:58
Syndication::Atom::Parser
parser implementation for Atom 1.0 and 0.3.
Definition parser.h:44
Syndication::Atom::Parser::parse
Syndication::SpecificDocumentPtr parse(const Syndication::DocumentSource &source) const
parses either an EntryDocument or a FeedDocument from a document source.
Definition parser.cpp:57
Syndication::Atom::Parser::~Parser
virtual ~Parser()
destructor
Definition parser.cpp:207
Syndication::Atom::Parser::Parser
Parser()
default constructor
Definition parser.cpp:206
Syndication::Atom::Parser::format
QString format() const
returns the format string for this parser implementation, which is "atom"
Definition parser.cpp:98
Syndication::Atom::Parser::accept
bool accept(const Syndication::DocumentSource &source) const
returns whether the source looks like an Atom 1.0 or 0.3 document, by checking the root element.
Definition parser.cpp:51
Syndication::Atom::atom1Namespace
QString atom1Namespace()
namespace used by Atom 1.0 elements
Definition constants.cpp:30
Syndication::Atom::atom0_3Namespace
QString atom0_3Namespace()
namespace used by Atom 0.3 elements
Definition constants.cpp:35
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Wed Jan 24 2024 00:00:00 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

syndication/atom

Skip menu "syndication/atom"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Members
  • File List

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal