• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

Syndication Library

elementwrapper.cpp
00001 /*
00002  * This file is part of the syndication library
00003  *
00004  * Copyright (C) 2006 Frank Osterfeld <osterfeld@kde.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 #include "elementwrapper.h"
00023 #include "constants.h"
00024 
00025 #include <kurl.h>
00026 
00027 #include <QtXml/QDomDocument>
00028 #include <QtXml/QDomElement>
00029 #include <QtCore/QString>
00030 #include <QtCore/QTextStream>
00031 
00032 namespace Syndication {
00033 
00034 class ElementWrapper::ElementWrapperPrivate
00035 {
00036     public:
00037         
00038     QDomElement element;
00039     QDomDocument ownerDoc;
00040     mutable QString xmlBase;
00041     mutable bool xmlBaseParsed;
00042     mutable QString xmlLang;
00043     mutable bool xmlLangParsed;
00044 };
00045 
00046 ElementWrapper::ElementWrapper() : d(new ElementWrapperPrivate)
00047 {
00048     d->xmlBaseParsed = true;
00049     d->xmlLangParsed = true;
00050 }
00051 
00052 ElementWrapper::ElementWrapper(const ElementWrapper& other)
00053 {
00054     *this = other;
00055 }
00056 
00057 ElementWrapper::ElementWrapper(const QDomElement& element) : d(new ElementWrapperPrivate)
00058 {
00059     d->element = element;
00060     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)
00061     d->xmlBaseParsed = false;
00062     d->xmlLangParsed = false;
00063 }
00064 
00065 ElementWrapper::~ElementWrapper()
00066 {
00067 }
00068 
00069 ElementWrapper& ElementWrapper::operator=(const ElementWrapper& other)
00070 {
00071     d = other.d;
00072     return *this;
00073 }
00074 
00075 bool ElementWrapper::operator==(const ElementWrapper& other) const
00076 {
00077     return d->element == other.d->element;
00078 }
00079 
00080 bool ElementWrapper::isNull() const
00081 {
00082     return d->element.isNull();
00083 }
00084 
00085 const QDomElement& ElementWrapper::element() const
00086 {
00087     return d->element;
00088 }
00089 
00090 QString ElementWrapper::xmlBase() const
00091 {
00092     if (!d->xmlBaseParsed) // xmlBase not computed yet
00093     {
00094         QDomElement current = d->element;
00095         
00096         while (!current.isNull())
00097         {
00098             if (current.hasAttributeNS(xmlNamespace(), QString::fromUtf8("base")))
00099             {
00100                 d->xmlBase = current.attributeNS(xmlNamespace(), QString::fromUtf8("base"));
00101                 return d->xmlBase;
00102             }
00103             
00104             QDomNode parent = current.parentNode();
00105 
00106             if (!parent.isNull() && parent.isElement())
00107                 current = parent.toElement();
00108             else
00109                 current = QDomElement();
00110         }
00111         
00112         d->xmlBaseParsed = true;
00113     }
00114     
00115     return d->xmlBase;
00116 }
00117 
00118 QString ElementWrapper::completeURI(const QString& uri) const
00119 {
00120     KUrl u(xmlBase(), uri);
00121     
00122     if (u.isValid())
00123         return u.url();
00124     
00125     return uri;
00126 }
00127 
00128 QString ElementWrapper::xmlLang() const
00129 {
00130     if (!d->xmlLangParsed) // xmlLang not computed yet
00131     {
00132         QDomElement current = d->element;
00133         
00134         while (!current.isNull())
00135         {
00136             if (current.hasAttributeNS(xmlNamespace(), QString::fromUtf8("lang")))
00137             {
00138                 d->xmlLang = current.attributeNS(xmlNamespace(), QString::fromUtf8("lang"));
00139                 return d->xmlLang;
00140             }
00141             
00142             QDomNode parent = current.parentNode();
00143 
00144             if (!parent.isNull() && parent.isElement())
00145                 current = parent.toElement();
00146             else
00147                 current = QDomElement();
00148         }
00149         d->xmlLangParsed = true;
00150     }
00151     return d->xmlLang;
00152 }
00153         
00154 QString ElementWrapper::extractElementText(const QString& tagName) const
00155 {
00156     QDomElement el = d->element.namedItem(tagName).toElement();
00157     return el.isNull() ? QString() : el.text().trimmed();
00158 }
00159 
00160 QString ElementWrapper::extractElementTextNS(const QString& namespaceURI, const QString& localName) const
00161 {
00162     QDomElement el = firstElementByTagNameNS(namespaceURI, localName);
00163     return el.isNull() ? QString() : el.text().trimmed();
00164 }
00165 
00166 QString ElementWrapper::childNodesAsXML(const QDomElement& parent)
00167 {
00168     ElementWrapper wrapper(parent);
00169     
00170     if (parent.isNull())
00171         return QString();
00172 
00173     QDomNodeList list = parent.childNodes();
00174     
00175     QString str;
00176     QTextStream ts( &str, QIODevice::WriteOnly );
00177     
00178     // if there is a xml:base in our scope, first set it for
00179     // each child element so the xml:base shows up in the
00180     // serialization
00181     QString base = wrapper.xmlBase();
00182 
00183 
00184     for (int i = 0; i < list.count(); ++i)
00185     {
00186         QDomNode it = list.item(i);
00187         if (!base.isEmpty() && it.isElement() 
00188              && !it.toElement().hasAttributeNS(xmlNamespace(), QString::fromUtf8("base")))
00189         {
00190             it.toElement().setAttributeNS(xmlNamespace(), QString::fromUtf8("base"), base);
00191         }
00192             
00193         ts << it;
00194     }
00195     return str.trimmed();
00196 }
00197 
00198 QString ElementWrapper::childNodesAsXML() const
00199 {
00200     return childNodesAsXML(d->element);
00201 }
00202 
00203 QList<QDomElement> ElementWrapper::elementsByTagName(const QString& tagName) const
00204 {
00205     QList<QDomElement> elements;
00206     for (QDomNode n = d->element.firstChild(); !n.isNull(); n = n.nextSibling())
00207     {
00208         if (n.isElement())
00209         {
00210             QDomElement e = n.toElement();
00211             if (e.tagName() == tagName)
00212                 elements.append(e);
00213         }
00214     }
00215     return elements;
00216 }
00217 
00218 QDomElement ElementWrapper::firstElementByTagNameNS(const QString& nsURI, const QString& localName) const
00219 {
00220     if (isNull())
00221         return QDomElement();
00222     
00223     for (QDomNode n = d->element.firstChild(); !n.isNull(); n = n.nextSibling())
00224     {
00225         if (n.isElement())
00226         {
00227             QDomElement e = n.toElement();
00228             if (e.localName() == localName && e.namespaceURI() == nsURI)
00229                 return e;
00230         }
00231     }
00232     
00233     return QDomElement();
00234 }
00235 
00236 
00237 QList<QDomElement> ElementWrapper::elementsByTagNameNS(const QString& nsURI, const QString& localName) const
00238 {
00239     if (isNull())
00240         return QList<QDomElement>();
00241     
00242     QList<QDomElement> elements;
00243     for (QDomNode n = d->element.firstChild(); !n.isNull(); n = n.nextSibling())
00244     {
00245         if (n.isElement())
00246         {
00247             QDomElement e = n.toElement();
00248             if (e.localName() == localName && e.namespaceURI() == nsURI)
00249                 elements.append(e);
00250         }
00251     }
00252     return elements;
00253 }
00254 
00255 QString ElementWrapper::text() const
00256 {
00257     return d->element.text();
00258 }
00259         
00260 QString ElementWrapper::attribute(const QString& name, const QString& defValue) const
00261 {
00262     return d->element.attribute(name, defValue);
00263 }
00264         
00265 QString ElementWrapper::attributeNS(const QString& nsURI, const QString& localName, const QString& defValue) const
00266 {
00267     return d->element.attributeNS(nsURI, localName, defValue);
00268 }
00269 
00270 bool ElementWrapper::hasAttribute(const QString& name) const
00271 {
00272     return d->element.hasAttribute(name);
00273 }
00274 
00275 bool ElementWrapper::hasAttributeNS(const QString& nsURI, const QString& localName) const
00276 {
00277     return d->element.hasAttributeNS(nsURI, localName);
00278 }
00279 
00280 } // namespace Syndication

Syndication Library

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal