]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/xml/xml.h
Fix memory leaks in wxXml unit test.
[wxWidgets.git] / interface / wx / xml / xml.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: xml/xml.h
f41d6c8c 3// Purpose: interface of wxXmlNode, wxXmlAttribute, wxXmlDocument
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
526954c5 6// Licence: wxWindows licence
23324ae1
FM
7/////////////////////////////////////////////////////////////////////////////
8
f41d6c8c
FM
9
10/// Represents XML node type.
11enum wxXmlNodeType
12{
13 // note: values are synchronized with xmlElementType from libxml
14 wxXML_ELEMENT_NODE = 1,
15 wxXML_ATTRIBUTE_NODE = 2,
16 wxXML_TEXT_NODE = 3,
17 wxXML_CDATA_SECTION_NODE = 4,
18 wxXML_ENTITY_REF_NODE = 5,
19 wxXML_ENTITY_NODE = 6,
20 wxXML_PI_NODE = 7,
21 wxXML_COMMENT_NODE = 8,
22 wxXML_DOCUMENT_NODE = 9,
23 wxXML_DOCUMENT_TYPE_NODE = 10,
24 wxXML_DOCUMENT_FRAG_NODE = 11,
25 wxXML_NOTATION_NODE = 12,
26 wxXML_HTML_DOCUMENT_NODE = 13
27};
28
23324ae1
FM
29/**
30 @class wxXmlNode
7c913512 31
23324ae1 32 Represents a node in an XML document. See wxXmlDocument.
7c913512 33
f41d6c8c
FM
34 Node has a name and may have content and attributes.
35
36 Most common node types are @c wxXML_TEXT_NODE (name and attributes are irrelevant)
4ce846b1
FM
37 and @c wxXML_ELEMENT_NODE.
38
39 Example: in <tt>\<title\>hi\</title\></tt> there is an element with the name
40 @c title and irrelevant content and one child of type @c wxXML_TEXT_NODE
41 with @c hi as content.
7c913512 42
23324ae1
FM
43 If @c wxUSE_UNICODE is 0, all strings are encoded in the encoding given to
44 wxXmlDocument::Load (default is UTF-8).
7c913512 45
23324ae1
FM
46 @library{wxxml}
47 @category{xml}
7c913512 48
e54c96f1 49 @see wxXmlDocument, wxXmlAttribute
23324ae1 50*/
7c913512 51class wxXmlNode
23324ae1
FM
52{
53public:
23324ae1 54 /**
f41d6c8c
FM
55 Creates this XML node and eventually insert it into an existing XML tree.
56
57 @param parent
58 The parent node to which append this node instance.
59 If this argument is @NULL this new node will be floating and it can
60 be appended later to another one using the AddChild() or InsertChild()
61 functions. Otherwise the child is already added to the XML tree by
62 this constructor and it shouldn't be done again.
63 @param type
64 One of the ::wxXmlNodeType enumeration value.
65 @param name
66 The name of the node. This is the string which appears between angular brackets.
67 @param content
68 The content of the node.
69 Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE.
70 @param attrs
71 If not @NULL, this wxXmlAttribute object and its eventual siblings are attached to the node.
72 @param next
73 If not @NULL, this node and its eventual siblings are attached to the node.
74 @param lineNo
75 Number of line this node was present at in input file or -1.
23324ae1
FM
76 */
77 wxXmlNode(wxXmlNode* parent, wxXmlNodeType type,
78 const wxString& name,
79 const wxString& content = wxEmptyString,
4cc4bfaf
FM
80 wxXmlAttribute* attrs = NULL,
81 wxXmlNode* next = NULL, int lineNo = -1);
f41d6c8c
FM
82
83 /**
84 A simplified version of the first constructor form, assuming a @NULL parent.
85
86 @param type
87 One of the ::wxXmlNodeType enumeration value.
88 @param name
89 The name of the node. This is the string which appears between angular brackets.
90 @param content
91 The content of the node.
92 Only meaningful when type is @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE.
93 @param lineNo
94 Number of line this node was present at in input file or -1.
95 */
7c913512
FM
96 wxXmlNode(wxXmlNodeType type, const wxString& name,
97 const wxString& content = wxEmptyString,
98 int lineNo = -1);
f41d6c8c
FM
99
100 /**
101 Copy constructor.
102
57ab6f23 103 Note that this does NOT copy siblings and parent pointer, i.e. GetParent()
f41d6c8c
FM
104 and GetNext() will return @NULL after using copy ctor and are never unmodified by operator=().
105 On the other hand, it DOES copy children and attributes.
106 */
107 wxXmlNode(const wxXmlNode& node);
23324ae1
FM
108
109 /**
110 The virtual destructor. Deletes attached children and attributes.
111 */
adaaa686 112 virtual ~wxXmlNode();
23324ae1 113
23324ae1 114 /**
f41d6c8c
FM
115 Appends a attribute with given @a name and @a value to the list of
116 attributes for this node.
23324ae1 117 */
adaaa686 118 virtual void AddAttribute(const wxString& name, const wxString& value);
f41d6c8c
FM
119
120 /**
121 Appends given attribute to the list of attributes for this node.
122 */
adaaa686 123 virtual void AddAttribute(wxXmlAttribute* attr);
23324ae1
FM
124
125 /**
43a302f2
VS
126 Adds node @a child as the last child of this node.
127
128 @note
129 Note that this function works in O(n) time where @e n is the number
130 of existing children. Consequently, adding large number of child
131 nodes using this method can be expensive, because it has O(n^2) time
132 complexity in number of nodes to be added. Use InsertChildAfter() to
133 populate XML tree in linear time.
134
135 @see InsertChild(), InsertChildAfter()
23324ae1 136 */
adaaa686 137 virtual void AddChild(wxXmlNode* child);
23324ae1
FM
138
139 /**
4cc4bfaf 140 Removes the first attributes which has the given @a name from the list of
23324ae1
FM
141 attributes for this node.
142 */
adaaa686 143 virtual bool DeleteAttribute(const wxString& name);
23324ae1 144
f41d6c8c
FM
145 /**
146 Returns true if a attribute named attrName could be found.
147 The value of that attribute is saved in value (which must not be @NULL).
148 */
149 bool GetAttribute(const wxString& attrName, wxString* value) const;
150
23324ae1 151 /**
4cc4bfaf
FM
152 Returns the value of the attribute named @a attrName if it does exist.
153 If it does not exist, the @a defaultVal is returned.
23324ae1 154 */
f41d6c8c
FM
155 wxString GetAttribute(const wxString& attrName,
156 const wxString& defaultVal = wxEmptyString) const;
23324ae1
FM
157
158 /**
159 Return a pointer to the first attribute of this node.
160 */
328f5751 161 wxXmlAttribute* GetAttributes() const;
23324ae1
FM
162
163 /**
164 Returns the first child of this node.
165 To get a pointer to the second child of this node (if it does exist), use the
166 GetNext() function on the returned value.
167 */
328f5751 168 wxXmlNode* GetChildren() const;
23324ae1
FM
169
170 /**
171 Returns the content of this node. Can be an empty string.
172 Be aware that for nodes of type @c wxXML_ELEMENT_NODE (the most used node type)
f41d6c8c 173 the content is an empty string. See GetNodeContent() for more details.
23324ae1 174 */
95b4a59e 175 const wxString& GetContent() const;
23324ae1
FM
176
177 /**
57ab6f23 178 Returns the number of nodes which separate this node from @c grandparent.
f41d6c8c
FM
179
180 This function searches only the parents of this node until it finds
181 @a grandparent or the @NULL node (which is the parent of non-linked
182 nodes or the parent of a wxXmlDocument's root node).
23324ae1 183 */
328f5751 184 int GetDepth(wxXmlNode* grandparent = NULL) const;
23324ae1 185
30f6914b
JS
186 /**
187 Returns a flag indicating whether encoding conversion is necessary when saving. The default is @false.
188
189 You can improve saving efficiency considerably by setting this value.
190 */
191 bool GetNoConversion() const;
192
23324ae1 193 /**
f41d6c8c 194 Returns line number of the node in the input XML file or @c -1 if it is unknown.
23324ae1 195 */
328f5751 196 int GetLineNumber() const;
23324ae1
FM
197
198 /**
f41d6c8c
FM
199 Returns the name of this node.
200 Can be an empty string (e.g. for nodes of type @c wxXML_TEXT_NODE or
201 @c wxXML_CDATA_SECTION_NODE).
23324ae1 202 */
95b4a59e 203 const wxString& GetName() const;
23324ae1
FM
204
205 /**
206 Returns a pointer to the sibling of this node or @NULL if there are no
207 siblings.
208 */
328f5751 209 wxXmlNode* GetNext() const;
23324ae1
FM
210
211 /**
f41d6c8c
FM
212 Returns the content of the first child node of type @c wxXML_TEXT_NODE
213 or @c wxXML_CDATA_SECTION_NODE.
214 This function is very useful since the XML snippet @c "tagnametagcontent/tagname"
215 is represented by expat with the following tag tree:
216
217 @code
218 wxXML_ENTITY_NODE name="tagname", content=""
219 |-- wxXML_TEXT_NODE name="", content="tagcontent"
220 @endcode
b5cc5cbd 221
23324ae1 222 or eventually:
b5cc5cbd 223
f41d6c8c
FM
224 @code
225 wxXML_ENTITY_NODE name="tagname", content=""
226 |-- wxXML_CDATA_SECTION_NODE name="", content="tagcontent"
227 @endcode
228
229 An empty string is returned if the node has no children of type
230 @c wxXML_TEXT_NODE or @c wxXML_CDATA_SECTION_NODE, or if the content
231 of the first child of such types is empty.
23324ae1 232 */
328f5751 233 wxString GetNodeContent() const;
23324ae1
FM
234
235 /**
236 Returns a pointer to the parent of this node or @NULL if this node has no
237 parent.
238 */
328f5751 239 wxXmlNode* GetParent() const;
23324ae1
FM
240
241 /**
242 Returns the type of this node.
243 */
328f5751 244 wxXmlNodeType GetType() const;
23324ae1
FM
245
246 /**
f41d6c8c 247 Returns @true if this node has a attribute named @a attrName.
23324ae1 248 */
328f5751 249 bool HasAttribute(const wxString& attrName) const;
23324ae1
FM
250
251 /**
5e05df3c
VS
252 Inserts the @a child node immediately before @a followingNode in the
253 children list.
254
255 @return @true if @a followingNode has been found and the @a child
256 node has been inserted.
257
258 @note
259 For historical reasons, @a followingNode may be @NULL. In that case,
260 then @a child is prepended to the list of children and becomes the
261 first child of this node, i.e. it behaves identically to using the
262 first children (as returned by GetChildren()) for @a followingNode).
43a302f2
VS
263
264 @see AddChild(), InsertChildAfter()
5e05df3c 265 */
adaaa686 266 virtual bool InsertChild(wxXmlNode* child, wxXmlNode* followingNode);
23324ae1 267
43a302f2
VS
268 /**
269 Inserts the @a child node immediately after @a precedingNode in the
270 children list.
271
272 @return @true if @a precedingNode has been found and the @a child
273 node has been inserted.
274
77bfb902
FM
275 @param child
276 The child to insert.
43a302f2
VS
277 @param precedingNode
278 The node to insert @a child after. As a special case, this can be
279 @NULL if this node has no children yet -- in that case, @a child
280 will become this node's only child node.
281
282 @since 2.8.8
283
284 @see InsertChild(), AddChild()
285 */
adaaa686 286 virtual bool InsertChildAfter(wxXmlNode* child, wxXmlNode* precedingNode);
43a302f2 287
23324ae1
FM
288 /**
289 Returns @true if the content of this node is a string containing only
f41d6c8c
FM
290 whitespaces (spaces, tabs, new lines, etc).
291
292 Note that this function is locale-independent since the parsing of XML
293 documents must always produce the exact same tree regardless of the
294 locale it runs under.
23324ae1 295 */
328f5751 296 bool IsWhitespaceOnly() const;
23324ae1
FM
297
298 /**
f41d6c8c
FM
299 Removes the given node from the children list.
300
301 Returns @true if the node was found and removed or @false if the node
302 could not be found.
57ab6f23 303 Note that the caller is responsible for deleting the removed node in order
f41d6c8c 304 to avoid memory leaks.
23324ae1 305 */
adaaa686 306 virtual bool RemoveChild(wxXmlNode* child);
23324ae1
FM
307
308 /**
309 Sets as first attribute the given wxXmlAttribute object.
f41d6c8c 310
30f6914b 311 The caller is responsible for deleting any previously present attributes
f41d6c8c 312 attached to this node.
23324ae1
FM
313 */
314 void SetAttributes(wxXmlAttribute* attr);
315
316 /**
f41d6c8c
FM
317 Sets as first child the given node.
318
30f6914b 319 The caller is responsible for deleting any previously present children node.
23324ae1
FM
320 */
321 void SetChildren(wxXmlNode* child);
322
323 /**
324 Sets the content of this node.
325 */
326 void SetContent(const wxString& con);
327
328 /**
329 Sets the name of this node.
330 */
331 void SetName(const wxString& name);
332
333 /**
f41d6c8c
FM
334 Sets as sibling the given node.
335
30f6914b 336 The caller is responsible for deleting any previously present sibling node.
23324ae1
FM
337 */
338 void SetNext(wxXmlNode* next);
339
30f6914b
JS
340 /**
341 Sets a flag to indicate whether encoding conversion is necessary when saving. The default is @false.
342
343 You can improve saving efficiency considerably by setting this value.
344 */
345 void SetNoConversion(bool noconversion);
346
23324ae1 347 /**
f41d6c8c
FM
348 Sets as parent the given node.
349
30f6914b 350 The caller is responsible for deleting any previously present parent node.
23324ae1
FM
351 */
352 void SetParent(wxXmlNode* parent);
353
354 /**
355 Sets the type of this node.
356 */
357 void SetType(wxXmlNodeType type);
358
359 /**
360 See the copy constructor for more info.
361 */
f41d6c8c 362 wxXmlNode& operator=(const wxXmlNode& node);
23324ae1
FM
363};
364
365
e54c96f1 366
23324ae1
FM
367/**
368 @class wxXmlAttribute
7c913512 369
23324ae1 370 Represents a node attribute.
7c913512 371
4ce846b1
FM
372 Example: in <tt>\<img src="hello.gif" id="3"/\></tt>, @c src is an attribute
373 with value @c hello.gif and @c id is an attribute with value @c 3.
7c913512 374
23324ae1
FM
375 @library{wxxml}
376 @category{xml}
7c913512 377
e54c96f1 378 @see wxXmlDocument, wxXmlNode
23324ae1 379*/
7c913512 380class wxXmlAttribute
23324ae1
FM
381{
382public:
23324ae1 383 /**
f41d6c8c 384 Default constructor.
23324ae1
FM
385 */
386 wxXmlAttribute();
f41d6c8c
FM
387
388 /**
389 Creates the attribute with given @a name and @a value.
390 If @a next is not @NULL, then sets it as sibling of this attribute.
391 */
7c913512 392 wxXmlAttribute(const wxString& name, const wxString& value,
4cc4bfaf 393 wxXmlAttribute* next = NULL);
23324ae1
FM
394
395 /**
396 The virtual destructor.
397 */
adaaa686 398 virtual ~wxXmlAttribute();
23324ae1
FM
399
400 /**
401 Returns the name of this attribute.
402 */
328f5751 403 wxString GetName() const;
23324ae1
FM
404
405 /**
406 Returns the sibling of this attribute or @NULL if there are no siblings.
407 */
328f5751 408 wxXmlAttribute* GetNext() const;
23324ae1
FM
409
410 /**
411 Returns the value of this attribute.
412 */
328f5751 413 wxString GetValue() const;
23324ae1
FM
414
415 /**
416 Sets the name of this attribute.
417 */
418 void SetName(const wxString& name);
419
420 /**
421 Sets the sibling of this attribute.
422 */
423 void SetNext(wxXmlAttribute* next);
424
425 /**
426 Sets the value of this attribute.
427 */
428 void SetValue(const wxString& value);
429};
430
431
e54c96f1 432
23324ae1
FM
433/**
434 @class wxXmlDocument
7c913512 435
23324ae1 436 This class holds XML data/document as parsed by XML parser in the root node.
7c913512 437
23324ae1
FM
438 wxXmlDocument internally uses the expat library which comes with wxWidgets to
439 parse the given stream.
7c913512 440
23324ae1 441 A simple example of using XML classes is:
7c913512 442
23324ae1
FM
443 @code
444 wxXmlDocument doc;
1707da0c 445 if (!doc.Load("myfile.xml"))
4ce846b1 446 return false;
7c913512 447
23324ae1 448 // start processing the XML file
1707da0c 449 if (doc.GetRoot()->GetName() != "myroot-node")
4ce846b1 450 return false;
7c913512 451
1707da0c 452 wxXmlNode *child = doc.GetRoot()->GetChildren();
23324ae1 453 while (child) {
7c913512 454
4ce846b1 455 if (child->GetName() == "tag1") {
7c913512 456
23324ae1 457 // process text enclosed by tag1/tag1
1707da0c 458 wxString content = child->GetNodeContent();
7c913512 459
23324ae1 460 ...
7c913512 461
23324ae1 462 // process attributes of tag1
7c913512 463 wxString attrvalue1 =
1707da0c 464 child->GetAttribute("attr1", "default-value");
7c913512 465 wxString attrvalue2 =
1707da0c 466 child->GetAttribute("attr2", "default-value");
7c913512 467
23324ae1 468 ...
7c913512 469
1707da0c 470 } else if (child->GetName() == "tag2") {
7c913512 471
23324ae1
FM
472 // process tag2 ...
473 }
7c913512 474
1707da0c 475 child = child->GetNext();
23324ae1
FM
476 }
477 @endcode
7c913512 478
f41d6c8c
FM
479 Note that if you want to preserve the original formatting of the loaded file
480 including whitespaces and indentation, you need to turn off whitespace-only
481 textnode removal and automatic indentation:
7c913512 482
23324ae1
FM
483 @code
484 wxXmlDocument doc;
1707da0c 485 doc.Load("myfile.xml", "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES);
7c913512 486
57ab6f23 487 // myfile2.xml will be identical to myfile.xml saving it this way:
1707da0c 488 doc.Save("myfile2.xml", wxXML_NO_INDENTATION);
23324ae1 489 @endcode
7c913512 490
23324ae1 491 Using default parameters, you will get a reformatted document which in general
f41d6c8c 492 is different from the original loaded content:
7c913512 493
23324ae1
FM
494 @code
495 wxXmlDocument doc;
1707da0c
FM
496 doc.Load("myfile.xml");
497 doc.Save("myfile2.xml"); // myfile2.xml != myfile.xml
23324ae1 498 @endcode
7c913512 499
23324ae1
FM
500 @library{wxxml}
501 @category{xml}
7c913512 502
e54c96f1 503 @see wxXmlNode, wxXmlAttribute
23324ae1
FM
504*/
505class wxXmlDocument : public wxObject
506{
507public:
23324ae1 508 /**
f41d6c8c 509 Default constructor.
23324ae1
FM
510 */
511 wxXmlDocument();
f41d6c8c
FM
512
513 /**
514 Copy constructor. Deep copies all the XML tree of the given document.
515 */
7c913512 516 wxXmlDocument(const wxXmlDocument& doc);
f41d6c8c
FM
517
518 /**
519 Loads the given filename using the given encoding. See Load().
520 */
521 wxXmlDocument(const wxString& filename,
1707da0c 522 const wxString& encoding = "UTF-8"));
f41d6c8c
FM
523
524 /**
525 Loads the XML document from given stream using the given encoding. See Load().
526 */
527 wxXmlDocument(wxInputStream& stream,
f8ebb70d 528 const wxString& encoding = "UTF-8");
23324ae1
FM
529
530 /**
531 Virtual destructor. Frees the document root node.
532 */
adaaa686 533 virtual ~wxXmlDocument();
23324ae1
FM
534
535 /**
f41d6c8c
FM
536 Detaches the document root node and returns it.
537
538 The document root node will be set to @NULL and thus IsOk() will
539 return @false after calling this function.
540
57ab6f23 541 Note that the caller is responsible for deleting the returned node in order
f41d6c8c 542 to avoid memory leaks.
23324ae1
FM
543 */
544 wxXmlNode* DetachRoot();
545
546 /**
547 Returns encoding of in-memory representation of the document
548 (same as passed to Load() or constructor, defaults to UTF-8).
f41d6c8c 549
1add55ae 550 @note this is meaningless in Unicode build where data are stored as @c wchar_t*.
23324ae1 551 */
328f5751 552 wxString GetEncoding() const;
23324ae1
FM
553
554 /**
555 Returns encoding of document (may be empty).
f41d6c8c
FM
556
557 @note This is the encoding original file was saved in, @b not the
558 encoding of in-memory representation!
23324ae1 559 */
95b4a59e 560 const wxString& GetFileEncoding() const;
23324ae1
FM
561
562 /**
563 Returns the root node of the document.
564 */
328f5751 565 wxXmlNode* GetRoot() const;
23324ae1
FM
566
567 /**
568 Returns the version of document.
f41d6c8c
FM
569
570 This is the value in the @c \<?xml version="1.0"?\> header of the XML document.
57ab6f23 571 If the version attribute was not explicitly given in the header, this function
23324ae1
FM
572 returns an empty string.
573 */
95b4a59e 574 const wxString& GetVersion() const;
23324ae1
FM
575
576 /**
577 Returns @true if the document has been loaded successfully.
578 */
328f5751 579 bool IsOk() const;
23324ae1 580
f41d6c8c 581
23324ae1 582 /**
f41d6c8c
FM
583 Parses @a filename as an xml document and loads its data.
584
585 If @a flags does not contain wxXMLDOC_KEEP_WHITESPACE_NODES, then, while loading,
586 all nodes of type @c wxXML_TEXT_NODE (see wxXmlNode) are automatically skipped
587 if they contain whitespaces only.
588
589 The removal of these nodes makes the load process slightly faster and requires
590 less memory however makes impossible to recreate exactly the loaded text with a
591 Save() call later. Read the initial description of this class for more info.
592
593 Returns true on success, false otherwise.
594 */
595 virtual bool Load(const wxString& filename,
f8ebb70d 596 const wxString& encoding = "UTF-8", int flags = wxXMLDOC_NONE);
f41d6c8c
FM
597
598 /**
599 Like Load(const wxString&, const wxString&, int) but takes the data from
600 given input stream.
601 */
602 virtual bool Load(wxInputStream& stream,
f8ebb70d 603 const wxString& encoding = "UTF-8", int flags = wxXMLDOC_NONE);
f41d6c8c
FM
604
605 /**
606 Saves XML tree creating a file named with given string.
607
608 If @a indentstep is greater than or equal to zero, then, while saving,
609 an automatic indentation is added with steps composed by indentstep spaces.
610
611 If @a indentstep is @c wxXML_NO_INDENTATION, then, automatic indentation
612 is turned off.
23324ae1 613 */
57cc93eb 614 virtual bool Save(const wxString& filename, int indentstep = 2) const;
23324ae1 615
23324ae1 616 /**
f41d6c8c
FM
617 Saves XML tree in the given output stream.
618 See Save(const wxString&, int) for a description of @a indentstep.
23324ae1 619 */
57cc93eb 620 virtual bool Save(wxOutputStream& stream, int indentstep = 2) const;
23324ae1
FM
621
622 /**
623 Sets the enconding of the document.
624 */
625 void SetEncoding(const wxString& enc);
626
627 /**
628 Sets the enconding of the file which will be used to save the document.
629 */
630 void SetFileEncoding(const wxString& encoding);
631
632 /**
633 Sets the root node of this document. Deletes previous root node.
f41d6c8c
FM
634 Use DetachRoot() and then SetRoot() if you want to replace the root
635 node without deleting the old document tree.
23324ae1
FM
636 */
637 void SetRoot(wxXmlNode* node);
638
639 /**
640 Sets the version of the XML file which will be used to save the document.
641 */
642 void SetVersion(const wxString& version);
643
644 /**
645 Deep copies the given document.
646 */
f41d6c8c 647 wxXmlDocument& operator=(const wxXmlDocument& doc);
ccec9093
VZ
648
649 /**
650 Get expat library version information.
651
652 @since 2.9.2
653 @see wxVersionInfo
654 */
655 static wxVersionInfo GetLibraryVersionInfo();
23324ae1 656};
e54c96f1 657