Tree.hpp
Go to the documentation of this file.
#ifndef UI_GXML_TREE_HPP
#define UI_GXML_TREE_HPP
// STDC++
#include <string>
#include <cassert>
#include <istream>
#include <vector>
// C++ libraries
#include <ui-gxmlcpp/Exception.hpp>
#include <ui-gxmlcpp/XPathContext.hpp>
#include <ui-gxmlcpp/XPathObject.hpp>
#include <ui-gxmlcpp/Buffer.hpp>
#include <ui-gxmlcpp/Util.hpp>
namespace UI {
namespace GXML {
class Tree: private UI::Util::auto_base<xmlDoc>
{
friend class XSLTransTree;
friend class SchemaTree;
friend class RelaxNGTree;
private:
/* Create a libxml2 <tt>xmlDoc</tt>.
*
* @param xmlBuffer Null-terminated C string or buffer of length len.
* @param base XML Base (@see RFC 2396).
* @param len Len of buffer. If not given, xmlBuffer will be treated as null-terminated c string.
* @param options Parse options; To avoid segfaults later, use the
* XML=XML_PARSE_NOBLANKS
* XSL=XML_PARSE_NOBLANKS | XSLT_PARSE_OPTIONS
*/
static xmlDoc * create(char const * xml, int len, std::string const & base, int const options);
static xmlDoc * create(xmlDoc const * const doc);
static xmlDoc * create(std::string const & file, int const options);
public:
enum ErrorCode
{
Parse_,
Mem_,
NoNode_,
Internal_
};
typedef CodeException<ErrorCode> Exception;
typedef std::map<std::string, std::string> NamespaceMap;
static std::string const DefaultEncoding_;
static std::string const DefaultEncodings_;
static std::string const DefaultDocbase_;
friend class XMLTree;
protected:
Tree();
NamespaceMap xPathNamespaces_;
public:
friend class XPathContext;
Tree(char const * xml, int len=-1, std::string const & base=DefaultDocbase_, int const options=0);
Tree(std::string const & xml, std::string const & base=DefaultDocbase_, int const options=0);
Tree(std::istream & xml, std::string const & base=DefaultDocbase_, int const options=0);
enum FileConstructor
{
File_
};
Tree(FileConstructor const & dummy, std::string const & file, int const options=0);
Tree(Tree const & tree);
private:
void dealloc();
public:
~Tree();
Tree & operator=(Tree const & tree);
bool operator==(Tree const & tree);
Tree & setContext(bool on=true);
XPathContext * getContext() const;
Tree & setTopLevelNamespaces();
Tree & setXPathNamespace(std::string const & prefix, std::string const & uri);
protected:
Tree & setDontFreeDoc(bool on=true);
public:
class NodeSet;
class Node: private UI::Util::auto_base<xmlNode>
{
friend class NodeSet;
public:
Node(xmlNode * const node=0);
bool isNull() const;
void unlink();
Node getParent() const;
Node getChild() const;
Node getNext() const;
Node getPrev() const;
NodeSet getChilds(std::string const & name="") const;
typedef xmlElementType Type;
Type getType() const;
std::string getXPath() const;
char const * getNameC() const;
std::string getName() const;
void setName(std::string const & name);
char const * getContentC() const;
std::string getContent() const;
void setContent(std::string const & content);
char const * hasAttribute(std::string const & name) const;
char const * getAttributeC(std::string const & name) const;
std::string getAttribute(std::string const & name) const;
void setAttribute(std::string const & name, std::string const & content);
NodeSet getNodeSet(std::string const & xpath) const;
Node getNode(std::string const & xpath, bool const & doThrow=true) const;
private:
Node copy() const;
public:
Node addChild(std::string const & name, std::string const & content="");
Node addChild(Node const & n);
Node addSiblingAfter(std::string const & name, std::string const & content);
Node addSiblingAfter(Node const & n);
std::string dump(bool const & format=false, std::string const & encoding=DefaultEncoding_) const;
using UI::Util::auto_base<xmlNode>::get;
using UI::Util::auto_base<xmlNode>::operator->;
private:
static xmlNode * const NullNode_;
};
class NodeSet: public std::vector<Node>
{
public:
NodeSet(XPathObject const & xPathObject);
NodeSet(Node const & node=Node(0), bool const & childs=false, std::string const & name="");
NodeSet(NodeSet const & ns, bool const & childs=false, std::string const & name="");
void add(Node const & node, bool const & childs=false, std::string const & name="");
void add(NodeSet const & ns, bool const & childs=false, std::string const & name="");
void unlink();
NodeSet getChilds(std::string const & name="") const;
std::string getContent(std::string const & nodeSeparator="") const;
class Dump
{
public:
Dump();
int concat(std::string const & text);
int concat(Node const & node,
bool const & format=false, std::string const & encoding=DefaultEncoding_);
Dump(Node const & node,
bool const & format=false, std::string const & encoding=DefaultEncoding_);
int concat(NodeSet const & nodeSet,
bool const & format=false, std::string const & encoding=DefaultEncoding_);
Dump(NodeSet const & ns,
bool const & format=false, std::string const & encoding=DefaultEncoding_);
char const * getC() const;
std::string get() const;
int getSize() const;
private:
OutputBuffer outbuf_;
};
std::string dump(bool const & format=false, std::string const & encoding=DefaultEncoding_) const;
};
class Dump
{
public:
Dump(Tree const & tree, bool const & format=false, std::string const & encoding=DefaultEncoding_);
~Dump();
char const * getC() const;
std::string get() const;
int getSize() const;
private:
xmlChar * buf_;
int size_;
};
std::string dump(bool const & format=false, std::string const & encoding=DefaultEncoding_) const;
std::string formatDump(std::string const & encoding=DefaultEncoding_) const;
XPathObject getXPathObject(std::string const & xpath) const;
NodeSet getNodeSet(std::string const & xpath) const;
Node getNode(std::string const & xpath, bool const & doThrow=true) const;
Node getRootNode() const;
std::string getOrigEncoding() const;
int getCount(std::string const & xpath) const;
bool exists(std::string const & xpath, int const times=1) const;
char const * getContentC(std::string const & xpath) const;
std::string getContent(std::string const & xpath) const;
void setContent(std::string const & xpath, std::string const & content);
void setName(std::string const & xpath, std::string const & name);
void setAttribute(std::string const & xpath, std::string const & name, std::string const & content);
std::string getAttribute(std::string const & xpath, std::string const & name) const;
Node addChild(std::string const & xpath, std::string const & name, std::string const & content="");
Node addChild(std::string const & xpath, Node const & node);
void unlink(std::string const & xpath);
void addTree(std::string const & xpath, Tree const & source, std::string const & sourceXPath="/*");
void addXML(std::string const & xpath, std::string const & source, std::string const & sourceXPath="/*");
protected:
using UI::Util::auto_base<xmlDoc>::get;
using UI::Util::auto_base<xmlDoc>::operator->;
private:
class XPathContextSwitch
{
public:
XPathContextSwitch(Tree const & tree, NamespaceMap const & namespaces);
~XPathContextSwitch();
XPathContext & get();
private:
XPathContext * treeContext_;
XPathContext * tempContext_;
};
bool dontFreeDoc_;
XPathContext * context_;
};
}}
#endif
Buffer.hpp
Exception.hpp
Util.hpp
XPathContext.hpp
XPathObject.hpp
UI::GXML::CodeException
Adding code facility to Exception.
Definition Exception.hpp:35
UI::GXML::OutputBuffer
OutputBuffer holder class.
Definition Buffer.hpp:53
UI::GXML::Tree::Dump::Dump
Dump(Tree const &tree, bool const &format=false, std::string const &encoding=DefaultEncoding_)
Constructor.
Definition Tree.cpp:581
UI::GXML::Tree::NodeSet::Dump::concat
int concat(std::string const &text)
Append a free text to the dump.
Definition Tree.cpp:520
UI::GXML::Tree::NodeSet::Dump::Dump
Dump()
Default constructor, creates an empty dump.
Definition Tree.cpp:516
UI::GXML::Tree::NodeSet
Vector of nodes with specialised support.
Definition Tree.hpp:241
UI::GXML::Tree::NodeSet::dump
std::string dump(bool const &format=false, std::string const &encoding=DefaultEncoding_) const
Dump shortcut.
Definition Tree.cpp:572
UI::GXML::Tree::NodeSet::unlink
void unlink()
Unlink all nodes.
Definition Tree.cpp:489
UI::GXML::Tree::NodeSet::getContent
std::string getContent(std::string const &nodeSeparator="") const
Get nodeset content.
Definition Tree.cpp:502
UI::GXML::Tree::NodeSet::getChilds
NodeSet getChilds(std::string const &name="") const
Get a nodeset with all childs of this set.
Definition Tree.cpp:497
UI::GXML::Tree::Node
A XML node.
Definition Tree.hpp:154
UI::GXML::Tree::Node::getXPath
std::string getXPath() const
Get associated XPath.
Definition Tree.cpp:266
UI::GXML::Tree::Node::addChild
Node addChild(std::string const &name, std::string const &content="")
Add a new (element) child node.
Definition Tree.cpp:366
UI::GXML::Tree::Node::getNameC
char const * getNameC() const
Definition Tree.cpp:272
UI::GXML::Tree::Node::getType
Type getType() const
Get node type.
Definition Tree.cpp:261
UI::GXML::Tree::Node::setName
void setName(std::string const &name)
Definition Tree.cpp:282
UI::GXML::Tree::Node::getAttribute
std::string getAttribute(std::string const &name) const
Like above, but return std::string.
Definition Tree.cpp:323
UI::GXML::Tree::Node::Node
Node(xmlNode *const node=0)
Constructor from libxml2 node.
Definition Tree.cpp:216
UI::GXML::Tree::Node::addSiblingAfter
Node addSiblingAfter(std::string const &name, std::string const &content)
Add a new (element) sibling node.
Definition Tree.cpp:387
UI::GXML::Tree::Node::Type
xmlElementType Type
Abstract from libxml2 type.
Definition Tree.hpp:177
UI::GXML::Tree::Node::getChilds
NodeSet getChilds(std::string const &name="") const
Get all child nodes of this node.
Definition Tree.cpp:256
UI::GXML::Tree::Node::dump
std::string dump(bool const &format=false, std::string const &encoding=DefaultEncoding_) const
Dump shortcut.
Definition Tree.cpp:418
UI::GXML::Tree::Node::getNodeSet
NodeSet getNodeSet(std::string const &xpath) const
Definition Tree.cpp:337
UI::GXML::Tree::Node::isNull
bool isNull() const
Test if this is null ("not a tree node").
Definition Tree.cpp:221
UI::GXML::Tree::Node::getParent
Node getParent() const
Definition Tree.cpp:236
UI::GXML::Tree::Node::hasAttribute
char const * hasAttribute(std::string const &name) const
Returns 0 if not found, else the attribute's content.
Definition Tree.cpp:311
UI::GXML::Tree::Node::unlink
void unlink()
Unlink this node (from underlying tree). Memory will be freed, and the node becomes a null node.
Definition Tree.cpp:226
UI::GXML::Tree::Node::getAttributeC
char const * getAttributeC(std::string const &name) const
Like above, but always return valid c string (length 0 if not found).
Definition Tree.cpp:317
UI::GXML::Tree
XML tree.
Definition Tree.hpp:31
UI::GXML::Tree::NamespaceMap
std::map< std::string, std::string > NamespaceMap
Structure holding namespaces for xPath expressions.
Definition Tree.hpp:62
UI::GXML::Tree::FileConstructor
FileConstructor
Helper enum for constructor from file name.
Definition Tree.hpp:103
UI::GXML::Tree::DefaultDocbase_
static std::string const DefaultDocbase_
Default document base. This is used for from-memory parsing if not specified otherwise.
Definition Tree.hpp:74
UI::GXML::Tree::DefaultEncodings_
static std::string const DefaultEncodings_
Comma-separated uppercase list of the name and all allowed aliases the default encoding.
Definition Tree.hpp:72
UI::GXML::Tree::XPathContext
friend class XPathContext
XPathContext needs access to libxml2 doc ptr.
Definition Tree.hpp:88
UI::GXML::Tree::ErrorCode
ErrorCode
Error codes for exceptions.
Definition Tree.hpp:52
UI::GXML::Tree::setDontFreeDoc
Tree & setDontFreeDoc(bool on=true)
En/disable freeing of underlying libxml2 doc_ tree. Use with caution.
Definition Tree.cpp:204
UI::GXML::Tree::operator==
bool operator==(Tree const &tree)
Comparison.
Definition Tree.cpp:136
UI::GXML::Tree::xPathNamespaces_
NamespaceMap xPathNamespaces_
Prefixs and URIs for custom xml namespaces in xPath queries.
Definition Tree.hpp:84
UI::GXML::Tree::setXPathNamespace
Tree & setXPathNamespace(std::string const &prefix, std::string const &uri)
Set custom xml namespace known in xPath queries.
Definition Tree.cpp:187
UI::GXML::Tree::dump
std::string dump(bool const &format=false, std::string const &encoding=DefaultEncoding_) const
Tree dump shortcut.
Definition Tree.cpp:611
UI::GXML::Tree::unlink
void unlink(std::string const &xpath)
Remove node(s) from tree.
Definition Tree.cpp:698
UI::GXML::Tree::setTopLevelNamespaces
Tree & setTopLevelNamespaces()
Definition Tree.cpp:163
UI::GXML::Tree::getOrigEncoding
std::string getOrigEncoding() const
Get original document's encoding.
Definition Tree.cpp:621
UI::GXML::Tree::operator=
Tree & operator=(Tree const &tree)
Copy operator.
Definition Tree.cpp:129
UI::GXML::Tree::setContext
Tree & setContext(bool on=true)
(Un)configure the tree to use a fix xpath context.
Definition Tree.cpp:141
UI::GXML::Tree::Tree
Tree()
Protected default constructor.
Definition Tree.cpp:74
UI::GXML::Tree::Exception
CodeException< ErrorCode > Exception
Exceptions for this class.
Definition Tree.hpp:59
UI::GXML::Tree::formatDump
std::string formatDump(std::string const &encoding=DefaultEncoding_) const
As dump, but will reparse the tree first.
Definition Tree.cpp:616
UI::GXML::Tree::XMLTree
friend class XMLTree
Compat only.
Definition Tree.hpp:77
UI::GXML::Tree::setName
void setName(std::string const &xpath, std::string const &name)
Set name of node(s).
Definition Tree.cpp:673
UI::GXML::Tree::getContext
XPathContext * getContext() const
Get fixed context.
Definition Tree.cpp:158
UI::GXML::Tree::getRootNode
Node getRootNode() const
Shortcut to get the root node.
Definition Tree.cpp:647
UI::GXML::Tree::DefaultEncoding_
static std::string const DefaultEncoding_
Default encoding. This is used for any dumps if not specified otherwise.
Definition Tree.hpp:65
UI::GXML::XPathContext
XPath context holder class.
Definition XPathContext.hpp:35
UI::GXML::XPathObject
XPath object holder class.
Definition XPathObject.hpp:29
UI::GXML
Namespace for library project "ui-gxmlcpp".
Definition Buffer.cpp:31
UI
Namespace for all Schlund+Partner Code.
Definition Buffer.cpp:30