2005-08-04 22:42:30 +02:00
|
|
|
#ifndef ARABICA_XPATH_NAMESPACE_CONTEXT_HPP
|
|
|
|
#define ARABICA_XPATH_NAMESPACE_CONTEXT_HPP
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <stdexcept>
|
2007-08-22 14:22:09 +02:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2005-08-04 22:42:30 +02:00
|
|
|
|
|
|
|
namespace Arabica
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace XPath
|
|
|
|
{
|
|
|
|
|
|
|
|
class UnboundNamespacePrefixException : public std::runtime_error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UnboundNamespacePrefixException(const std::string& thing) : std::runtime_error("The prefix '" + thing + "' is not bound to a namespace URI") { }
|
|
|
|
}; // class UnboundNamespacePrefixException
|
|
|
|
|
2005-08-17 10:50:41 +02:00
|
|
|
template<class string_type, class string_adaptor>
|
2005-08-04 22:42:30 +02:00
|
|
|
class NamespaceContext
|
|
|
|
{
|
|
|
|
public:
|
2005-08-05 23:02:24 +02:00
|
|
|
NamespaceContext() { }
|
|
|
|
virtual ~NamespaceContext() { };
|
2005-08-04 22:42:30 +02:00
|
|
|
|
2005-08-17 10:50:41 +02:00
|
|
|
virtual string_type namespaceURI(const string_type& prefix) const = 0;
|
2005-08-04 22:42:30 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
NamespaceContext(const NamespaceContext&);
|
|
|
|
NamespaceContext& operator=(const NamespaceContext&);
|
|
|
|
bool operator==(const NamespaceContext&) const;
|
|
|
|
}; // class NamespaceContext
|
|
|
|
|
2005-09-25 22:09:49 +02:00
|
|
|
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
|
2005-08-17 10:50:41 +02:00
|
|
|
class NamespaceContextPtr : public boost::shared_ptr<NamespaceContext<string_type, string_adaptor> >
|
2005-08-16 14:10:56 +02:00
|
|
|
{
|
|
|
|
public:
|
2005-08-17 10:50:41 +02:00
|
|
|
explicit NamespaceContextPtr(NamespaceContext<string_type, string_adaptor> * nc) :
|
|
|
|
boost::shared_ptr<NamespaceContext<string_type, string_adaptor> >(nc)
|
|
|
|
{
|
|
|
|
} // NamespaceContextPtr
|
2005-08-16 14:10:56 +02:00
|
|
|
}; // class NamespaceContextPtr
|
2005-08-04 22:42:30 +02:00
|
|
|
|
2005-09-25 22:09:49 +02:00
|
|
|
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
|
2005-08-17 10:50:41 +02:00
|
|
|
class NullNamespaceContext : public NamespaceContext<string_type, string_adaptor>
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
|
|
|
public:
|
2005-08-17 10:50:41 +02:00
|
|
|
virtual string_type namespaceURI(const string_type& prefix) const
|
|
|
|
{
|
|
|
|
throw UnboundNamespacePrefixException(string_adaptor().asStdString(prefix));
|
|
|
|
} // namespaceURI
|
2005-08-04 22:42:30 +02:00
|
|
|
}; // class NullNamespaceContext
|
|
|
|
|
2005-09-25 22:09:49 +02:00
|
|
|
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
|
2005-08-17 10:50:41 +02:00
|
|
|
class StandardNamespaceContext : public NamespaceContext<string_type, string_adaptor>
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
StandardNamespaceContext() { }
|
|
|
|
~StandardNamespaceContext() { }
|
|
|
|
|
2005-08-17 10:50:41 +02:00
|
|
|
virtual string_type namespaceURI(const string_type& prefix) const
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
2005-08-21 14:48:00 +02:00
|
|
|
typename NSMap::const_iterator n = map_.find(prefix);
|
2005-08-04 22:42:30 +02:00
|
|
|
if(n == map_.end())
|
2005-08-17 10:50:41 +02:00
|
|
|
throw UnboundNamespacePrefixException(string_adaptor().asStdString(prefix));
|
2005-08-04 22:42:30 +02:00
|
|
|
return (*n).second;
|
|
|
|
} // namespaceURI
|
|
|
|
|
2005-08-17 10:50:41 +02:00
|
|
|
void addNamespaceDeclaration(const string_type& namespaceURI, const string_type& prefix)
|
2005-08-04 22:42:30 +02:00
|
|
|
{
|
|
|
|
map_[prefix] = namespaceURI;
|
|
|
|
} // addNamespaceDeclaration
|
|
|
|
|
|
|
|
private:
|
|
|
|
StandardNamespaceContext(const StandardNamespaceContext& rhs) : map_(rhs.map_) { }
|
|
|
|
|
2005-08-17 10:50:41 +02:00
|
|
|
typedef std::map<string_type, string_type> NSMap;
|
2005-08-04 22:42:30 +02:00
|
|
|
NSMap map_;
|
|
|
|
}; // class StandardNamespaceContext
|
|
|
|
|
|
|
|
} // namespace XPath
|
|
|
|
|
|
|
|
} // namespace Arabica
|
|
|
|
#endif
|