ui-gxmlcpp 1.4.6
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
23#include <ui-gxmlcpp/Buffer.hpp>
24#include <ui-gxmlcpp/Util.hpp>
25
26namespace UI {
27namespace GXML {
28
30class Tree: private UI::Util::auto_base<xmlDoc>
31{
32 friend class XSLTransTree;
33 friend class SchemaTree;
34 friend class RelaxNGTree;
35private:
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
49public:
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
79protected:
81 Tree();
82
85
86public:
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);
112private:
113 void dealloc();
114
115public:
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
145protected:
147 Tree & setDontFreeDoc(bool on=true);
148
149public:
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="/*");
385protected:
386 using UI::Util::auto_base<xmlDoc>::get;
387 using UI::Util::auto_base<xmlDoc>::operator->;
388
389private:
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
Adding code facility to Exception.
Definition Exception.hpp:35
OutputBuffer holder class.
Definition Buffer.hpp:53
RelaxNG Schema XML Tree.
Definition RelaxNGTree.hpp:27
XML Schema Tree.
Definition SchemaTree.hpp:27
Serializer for Tree.
Definition Tree.hpp:310
NodeSet serializer.
Definition Tree.hpp:270
int concat(std::string const &text)
Append a free text to the dump.
Definition Tree.cpp:520
Dump()
Default constructor, creates an empty dump.
Definition Tree.cpp:516
Vector of nodes with specialised support.
Definition Tree.hpp:241
std::string dump(bool const &format=false, std::string const &encoding=DefaultEncoding_) const
Dump shortcut.
Definition Tree.cpp:572
void unlink()
Unlink all nodes.
Definition Tree.cpp:489
std::string getContent(std::string const &nodeSeparator="") const
Get nodeset content.
Definition Tree.cpp:502
NodeSet getChilds(std::string const &name="") const
Get a nodeset with all childs of this set.
Definition Tree.cpp:497
A XML node.
Definition Tree.hpp:154
std::string getXPath() const
Get associated XPath.
Definition Tree.cpp:266
Node addChild(std::string const &name, std::string const &content="")
Add a new (element) child node.
Definition Tree.cpp:366
char const * getNameC() const
Definition Tree.cpp:272
Type getType() const
Get node type.
Definition Tree.cpp:261
void setName(std::string const &name)
Definition Tree.cpp:282
std::string getAttribute(std::string const &name) const
Like above, but return std::string.
Definition Tree.cpp:323
Node addSiblingAfter(std::string const &name, std::string const &content)
Add a new (element) sibling node.
Definition Tree.cpp:387
xmlElementType Type
Abstract from libxml2 type.
Definition Tree.hpp:177
NodeSet getChilds(std::string const &name="") const
Get all child nodes of this node.
Definition Tree.cpp:256
std::string dump(bool const &format=false, std::string const &encoding=DefaultEncoding_) const
Dump shortcut.
Definition Tree.cpp:418
NodeSet getNodeSet(std::string const &xpath) const
Definition Tree.cpp:337
bool isNull() const
Test if this is null ("not a tree node").
Definition Tree.cpp:221
Node getParent() const
Definition Tree.cpp:236
char const * hasAttribute(std::string const &name) const
Returns 0 if not found, else the attribute's content.
Definition Tree.cpp:311
void unlink()
Unlink this node (from underlying tree). Memory will be freed, and the node becomes a null node.
Definition Tree.cpp:226
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
XML tree.
Definition Tree.hpp:31
std::map< std::string, std::string > NamespaceMap
Structure holding namespaces for xPath expressions.
Definition Tree.hpp:62
FileConstructor
Helper enum for constructor from file name.
Definition Tree.hpp:103
static std::string const DefaultDocbase_
Default document base. This is used for from-memory parsing if not specified otherwise.
Definition Tree.hpp:74
static std::string const DefaultEncodings_
Comma-separated uppercase list of the name and all allowed aliases the default encoding.
Definition Tree.hpp:72
ErrorCode
Error codes for exceptions.
Definition Tree.hpp:52
Tree & setDontFreeDoc(bool on=true)
En/disable freeing of underlying libxml2 doc_ tree. Use with caution.
Definition Tree.cpp:204
bool operator==(Tree const &tree)
Comparison.
Definition Tree.cpp:136
NamespaceMap xPathNamespaces_
Prefixs and URIs for custom xml namespaces in xPath queries.
Definition Tree.hpp:84
Tree & setXPathNamespace(std::string const &prefix, std::string const &uri)
Set custom xml namespace known in xPath queries.
Definition Tree.cpp:187
std::string dump(bool const &format=false, std::string const &encoding=DefaultEncoding_) const
Tree dump shortcut.
Definition Tree.cpp:611
void unlink(std::string const &xpath)
Remove node(s) from tree.
Definition Tree.cpp:698
Tree & setTopLevelNamespaces()
Definition Tree.cpp:163
std::string getOrigEncoding() const
Get original document's encoding.
Definition Tree.cpp:621
Tree & operator=(Tree const &tree)
Copy operator.
Definition Tree.cpp:129
Tree & setContext(bool on=true)
(Un)configure the tree to use a fix xpath context.
Definition Tree.cpp:141
Tree()
Protected default constructor.
Definition Tree.cpp:74
CodeException< ErrorCode > Exception
Exceptions for this class.
Definition Tree.hpp:59
std::string formatDump(std::string const &encoding=DefaultEncoding_) const
As dump, but will reparse the tree first.
Definition Tree.cpp:616
friend class XMLTree
Compat only.
Definition Tree.hpp:77
void setName(std::string const &xpath, std::string const &name)
Set name of node(s).
Definition Tree.cpp:673
XPathContext * getContext() const
Get fixed context.
Definition Tree.cpp:158
Node getRootNode() const
Shortcut to get the root node.
Definition Tree.cpp:647
static std::string const DefaultEncoding_
Default encoding. This is used for any dumps if not specified otherwise.
Definition Tree.hpp:65
XPath context holder class.
Definition XPathContext.hpp:35
XPath object holder class.
Definition XPathObject.hpp:29
XSL translation result tree.
Definition XSLTransTree.hpp:36
Namespace for all Schlund+Partner Code.
Definition Buffer.cpp:30