ui-gxmlcpp  1.4.4
Tree.hpp
Go to the documentation of this file.
1 
10 #ifndef UI_GXML_TREE_HPP
11 #define UI_GXML_TREE_HPP
12 
13 // STDC++
14 #include <string>
15 #include <cassert>
16 #include <istream>
17 #include <vector>
18 
19 // C++ libraries
20 #include <ui-gxmlcpp/Exception.hpp>
23 #include <ui-gxmlcpp/Buffer.hpp>
24 #include <ui-gxmlcpp/Util.hpp>
25 
26 namespace UI {
27 namespace GXML {
28 
30 class Tree: private UI::Util::auto_base<xmlDoc>
31 {
32  friend class XSLTransTree;
33  friend class SchemaTree;
34  friend class RelaxNGTree;
35 private:
36  /* Create a libxml2 <tt>xmlDoc</tt>.
37  *
38  * @param xmlBuffer Null-terminated C string or buffer of length len.
39  * @param base XML Base (@see RFC 2396).
40  * @param len Len of buffer. If not given, xmlBuffer will be treated as null-terminated c string.
41  * @param options Parse options; To avoid segfaults later, use the
42  * XML=XML_PARSE_NOBLANKS
43  * XSL=XML_PARSE_NOBLANKS | XSLT_PARSE_OPTIONS
44  */
45  static xmlDoc * create(char const * xml, int len, std::string const & base, int const options);
46  static xmlDoc * create(xmlDoc const * const doc);
47  static xmlDoc * create(std::string const & file, int const options);
48 
49 public:
51  enum ErrorCode
52  {
53  Parse_,
54  Mem_,
55  NoNode_,
56  Internal_
57  };
60 
62  typedef std::map<std::string, std::string> NamespaceMap;
63 
65  static std::string const DefaultEncoding_;
72  static std::string const DefaultEncodings_;
74  static std::string const DefaultDocbase_;
75 
77  friend class XMLTree;
78 
79 protected:
81  Tree();
82 
85 
86 public:
88  friend class XPathContext;
89 
93  Tree(char const * xml, int len=-1, std::string const & base=DefaultDocbase_, int const options=0);
94 
96  Tree(std::string const & xml, std::string const & base=DefaultDocbase_, int const options=0);
97 
99  Tree(std::istream & xml, std::string const & base=DefaultDocbase_, int const options=0);
100 
103  {
104  File_
105  };
107  Tree(FileConstructor const & dummy, std::string const & file, int const options=0);
109  Tree(Tree const & tree);
112 private:
113  void dealloc();
114 
115 public:
116  ~Tree();
117 
119  Tree & operator=(Tree const & tree);
120 
125  bool operator==(Tree const & tree);
126 
130  Tree & setContext(bool on=true);
131 
136  XPathContext * getContext() const;
141 
143  Tree & setXPathNamespace(std::string const & prefix, std::string const & uri);
144 
145 protected:
147  Tree & setDontFreeDoc(bool on=true);
148 
149 public:
150  class NodeSet;
151 
153  class Node: private UI::Util::auto_base<xmlNode>
154  {
155  friend class NodeSet;
156  public:
158  Node(xmlNode * const node=0);
160  bool isNull() const;
162  void unlink();
163 
167  Node getParent() const;
168  Node getChild() const;
169  Node getNext() const;
170  Node getPrev() const;
174  NodeSet getChilds(std::string const & name="") const;
175 
177  typedef xmlElementType Type;
178 
180  Type getType() const;
181 
183  std::string getXPath() const;
184 
188  char const * getNameC() const;
189  std::string getName() const;
190  void setName(std::string const & name);
195  char const * getContentC() const;
196  std::string getContent() const;
197  void setContent(std::string const & content);
201  char const * hasAttribute(std::string const & name) const;
203  char const * getAttributeC(std::string const & name) const;
205  std::string getAttribute(std::string const & name) const;
206  void setAttribute(std::string const & name, std::string const & content);
207 
210  NodeSet getNodeSet(std::string const & xpath) const;
211  Node getNode(std::string const & xpath, bool const & doThrow=true) const;
214  private:
215  Node copy() const;
216 
217  public:
219  Node addChild(std::string const & name, std::string const & content="");
221  Node addChild(Node const & n);
222 
224  Node addSiblingAfter(std::string const & name, std::string const & content);
226  Node addSiblingAfter(Node const & n);
227 
229  std::string dump(bool const & format=false, std::string const & encoding=DefaultEncoding_) const;
230 
232  using UI::Util::auto_base<xmlNode>::get;
233  using UI::Util::auto_base<xmlNode>::operator->;
234 
235  private:
236  static xmlNode * const NullNode_;
237  };
238 
240  class NodeSet: public std::vector<Node>
241  {
242  public:
248  NodeSet(XPathObject const & xPathObject);
249  NodeSet(Node const & node=Node(0), bool const & childs=false, std::string const & name="");
250  NodeSet(NodeSet const & ns, bool const & childs=false, std::string const & name="");
251  void add(Node const & node, bool const & childs=false, std::string const & name="");
252  void add(NodeSet const & ns, bool const & childs=false, std::string const & name="");
259  void unlink();
261  NodeSet getChilds(std::string const & name="") const;
266  std::string getContent(std::string const & nodeSeparator="") const;
267 
269  class Dump
270  {
271  public:
273  Dump();
274 
276  int concat(std::string const & text);
277 
279  int concat(Node const & node,
280  bool const & format=false, std::string const & encoding=DefaultEncoding_);
281 
283  Dump(Node const & node,
284  bool const & format=false, std::string const & encoding=DefaultEncoding_);
285 
287  int concat(NodeSet const & nodeSet,
288  bool const & format=false, std::string const & encoding=DefaultEncoding_);
289 
291  Dump(NodeSet const & ns,
292  bool const & format=false, std::string const & encoding=DefaultEncoding_);
293 
296  char const * getC() const;
297  std::string get() const;
298  int getSize() const;
300  private:
301  OutputBuffer outbuf_;
302  };
303 
305  std::string dump(bool const & format=false, std::string const & encoding=DefaultEncoding_) const;
306  };
307 
309  class Dump
310  {
311  public:
313  Dump(Tree const & tree, bool const & format=false, std::string const & encoding=DefaultEncoding_);
314  ~Dump();
317  char const * getC() const;
318  std::string get() const;
319  int getSize() const;
321  private:
322  xmlChar * buf_;
323  int size_;
324  };
325 
327  std::string dump(bool const & format=false, std::string const & encoding=DefaultEncoding_) const;
328 
334  std::string formatDump(std::string const & encoding=DefaultEncoding_) const;
335 
338  XPathObject getXPathObject(std::string const & xpath) const;
339  NodeSet getNodeSet(std::string const & xpath) const;
340  Node getNode(std::string const & xpath, bool const & doThrow=true) const;
342  Node getRootNode() const;
346  std::string getOrigEncoding() const;
347 
350  int getCount(std::string const & xpath) const;
351  bool exists(std::string const & xpath, int const times=1) const;
356  char const * getContentC(std::string const & xpath) const;
357  std::string getContent(std::string const & xpath) const;
358  void setContent(std::string const & xpath, std::string const & content);
362  void setName(std::string const & xpath, std::string const & name);
363 
366  void setAttribute(std::string const & xpath, std::string const & name, std::string const & content);
367  std::string getAttribute(std::string const & xpath, std::string const & name) const;
372  Node addChild(std::string const & xpath, std::string const & name, std::string const & content="");
373  Node addChild(std::string const & xpath, Node const & node);
377  void unlink(std::string const & xpath);
378 
381  void addTree(std::string const & xpath, Tree const & source, std::string const & sourceXPath="/*");
382  void addXML(std::string const & xpath, std::string const & source, std::string const & sourceXPath="/*");
385 protected:
386  using UI::Util::auto_base<xmlDoc>::get;
387  using UI::Util::auto_base<xmlDoc>::operator->;
388 
389 private:
391  class XPathContextSwitch
392  {
393  public:
394  XPathContextSwitch(Tree const & tree, NamespaceMap const & namespaces);
395  ~XPathContextSwitch();
396  XPathContext & get();
397  private:
398  XPathContext * treeContext_;
399  XPathContext * tempContext_;
400  };
401 
402  bool dontFreeDoc_;
403  XPathContext * context_;
404 };
405 
406 }}
407 #endif
UI::GXML::Tree::getContext
XPathContext * getContext() const
Get fixed context.
Definition: Tree.cpp:158
UI::GXML::auto_xmlfree
Control freeing of memory via xmlFree.
Definition: Util.hpp:27
XPathObject.hpp
UI::GXML::SchemaTree
XML Schema Tree.
Definition: SchemaTree.hpp:26
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::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::Type
xmlElementType Type
Abstract from libxml2 type.
Definition: Tree.hpp:177
UI::GXML::Tree::Dump
Serializer for Tree.
Definition: Tree.hpp:309
UI::GXML::Tree::getOrigEncoding
std::string getOrigEncoding() const
Get original document's encoding.
Definition: Tree.cpp:621
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::setTopLevelNamespaces
Tree & setTopLevelNamespaces()
Definition: Tree.cpp:163
UI::GXML::XPathObject
XPath object holder class.
Definition: XPathObject.hpp:28
UI::GXML::Tree::XPathContext
friend class XPathContext
XPathContext needs access to libxml2 doc ptr.
Definition: Tree.hpp:88
UI::GXML::XPathContext
XPath context holder class.
Definition: XPathContext.hpp:34
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::FileConstructor
FileConstructor
Helper enum for constructor from file name.
Definition: Tree.hpp:102
UI::GXML::Tree::DefaultEncoding_
static const std::string DefaultEncoding_
Default encoding. This is used for any dumps if not specified otherwise.
Definition: Tree.hpp:65
UI::GXML::Tree::Node::Node
Node(xmlNode *const node=0)
Constructor from libxml2 node.
Definition: Tree.cpp:216
XPathContext.hpp
UI::GXML::RelaxNGTree
RelaxNG Schema XML Tree.
Definition: RelaxNGTree.hpp:26
Tree.hpp
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::getNodeSet
NodeSet getNodeSet(std::string const &xpath) const
Definition: Tree.cpp:337
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::hasAttribute
const char * hasAttribute(std::string const &name) const
Returns 0 if not found, else the attribute's content.
Definition: Tree.cpp:311
UI
Namespace for all Schlund+Partner Code.
Definition: Buffer.cpp:30
UI::GXML::CodeException
Adding code facility to Exception.
Definition: Exception.hpp:34
UI::GXML::Tree::ErrorCode
ErrorCode
Error codes for exceptions.
Definition: Tree.hpp:51
UI::GXML::Tree::Node::getAttributeC
const char * 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::Node::getXPath
std::string getXPath() const
Get associated XPath.
Definition: Tree.cpp:266
UI::GXML::XSLTransTree
XSL translation result tree.
Definition: XSLTransTree.hpp:35
UI::GXML::Tree::Node::getNameC
const char * getNameC() const
Definition: Tree.cpp:272
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::setContext
Tree & setContext(bool on=true)
(Un)configure the tree to use a fix xpath context.
Definition: Tree.cpp:141
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::NamespaceMap
std::map< std::string, std::string > NamespaceMap
Structure holding namespaces for xPath expressions.
Definition: Tree.hpp:62
UI::GXML::Tree::Exception
CodeException< ErrorCode > Exception
Exceptions for this class.
Definition: Tree.hpp:59
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::DefaultEncodings_
static const std::string DefaultEncodings_
Comma-separated uppercase list of the name and all allowed aliases the default encoding.
Definition: Tree.hpp:72
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
XML tree.
Definition: Tree.hpp:30
UI::GXML::OutputBuffer
OutputBuffer holder class.
Definition: Buffer.hpp:52
UI::GXML::Tree::operator=
Tree & operator=(Tree const &tree)
Copy operator.
Definition: Tree.cpp:129
UI::GXML::Tree::DefaultDocbase_
static const std::string DefaultDocbase_
Default document base. This is used for from-memory parsing if not specified otherwise.
Definition: Tree.hpp:74
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::Node::setName
void setName(std::string const &name)
Definition: Tree.cpp:282
UI::GXML::Tree::NodeSet::unlink
void unlink()
Unlink all nodes.
Definition: Tree.cpp:489
UI::GXML::Tree::NodeSet::Dump::Dump
Dump()
Default constructor, creates an empty dump.
Definition: Tree.cpp:516
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
Util.hpp
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::NodeSet::getContent
std::string getContent(std::string const &nodeSeparator="") const
Get nodeset content.
Definition: Tree.cpp:502
Exception.hpp
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::getRootNode
Node getRootNode() const
Shortcut to get the root node.
Definition: Tree.cpp:647
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::XMLTree
friend class XMLTree
Compat only.
Definition: Tree.hpp:77
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::Node
A XML node.
Definition: Tree.hpp:153
UI::GXML::Tree::Node::getType
Type getType() const
Get node type.
Definition: Tree.cpp:261
Buffer.hpp
UI::GXML::Tree::Tree
Tree()
Protected default constructor.
Definition: Tree.cpp:74
UI::GXML::Tree::NodeSet
Vector of nodes with specialised support.
Definition: Tree.hpp:240
UI::GXML::Tree::operator==
bool operator==(Tree const &tree)
Comparison.
Definition: Tree.cpp:136
UI::GXML::Tree::unlink
void unlink(std::string const &xpath)
Remove node(s) from tree.
Definition: Tree.cpp:698
UI::GXML::Tree::NodeSet::Dump
NodeSet serializer.
Definition: Tree.hpp:269