F:/KPlato/koffice/libs/store/KoXmlReader.h

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2005-2006 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 <koffice_export.h>
00027 
00028 #include <qxml.h>
00029 #include <qdom.h>
00030 
00031 class QIODevice;
00032 class QTextDecoder;
00033 
00034 #ifdef KOXML_USE_QDOM
00035 
00036 typedef QDomNode KoXmlNode;
00037 typedef QDomElement KoXmlElement;
00038 typedef QDomText KoXmlText;
00039 typedef QDomCDATASection KoXmlCDATASection;
00040 typedef QDomDocument KoXmlDocument;
00041 
00042 #else
00043 
00044 class QString;
00045 class QXmlReader;
00046 class QXmlInputSource;
00047 
00048 class KoXmlElement;
00049 class KoXmlText;
00050 class KoXmlCDATASection;
00051 class KoXmlDocument;
00052 class KoXmlNodeData;
00053 
00067 class KOSTORE_EXPORT KoXmlNode
00068 {
00069 public:
00070 
00071   enum NodeType 
00072   {
00073     NullNode = 0,
00074     ElementNode,
00075     TextNode,
00076     CDATASectionNode,
00077     ProcessingInstructionNode,
00078     DocumentNode 
00079   };
00080 
00081   KoXmlNode();
00082   KoXmlNode( const KoXmlNode& node );
00083   KoXmlNode& operator=( const KoXmlNode& node );
00084   bool operator== ( const KoXmlNode& ) const;
00085   bool operator!= ( const KoXmlNode& ) const;
00086   virtual ~KoXmlNode();
00087 
00088   virtual KoXmlNode::NodeType nodeType() const;
00089   virtual bool isNull() const;
00090   virtual bool isElement() const;
00091   virtual bool isText() const;
00092   virtual bool isCDATASection() const;
00093   virtual bool isDocument() const;
00094 
00095   virtual void clear();
00096   KoXmlElement toElement() const;
00097   KoXmlText toText() const;
00098   KoXmlCDATASection toCDATASection() const;
00099   KoXmlDocument toDocument() const;
00100 
00101   virtual QString nodeName() const;
00102   virtual QString namespaceURI() const;
00103   virtual QString prefix() const;
00104   virtual QString localName() const;
00105 
00106   KoXmlDocument ownerDocument() const;
00107   KoXmlNode parentNode() const;
00108 
00109   bool hasChildNodes() const;
00110   KoXmlNode firstChild() const;
00111   KoXmlNode lastChild() const;
00112   KoXmlNode nextSibling() const;
00113   KoXmlNode previousSibling() const;
00114   
00115   // equivalen to node.childNodes().count() if node is a QDomNode instance
00116   int childNodesCount() const;
00117 
00118   KoXmlNode namedItem( const QString& name ) const;
00119   KoXmlNode namedItemNS( const QString& nsURI, const QString& name ) const;
00120 
00126   void load( int depth=1 );
00127 
00131   void unload();
00132 
00133   // compatibility
00134   QDomNode asQDomNode( QDomDocument ownerDoc ) const;
00135 
00136 protected:
00137   KoXmlNodeData* d;
00138   KoXmlNode( KoXmlNodeData* );
00139 };
00140 
00149 class KOSTORE_EXPORT KoXmlElement: public KoXmlNode
00150 {
00151 public:
00152   KoXmlElement();
00153   KoXmlElement( const KoXmlElement& element );
00154   KoXmlElement& operator=( const KoXmlElement& element );
00155   virtual ~KoXmlElement();
00156   bool operator== ( const KoXmlElement& ) const;
00157   bool operator!= ( const KoXmlElement& ) const;
00158 
00159   QString tagName() const;
00160   QString text() const;
00161 
00162   QString attribute( const QString& name ) const;
00163   QString attribute( const QString& name, const QString& defaultValue ) const;
00164   QString attributeNS( const QString& namespaceURI, const QString& localName, 
00165     const QString& defaultValue ) const;
00166   bool hasAttribute( const QString& name ) const;
00167   bool hasAttributeNS( const QString& namespaceURI, const QString& localName ) const;
00168 
00169 private:
00170   friend class KoXmlNode;
00171   friend class KoXmlDocument;
00172   KoXmlElement( KoXmlNodeData* );
00173 };
00174 
00179 class KOSTORE_EXPORT KoXmlText: public KoXmlNode
00180 {
00181 public:
00182   KoXmlText();
00183   KoXmlText( const KoXmlText& text );
00184   KoXmlText& operator=( const KoXmlText& text );
00185   virtual ~KoXmlText();
00186 
00187   QString data() const;
00188   void setData( const QString& data );
00189   virtual bool isText() const;
00190 
00191 private:
00192   friend class KoXmlNode;
00193   friend class KoXmlCDATASection;
00194   friend class KoXmlDocument;
00195   KoXmlText( KoXmlNodeData* );
00196 };
00197 
00202 class KOSTORE_EXPORT KoXmlCDATASection: public KoXmlText
00203 {
00204 public:
00205   KoXmlCDATASection();
00206   KoXmlCDATASection( const KoXmlCDATASection& cdata );
00207   KoXmlCDATASection& operator=( const KoXmlCDATASection& cdata );
00208   virtual ~KoXmlCDATASection();
00209 
00210   virtual bool isCDATASection() const;
00211 
00212 private:
00213   friend class KoXmlNode;
00214   friend class KoXmlDocument;
00215   KoXmlCDATASection( KoXmlNodeData* );
00216 };
00217 
00230 class KOSTORE_EXPORT KoXmlDocument: public KoXmlNode
00231 {
00232 public:
00233   KoXmlDocument();
00234   KoXmlDocument( const KoXmlDocument& node );
00235   KoXmlDocument& operator=( const KoXmlDocument& node );
00236   bool operator==( const KoXmlDocument& ) const;
00237   bool operator!=( const KoXmlDocument& ) const;
00238   virtual ~KoXmlDocument();
00239 
00240   KoXmlElement documentElement() const;
00241 
00242   virtual QString nodeName() const;
00243   virtual void clear();
00244 
00245   bool setContent( QIODevice* device, bool namespaceProcessing, 
00246     QString* errorMsg = 0, int* errorLine = 0, int* errorColumn = 0 );
00247   bool setContent( QIODevice* device, 
00248     QString* errorMsg = 0, int* errorLine = 0, int* errorColumn = 0 );
00249   bool setContent( QXmlInputSource *source, QXmlReader *reader, 
00250     QString* errorMsg = 0, int* errorLine = 0, int* errorColumn = 0 );
00251   bool setContent( const QByteArray& text, bool namespaceProcessing,
00252     QString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );    
00253   bool setContent( const QString& text, bool namespaceProcessing, 
00254     QString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
00255     
00256   // no namespace processing
00257   bool setContent( const QString& text, 
00258     QString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
00259 
00260 private:
00261   friend class KoXmlNode;
00262   KoXmlDocument( KoXmlNodeData* );
00263 };
00264 
00265 #endif // KOXML_USE_QDOM
00266 
00267 class KOSTORE_EXPORT KoXmlInputSource: public QXmlInputSource
00268 {
00269 public:
00270   KoXmlInputSource(QIODevice *dev);
00271   ~KoXmlInputSource();
00272 
00273   virtual void setData(const QString& dat);
00274   virtual void setData(const QByteArray& dat);
00275   virtual void fetchData();
00276   virtual QString data() const;
00277   virtual QChar next();
00278   virtual void reset();
00279 
00280 protected:
00281   virtual QString fromRawData(const QByteArray &data, bool beginning = false);
00282     
00283 private:
00284   QIODevice* device;
00285   QTextDecoder* decoder;
00286   QString stringData;
00287   int stringLength;
00288   int stringIndex;
00289   char* buffer;
00290 };
00291 
00318 namespace KoXml {
00319 
00328     KOSTORE_EXPORT KoXmlElement namedItemNS( const KoXmlNode& node, 
00329         const char* nsURI, const char* localName );
00330 
00335     KOSTORE_EXPORT void load( KoXmlNode& node, int depth = 1 );
00336 
00341     KOSTORE_EXPORT void unload( KoXmlNode& node );
00342     
00346     KOSTORE_EXPORT int childNodesCount( const KoXmlNode& node );
00347     
00352         KOSTORE_EXPORT QDomNode asQDomNode( QDomDocument ownerDoc, const KoXmlNode& node );
00353         KOSTORE_EXPORT QDomElement asQDomElement( QDomDocument ownerDoc, const KoXmlElement& element );
00354         KOSTORE_EXPORT QDomDocument asQDomDocument( QDomDocument ownerDoc, const KoXmlDocument& document );
00355 
00356     /*
00357      * Load an XML document from specified device to a document. You can of 
00358      * course use it with QFile (which inherits QIODevice).     
00359      * This is much more memory efficient than standard QDomDocument::setContent
00360      * because the data from the device is buffered, unlike 
00361      * QDomDocument::setContent which just loads everything in memory.
00362      * 
00363      * Note: it is assumed that the XML uses UTF-8 encoding.          
00364      */     
00365     KOSTORE_EXPORT bool setDocument( KoXmlDocument& doc, QIODevice* device,
00366       bool namespaceProcessing, QString* errorMsg = 0, 
00367           int* errorLine = 0, int* errorColumn = 0 );
00368 
00369     KOSTORE_EXPORT bool setDocument( KoXmlDocument& doc, QIODevice* device,
00370       QXmlSimpleReader* reader, QString* errorMsg = 0, 
00371           int* errorLine = 0, int* errorColumn = 0 );
00372 }
00373 
00374 #define forEachElement( elem, parent ) \
00375       for ( KoXmlNode _node = parent.firstChild(); !_node.isNull(); _node = _node.nextSibling() ) \
00376         if ( !( elem = _node.toElement() ).isNull() )
00377 
00378 
00379 #endif // KOFFICE_XMLREADER

Généré le Wed Nov 22 23:41:14 2006 pour KPlato par  doxygen 1.5.1-p1