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

Syndication Library

  • syndication
elementwrapper.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 #include "elementwrapper.h"
23 #include "constants.h"
24 
25 #include <kurl.h>
26 
27 #include <QtXml/QDomDocument>
28 #include <QtXml/QDomElement>
29 #include <QtCore/QString>
30 #include <QtCore/QTextStream>
31 
32 namespace Syndication {
33 
34 class ElementWrapper::ElementWrapperPrivate
35 {
36  public:
37 
38  QDomElement element;
39  QDomDocument ownerDoc;
40  mutable QString xmlBase;
41  mutable bool xmlBaseParsed;
42  mutable QString xmlLang;
43  mutable bool xmlLangParsed;
44 };
45 
46 ElementWrapper::ElementWrapper() : d(new ElementWrapperPrivate)
47 {
48  d->xmlBaseParsed = true;
49  d->xmlLangParsed = true;
50 }
51 
52 ElementWrapper::ElementWrapper(const ElementWrapper& other)
53 {
54  *this = other;
55 }
56 
57 ElementWrapper::ElementWrapper(const QDomElement& element) : d(new ElementWrapperPrivate)
58 {
59  d->element = element;
60  d->ownerDoc = element.ownerDocument(); //keep a copy of the (shared, thus cheap) document around to ensure the element isn't deleted too early (Bug 190068)
61  d->xmlBaseParsed = false;
62  d->xmlLangParsed = false;
63 }
64 
65 ElementWrapper::~ElementWrapper()
66 {
67 }
68 
69 ElementWrapper& ElementWrapper::operator=(const ElementWrapper& other)
70 {
71  d = other.d;
72  return *this;
73 }
74 
75 bool ElementWrapper::operator==(const ElementWrapper& other) const
76 {
77  return d->element == other.d->element;
78 }
79 
80 bool ElementWrapper::isNull() const
81 {
82  return d->element.isNull();
83 }
84 
85 const QDomElement& ElementWrapper::element() const
86 {
87  return d->element;
88 }
89 
90 QString ElementWrapper::xmlBase() const
91 {
92  if (!d->xmlBaseParsed) // xmlBase not computed yet
93  {
94  QDomElement current = d->element;
95 
96  while (!current.isNull())
97  {
98  if (current.hasAttributeNS(xmlNamespace(), QLatin1String("base")))
99  {
100  d->xmlBase = current.attributeNS(xmlNamespace(), QLatin1String("base"));
101  return d->xmlBase;
102  }
103 
104  QDomNode parent = current.parentNode();
105 
106  if (!parent.isNull() && parent.isElement())
107  current = parent.toElement();
108  else
109  current = QDomElement();
110  }
111 
112  d->xmlBaseParsed = true;
113  }
114 
115  return d->xmlBase;
116 }
117 
118 QString ElementWrapper::completeURI(const QString& uri) const
119 {
120  KUrl u(xmlBase(), uri);
121 
122  if (u.isValid())
123  return u.url();
124 
125  return uri;
126 }
127 
128 QString ElementWrapper::xmlLang() const
129 {
130  if (!d->xmlLangParsed) // xmlLang not computed yet
131  {
132  QDomElement current = d->element;
133 
134  while (!current.isNull())
135  {
136  if (current.hasAttributeNS(xmlNamespace(), QLatin1String("lang")))
137  {
138  d->xmlLang = current.attributeNS(xmlNamespace(), QLatin1String("lang"));
139  return d->xmlLang;
140  }
141 
142  QDomNode parent = current.parentNode();
143 
144  if (!parent.isNull() && parent.isElement())
145  current = parent.toElement();
146  else
147  current = QDomElement();
148  }
149  d->xmlLangParsed = true;
150  }
151  return d->xmlLang;
152 }
153 
154 QString ElementWrapper::extractElementText(const QString& tagName) const
155 {
156  QDomElement el = d->element.namedItem(tagName).toElement();
157  return el.isNull() ? QString() : el.text().trimmed();
158 }
159 
160 QString ElementWrapper::extractElementTextNS(const QString& namespaceURI, const QString& localName) const
161 {
162  QDomElement el = firstElementByTagNameNS(namespaceURI, localName);
163  return el.isNull() ? QString() : el.text().trimmed();
164 }
165 
166 QString ElementWrapper::childNodesAsXML(const QDomElement& parent)
167 {
168  ElementWrapper wrapper(parent);
169 
170  if (parent.isNull())
171  return QString();
172 
173  QDomNodeList list = parent.childNodes();
174 
175  QString str;
176  QTextStream ts( &str, QIODevice::WriteOnly );
177 
178  // if there is a xml:base in our scope, first set it for
179  // each child element so the xml:base shows up in the
180  // serialization
181  QString base = wrapper.xmlBase();
182 
183 
184  for (int i = 0; i < list.count(); ++i)
185  {
186  QDomNode it = list.item(i);
187  if (!base.isEmpty() && it.isElement()
188  && !it.toElement().hasAttributeNS(xmlNamespace(), QLatin1String("base")))
189  {
190  it.toElement().setAttributeNS(xmlNamespace(), QLatin1String("base"), base);
191  }
192 
193  ts << it;
194  }
195  return str.trimmed();
196 }
197 
198 QString ElementWrapper::childNodesAsXML() const
199 {
200  return childNodesAsXML(d->element);
201 }
202 
203 QList<QDomElement> ElementWrapper::elementsByTagName(const QString& tagName) const
204 {
205  QList<QDomElement> elements;
206  for (QDomNode n = d->element.firstChild(); !n.isNull(); n = n.nextSibling())
207  {
208  if (n.isElement())
209  {
210  QDomElement e = n.toElement();
211  if (e.tagName() == tagName)
212  elements.append(e);
213  }
214  }
215  return elements;
216 }
217 
218 QDomElement ElementWrapper::firstElementByTagNameNS(const QString& nsURI, const QString& localName) const
219 {
220  if (isNull())
221  return QDomElement();
222 
223  for (QDomNode n = d->element.firstChild(); !n.isNull(); n = n.nextSibling())
224  {
225  if (n.isElement())
226  {
227  QDomElement e = n.toElement();
228  if (e.localName() == localName && e.namespaceURI() == nsURI)
229  return e;
230  }
231  }
232 
233  return QDomElement();
234 }
235 
236 
237 QList<QDomElement> ElementWrapper::elementsByTagNameNS(const QString& nsURI, const QString& localName) const
238 {
239  if (isNull())
240  return QList<QDomElement>();
241 
242  QList<QDomElement> elements;
243  for (QDomNode n = d->element.firstChild(); !n.isNull(); n = n.nextSibling())
244  {
245  if (n.isElement())
246  {
247  QDomElement e = n.toElement();
248  if (e.localName() == localName && e.namespaceURI() == nsURI)
249  elements.append(e);
250  }
251  }
252  return elements;
253 }
254 
255 QString ElementWrapper::text() const
256 {
257  return d->element.text();
258 }
259 
260 QString ElementWrapper::attribute(const QString& name, const QString& defValue) const
261 {
262  return d->element.attribute(name, defValue);
263 }
264 
265 QString ElementWrapper::attributeNS(const QString& nsURI, const QString& localName, const QString& defValue) const
266 {
267  return d->element.attributeNS(nsURI, localName, defValue);
268 }
269 
270 bool ElementWrapper::hasAttribute(const QString& name) const
271 {
272  return d->element.hasAttribute(name);
273 }
274 
275 bool ElementWrapper::hasAttributeNS(const QString& nsURI, const QString& localName) const
276 {
277  return d->element.hasAttributeNS(nsURI, localName);
278 }
279 
280 } // namespace Syndication
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:26:15 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Syndication Library

Skip menu "Syndication Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

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