File: xpath.hpp

package info (click to toggle)
libzeep 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,356 kB
  • ctags: 3,341
  • sloc: cpp: 15,502; makefile: 118; xml: 45; ansic: 12
file content (129 lines) | stat: -rw-r--r-- 3,300 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright Maarten L. Hekkelman, Radboud University 2008-2012.
//  Distributed under the Boost Software License, Version 1.0.
//     (See accompanying file LICENSE_1_0.txt or copy at
//           http://www.boost.org/LICENSE_1_0.txt)

#include <string>

#include <boost/static_assert.hpp>
#include <zeep/xml/node.hpp>

namespace zeep { namespace xml {

class document;

// --------------------------------------------------------------------
/// XPath's can contain variables. And variables can contain all kinds of data
/// like strings, numbers and even node_sets. If you want to use variables,
/// you can define a context, add your variables to it and then pass it on
/// in the xpath::evaluate method.

class context
{
  public:
#ifndef LIBZEEP_DOXYGEN_INVOKED
							context();
	virtual					~context();
#endif

	template<typename T>
	void					set(const std::string& name, const T& value);

	template<typename T>
	T						get(const std::string& name);

#ifndef LIBZEEP_DOXYGEN_INVOKED
  private:

							context(const context&);
	context&				operator=(const context&);

	friend class xpath;

	struct context_imp*		m_impl;
#endif
};

// --------------------------------------------------------------------
/// The actual xpath implementation. It expects an xpath in the constructor and
/// this path _must_ be UTF-8 encoded.

class xpath
{
  public:
							xpath(const std::string& path);
							xpath(const char* path);
							xpath(const xpath& rhs);
	xpath&					operator=(const xpath&);

#ifndef LIBZEEP_DOXYGEN_INVOKED
	virtual					~xpath();
#endif

	/// evaluate returns a node_set. If you're only interested in zeep::xml::element
	/// results, you should call the evaluate<element>() instantiation.
	template<typename NODE_TYPE>
	std::list<NODE_TYPE*>	evaluate(const node& root) const;

	/// The second evaluate method is used for xpaths that contain variables.
	template<typename NODE_TYPE>
	std::list<NODE_TYPE*>	evaluate(const node& root, context& ctxt) const;

	/// Returns true if the \a n node matches the XPath
	bool					matches(const node* n) const;

#ifndef LIBZEEP_DOXYGEN_INVOKED
  private:
	struct xpath_imp*		m_impl;
#endif
};

#ifndef LIBZEEP_DOXYGEN_INVOKED

// --------------------------------------------------------------------
// template specialisations. 

template<typename T>
void context::set(const std::string& name, const T& value)
{
	// only implemented for the cases specialised below
	assert(false);
}

template<>
void context::set<double>(const std::string& name, const double& value);

template<>
void context::set<std::string>(const std::string& name, const std::string& value);

template<typename T>
T context::get(const std::string& name)
{
	// only implemented for the cases specialised below
	assert(false);
}

template<>
double context::get<double>(const std::string& name);

template<>
std::string context::get<std::string>(const std::string& name);

// --------------------------------------------------------------------

template<>
node_set xpath::evaluate<node>(const node& root) const;

template<>
element_set xpath::evaluate<element>(const node& root) const;
	
template<>
node_set xpath::evaluate<node>(const node& root, context& ctxt) const;

template<>
element_set xpath::evaluate<element>(const node& root, context& ctxt) const;

#endif
	
}
}