summaryrefslogtreecommitdiffstats
path: root/lib/kofficecore/KoXmlReader.h
blob: d450539814be2465d662777b3d12a55b5312d8a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* This file is part of the KDE project
   Copyright (C) 2005 Ariya Hidayat <ariya@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef KOFFICE_XMLREADER
#define KOFFICE_XMLREADER

// use standard TQDom, useful to test KoXml classes against TQt's TQDom
//#define KOXML_USE_TQDOM

#include <tqdom.h> 
#include <koffice_export.h>

#ifdef KOXML_USE_TQDOM

#define KoXmlNode TQDomNode
#define KoXmlElement TQDomElement
#define KoXmlText TQDomText
#define KoXmlCDATASection TQDomCDATASection
#define KoXmlDocument TQDomDocument

#else

class TQIODevice;
class TQString;
class TQTextStream;
class TQXmlReader;
class TQXmlInputSource;

class KoXmlElement;
class KoXmlText;
class KoXmlCDATASection;
class KoXmlDocument;
class KoXmlNodeData;

/**
 * KoXmlNode represents a node in a DOM tree.
 *
 * KoXmlNode is a base class for KoXmlElement, KoXmlText.
 * Often, these subclasses are used for getting the data instead of KoXmlNode.
 * However, as base class, KoXmlNode is very helpful when for example iterating 
 * all child nodes within one tqparent node.
 *
 * KoXmlNode implements an explicit sharing, a node shares its data with 
 * other copies (if exist).
 *
 * @author Ariya Hidayat <ariya@kde.org>
 */
class KOFFICECORE_EXPORT KoXmlNode
{
public:

  enum NodeType 
  {
    NullNode = 0,
    ElementNode,
    TextNode,
    CDATASectionNode,
    ProcessingInstructionNode,
    DocumentNode 
  };

  KoXmlNode();
  KoXmlNode( const KoXmlNode& node );
  KoXmlNode& operator=( const KoXmlNode& node );
  bool operator== ( const KoXmlNode& ) const;
  bool operator!= ( const KoXmlNode& ) const;
  virtual ~KoXmlNode();

  virtual KoXmlNode::NodeType nodeType() const;
  virtual bool isNull() const;
  virtual bool isElement() const;
  virtual bool isText() const;
  virtual bool isCDATASection() const;
  virtual bool isDocument() const;

  void clear();
  KoXmlElement toElement();
  KoXmlText toText();
  KoXmlCDATASection toCDATASection();
  KoXmlDocument toDocument();

  virtual TQString nodeName() const;
  virtual TQString namespaceURI() const;
  virtual TQString prefix() const;
  virtual TQString localName() const;

  KoXmlDocument ownerDocument() const;
  KoXmlNode parentNode() const;

  bool hasChildNodes() const;
  KoXmlNode firstChild() const;
  KoXmlNode lastChild() const;
  KoXmlNode nextSibling() const;
  KoXmlNode previousSibling() const;

  KoXmlNode namedItem( const TQString& name ) const;
  KoXmlNode namedItemNS( const TQString& nsURI, const TQString& name ) const;

  /**
   * Loads all child nodes (if any) of this node. Normally you do not need
   * to call this function as the child nodes will be automatically 
   * loaded when necessary.
   */
  void load( int depth=1 );

  /**
   * Releases all child nodes of this node. 
   */
  void unload();

protected:
  KoXmlNodeData* d;
  KoXmlNode( KoXmlNodeData* );
};

/**
 * KoXmlElement represents a tag element in a DOM tree.
 *
 * KoXmlElement holds information about an XML tag, along with its attributes.
 *
 * @author Ariya Hidayat <ariya@kde.org>
 */

class KOFFICECORE_EXPORT KoXmlElement: public KoXmlNode
{
public:
  KoXmlElement();
  KoXmlElement( const KoXmlElement& element );
  KoXmlElement& operator=( const KoXmlElement& element );
  virtual ~KoXmlElement();
  bool operator== ( const KoXmlElement& ) const;
  bool operator!= ( const KoXmlElement& ) const;

  TQString tagName() const;
  TQString text() const;
  virtual bool isElement() const;

  TQString attribute( const TQString& name ) const;
  TQString attribute( const TQString& name, const TQString& defaultValue ) const;
  TQString attributeNS( const TQString& namespaceURI, const TQString& localName, 
    const TQString& defaultValue ) const;
  bool hasAttribute( const TQString& name ) const;
  bool hasAttributeNS( const TQString& namespaceURI, const TQString& localName ) const;

private:
  friend class KoXmlNode;
  friend class KoXmlDocument;
  KoXmlElement( KoXmlNodeData* );
};

/**
 * KoXmlText represents a text in a DOM tree.
 * @author Ariya Hidayat <ariya@kde.org>
 */
class KOFFICECORE_EXPORT KoXmlText: public KoXmlNode
{
public:
  KoXmlText();
  KoXmlText( const KoXmlText& text );
  KoXmlText& operator=( const KoXmlText& text );
  virtual ~KoXmlText();

  TQString data() const;
  void setData( const TQString& data );
  virtual bool isText() const;

private:
  friend class KoXmlNode;
  friend class KoXmlDocument;
  KoXmlText( KoXmlNodeData* );
};

