VirtualBox

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

Last change on this file since 48862 was 48862, checked in by vboxsync, 11 years ago

build fix

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