Scopira  20080306
xml.h
1 
2 /*
3  * Copyright (c) 2003-2007 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INLUCDED__SCOPIRA_TOOL_XML_H_
15 #define __INLUCDED__SCOPIRA_TOOL_XML_H_
16 
17 #include <libxml/parser.h>
18 
19 #include <string>
20 
21 #include <scopira/tool/object.h>
22 #include <scopira/tool/export.h>
23 
24 // THIS FILE HAS BEEN FULLY DOCUMENTED
25 
26 namespace scopira
27 {
28  namespace tool
29  {
30  class xml_doc;
31  class xml_node;
32  class xml_attrib;
33  // forward
34  class property;
35  }
36 }
37 
38 //
39 // TIPS:
40 // use xmllint.
41 // or xmlstarlet val
42 //
43 
51 {
52  private:
53  xmlDocPtr dm_doc;
54 
55  public:
57 
62  xml_doc(void) : dm_doc(0) { }
64  ~xml_doc() { clear(); }
65 
67  xmlDocPtr get_ptr(void) const { return dm_doc; }
68 
73  bool is_null(void) const { return dm_doc == 0; }
78  bool is_valid(void) const { return dm_doc != 0; }
79 
84  const char * get_c_name(void) const { return dm_doc->name; }
89  std::string get_name(void) const
90  { return std::string(reinterpret_cast<const char*>(dm_doc->name)); }
91 
96  SCOPIRA_EXPORT void clear(void);
97 
102  SCOPIRA_EXPORT void init(const std::string &rootnodename);
103 
109  SCOPIRA_EXPORT void add_pi(const std::string &name, const std::string &content = "");
110 
119  SCOPIRA_EXPORT void load_ptr(xmlDocPtr newdoc);
120 
128  SCOPIRA_EXPORT bool load_file(const std::string &filename);
129 
136  SCOPIRA_EXPORT bool load_string(const std::string &data);
137 
144  SCOPIRA_EXPORT bool load_c_str(const char *str);
145 
151  SCOPIRA_EXPORT void load_prop(property *p);
152 
157  SCOPIRA_EXPORT xml_node get_root(void) const;
158 
166  SCOPIRA_EXPORT bool save_file(const std::string &filename, bool compress = false) const;
167 
174  SCOPIRA_EXPORT bool save_string(std::string &out) const;
175 
181  SCOPIRA_EXPORT void save_prop(property *p) const;
182 
183  private:
184  // diable
185  xml_doc(const xml_doc &) { }
186 };
187 
196 {
197  public:
199  enum {
200  element_c = XML_ELEMENT_NODE,
201  //attrib_c = XML_ATTRIBUTE_NODE,
202  text_c = XML_TEXT_NODE,
203  };
204 
205  private:
206  xmlNodePtr dm_node;
207 
208  public:
213  xml_node(void) : dm_node(0) { }
218  explicit xml_node(xmlNodePtr ptr) : dm_node(ptr) { }
219 
221  xmlNodePtr get_ptr(void) const { return dm_node; }
222 
227  int get_type(void) const { return dm_node->type; }
228 
233  const char * get_c_name(void) const
234  { return reinterpret_cast<const char*>(dm_node->name); }
239  std::string get_name(void) const
240  { return std::string(reinterpret_cast<const char*>(dm_node->name)); }
241 
248  const char * get_c_content(void) const
249  { return reinterpret_cast<const char *>(dm_node->content); }
250 
255  bool is_null(void) const { return dm_node == 0; }
260  bool is_valid(void) const { return dm_node != 0; }
261 
266  void next(void) { dm_node = dm_node->next; }
272  SCOPIRA_EXPORT void next_type(int typ);
278  SCOPIRA_EXPORT void next_name(const std::string &name);
279 
284  SCOPIRA_EXPORT int count_children(const std::string &name) const;
285 
292  { return xml_node(dm_node->children); }
298  SCOPIRA_EXPORT xml_node get_first_child_type(int typ) const;
304  SCOPIRA_EXPORT xml_node get_first_child_name(const std::string &name) const;
305 
310  SCOPIRA_EXPORT xml_attrib get_first_attrib(void) const;
319  SCOPIRA_EXPORT const char * get_attrib_c_text(const std::string &aname) const;
328  SCOPIRA_EXPORT const char * get_child_c_text(const std::string &nname) const;
329 
334  SCOPIRA_EXPORT bool has_attrib(const std::string &aname) const;
335 
340  SCOPIRA_EXPORT xml_node add_child(const std::string &name);
341 
346  SCOPIRA_EXPORT xml_node add_comment(const std::string &comment_contents);
347 
353  SCOPIRA_EXPORT void clear_this(void);
358  SCOPIRA_EXPORT void add_attrib(const std::string &key, const std::string &val);
363  SCOPIRA_EXPORT xml_node add_content_child(const std::string &content);
369  SCOPIRA_EXPORT void clear_content_children(void);
370 
379  SCOPIRA_EXPORT const char * get_text_c_content(void) const;
380 
393  SCOPIRA_EXPORT const char * get_leaf_text_content(const std::string &pathnodespec) const;
394 
395  // Mass loader helpers
396 
402  SCOPIRA_EXPORT void load_string_attrib(const std::string &name, std::string &outval);
407  template <class I>
408  void children_load_xml(const std::string &elementname, I iter) const {
409  for (xml_node sub = get_first_child_name(elementname);
410  sub.is_valid(); sub.next_name(elementname), ++iter)
411  (*iter).load_xml(sub);
412  }
417  template <class I>
418  void children_load_attrib(const std::string &elementname,
419  const std::string &attribname, I iter) const {
420  for (xml_node sub = get_first_child_name(elementname);
421  sub.is_valid(); sub.next_name(elementname), ++iter)
422  sub.load_string_attrib(attribname, *iter);
423  }
428  template <class I>
429  void children_load_text(const std::string &elementname, I iter) const {
430  for (xml_node sub = get_first_child_name(elementname);
431  sub.is_valid(); sub.next_name(elementname), ++iter)
432  *iter = sub.get_text_c_content();
433  }
434 };
435 
442 {
443  private:
444  xmlAttrPtr dm_attr;
445 
446  public:
451  xml_attrib(void) : dm_attr(0) { }
456  xml_attrib(xmlAttrPtr ptr) : dm_attr(ptr) { }
457 
459  xmlAttrPtr get_ptr(void) const { return dm_attr; }
460 
465  const char * get_c_name(void) const
466  { return reinterpret_cast<const char*>(dm_attr->name); }
471  std::string get_name(void) const
472  { return std::string(reinterpret_cast<const char*>(dm_attr->name)); }
473 
478  bool is_null(void) const { return dm_attr == 0; }
483  bool is_valid(void) const { return dm_attr != 0; }
484 
489  void next(void) { dm_attr = dm_attr->next; }
490 
496  SCOPIRA_EXPORT const char * get_text_c_content(void) const;
497 };
498 
499 #endif
500 
bool is_valid(void) const
Definition: xml.h:483
xml_doc(void)
ctor, creates a NULL doc, you should parse or init one
Definition: xml.h:62
bool is_null(void) const
Definition: xml.h:73
Definition: archiveflow.h:20
xmlNodePtr get_ptr(void) const
returns the internal pointer – use with care
Definition: xml.h:221
bool is_null(void) const
Definition: xml.h:255
int get_type(void) const
Definition: xml.h:227
void load_ptr(xmlDocPtr newdoc)
bool load_string(const std::string &data)
const char * get_c_content(void) const
Definition: xml.h:248
Definition: xml.h:195
Definition: object.h:71
bool save_file(const std::string &filename, bool compress=false) const
xml_node(void)
Definition: xml.h:213
bool load_c_str(const char *str)
void children_load_attrib(const std::string &elementname, const std::string &attribname, I iter) const
Definition: xml.h:418
std::string get_name(void) const
Definition: xml.h:239
const char * get_c_name(void) const
Definition: xml.h:233
void next(void)
Definition: xml.h:489
void load_prop(property *p)
Definition: xml.h:50
~xml_doc()
Destructor.
Definition: xml.h:64
void children_load_xml(const std::string &elementname, I iter) const
Definition: xml.h:408
void init(const std::string &rootnodename)
xml_attrib(xmlAttrPtr ptr)
Definition: xml.h:456
std::string get_name(void) const
Definition: xml.h:471
void save_prop(property *p) const
xmlAttrPtr get_ptr(void) const
returns the internal pointer – use with care
Definition: xml.h:459
xml_node get_first_child(void) const
Definition: xml.h:291
Definition: prop.h:38
const char * get_c_name(void) const
Definition: xml.h:84
bool is_valid(void) const
Definition: xml.h:78
bool is_valid(void) const
Definition: xml.h:260
xmlDocPtr get_ptr(void) const
returns the internal pointer – use with care
Definition: xml.h:67
void add_pi(const std::string &name, const std::string &content="")
Definition: xml.h:441
bool save_string(std::string &out) const
xml_node(xmlNodePtr ptr)
Definition: xml.h:218
xml_attrib(void)
Definition: xml.h:451
std::string get_name(void) const
Definition: xml.h:89
void next(void)
Definition: xml.h:266
bool is_null(void) const
Definition: xml.h:478
xml_node get_root(void) const
bool load_file(const std::string &filename)
void children_load_text(const std::string &elementname, I iter) const
Definition: xml.h:429
const char * get_c_name(void) const
Definition: xml.h:465