lib

KoXmlReader.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Ariya Hidayat <ariya@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef KOFFICE_XMLREADER
00021 #define KOFFICE_XMLREADER
00022 
00023 // use standard QDom, useful to test KoXml classes against Qt's QDom
00024 //#define KOXML_USE_QDOM
00025 
00026 #include <qdom.h> 
00027 #include <koffice_export.h>
00028 
00029 #ifdef KOXML_USE_QDOM
00030 
00031 #define KoXmlNode QDomNode
00032 #define KoXmlElement QDomElement
00033 #define KoXmlText QDomText
00034 #define KoXmlCDATASection QDomCDATASection
00035 #define KoXmlDocument QDomDocument
00036 
00037 #else
00038 
00039 class QIODevice;
00040 class QString;
00041 class QTextStream;
00042 class QXmlReader;
00043 class QXmlInputSource;
00044 
00045 class KoXmlElement;
00046 class KoXmlText;
00047 class KoXmlCDATASection;
00048 class KoXmlDocument;
00049 class KoXmlNodeData;
00050 
00064 class KOFFICECORE_EXPORT KoXmlNode
00065 {
00066 public:
00067 
00068   enum NodeType 
00069   {
00070     NullNode = 0,
00071     ElementNode,
00072     TextNode,
00073     CDATASectionNode,
00074     ProcessingInstructionNode,
00075     DocumentNode 
00076   };
00077 
00078   KoXmlNode();
00079   KoXmlNode( const KoXmlNode& node );
00080   KoXmlNode& operator=( const KoXmlNode& node );
00081   bool operator== ( const KoXmlNode& ) const;
00082   bool operator!= ( const KoXmlNode& ) const;
00083   virtual ~KoXmlNode();
00084 
00085   virtual KoXmlNode::NodeType nodeType() const;
00086   virtual bool isNull() const;
00087   virtual bool isElement() const;
00088   virtual bool isText() const;
00089   virtual bool isCDATASection() const;
00090   virtual bool isDocument() const;
00091 
00092   void clear();
00093   KoXmlElement toElement();
00094   KoXmlText toText();
00095   KoXmlCDATASection toCDATASection();
00096   KoXmlDocument toDocument();
00097 
00098   virtual QString nodeName() const;
00099   virtual QString namespaceURI() const;
00100   virtual QString prefix() const;
00101   virtual QString localName() const;
00102 
00103   KoXmlDocument ownerDocument() const;
00104   KoXmlNode parentNode() const;
00105 
00106   bool hasChildNodes() const;
00107   KoXmlNode firstChild() const;
00108   KoXmlNode lastChild() const;
00109   KoXmlNode nextSibling() const;
00110   KoXmlNode previousSibling() const;
00111 
00112   KoXmlNode namedItem( const QString& name ) const;
00113   KoXmlNode namedItemNS( const QString& nsURI, const QString& name ) const;
00114 
00120   void load( int depth=1 );
00121 
00125   void unload();
00126 
00127 protected:
00128   KoXmlNodeData* d;
00129   KoXmlNode( KoXmlNodeData* );
00130 };
00131 
00140 class KOFFICECORE_EXPORT KoXmlElement: public KoXmlNode
00141 {
00142 public:
00143   KoXmlElement();
00144   KoXmlElement( const KoXmlElement& element );
00145   KoXmlElement& operator=( const KoXmlElement& element );
00146   virtual ~KoXmlElement();
00147   bool operator== ( const KoXmlElement& ) const;
00148   bool operator!= ( const KoXmlElement& ) const;
00149 
00150   QString tagName() const;
00151   QString text() const;
00152   virtual bool isElement() const;
00153 
00154   QString attribute( const QString& name ) const;
00155   QString attribute( const QString& name, const QString& defaultValue ) const;
00156   QString attributeNS( const QString& namespaceURI, const QString& localName, 
00157     const QString& defaultValue ) const;
00158   bool hasAttribute( const QString& name ) const;
00159   bool hasAttributeNS( const QString& namespaceURI, const QString& localName ) const;
00160 
00161 private:
00162   friend class KoXmlNode;
00163   friend class KoXmlDocument;
00164   KoXmlElement( KoXmlNodeData* );
00165 };
00166 
00171 class KOFFICECORE_EXPORT KoXmlText: public KoXmlNode
00172 {
00173 public:
00174   KoXmlText();
00175   KoXmlText( const KoXmlText& text );
00176   KoXmlText& operator=( const KoXmlText& text );
00177   virtual ~KoXmlText();
00178 
00179   QString data() const;
00180   void setData( const QString& data );
00181   virtual bool isText() const;
00182 
00183 private:
00184   friend class KoXmlNode;
00185   friend class KoXmlDocument;
00186   KoXmlText( KoXmlNodeData* );
00187 };
00188 
00193 class KOFFICECORE_EXPORT KoXmlCDATASection: public KoXmlText
00194 {
00195 public:
00196   KoXmlCDATASection();
00197   KoXmlCDATASection( const KoXmlCDATASection& cdata );
00198   KoXmlCDATASection& operator=( const KoXmlCDATASection& cdata );
00199   virtual ~KoXmlCDATASection();
00200 
00201   virtual bool isCDATASection() const;
00202 
00203 private:
00204   friend class KoXmlNode;
00205   friend class KoXmlDocument;
00206   KoXmlCDATASection( KoXmlNodeData* );
00207 };
00208 
00221 class KOFFICECORE_EXPORT KoXmlDocument: public KoXmlNode
00222 {
00223 public:
00224   KoXmlDocument();
00225   KoXmlDocument( const KoXmlDocument& node );
00226   KoXmlDocument& operator=( const KoXmlDocument& node );
00227   bool operator==( const KoXmlDocument& ) const;
00228   bool operator!=( const KoXmlDocument& ) const;
00229   virtual ~KoXmlDocument();
00230 
00231   virtual bool isDocument() const;
00232 
00233   KoXmlElement documentElement() const;
00234 
00235   void setFastLoading( bool f );
00236   bool fastLoading() const;
00237 
00238   bool setContent( QIODevice* device, bool namespaceProcessing, 
00239     QString* errorMsg = 0, int* errorLine = 0, int* errorColumn = 0 );
00240   bool setContent( QIODevice* device, 
00241     QString* errorMsg = 0, int* errorLine = 0, int* errorColumn = 0 );
00242   bool setContent( QXmlInputSource *source, QXmlReader *reader, 
00243     QString* errorMsg = 0, int* errorLine = 0, int* errorColumn = 0 );
00244 
00245 //  bool setContent( const QCString& text, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
00246 //  bool setContent( const QByteArray& text, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
00247 //  bool setContent( const QString& text, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
00248 //  bool setContent( QIODevice* dev, bool namespaceProcessing, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
00249 //  bool setContent( const QCString& text, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0 );
00250 //  bool setContent( const QByteArray& text, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
00251 //  bool setContent( const QString& text, QString *errorMsg=0, int *errorLine=0, 
00252 //    int *errorColumn=0  );
00253 
00254     
00255 private:
00256   friend class KoXmlNode;
00257   KoXmlDocument( KoXmlNodeData* );
00258 };
00259 
00260 #endif // KOXML_USE_QDOM
00261 
00288 namespace KoXml {
00289 
00298     KOFFICECORE_EXPORT KoXmlElement namedItemNS( const KoXmlNode& node, 
00299         const char* nsURI, const char* localName );
00300 
00305     KOFFICECORE_EXPORT void load( KoXmlNode& node, int depth = 1 );
00306 
00311     KOFFICECORE_EXPORT void unload( KoXmlNode& node );
00312 
00313 }
00314 
00315 #define forEachElement( elem, parent ) \
00316       for ( KoXmlNode _node = parent.firstChild(); !_node.isNull(); _node = _node.nextSibling() ) \
00317         if ( !( elem = _node.toElement() ).isNull() )
00318 
00319 
00320 #endif // KOFFICE_XMLREADER
KDE Home | KDE Accessibility Home | Description of Access Keys