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

syndication/atom

  • sources
  • kde-4.14
  • kdepimlibs
  • 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 
41 namespace Syndication {
42 namespace Atom {
43 
44 class 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 
51 bool 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 
57 Syndication::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 
98 QString Parser::format() const
99 {
100  return QLatin1String("atom");
101 }
102 
103 QDomNode 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 
184 QDomDocument 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 
206 Parser::Parser() : d(0) {}
207 Parser::~Parser() {}
208 Parser::Parser(const Parser& other) : AbstractParser(other), d(0) {}
209 Parser& Parser::operator=(const Parser& /*other*/) { return *this; }
210 
211 } // namespace Atom
212 } // namespace Syndication
QHash::insert
iterator insert(const Key &key, const T &value)
QDomNodeList::item
QDomNode item(int index) const
Syndication::Atom::Content::EscapedHTML
the content is escaped HTML, (i.e., "<", ">" etc.
Definition: content.h:58
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::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
QDomAttr::name
QString name() const
QDomNode::appendChild
QDomNode appendChild(const QDomNode &newChild)
QDomElement::attribute
QString attribute(const QString &name, const QString &defValue) const
QDomNode::isElement
bool isElement() const
QDomNodeList
Syndication::Atom::Content::PlainText
the content is plain text (i.e.
Definition: content.h:55
QDomNode::namespaceURI
QString namespaceURI() const
QDomNode
QDomDocument::createElementNS
QDomElement createElementNS(const QString &nsURI, const QString &qName)
QDomNode::childNodes
QDomNodeList childNodes() const
QDomNode::toElement
QDomElement toElement() const
Syndication::Atom::FeedDocument
An Atom 1.0 Feed Document, containing metadata describing the feed and a number of entries...
Definition: document.h:57
Syndication::Atom::Content::Binary
the content is base64-encoded binary content
Definition: content.h:61
QDomNamedNodeMap
Syndication::Atom::atom1Namespace
QString atom1Namespace()
namespace used by Atom 1.0 elements
Definition: constants.cpp:30
QDomElement::setAttributeNS
void setAttributeNS(const QString nsURI, const QString &qName, const QString &value)
QDomNodeList::count
int count() const
QDomNode::localName
QString localName() const
QDomElement::hasAttribute
bool hasAttribute(const QString &name) const
QHash
Definition: parser.h:29
QDomAttr
QDomElement::setAttribute
void setAttribute(const QString &name, const QString &value)
QString::isEmpty
bool isEmpty() const
Syndication::Atom::Parser::Parser
Parser()
default constructor
Definition: parser.cpp:206
QDomNamedNodeMap::count
int count() const
Syndication::Atom::atom0_3Namespace
QString atom0_3Namespace()
namespace used by Atom 0.3 elements
Definition: constants.cpp:35
QString
Syndication::Atom::Content::XML
the content is embedded XML
Definition: content.h:60
QDomNode::namedItem
QDomNode namedItem(const QString &name) const
QDomDocument
QDomAttr::value
QString value() const
QDomNode::isNull
bool isNull() const
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
QLatin1String
QDomNode::cloneNode
QDomNode cloneNode(bool deep) const
Syndication::Atom::Parser::format
QString format() const
returns the format string for this parser implementation, which is "atom"
Definition: parser.cpp:98
Syndication::Atom::Content::Format
Format
format of the content.
Definition: content.h:53
QDomNode::toAttr
QDomAttr toAttr() const
QHash::contains
bool contains(const Key &key) const
Syndication::Atom::Parser::~Parser
virtual ~Parser()
destructor
Definition: parser.cpp:207
Syndication::Atom::Parser
parser implementation for Atom 1.0 and 0.3.
Definition: parser.h:43
QDomNamedNodeMap::item
QDomNode item(int index) const
Syndication::Atom::EntryDocument
An Atom 1.0 Entry Document, containing a single Atom entry outside of the context of a feed...
Definition: document.h:202
QDomElement
QDomNode::toDocument
QDomDocument toDocument() const
QDomElement::attributes
QDomNamedNodeMap attributes() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:37:35 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs 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

Search



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