xml.h revision ffe12014e6efe10ff854b5254d99648a0a377961
/** @file
* IPRT - XML Helper APIs.
*/
/*
* Copyright (C) 2007-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
#ifndef ___iprt_xml_h
#define ___iprt_xml_h
#ifndef IN_RING3
# error "There are no XML APIs available in Ring-0 Context!"
#endif
/*#define USE_STD_LIST_FOR_CHILDREN*/
#include <list>
#include <memory>
/** @defgroup grp_rt_cpp_xml C++ XML support
* @ingroup grp_rt_cpp
* @{
*/
/* Forwards */
typedef struct _xmlParserInput xmlParserInput;
typedef xmlParserInput *xmlParserInputPtr;
typedef struct _xmlParserCtxt xmlParserCtxt;
typedef xmlParserCtxt *xmlParserCtxtPtr;
typedef xmlError *xmlErrorPtr;
/** @} */
{
/**
* @addtogroup grp_rt_cpp_xml
* @{
*/
// Exceptions
//////////////////////////////////////////////////////////////////////////////
{
{}
};
{
{}
};
{
};
// Logical errors
//////////////////////////////////////////////////////////////////////////////
{
};
{
};
{
};
{
};
// Runtime errors
//////////////////////////////////////////////////////////////////////////////
{
int rc() const
{
return mRC;
}
int mRC;
};
/**
* The Stream class is a base class for I/O streams.
*/
{
/**
* position is a zero-based byte offset from the beginning of the file.
*
* Throws ENotImplemented if this operation is not implemented for the
* given stream.
*/
/**
*
* @param aPos Zero-based byte offset from the beginning of the stream.
*
* Throws ENotImplemented if this operation is not implemented for the
* given stream.
*/
};
/**
* The Input class represents an input stream.
*
* This input stream is used to read the settings tree from.
* This is an abstract class that must be subclassed in order to fill it with
* useful functionality.
*/
{
/**
* Reads from the stream to the supplied buffer.
*
* @param aBuf Buffer to store read data to.
* @param aLen Buffer length.
*
* @return Number of bytes read.
*/
};
/**
*
*/
{
/**
* Writes to the stream from the supplied buffer.
*
* @param aBuf Buffer to write data from.
* @param aLen Buffer length.
*
* @return Number of bytes written.
*/
/**
* Truncates the stream from the current position and upto the end.
* The new file size will become exactly #pos() bytes.
*
* Throws ENotImplemented if this operation is not implemented for the
* given stream.
*/
};
//////////////////////////////////////////////////////////////////////////////
/**
* The File class is a stream implementation that reads from and writes to
* regular files.
*
* The File class uses IPRT File API for file operations. Note that IPRT File
* API is not thread-safe. This means that if you pass the same RTFILE handle to
* different File instances that may be simultaneously used on different
* threads, you should care about serialization; otherwise you will get garbage
* when reading from or writing to such File instances.
*/
{
/**
* Possible file access modes.
*/
/**
* Opens a file with the given name in the given mode. If @a aMode is Read
* or ReadWrite, the file must exist. If @a aMode is Write, the file must
* not exist. Otherwise, an EIPRTFailure excetion will be thrown.
*
* @param aMode File mode.
* @param aFileName File name.
* @param aFlushIt Whether to flush a writable file before closing it.
*/
/**
* Uses the given file handle to perform file operations. This file
* handle must be already open in necessary mode (read, or write, or mixed).
*
* beginning of the file on success.
*
* Note that the given file handle will not be automatically closed upon
* this object destruction.
*
* @note It you pass the same RTFILE handle to more than one File instance,
* please make sure you have provided serialization in case if these
* instasnces are to be simultaneously used by different threads.
* Otherwise you may get garbage when reading or writing.
*
* @param aHandle Open file handle.
* @param aFileName File name (for reference).
* @param aFlushIt Whether to flush a writable file before closing it.
*/
/**
* Destroys the File object. If the object was created from a file name
* the corresponding file will be automatically closed. If the object was
* created from a file handle, it will remain open.
*/
const char *uri() const;
/**
* See Input::read(). If this method is called in wrong file mode,
* LogicError will be thrown.
*/
/**
* See Output::write(). If this method is called in wrong file mode,
* LogicError will be thrown.
*/
/**
* See Output::truncate(). If this method is called in wrong file mode,
* LogicError will be thrown.
*/
void truncate();
/* Obscure class data */
struct Data;
Data *m;
/* auto_ptr data doesn't have proper copy semantics */
};
/**
* The MemoryBuf class represents a stream implementation that reads from the
* memory buffer.
*/
{
const char *uri() const;
/* Obscure class data */
struct Data;
Data *m;
/* auto_ptr data doesn't have proper copy semantics */
};
/*
* GlobalLock
*
*
*/
const char *aID,
{
GlobalLock();
~GlobalLock();
const char *aID,
/* Obscure class data. */
struct Data;
struct Data *m;
};
/**
* Node base class.
*
* Cannot be used directly, but ElementNode, ContentNode and AttributeNode
* derive from this. This does implement useful public methods though.
*
*
*/
{
~Node();
const char *getName() const;
const char *getPrefix() const;
const char *getNamespaceURI() const;
bool nameEquals(const char *pcsz) const
{
}
const char *getValue() const;
/** @name Introspection.
* @{ */
/** Is this an ElementNode instance.
* @returns true / false */
bool isElement() const
{
}
/** Is this an ContentNode instance.
* @returns true / false */
bool isContent() const
{
}
/** Is this an AttributeNode instance.
* @returns true / false */
bool isAttribute() const
{
}
int getLineNumber() const;
/** @} */
#ifndef USE_STD_LIST_FOR_CHILDREN
/** @name General tree enumeration.
*
* Use the introspection methods isElement() and isContent() before doing static
* casting. Parents are always or ElementNode type, but siblings and children
* can be of both ContentNode and ElementNode types.
*
* @remarks Careful mixing tree walking with node removal!
* @{
*/
/** Get the parent node
* @returns Pointer to the parent node, or NULL if root. */
{
return m_pParent;
}
/** Get the first child node.
* @returns Pointer to the first child node, NULL if no children. */
const Node *getFirstChild() const
{
}
/** Get the last child node.
* @returns Pointer to the last child node, NULL if no children. */
const Node *getLastChild() const
{
}
/** Get the previous sibling.
* @returns Pointer to the previous sibling node, NULL if first child. */
const Node *getPrevSibiling() const
{
if (!m_pParent)
return NULL;
}
/** Get the next sibling.
* @returns Pointer to the next sibling node, NULL if last child. */
const Node *getNextSibiling() const
{
if (!m_pParent)
return NULL;
}
/** @} */
#endif
/** Node types. */
const char *m_pcszNamespacePrefix; ///< not always set
const char *m_pcszNamespaceHref; ///< full http:// spec
const char *m_pcszName; ///< element or attribute name, points either into plibNode or plibAttr;
///< NULL if this is a content node
#ifndef USE_STD_LIST_FOR_CHILDREN
/** Child list entry of this node. (List head m_pParent->m_children.) */
/** Child elements, if this is an element; can be empty. */
#endif
// hide the default constructor so people use only our factory methods
/* Obscure class data */
struct Data;
Data *m;
};
/**
* Node subclass that represents an attribute of an element.
*
* For attributes, Node::getName() returns the attribute name, and Node::getValue()
* returns the attribute value, if any.
*
* Since the Node constructor is private, one can create new attribute nodes
* only through the following factory methods:
*
* -- ElementNode::setAttribute()
*/
{
// hide the default constructor so people use only our factory methods
const char **ppcszKey);
};
/**
* Node subclass that represents an element.
*
* For elements, Node::getName() returns the element name, and Node::getValue()
* returns the text contents, if any.
*
* Since the Node constructor is private, one can create element nodes
* only through the following factory methods:
*
* -- Document::createRootElement()
* -- ElementNode::createChild()
*/
{
const char *pcszMatch) const;
{
}
/** Finds the first decendant matching the name at the end of @a pcszPath and
* optionally namespace.
*
* @returns Pointer to the child string value, NULL if not found or no value.
* @param pcszPath The attribute name. Slashes can be used to make a
* simple path to any decendant.
* @param pcszNamespace The namespace to match, NULL (default) match any
* namespace. When using a path, this matches all
* elements along the way.
* @see findChildElement, findChildElementP
*/
/** Finds the first child with matching the give name and optionally namspace,
* returning its value.
*
* @returns Pointer to the child string value, NULL if not found or no value.
* @param pcszPath The attribute name. Slashes can be used to make a
* simple path to any decendant.
* @param pcszNamespace The namespace to match, NULL (default) match any
* namespace. When using a path, this matches all
* elements along the way.
* @see findChildElement, findChildElementP
*/
{
if (pElem)
return NULL;
}
/** @name Element enumeration.
* @{ */
/** Get the previous sibling element.
* @returns Pointer to the previous sibling element, NULL if first child
* element.
* @see getNextSibilingElement, getPrevSibling
*/
const ElementNode *getPrevSibilingElement() const;
/** Get the next sibling element.
* @returns Pointer to the next sibling element, NULL if last child element.
* @see getPrevSibilingElement, getNextSibling
*/
const ElementNode *getNextSibilingElement() const;
/** Find the previous element matching the given name and namespace (optionally).
* @returns Pointer to the previous sibling element, NULL if first child
* element.
* @param pcszName The element name to match.
* @param pcszNamespace The namespace name, default is NULL which means
* anything goes.
* @note Changed the order of the arguments.
*/
const ElementNode *findPrevSibilingElement(const char *pcszName, const char *pcszNamespace = NULL) const;
/** Find the next element matching the given name and namespace (optionally).
* @returns Pointer to the previous sibling element, NULL if first child
* element.
* @param pcszName The element name to match.
* @param pcszNamespace The namespace name, default is NULL which means
* anything goes.
* @note Changed the order of the arguments.
*/
const ElementNode *findNextSibilingElement(const char *pcszName, const char *pcszNamespace = NULL) const;
/** @} */
/** Find the first attribute with the given name, returning its value string.
* @returns Pointer to the attribute string value.
* @param pcszName The attribute name.
* @see getAttributeValue
*/
const char *findAttributeValue(const char *pcszName) const
{
if (pAttr)
return NULL;
}
bool getAttributeValue(const char *pcszMatch, bool &f) const;
/** @name Variants that for clarity does not use references for output params.
* @{ */
bool getAttributeValue(const char *pcszMatch, const char **ppcsz) const { return getAttributeValue(pcszMatch, *ppcsz); }
bool getAttributeValue(const char *pcszMatch, RTCString *pStr) const { return getAttributeValue(pcszMatch, *pStr); }
bool getAttributeValuePath(const char *pcszMatch, RTCString *pStr) const { return getAttributeValuePath(pcszMatch, *pStr); }
bool getAttributeValue(const char *pcszMatch, int32_t *pi) const { return getAttributeValue(pcszMatch, *pi); }
bool getAttributeValue(const char *pcszMatch, uint32_t *pu) const { return getAttributeValue(pcszMatch, *pu); }
bool getAttributeValue(const char *pcszMatch, int64_t *pi) const { return getAttributeValue(pcszMatch, *pi); }
bool getAttributeValue(const char *pcszMatch, uint64_t *pu) const { return getAttributeValue(pcszMatch, *pu); }
bool getAttributeValue(const char *pcszMatch, bool *pf) const { return getAttributeValue(pcszMatch, *pf); }
/** @} */
{
}
{
}
// hide the default constructor so people use only our factory methods
const ElementNode *m_pelmRoot;
};
/**
* Node subclass that represents content (non-element text).
*
* Since the Node constructor is private, one can create new content nodes
* only through the following factory methods:
*
* -- ElementNode::addContent()
*/
{
// hide the default constructor so people use only our factory methods
};
/**
* Handy helper class with which one can loop through all or some children
* of a particular element. See NodesLoop::forAllNodes() for details.
*/
{
~NodesLoop();
const ElementNode* forAllNodes() const;
/* Obscure class data */
struct Data;
Data *m;
};
/**
* The XML document class. An instance of this needs to be created by a user
* of the XML classes and then passed to
*
* -- XmlMemParser or XmlFileParser to read an XML document; those classes then
* fill the caller's Document with ElementNode, ContentNode and AttributeNode
* instances. The typical sequence then is:
* @code
Document doc;
XmlFileParser parser;
parser.read("file.xml", doc);
Element *pelmRoot = doc.getRootElement();
@endcode
*
* -- XmlMemWriter or XmlFileWriter to write out an XML document after it has
* been created and filled. Example:
*
* @code
Document doc;
Element *pelmRoot = doc.createRootElement();
// add children
xml::XmlFileWriter writer(doc);
writer.write("file.xml", true);
@endcode
*/
{
Document();
~Document();
const ElementNode* getRootElement() const;
const char *pcszComment = NULL);
void refreshInternals();
/* Obscure class data */
struct Data;
Data *m;
};
/*
* XmlParserBase
*
*/
{
~XmlParserBase();
};
/*
* XmlMemParser
*
*/
{
XmlMemParser();
~XmlMemParser();
};
/*
* XmlFileParser
*
*/
{
~XmlFileParser();
/* Obscure class data */
struct Data;
struct Data *m;
static int CloseCallback (void *aCtxt);
};
/*
* XmlMemParser
*
*/
{
XmlMemWriter();
~XmlMemWriter();
void* m_pBuf;
};
/*
* XmlFileWriter
*
*/
{
~XmlFileWriter();
/**
* Writes the XML document to the specified file.
*
* @param pcszFilename The name of the output file.
* @param fSafe If @c true, some extra safety precautions will be
* taken when writing the file:
* -# The file is written with a '-tmp' suffix.
* -# It is flushed to disk after writing.
* -# Any original file is renamed to '-prev'.
* -# The '-tmp' file is then renamed to the
* specified name.
* -# The directory changes are flushed to disk.
* The suffixes are available via s_pszTmpSuff and
* s_pszPrevSuff.
*/
static int CloseCallback(void *aCtxt);
/** The suffix used by XmlFileWriter::write() for the temporary file. */
static const char * const s_pszTmpSuff;
/** The suffix used by XmlFileWriter::write() for the previous (backup) file. */
static const char * const s_pszPrevSuff;
/* Obscure class data */
struct Data;
Data *m;
};
#if defined(_MSC_VER)
#pragma warning (default:4251)
#endif
/** @} */
} // end namespace xml
#endif /* !___iprt_xml_h */