VirtualBox

source: vbox/trunk/include/iprt/cpp/xml.h@ 86716

Last change on this file since 86716 was 85160, checked in by vboxsync, 4 years ago

*: Some missing DECLCALLBACK/RTDECL and related nothrow issues raised by Clang. bugref:9794 bugref:9790

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.6 KB
Line 
1/** @file
2 * IPRT - XML Helper APIs.
3 */
4
5/*
6 * Copyright (C) 2007-2020 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_cpp_xml_h
27#define IPRT_INCLUDED_cpp_xml_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#ifndef IN_RING3
33# error "There are no XML APIs available in Ring-0 Context!"
34#endif
35
36#include <iprt/list.h>
37#include <iprt/cpp/exception.h>
38#include <iprt/cpp/utils.h>
39
40#include <list>
41#include <memory>
42
43
44/** @defgroup grp_rt_cpp_xml C++ XML support
45 * @ingroup grp_rt_cpp
46 * @{
47 */
48
49/* Forwards */
50typedef struct _xmlParserInput xmlParserInput;
51typedef xmlParserInput *xmlParserInputPtr;
52typedef struct _xmlParserCtxt xmlParserCtxt;
53typedef xmlParserCtxt *xmlParserCtxtPtr;
54typedef struct _xmlError xmlError;
55typedef xmlError *xmlErrorPtr;
56
57typedef struct _xmlAttr xmlAttr;
58typedef struct _xmlNode xmlNode;
59
60#define RT_XML_CONTENT_SMALL _8K
61#define RT_XML_CONTENT_LARGE _128K
62#define RT_XML_ATTR_TINY 64
63#define RT_XML_ATTR_SMALL _1K
64#define RT_XML_ATTR_MEDIUM _8K
65#define RT_XML_ATTR_LARGE _64K
66
67/** @} */
68
69namespace xml
70{
71
72/**
73 * @addtogroup grp_rt_cpp_xml
74 * @{
75 */
76
77// Exceptions
78//////////////////////////////////////////////////////////////////////////////
79
80class RT_DECL_CLASS LogicError : public RTCError
81{
82public:
83
84 LogicError(const char *aMsg = NULL)
85 : RTCError(aMsg)
86 {}
87
88 LogicError(RT_SRC_POS_DECL);
89};
90
91class RT_DECL_CLASS RuntimeError : public RTCError
92{
93public:
94
95 RuntimeError(const char *aMsg = NULL)
96 : RTCError(aMsg)
97 {}
98};
99
100class RT_DECL_CLASS XmlError : public RuntimeError
101{
102public:
103 XmlError(xmlErrorPtr aErr);
104
105 static char* Format(xmlErrorPtr aErr);
106};
107
108// Logical errors
109//////////////////////////////////////////////////////////////////////////////
110
111class RT_DECL_CLASS ENotImplemented : public LogicError
112{
113public:
114 ENotImplemented(const char *aMsg = NULL) : LogicError(aMsg) {}
115 ENotImplemented(RT_SRC_POS_DECL) : LogicError(RT_SRC_POS_ARGS) {}
116};
117
118class RT_DECL_CLASS EInvalidArg : public LogicError
119{
120public:
121 EInvalidArg(const char *aMsg = NULL) : LogicError(aMsg) {}
122 EInvalidArg(RT_SRC_POS_DECL) : LogicError(RT_SRC_POS_ARGS) {}
123};
124
125class RT_DECL_CLASS EDocumentNotEmpty : public LogicError
126{
127public:
128 EDocumentNotEmpty(const char *aMsg = NULL) : LogicError(aMsg) {}
129 EDocumentNotEmpty(RT_SRC_POS_DECL) : LogicError(RT_SRC_POS_ARGS) {}
130};
131
132class RT_DECL_CLASS ENodeIsNotElement : public LogicError
133{
134public:
135 ENodeIsNotElement(const char *aMsg = NULL) : LogicError(aMsg) {}
136 ENodeIsNotElement(RT_SRC_POS_DECL) : LogicError(RT_SRC_POS_ARGS) {}
137};
138
139// Runtime errors
140//////////////////////////////////////////////////////////////////////////////
141
142class RT_DECL_CLASS EIPRTFailure : public RuntimeError
143{
144public:
145
146 EIPRTFailure(int aRC, const char *pszContextFmt, ...);
147
148 int rc() const
149 {
150 return mRC;
151 }
152
153private:
154 int mRC;
155};
156
157/**
158 * The Stream class is a base class for I/O streams.
159 */
160class RT_DECL_CLASS Stream
161{
162public:
163
164 virtual ~Stream() {}
165
166 virtual const char *uri() const = 0;
167
168 /**
169 * Returns the current read/write position in the stream. The returned
170 * position is a zero-based byte offset from the beginning of the file.
171 *
172 * Throws ENotImplemented if this operation is not implemented for the
173 * given stream.
174 */
175 virtual uint64_t pos() const = 0;
176
177 /**
178 * Sets the current read/write position in the stream.
179 *
180 * @param aPos Zero-based byte offset from the beginning of the stream.
181 *
182 * Throws ENotImplemented if this operation is not implemented for the
183 * given stream.
184 */
185 virtual void setPos (uint64_t aPos) = 0;
186};
187
188/**
189 * The Input class represents an input stream.
190 *
191 * This input stream is used to read the settings tree from.
192 * This is an abstract class that must be subclassed in order to fill it with
193 * useful functionality.
194 */
195class RT_DECL_CLASS Input : virtual public Stream
196{
197public:
198
199 /**
200 * Reads from the stream to the supplied buffer.
201 *
202 * @param aBuf Buffer to store read data to.
203 * @param aLen Buffer length.
204 *
205 * @return Number of bytes read.
206 */
207 virtual int read (char *aBuf, int aLen) = 0;
208};
209
210/**
211 *
212 */
213class RT_DECL_CLASS Output : virtual public Stream
214{
215public:
216
217 /**
218 * Writes to the stream from the supplied buffer.
219 *
220 * @param aBuf Buffer to write data from.
221 * @param aLen Buffer length.
222 *
223 * @return Number of bytes written.
224 */
225 virtual int write (const char *aBuf, int aLen) = 0;
226
227 /**
228 * Truncates the stream from the current position and upto the end.
229 * The new file size will become exactly #pos() bytes.
230 *
231 * Throws ENotImplemented if this operation is not implemented for the
232 * given stream.
233 */
234 virtual void truncate() = 0;
235};
236
237
238//////////////////////////////////////////////////////////////////////////////
239
240/**
241 * The File class is a stream implementation that reads from and writes to
242 * regular files.
243 *
244 * The File class uses IPRT File API for file operations. Note that IPRT File
245 * API is not thread-safe. This means that if you pass the same RTFILE handle to
246 * different File instances that may be simultaneously used on different
247 * threads, you should care about serialization; otherwise you will get garbage
248 * when reading from or writing to such File instances.
249 */
250class RT_DECL_CLASS File : public Input, public Output
251{
252public:
253
254 /**
255 * Possible file access modes.
256 */
257 enum Mode { Mode_Read, Mode_WriteCreate, Mode_Overwrite, Mode_ReadWrite };
258
259 /**
260 * Opens a file with the given name in the given mode. If @a aMode is Read
261 * or ReadWrite, the file must exist. If @a aMode is Write, the file must
262 * not exist. Otherwise, an EIPRTFailure exception will be thrown.
263 *
264 * @param aMode File mode.
265 * @param aFileName File name.
266 * @param aFlushIt Whether to flush a writable file before closing it.
267 */
268 File(Mode aMode, const char *aFileName, bool aFlushIt = false);
269
270 /**
271 * Uses the given file handle to perform file operations. This file
272 * handle must be already open in necessary mode (read, or write, or mixed).
273 *
274 * The read/write position of the given handle will be reset to the
275 * beginning of the file on success.
276 *
277 * Note that the given file handle will not be automatically closed upon
278 * this object destruction.
279 *
280 * @note It you pass the same RTFILE handle to more than one File instance,
281 * please make sure you have provided serialization in case if these
282 * instasnces are to be simultaneously used by different threads.
283 * Otherwise you may get garbage when reading or writing.
284 *
285 * @param aHandle Open file handle.
286 * @param aFileName File name (for reference).
287 * @param aFlushIt Whether to flush a writable file before closing it.
288 */
289 File(RTFILE aHandle, const char *aFileName = NULL, bool aFlushIt = false);
290
291 /**
292 * Destroys the File object. If the object was created from a file name
293 * the corresponding file will be automatically closed. If the object was
294 * created from a file handle, it will remain open.
295 */
296 virtual ~File();
297
298 const char *uri() const;
299
300 uint64_t pos() const;
301 void setPos(uint64_t aPos);
302
303 /**
304 * See Input::read(). If this method is called in wrong file mode,
305 * LogicError will be thrown.
306 */
307 int read(char *aBuf, int aLen);
308
309 /**
310 * See Output::write(). If this method is called in wrong file mode,
311 * LogicError will be thrown.
312 */
313 int write(const char *aBuf, int aLen);
314
315 /**
316 * See Output::truncate(). If this method is called in wrong file mode,
317 * LogicError will be thrown.
318 */
319 void truncate();
320
321private:
322
323 /* Obscure class data */
324 struct Data;
325 Data *m;
326
327 /* auto_ptr data doesn't have proper copy semantics */
328 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(File);
329};
330
331/**
332 * The MemoryBuf class represents a stream implementation that reads from the
333 * memory buffer.
334 */
335class RT_DECL_CLASS MemoryBuf : public Input
336{
337public:
338
339 MemoryBuf (const char *aBuf, size_t aLen, const char *aURI = NULL);
340
341 virtual ~MemoryBuf();
342
343 const char *uri() const;
344
345 int read(char *aBuf, int aLen);
346 uint64_t pos() const;
347 void setPos(uint64_t aPos);
348
349private:
350 /* Obscure class data */
351 struct Data;
352 Data *m;
353
354 /* auto_ptr data doesn't have proper copy semantics */
355 DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(MemoryBuf);
356};
357
358
359/*
360 * GlobalLock
361 *
362 *
363 */
364
365
366#if RT_CLANG_PREREQ(4, 0) /* VC++ needs the nothrow'ed-ness, while clang barfs at it. */
367typedef xmlParserInput *FNEXTERNALENTITYLOADER(const char *aURI, const char *aID, xmlParserCtxt *aCtxt);
368#else
369typedef DECLCALLBACKTYPE_EX(xmlParserInput *, RT_NOTHING, FNEXTERNALENTITYLOADER,(const char *aURI, const char *aID,
370 xmlParserCtxt *aCtxt));
371#endif
372typedef FNEXTERNALENTITYLOADER *PFNEXTERNALENTITYLOADER; /**< xmlExternalEntityLoader */
373
374class RT_DECL_CLASS GlobalLock
375{
376public:
377 GlobalLock();
378 ~GlobalLock();
379
380 void setExternalEntityLoader(PFNEXTERNALENTITYLOADER pFunc);
381
382 static xmlParserInput* callDefaultLoader(const char *aURI,
383 const char *aID,
384 xmlParserCtxt *aCtxt);
385
386private:
387 /* Obscure class data. */
388 struct Data;
389 struct Data *m;
390};
391
392class ElementNode;
393typedef std::list<const ElementNode*> ElementNodesList;
394
395class AttributeNode;
396
397class ContentNode;
398
399/**
400 * Node base class.
401 *
402 * Cannot be used directly, but ElementNode, ContentNode and AttributeNode
403 * derive from this. This does implement useful public methods though.
404 *
405 *
406 */
407class RT_DECL_CLASS Node
408{
409public:
410 virtual ~Node();
411
412 const char *getName() const;
413 const char *getPrefix() const;
414 const char *getNamespaceURI() const;
415 bool nameEqualsNS(const char *pcszNamespace, const char *pcsz) const;
416 bool nameEquals(const char *pcsz) const
417 {
418 return nameEqualsNS(NULL, pcsz);
419 }
420 bool nameEqualsN(const char *pcsz, size_t cchMax, const char *pcszNamespace = NULL) const;
421
422 const char *getValue() const;
423 const char *getValueN(size_t cchValueLimit) const;
424 bool copyValue(int32_t &i) const;
425 bool copyValue(uint32_t &i) const;
426 bool copyValue(int64_t &i) const;
427 bool copyValue(uint64_t &i) const;
428
429 /** @name Introspection.
430 * @{ */
431 /** Is this an ElementNode instance.
432 * @returns true / false */
433 bool isElement() const
434 {
435 return m_Type == IsElement;
436 }
437
438 /** Is this an ContentNode instance.
439 * @returns true / false */
440 bool isContent() const
441 {
442 return m_Type == IsContent;
443 }
444
445 /** Is this an AttributeNode instance.
446 * @returns true / false */
447 bool isAttribute() const
448 {
449 return m_Type == IsAttribute;
450 }
451
452 int getLineNumber() const;
453 /** @} */
454
455 /** @name General tree enumeration.
456 *
457 * Use the introspection methods isElement() and isContent() before doing static
458 * casting. Parents are always or ElementNode type, but siblings and children
459 * can be of both ContentNode and ElementNode types.
460 *
461 * @remarks Attribute node are in the attributes list, while both content and
462 * element nodes are in the list of children. See ElementNode.
463 *
464 * @remarks Careful mixing tree walking with node removal!
465 * @{
466 */
467 /** Get the parent node
468 * @returns Pointer to the parent node, or NULL if root. */
469 const Node *getParent() const
470 {
471 return m_pParent;
472 }
473
474 /** Get the previous sibling.
475 * @returns Pointer to the previous sibling node, NULL if first child.
476 */
477 const Node *getPrevSibiling() const
478 {
479 if (!m_pParentListAnchor)
480 return NULL;
481 return RTListGetPrevCpp(m_pParentListAnchor, this, const Node, m_listEntry);
482 }
483
484 /** Get the next sibling.
485 * @returns Pointer to the next sibling node, NULL if last child. */
486 const Node *getNextSibiling() const
487 {
488 if (!m_pParentListAnchor)
489 return NULL;
490 return RTListGetNextCpp(m_pParentListAnchor, this, const Node, m_listEntry);
491 }
492 /** @} */
493
494protected:
495 /** Node types. */
496 typedef enum { IsElement, IsAttribute, IsContent } EnumType;
497
498 /** The type of node this is an instance of. */
499 EnumType m_Type;
500 /** The parent node (always an element), NULL if root. */
501 Node *m_pParent;
502
503 xmlNode *m_pLibNode; ///< != NULL if this is an element or content node
504 xmlAttr *m_pLibAttr; ///< != NULL if this is an attribute node
505 const char *m_pcszNamespacePrefix; ///< not always set
506 const char *m_pcszNamespaceHref; ///< full http:// spec
507 const char *m_pcszName; ///< element or attribute name, points either into pLibNode or pLibAttr;
508 ///< NULL if this is a content node
509
510 /** Child list entry of this node. (List head m_pParent->m_children or
511 * m_pParent->m_attribute depending on the type.) */
512 RTLISTNODE m_listEntry;
513 /** Pointer to the parent list anchor.
514 * This allows us to use m_listEntry both for children and attributes. */
515 PRTLISTANCHOR m_pParentListAnchor;
516
517 // hide the default constructor so people use only our factory methods
518 Node(EnumType type,
519 Node *pParent,
520 PRTLISTANCHOR pListAnchor,
521 xmlNode *pLibNode,
522 xmlAttr *pLibAttr);
523 Node(const Node &x); // no copying
524
525 friend class AttributeNode;
526 friend class ElementNode; /* C list hack. */
527};
528
529/**
530 * Node subclass that represents an attribute of an element.
531 *
532 * For attributes, Node::getName() returns the attribute name, and Node::getValue()
533 * returns the attribute value, if any.
534 *
535 * Since the Node constructor is private, one can create new attribute nodes
536 * only through the following factory methods:
537 *
538 * -- ElementNode::setAttribute()
539 */
540class RT_DECL_CLASS AttributeNode : public Node
541{
542public:
543
544protected:
545 // hide the default constructor so people use only our factory methods
546 AttributeNode(const ElementNode *pElmRoot,
547 Node *pParent,
548 PRTLISTANCHOR pListAnchor,
549 xmlAttr *pLibAttr);
550 AttributeNode(const AttributeNode &x); // no copying
551
552 friend class Node;
553 friend class ElementNode;
554};
555
556/**
557 * Node subclass that represents an element.
558 *
559 * For elements, Node::getName() returns the element name, and Node::getValue()
560 * returns the text contents, if any.
561 *
562 * Since the Node constructor is private, one can create element nodes
563 * only through the following factory methods:
564 *
565 * -- Document::createRootElement()
566 * -- ElementNode::createChild()
567 */
568class RT_DECL_CLASS ElementNode : public Node
569{
570public:
571 int getChildElements(ElementNodesList &children, const char *pcszMatch = NULL) const;
572
573 const ElementNode *findChildElementNS(const char *pcszNamespace, const char *pcszMatch) const;
574 const ElementNode *findChildElement(const char *pcszMatch) const
575 {
576 return findChildElementNS(NULL, pcszMatch);
577 }
578 const ElementNode *findChildElementFromId(const char *pcszId) const;
579
580 /** Finds the first decendant matching the name at the end of @a pcszPath and
581 * optionally namespace.
582 *
583 * @returns Pointer to the child string value, NULL if not found or no value.
584 * @param pcszPath Path to the child element. Slashes can be used to
585 * make a simple path to any decendant.
586 * @param pcszNamespace The namespace to match, NULL (default) match any
587 * namespace. When using a path, this matches all
588 * elements along the way.
589 * @see findChildElement, findChildElementP
590 */
591 const ElementNode *findChildElementP(const char *pcszPath, const char *pcszNamespace = NULL) const;
592
593 /** Finds the first child with matching the give name and optionally namspace,
594 * returning its value.
595 *
596 * @returns Pointer to the child string value, NULL if not found or no value.
597 * @param pcszPath Path to the child element. Slashes can be used to
598 * make a simple path to any decendant.
599 * @param pcszNamespace The namespace to match, NULL (default) match any
600 * namespace. When using a path, this matches all
601 * elements along the way.
602 * @see findChildElement, findChildElementP
603 */
604 const char *findChildElementValueP(const char *pcszPath, const char *pcszNamespace = NULL) const
605 {
606 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
607 if (pElem)
608 return pElem->getValue();
609 return NULL;
610 }
611
612 /** Finds the first child with matching the give name and optionally namspace,
613 * returning its value. Checks the length against the limit.
614 *
615 * @returns Pointer to the child string value, NULL if not found or no value.
616 * @param pcszPath Path to the child element. Slashes can be used to
617 * make a simple path to any decendant.
618 * @param cchValueLimit If the length of the returned value exceeds this
619 * limit a EIPRTFailure exception will be thrown.
620 * @param pcszNamespace The namespace to match, NULL (default) match any
621 * namespace. When using a path, this matches all
622 * elements along the way.
623 * @see findChildElement, findChildElementP
624 */
625 const char *findChildElementValuePN(const char *pcszPath, size_t cchValueLimit, const char *pcszNamespace = NULL) const
626 {
627 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
628 if (pElem)
629 return pElem->getValueN(cchValueLimit);
630 return NULL;
631 }
632
633 /** Combines findChildElementNS and findAttributeValue.
634 *
635 * @returns Pointer to attribute string value, NULL if either the element or
636 * the attribute was not found.
637 * @param pcszChild The child element name.
638 * @param pcszAttribute The attribute name.
639 * @param pcszChildNamespace The namespace to match @a pcszChild with, NULL
640 * (default) match any namespace.
641 * @param pcszAttributeNamespace The namespace prefix to apply to the
642 * attribute, NULL (default) match any namespace.
643 * @see findChildElementNS and findAttributeValue
644 * @note The findChildElementAttributeValueP() method would do the same thing
645 * given the same inputs, but it would be slightly slower, thus the
646 * separate method.
647 */
648 const char *findChildElementAttributeValue(const char *pcszChild, const char *pcszAttribute,
649 const char *pcszChildNamespace = NULL,
650 const char *pcszAttributeNamespace = NULL) const
651 {
652 const ElementNode *pElem = findChildElementNS(pcszChildNamespace, pcszChild);
653 if (pElem)
654 return pElem->findAttributeValue(pcszAttribute, pcszAttributeNamespace);
655 return NULL;
656 }
657
658 /** Combines findChildElementP and findAttributeValue.
659 *
660 * @returns Pointer to attribute string value, NULL if either the element or
661 * the attribute was not found.
662 * @param pcszPath Path to the child element. Slashes can be used
663 * to make a simple path to any decendant.
664 * @param pcszAttribute The attribute name.
665 * @param pcszPathNamespace The namespace to match @a pcszPath with, NULL
666 * (default) match any namespace. When using a
667 * path, this matches all elements along the way.
668 * @param pcszAttributeNamespace The namespace prefix to apply to the
669 * attribute, NULL (default) match any namespace.
670 * @see findChildElementP and findAttributeValue
671 */
672 const char *findChildElementAttributeValueP(const char *pcszPath, const char *pcszAttribute,
673 const char *pcszPathNamespace = NULL,
674 const char *pcszAttributeNamespace = NULL) const
675 {
676 const ElementNode *pElem = findChildElementP(pcszPath, pcszPathNamespace);
677 if (pElem)
678 return pElem->findAttributeValue(pcszAttribute, pcszAttributeNamespace);
679 return NULL;
680 }
681
682 /** Combines findChildElementP and findAttributeValueN.
683 *
684 * @returns Pointer to attribute string value, NULL if either the element or
685 * the attribute was not found.
686 * @param pcszPath The attribute name. Slashes can be used to make a
687 * simple path to any decendant.
688 * @param pcszAttribute The attribute name.
689 * @param cchValueLimit If the length of the returned value exceeds this
690 * limit a EIPRTFailure exception will be thrown.
691 * @param pcszPathNamespace The namespace to match @a pcszPath with, NULL
692 * (default) match any namespace. When using a
693 * path, this matches all elements along the way.
694 * @param pcszAttributeNamespace The namespace prefix to apply to the
695 * attribute, NULL (default) match any namespace.
696 * @see findChildElementP and findAttributeValue
697 */
698 const char *findChildElementAttributeValuePN(const char *pcszPath, const char *pcszAttribute,
699 size_t cchValueLimit,
700 const char *pcszPathNamespace = NULL,
701 const char *pcszAttributeNamespace = NULL) const
702 {
703 const ElementNode *pElem = findChildElementP(pcszPath, pcszPathNamespace);
704 if (pElem)
705 return pElem->findAttributeValueN(pcszAttribute, cchValueLimit, pcszAttributeNamespace);
706 return NULL;
707 }
708
709
710 /** @name Tree enumeration.
711 * @{ */
712
713 /** Get the next tree element in a full tree enumeration.
714 *
715 * By starting with the root node, this can be used to enumerate the entire tree
716 * (or sub-tree if @a pElmRoot is used).
717 *
718 * @returns Pointer to the next element in the tree, NULL if we're done.
719 * @param pElmRoot The root of the tree we're enumerating. NULL if
720 * it's the entire tree.
721 */
722 ElementNode const *getNextTreeElement(ElementNode const *pElmRoot = NULL) const;
723 RT_CPP_GETTER_UNCONST_RET(ElementNode *, ElementNode, getNextTreeElement, (const ElementNode *pElmRoot = NULL), (pElmRoot))
724
725 /** Get the first child node.
726 * @returns Pointer to the first child node, NULL if no children. */
727 const Node *getFirstChild() const
728 {
729 return RTListGetFirstCpp(&m_children, const Node, m_listEntry);
730 }
731 RT_CPP_GETTER_UNCONST_RET(Node *, ElementNode, getFirstChild,(),())
732
733 /** Get the last child node.
734 * @returns Pointer to the last child node, NULL if no children. */
735 const Node *getLastChild() const
736 {
737 return RTListGetLastCpp(&m_children, const Node, m_listEntry);
738 }
739
740 /** Get the first child node.
741 * @returns Pointer to the first child node, NULL if no children. */
742 const ElementNode *getFirstChildElement() const;
743
744 /** Get the last child node.
745 * @returns Pointer to the last child node, NULL if no children. */
746 const ElementNode *getLastChildElement() const;
747
748 /** Get the previous sibling element.
749 * @returns Pointer to the previous sibling element, NULL if first child
750 * element.
751 * @see getNextSibilingElement, getPrevSibling
752 */
753 const ElementNode *getPrevSibilingElement() const;
754
755 /** Get the next sibling element.
756 * @returns Pointer to the next sibling element, NULL if last child element.
757 * @see getPrevSibilingElement, getNextSibling
758 */
759 const ElementNode *getNextSibilingElement() const;
760
761 /** Find the previous element matching the given name and namespace (optionally).
762 * @returns Pointer to the previous sibling element, NULL if first child
763 * element.
764 * @param pcszName The element name to match.
765 * @param pcszNamespace The namespace name, default is NULL which means
766 * anything goes.
767 * @note Changed the order of the arguments.
768 */
769 const ElementNode *findPrevSibilingElement(const char *pcszName, const char *pcszNamespace = NULL) const;
770
771 /** Find the next element matching the given name and namespace (optionally).
772 * @returns Pointer to the previous sibling element, NULL if first child
773 * element.
774 * @param pcszName The element name to match.
775 * @param pcszNamespace The namespace name, default is NULL which means
776 * anything goes.
777 * @note Changed the order of the arguments.
778 */
779 const ElementNode *findNextSibilingElement(const char *pcszName, const char *pcszNamespace = NULL) const;
780 /** @} */
781
782 /** @name Attribute enumeration
783 * @{ */
784
785 /** Get the first attribute node.
786 * @returns Pointer to the first child node, NULL if no attributes. */
787 const AttributeNode *getFirstAttribute() const
788 {
789 return RTListGetFirstCpp(&m_attributes, const AttributeNode, m_listEntry);
790 }
791
792 /** Get the last attribute node.
793 * @returns Pointer to the last child node, NULL if no attributes. */
794 const AttributeNode *getLastAttribute() const
795 {
796 return RTListGetLastCpp(&m_attributes, const AttributeNode, m_listEntry);
797 }
798
799 /** @} */
800
801 const AttributeNode *findAttribute(const char *pcszMatch, const char *pcszNamespace = NULL) const;
802 /** Find the first attribute with the given name, returning its value string.
803 * @returns Pointer to the attribute string value.
804 * @param pcszName The attribute name.
805 * @param pcszNamespace The namespace name, default is NULL which means
806 * anything goes.
807 * @see getAttributeValue
808 */
809 const char *findAttributeValue(const char *pcszName, const char *pcszNamespace = NULL) const
810 {
811 const AttributeNode *pAttr = findAttribute(pcszName, pcszNamespace);
812 if (pAttr)
813 return pAttr->getValue();
814 return NULL;
815 }
816 /** Find the first attribute with the given name, returning its value string.
817 * @returns Pointer to the attribute string value.
818 * @param pcszName The attribute name.
819 * @param cchValueLimit If the length of the returned value exceeds this
820 * limit a EIPRTFailure exception will be thrown.
821 * @param pcszNamespace The namespace name, default is NULL which means
822 * anything goes.
823 * @see getAttributeValue
824 */
825 const char *findAttributeValueN(const char *pcszName, size_t cchValueLimit, const char *pcszNamespace = NULL) const
826 {
827 const AttributeNode *pAttr = findAttribute(pcszName, pcszNamespace);
828 if (pAttr)
829 return pAttr->getValueN(cchValueLimit);
830 return NULL;
831 }
832
833 bool getAttributeValue(const char *pcszMatch, const char *&pcsz, const char *pcszNamespace = NULL) const
834 { return getAttributeValue(pcszMatch, &pcsz, pcszNamespace); }
835 bool getAttributeValue(const char *pcszMatch, RTCString &str, const char *pcszNamespace = NULL) const
836 { return getAttributeValue(pcszMatch, &str, pcszNamespace); }
837 bool getAttributeValuePath(const char *pcszMatch, RTCString &str, const char *pcszNamespace = NULL) const
838 { return getAttributeValue(pcszMatch, &str, pcszNamespace); }
839 bool getAttributeValue(const char *pcszMatch, int32_t &i, const char *pcszNamespace = NULL) const
840 { return getAttributeValue(pcszMatch, &i, pcszNamespace); }
841 bool getAttributeValue(const char *pcszMatch, uint32_t &i, const char *pcszNamespace = NULL) const
842 { return getAttributeValue(pcszMatch, &i, pcszNamespace); }
843 bool getAttributeValue(const char *pcszMatch, int64_t &i, const char *pcszNamespace = NULL) const
844 { return getAttributeValue(pcszMatch, &i, pcszNamespace); }
845 bool getAttributeValue(const char *pcszMatch, uint64_t &u, const char *pcszNamespace = NULL) const
846 { return getAttributeValue(pcszMatch, &u, pcszNamespace); }
847 bool getAttributeValue(const char *pcszMatch, bool &f, const char *pcszNamespace = NULL) const
848 { return getAttributeValue(pcszMatch, &f, pcszNamespace); }
849 bool getAttributeValueN(const char *pcszMatch, const char *&pcsz, size_t cchValueLimit, const char *pcszNamespace = NULL) const
850 { return getAttributeValueN(pcszMatch, &pcsz, cchValueLimit, pcszNamespace); }
851 bool getAttributeValueN(const char *pcszMatch, RTCString &str, size_t cchValueLimit, const char *pcszNamespace = NULL) const
852 { return getAttributeValueN(pcszMatch, &str, cchValueLimit, pcszNamespace); }
853 bool getAttributeValuePathN(const char *pcszMatch, RTCString &str, size_t cchValueLimit, const char *pcszNamespace = NULL) const
854 { return getAttributeValueN(pcszMatch, &str, cchValueLimit, pcszNamespace); }
855
856 /** @name Variants that for clarity does not use references for output params.
857 * @{ */
858 bool getAttributeValue(const char *pcszMatch, const char **ppcsz, const char *pcszNamespace = NULL) const;
859 bool getAttributeValue(const char *pcszMatch, RTCString *pStr, const char *pcszNamespace = NULL) const;
860 bool getAttributeValuePath(const char *pcszMatch, RTCString *pStr, const char *pcszNamespace = NULL) const;
861 bool getAttributeValue(const char *pcszMatch, int32_t *pi, const char *pcszNamespace = NULL) const;
862 bool getAttributeValue(const char *pcszMatch, uint32_t *pu, const char *pcszNamespace = NULL) const;
863 bool getAttributeValue(const char *pcszMatch, int64_t *piValue, const char *pcszNamespace = NULL) const;
864 bool getAttributeValue(const char *pcszMatch, uint64_t *pu, const char *pcszNamespace = NULL) const;
865 bool getAttributeValue(const char *pcszMatch, bool *pf, const char *pcszNamespace = NULL) const;
866 bool getAttributeValueN(const char *pcszMatch, const char **ppcsz, size_t cchValueLimit, const char *pcszNamespace = NULL) const;
867 bool getAttributeValueN(const char *pcszMatch, RTCString *pStr, size_t cchValueLimit, const char *pcszNamespace = NULL) const;
868 bool getAttributeValuePathN(const char *pcszMatch, RTCString *pStr, size_t cchValueLimit, const char *pcszNamespace = NULL) const;
869 /** @} */
870
871 /** @name Convenience methods for convering the element value.
872 * @{ */
873 bool getElementValue(int32_t *piValue) const;
874 bool getElementValue(uint32_t *puValue) const;
875 bool getElementValue(int64_t *piValue) const;
876 bool getElementValue(uint64_t *puValue) const;
877 bool getElementValue(bool *pfValue) const;
878 /** @} */
879
880 /** @name Convenience findChildElementValueP and getElementValue.
881 * @{ */
882 bool getChildElementValueP(const char *pcszPath, int32_t *piValue, const char *pcszNamespace = NULL) const
883 {
884 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
885 return pElem && pElem->getElementValue(piValue);
886 }
887 bool getChildElementValueP(const char *pcszPath, uint32_t *puValue, const char *pcszNamespace = NULL) const
888 {
889 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
890 return pElem && pElem->getElementValue(puValue);
891 }
892 bool getChildElementValueP(const char *pcszPath, int64_t *piValue, const char *pcszNamespace = NULL) const
893 {
894 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
895 return pElem && pElem->getElementValue(piValue);
896 }
897 bool getChildElementValueP(const char *pcszPath, uint64_t *puValue, const char *pcszNamespace = NULL) const
898 {
899 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
900 return pElem && pElem->getElementValue(puValue);
901 }
902 bool getChildElementValueP(const char *pcszPath, bool *pfValue, const char *pcszNamespace = NULL) const
903 {
904 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
905 return pElem && pElem->getElementValue(pfValue);
906 }
907
908 /** @} */
909
910 /** @name Convenience findChildElementValueP and getElementValue with a
911 * default value being return if the child element isn't present.
912 *
913 * @remarks These will return false on conversion errors.
914 * @{ */
915 bool getChildElementValueDefP(const char *pcszPath, int32_t iDefault, int32_t *piValue, const char *pcszNamespace = NULL) const
916 {
917 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
918 if (pElem)
919 return pElem->getElementValue(piValue);
920 *piValue = iDefault;
921 return true;
922 }
923 bool getChildElementValueDefP(const char *pcszPath, uint32_t uDefault, uint32_t *puValue, const char *pcszNamespace = NULL) const
924 {
925 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
926 if (pElem)
927 return pElem->getElementValue(puValue);
928 *puValue = uDefault;
929 return true;
930 }
931 bool getChildElementValueDefP(const char *pcszPath, int64_t iDefault, int64_t *piValue, const char *pcszNamespace = NULL) const
932 {
933 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
934 if (pElem)
935 return pElem->getElementValue(piValue);
936 *piValue = iDefault;
937 return true;
938 }
939 bool getChildElementValueDefP(const char *pcszPath, uint64_t uDefault, uint64_t *puValue, const char *pcszNamespace = NULL) const
940 {
941 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
942 if (pElem)
943 return pElem->getElementValue(puValue);
944 *puValue = uDefault;
945 return true;
946 }
947 bool getChildElementValueDefP(const char *pcszPath, bool fDefault, bool *pfValue, const char *pcszNamespace = NULL) const
948 {
949 const ElementNode *pElem = findChildElementP(pcszPath, pcszNamespace);
950 if (pElem)
951 return pElem->getElementValue(pfValue);
952 *pfValue = fDefault;
953 return true;
954 }
955 /** @} */
956
957 ElementNode *createChild(const char *pcszElementName);
958
959 ContentNode *addContent(const char *pcszContent);
960 ContentNode *addContent(const RTCString &strContent)
961 {
962 return addContent(strContent.c_str());
963 }
964
965 ContentNode *setContent(const char *pcszContent);
966 ContentNode *setContent(const RTCString &strContent)
967 {
968 return setContent(strContent.c_str());
969 }
970
971 AttributeNode *setAttribute(const char *pcszName, const char *pcszValue);
972 AttributeNode *setAttribute(const char *pcszName, const RTCString &strValue)
973 {
974 return setAttribute(pcszName, strValue.c_str());
975 }
976 AttributeNode *setAttributePath(const char *pcszName, const RTCString &strValue);
977 AttributeNode *setAttribute(const char *pcszName, int32_t i);
978 AttributeNode *setAttribute(const char *pcszName, uint32_t i);
979 AttributeNode *setAttribute(const char *pcszName, int64_t i);
980 AttributeNode *setAttribute(const char *pcszName, uint64_t i);
981 AttributeNode *setAttributeHex(const char *pcszName, uint32_t i);
982 AttributeNode *setAttribute(const char *pcszName, bool f);
983
984 virtual ~ElementNode();
985
986protected:
987 // hide the default constructor so people use only our factory methods
988 ElementNode(const ElementNode *pElmRoot, Node *pParent, PRTLISTANCHOR pListAnchor, xmlNode *pLibNode);
989 ElementNode(const ElementNode &x); // no copying
990
991 /** We keep a pointer to the root element for attribute namespace handling. */
992 const ElementNode *m_pElmRoot;
993
994 /** List of child elements and content nodes. */
995 RTLISTANCHOR m_children;
996 /** List of attributes nodes. */
997 RTLISTANCHOR m_attributes;
998
999 static void buildChildren(ElementNode *pElmRoot);
1000
1001 friend class Node;
1002 friend class Document;
1003 friend class XmlFileParser;
1004};
1005
1006/**
1007 * Node subclass that represents content (non-element text).
1008 *
1009 * Since the Node constructor is private, one can create new content nodes
1010 * only through the following factory methods:
1011 *
1012 * -- ElementNode::addContent()
1013 */
1014class RT_DECL_CLASS ContentNode : public Node
1015{
1016public:
1017
1018protected:
1019 // hide the default constructor so people use only our factory methods
1020 ContentNode(Node *pParent, PRTLISTANCHOR pListAnchor, xmlNode *pLibNode);
1021 ContentNode(const ContentNode &x); // no copying
1022
1023 friend class Node;
1024 friend class ElementNode;
1025};
1026
1027
1028/**
1029 * Handy helper class with which one can loop through all or some children
1030 * of a particular element. See NodesLoop::forAllNodes() for details.
1031 */
1032class RT_DECL_CLASS NodesLoop
1033{
1034public:
1035 NodesLoop(const ElementNode &node, const char *pcszMatch = NULL);
1036 ~NodesLoop();
1037 const ElementNode* forAllNodes() const;
1038
1039private:
1040 /* Obscure class data */
1041 struct Data;
1042 Data *m;
1043};
1044
1045/**
1046 * The XML document class. An instance of this needs to be created by a user
1047 * of the XML classes and then passed to
1048 *
1049 * -- XmlMemParser or XmlFileParser to read an XML document; those classes then
1050 * fill the caller's Document with ElementNode, ContentNode and AttributeNode
1051 * instances. The typical sequence then is:
1052 * @code
1053 Document doc;
1054 XmlFileParser parser;
1055 parser.read("file.xml", doc);
1056 Element *pElmRoot = doc.getRootElement();
1057 @endcode
1058 *
1059 * -- XmlMemWriter or XmlFileWriter to write out an XML document after it has
1060 * been created and filled. Example:
1061 *
1062 * @code
1063 Document doc;
1064 Element *pElmRoot = doc.createRootElement();
1065 // add children
1066 xml::XmlFileWriter writer(doc);
1067 writer.write("file.xml", true);
1068 @endcode
1069 */
1070class RT_DECL_CLASS Document
1071{
1072public:
1073 Document();
1074 ~Document();
1075
1076 Document(const Document &x);
1077 Document& operator=(const Document &x);
1078
1079 const ElementNode* getRootElement() const;
1080 ElementNode* getRootElement();
1081
1082 ElementNode* createRootElement(const char *pcszRootElementName,
1083 const char *pcszComment = NULL);
1084
1085private:
1086 friend class XmlMemParser;
1087 friend class XmlFileParser;
1088 friend class XmlMemWriter;
1089 friend class XmlStringWriter;
1090 friend class XmlFileWriter;
1091
1092 void refreshInternals();
1093
1094 /* Obscure class data */
1095 struct Data;
1096 Data *m;
1097};
1098
1099/*
1100 * XmlParserBase
1101 *
1102 */
1103
1104class RT_DECL_CLASS XmlParserBase
1105{
1106protected:
1107 XmlParserBase();
1108 ~XmlParserBase();
1109
1110 xmlParserCtxtPtr m_ctxt;
1111};
1112
1113/*
1114 * XmlMemParser
1115 *
1116 */
1117
1118class RT_DECL_CLASS XmlMemParser : public XmlParserBase
1119{
1120public:
1121 XmlMemParser();
1122 ~XmlMemParser();
1123
1124 void read(const void* pvBuf, size_t cbSize, const RTCString &strFilename, Document &doc);
1125};
1126
1127/*
1128 * XmlFileParser
1129 *
1130 */
1131
1132class RT_DECL_CLASS XmlFileParser : public XmlParserBase
1133{
1134public:
1135 XmlFileParser();
1136 ~XmlFileParser();
1137
1138 void read(const RTCString &strFilename, Document &doc);
1139
1140private:
1141 /* Obscure class data */
1142 struct Data;
1143 struct Data *m;
1144
1145 static int ReadCallback(void *aCtxt, char *aBuf, int aLen) RT_NOTHROW_PROTO;
1146 static int CloseCallback(void *aCtxt) RT_NOTHROW_PROTO;
1147};
1148
1149/**
1150 * XmlMemWriter
1151 */
1152class RT_DECL_CLASS XmlMemWriter
1153{
1154public:
1155 XmlMemWriter();
1156 ~XmlMemWriter();
1157
1158 void write(const Document &doc, void** ppvBuf, size_t *pcbSize);
1159
1160private:
1161 void* m_pBuf;
1162};
1163
1164
1165/**
1166 * XmlStringWriter - writes the XML to an RTCString instance.
1167 */
1168class RT_DECL_CLASS XmlStringWriter
1169{
1170public:
1171 XmlStringWriter();
1172
1173 int write(const Document &rDoc, RTCString *pStrDst);
1174
1175private:
1176 static int WriteCallbackForSize(void *pvUser, const char *pachBuf, int cbToWrite) RT_NOTHROW_PROTO;
1177 static int WriteCallbackForReal(void *pvUser, const char *pachBuf, int cbToWrite) RT_NOTHROW_PROTO;
1178 static int CloseCallback(void *pvUser) RT_NOTHROW_PROTO;
1179
1180 /** Pointer to the destination string while we're in the write() call. */
1181 RTCString *m_pStrDst;
1182 /** Set by WriteCallback if we cannot grow the destination string. */
1183 bool m_fOutOfMemory;
1184};
1185
1186
1187/**
1188 * XmlFileWriter
1189 */
1190class RT_DECL_CLASS XmlFileWriter
1191{
1192public:
1193 XmlFileWriter(Document &doc);
1194 ~XmlFileWriter();
1195
1196 /**
1197 * Writes the XML document to the specified file.
1198 *
1199 * @param pcszFilename The name of the output file.
1200 * @param fSafe If @c true, some extra safety precautions will be
1201 * taken when writing the file:
1202 * -# The file is written with a '-tmp' suffix.
1203 * -# It is flushed to disk after writing.
1204 * -# Any original file is renamed to '-prev'.
1205 * -# The '-tmp' file is then renamed to the
1206 * specified name.
1207 * -# The directory changes are flushed to disk.
1208 * The suffixes are available via s_pszTmpSuff and
1209 * s_pszPrevSuff.
1210 */
1211 void write(const char *pcszFilename, bool fSafe);
1212
1213 static int WriteCallback(void *aCtxt, const char *aBuf, int aLen) RT_NOTHROW_PROTO;
1214 static int CloseCallback(void *aCtxt) RT_NOTHROW_PROTO;
1215
1216 /** The suffix used by XmlFileWriter::write() for the temporary file. */
1217 static const char * const s_pszTmpSuff;
1218 /** The suffix used by XmlFileWriter::write() for the previous (backup) file. */
1219 static const char * const s_pszPrevSuff;
1220
1221private:
1222 void writeInternal(const char *pcszFilename, bool fSafe);
1223
1224 /* Obscure class data */
1225 struct Data;
1226 Data *m;
1227};
1228
1229#if defined(_MSC_VER)
1230#pragma warning (default:4251)
1231#endif
1232
1233/** @} */
1234
1235} // end namespace xml
1236
1237#endif /* !IPRT_INCLUDED_cpp_xml_h */
1238
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette