VirtualBox

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

Last change on this file since 76557 was 76557, checked in by vboxsync, 6 years ago

include/iprt: Use IPRT_INCLUDED_ rather than _iprt_ as header guard prefix, letting scm enforce this (thereby avoiding copy&paste errors like rsa.h).

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