ClockWork DB CoreAPI 1.0.48
Abstract Time Series and Storage/Management Library
Loading...
Searching...
No Matches
xml_config.hpp
1#ifndef HAVE_TOM__XML_CONFIG_HPP
2#define HAVE_TOM__XML_CONFIG_HPP
3
4#include <tom-util/defines.hpp>
5#include <tom-util/scalar.hpp>
6#include <libxml/xpath.h>
7#include <libxml/parser.h>
8#include <vector>
9#include <map>
10#include <boost/shared_ptr.hpp>
11
12namespace tom {
13
14class xml_doc_factory;
15class xml_node;
16class xml_doc;
17
18typedef std::vector< xml_node > xml_node_set;
19typedef xml_node_set::const_iterator xml_node_set_it;
20
21class TOM_UTIL_API xml_doc_factory
22{
23 public:
24 static xml_doc doc_from_file( const char * path );
25 static xml_doc doc_from_file( const tom::observation & path );
26
27 // call to clean up libxml2 resources...this is called when the environment
28 // goes out of scope so users can ignore this
29 static void finalize( );
30 protected:
31 static void init( );
32 static bool s_is_init;
33};
34
35class TOM_UTIL_API xml_doc
36{
37public:
39 {
40 public:
41 explicit xmlDocPtrHandle( xmlDocPtr doc ) : m_doc( doc ) { }
42 xmlDocPtr
43 doc( ) const { return m_doc; }
44 ~xmlDocPtrHandle ( ) { xmlFreeDoc( m_doc ); }
45
46 private:
50 operator=( const xmlDocPtrHandle & );
51
52 xmlDocPtr m_doc;
53 };
54
55 xml_doc( ) : m_xmlDocPtr(), m_namespaces_init(false) { }
56 xml_doc( xmlDocPtr doc ) : m_xmlDocPtr( new xmlDocPtrHandle(doc) ), m_namespaces_init(false) { }
57 virtual ~xml_doc( );
58
59 xml_doc( const xml_doc &rhs ) : m_xmlDocPtr( rhs.m_xmlDocPtr ), m_namespaces_init(false) { }
60 xml_doc &
61 operator=( const xml_doc &rhs )
62 {
63 m_xmlDocPtr = rhs.m_xmlDocPtr;
64 m_namespaces_init = false;
65 return *this;
66 }
67
68 void doc_to_file(const char * path, bool formatted=false);
69 bool valid() const { return m_xmlDocPtr == NULL ? false : true; }
70 xml_node get_root_node() const;
71 // add a new text node in "xpath"
72 xml_node add_node(const char *xpath, const char* ns_prefix, const char* name,
73 const char* content);
74 // add a new text node in "root"
75 xml_node add_node(const char* ns_prefix, const char* name, const char* content);
76 xml_node_set exec_xpath( const char *xpath );
77 xml_node_set exec_xpath( const observation &xpath );
78
79protected:
80 typedef std::map<const xmlChar *, const xmlChar *> namespace_map_t;
81 namespace_map_t &get_namespaces();
82 void register_namespaces( xmlXPathContextPtr ctx );
83 void collect_namespaces( xmlNodePtr node );
84
85 namespace_map_t m_namespaces;
86 boost::shared_ptr<xmlDocPtrHandle> m_xmlDocPtr;
87 bool m_namespaces_init;
88};
89
90class TOM_UTIL_API xml_node
91{
92public:
93 xml_node() : m_node( 0 ), m_doc(0 ) { }
94 xml_node( xmlNodePtr node, xmlDocPtr doc) : m_node( node ), m_doc( doc ) { }
95 xml_node(const xml_node& node) : m_node(node.m_node), m_doc(node.m_doc) { }
96 xml_node& operator=(const xml_node& doc);
97
98 virtual ~xml_node() { }
99
100 bool valid( ) const { return m_node == NULL ? false : true; }
101 xml_node add_node(const char* ns_prefix, const char* name, const char* content);
102 void add_attribute(const char* ns_prefix, const char* name, const char* value);
103 void set_attribute(const char* ns_prefix, const char* name, const char* value);
104 xml_node_set get_sub_nodes( ) const;
105
106 tom::String get_name( ) const;
107 void set_content( const char *content );
108 tom::String get_content( const char *sub_node ) const;
109 tom::String get_content( ) const;
110 tom::String get_attribute( const observation & att_name ) const;
111 tom::String get_attribute( const char * att_name ) const;
112
113protected:
114 xmlNodePtr m_node;
115 xmlDocPtr m_doc;
116};
117
118//TOM_NAMESPACE_END;
119} // end tom namespace
120
121#endif
Definition string.hpp:15
Definition observation.hpp:13
Definition xml_config.hpp:39
Definition xml_config.hpp:22
Definition xml_config.hpp:36
Definition xml_config.hpp:91