/**
 * KoXmlCDATASection represents a CDATA section in a DOM tree.
 * @author Ariya Hidayat <ariya@kde.org>
 */
class KOFFICECORE_EXPORT KoXmlCDATASection: public KoXmlText
{
public:
  KoXmlCDATASection();
  KoXmlCDATASection( const KoXmlCDATASection& cdata );
  KoXmlCDATASection& operator=( const KoXmlCDATASection& cdata );
  virtual ~KoXmlCDATASection();

  virtual bool isCDATASection() const;

private:
  friend class KoXmlNode;
  friend class KoXmlDocument;
  KoXmlCDATASection( KoXmlNodeData* );
};

/**
 * KoXmlDocument represents an XML document, structured in a DOM tree.
 *
 * KoXmlDocument is designed to be memory efficient. Unlike TQDomDocument from 
 * TQt's XML module, KoXmlDocument does not store all nodes in the DOM tree.
 * Some nodes will be loaded and parsed on-demand only.
 *
 * KoXmlDocument is read-only, you can not modify its content.
 *
 * @author Ariya Hidayat <ariya@kde.org>
 */

class KOFFICECORE_EXPORT KoXmlDocument: public KoXmlNode
{
public:
  KoXmlDocument();
  KoXmlDocument( const KoXmlDocument& node );
  KoXmlDocument& operator=( const KoXmlDocument& node );
  bool operator==( const KoXmlDocument& ) const;
  bool operator!=( const KoXmlDocument& ) const;
  virtual ~KoXmlDocument();

  virtual bool isDocument() const;

  KoXmlElement documentElement() const;

  void setFastLoading( bool f );
  bool fastLoading() const;

  bool setContent( TQIODevice* device, bool namespaceProcessing, 
    TQString* errorMsg = 0, int* errorLine = 0, int* errorColumn = 0 );
  bool setContent( TQIODevice* device, 
    TQString* errorMsg = 0, int* errorLine = 0, int* errorColumn = 0 );
  bool setContent( TQXmlInputSource *source, TQXmlReader *reader, 
    TQString* errorMsg = 0, int* errorLine = 0, int* errorColumn = 0 );

//  bool setContent( const TQCString& text, bool namespaceProcessing, TQString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
//  bool setContent( const TQByteArray& text, bool namespaceProcessing, TQString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
//  bool setContent( const TQString& text, bool namespaceProcessing, TQString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
//  bool setContent( TQIODevice* dev, bool namespaceProcessing, TQString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
//  bool setContent( const TQCString& text, TQString *errorMsg=0, int *errorLine=0, int *errorColumn=0 );
//  bool setContent( const TQByteArray& text, TQString *errorMsg=0, int *errorLine=0, int *errorColumn=0  );
//  bool setContent( const TQString& text, TQString *errorMsg=0, int *errorLine=0, 
//    int *errorColumn=0  );

    
private:
  friend class KoXmlNode;
  KoXmlDocument( KoXmlNodeData* );
};

#endif // KOXML_USE_TQDOM

/**
 * This namespace contains a few convenience functions to simplify code using TQDom
 * (when loading OASIS documents, in particular).
 *
 * To find the child element with a given name, use KoXml::namedItemNS.
 *
 * To find all child elements with a given name, use
 * TQDomElement e;
 * forEachElement( e, tqparent )
 * {
 *     if ( e.localName() == "..." && e.namespaceURI() == KoXmlNS::... )
 *     {
 *         ...
 *     }
 * }
 * Note that this means you don't ever need to use TQDomNode nor toElement anymore!
 * Also note that localName is the part without the prefix, this is the whole point
 * of namespace-aware methods.
 *
 * To find the attribute with a given name, use TQDomElement::attributeNS.
 *
 * Do not use getElementsByTagNameNS, it's recursive (which is never needed in KOffice).
 * Do not use tagName() or nodeName() or prefix(), since the prefix isn't fixed.
 *
 * @author David Faure <faure@kde.org>
 */
namespace KoXml {

    /**
     * A namespace-aware version of TQDomNode::namedItem(),
     * which also takes care of casting to a TQDomElement.
     * Use this when a domelement is known to have only *one* child element
     * with a given tagname.
     *
     * Note: do *NOT* use getElementsByTagNameNS, it's recursive!
     */
    KOFFICECORE_EXPORT KoXmlElement namedItemNS( const KoXmlNode& node, 
        const char* nsURI, const char* localName );

    /**
     * Explicitly load child nodes of specified node, up to given depth.
     * This function has no effect if TQDom is used.
     */
    KOFFICECORE_EXPORT void load( KoXmlNode& node, int depth = 1 );

    /**
     * Unload child nodes of specified node.
     * This function has no effect if TQDom is used.
     */
    KOFFICECORE_EXPORT void unload( KoXmlNode& node );

}

#define forEachElement( elem, tqparent ) \
      for ( KoXmlNode _node = tqparent.firstChild(); !_node.isNull(); _node = _node.nextSibling() ) \
        if ( !( elem = _node.toElement() ).isNull() )


#endif // KOFFICE_XMLREADER