• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • 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/QDomElement>
00028 #include <QtCore/QString>
00029 #include <QtCore/QTextStream>
00030 
00031 namespace Syndication {
00032 
00033 class ElementWrapper::ElementWrapperPrivate
00034 {
00035     public:
00036         
00037     QDomElement element;
00038     mutable QString xmlBase;
00039     mutable bool xmlBaseParsed;
00040     mutable QString xmlLang;
00041     mutable bool xmlLangParsed;
00042 };
00043 
00044 ElementWrapper::ElementWrapper() : d(new ElementWrapperPrivate)
00045 {
00046     d->xmlBaseParsed = true;
00047     d->xmlLangParsed = true;
00048 }
00049 
00050 ElementWrapper::ElementWrapper(const ElementWrapper& other)
00051 {
00052     *this = other;
00053 }
00054 
00055 ElementWrapper::ElementWrapper(const QDomElement& element) : d(new ElementWrapperPrivate)
00056 {
00057     d->element = element;
00058     d->xmlBaseParsed = false;
00059     d->xmlLangParsed = false;
00060 }
00061 
00062 ElementWrapper::~ElementWrapper()
00063 {
00064 }
00065 
00066 ElementWrapper& ElementWrapper::operator=(const ElementWrapper& other)
00067 {
00068     d = other.d;
00069     return *this;
00070 }
00071 
00072 bool ElementWrapper::operator==(const ElementWrapper& other) const
00073 {
00074     return d->element == other.d->element;
00075 }
00076 
00077 bool ElementWrapper::isNull() const
00078 {
00079     return d->element.isNull();
00080 }
00081 
00082 const QDomElement& ElementWrapper::element() const
00083 {
00084     return d->element;
00085 }
00086 
00087 QString ElementWrapper::xmlBase() const
00088 {
00089     if (!d->xmlBaseParsed) // xmlBase not computed yet
00090     {
00091         QDomElement current = d->element;
00092         
00093         while (!current.isNull())
00094         {
00095             if (current.hasAttributeNS(xmlNamespace(), QString::fromUtf8("base")))
00096             {
00097                 d->xmlBase = current.attributeNS(xmlNamespace(), QString::fromUtf8("base"));
00098                 return d->xmlBase;
00099             }
00100             
00101             QDomNode parent = current.parentNode();
00102 
00103             if (!parent.isNull() && parent.isElement())
00104                 current = parent.toElement();
00105             else
00106                 current = QDomElement();
00107         }
00108         
00109         d->xmlBaseParsed = true;
00110     }
00111     
00112     return d->xmlBase;
00113 }
00114 
00115 QString ElementWrapper::completeURI(const QString& uri) const
00116 {
00117     KUrl u(xmlBase(), uri);
00118     
00119     if (u.isValid())
00120         return u.url();
00121     
00122     return uri;
00123 }
00124 
00125 QString ElementWrapper::xmlLang() const
00126 {
00127     if (!d->xmlLangParsed) // xmlLang not computed yet
00128     {
00129         QDomElement current = d->element;
00130         
00131         while (!current.isNull())
00132         {
00133             if (current.hasAttributeNS(xmlNamespace(), QString::fromUtf8("lang")))
00134             {
00135                 d->xmlLang = current.attributeNS(xmlNamespace(), QString::fromUtf8("lang"));
00136                 return d->xmlLang;
00137             }
00138             
00139             QDomNode parent = current.parentNode();
00140 
00141             if (!parent.isNull() && parent.isElement())
00142                 current = parent.toElement();
00143             else
00144                 current = QDomElement();
00145         }
00146         d->xmlLangParsed = true;
00147     }
00148     return d->xmlLang;
00149 }
00150         
00151 QString ElementWrapper::extractElementText(const QString& tagName) const
00152 {
00153     QDomElement el = d->element.namedItem(tagName).toElement();
00154     return el.isNull() ? QString() : el.text().trimmed();
00155 }
00156 
00157 QString ElementWrapper::extractElementTextNS(const QString& namespaceURI, const QString& localName) const
00158 {
00159     QDomElement el = firstElementByTagNameNS(namespaceURI, localName);
00160     return el.isNull() ? QString() : el.text().trimmed();
00161 }
00162 
00163 QString ElementWrapper::childNodesAsXML(const QDomElement& parent)
00164 {
00165     ElementWrapper wrapper(parent);
00166     
00167     if (parent.isNull())
00168         return QString();
00169 
00170     QDomNodeList list = parent.childNodes();
00171     
00172     QString str;
00173     QTextStream ts( &str, QIODevice::WriteOnly );
00174     
00175     // if there is a xml:base in our scope, first set it for
00176     // each child element so the xml:base shows up in the
00177     // serialization
00178     QString base = wrapper.xmlBase();
00179 
00180 
00181     for (int i = 0; i < list.count(); ++i)
00182     {
00183         QDomNode it = list.item(i);
00184         if (!base.isEmpty() && it.isElement() 
00185              && !it.toElement().hasAttributeNS(xmlNamespace(), QString::fromUtf8("base")))
00186         {
00187             it.toElement().setAttributeNS(xmlNamespace(), QString::fromUtf8("base"), base);
00188         }
00189             
00190         ts << it;
00191     }
00192     return str.trimmed();
00193 }
00194 
00195 QString ElementWrapper::childNodesAsXML() const
00196 {
00197     return childNodesAsXML(d->element);
00198 }
00199 
00200 QList<QDomElement> ElementWrapper::elementsByTagName(const QString& tagName) const
00201 {
00202     QList<QDomElement> elements;
00203     for (QDomNode n = d->element.firstChild(); !n.isNull(); n = n.nextSibling())
00204     {
00205         if (n.isElement())
00206         {
00207             QDomElement e = n.toElement();
00208             if (e.tagName() == tagName)
00209                 elements.append(e);
00210         }
00211     }
00212     return elements;
00213 }
00214 
00215 QDomElement ElementWrapper::firstElementByTagNameNS(const QString& nsURI, const QString& localName) const
00216 {
00217     if (isNull())
00218         return QDomElement();
00219     
00220     for (QDomNode n = d->element.firstChild(); !n.isNull(); n = n.nextSibling())
00221     {
00222         if (n.isElement())
00223         {
00224             QDomElement e = n.toElement();
00225             if (e.localName() == localName && e.namespaceURI() == nsURI)
00226                 return e;
00227         }
00228     }
00229     
00230     return QDomElement();
00231 }
00232 
00233 
00234 QList<QDomElement> ElementWrapper::elementsByTagNameNS(const QString& nsURI, const QString& localName) const
00235 {
00236     if (isNull())
00237         return QList<QDomElement>();
00238     
00239     QList<QDomElement> elements;
00240     for (QDomNode n = d->element.firstChild(); !n.isNull(); n = n.nextSibling())
00241     {
00242         if (n.isElement())
00243         {
00244             QDomElement e = n.toElement();
00245             if (e.localName() == localName && e.namespaceURI() == nsURI)
00246                 elements.append(e);
00247         }
00248     }
00249     return elements;
00250 }
00251 
00252 QString ElementWrapper::text() const
00253 {
00254     return d->element.text();
00255 }
00256         
00257 QString ElementWrapper::attribute(const QString& name, const QString& defValue) const
00258 {
00259     return d->element.attribute(name, defValue);
00260 }
00261         
00262 QString ElementWrapper::attributeNS(const QString& nsURI, const QString& localName, const QString& defValue) const
00263 {
00264     return d->element.attributeNS(nsURI, localName, defValue);
00265 }
00266 
00267 bool ElementWrapper::hasAttribute(const QString& name) const
00268 {
00269     return d->element.hasAttribute(name);
00270 }
00271 
00272 bool ElementWrapper::hasAttributeNS(const QString& nsURI, const QString& localName) const
00273 {
00274     return d->element.hasAttributeNS(nsURI, localName);
00275 }
00276 
00277 } // 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
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
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