2009-02-18 23:48:35 +01:00
|
|
|
#ifndef ARABICA_XML_QNAME_HPP
|
|
|
|
#define ARABICA_XML_QNAME_HPP
|
|
|
|
|
2009-02-23 09:38:50 +01:00
|
|
|
#include <SAX/ArabicaConfig.hpp>
|
|
|
|
#include <Arabica/StringAdaptor.hpp>
|
2009-02-23 20:23:12 +01:00
|
|
|
#include <XML/strings.hpp>
|
2010-02-21 00:24:31 +01:00
|
|
|
#include <map>
|
2009-02-23 10:14:35 +01:00
|
|
|
#include <stdexcept>
|
2009-02-18 23:48:35 +01:00
|
|
|
|
2009-02-24 13:21:35 +01:00
|
|
|
template<class string_type, class string_adaptor> class QualifiedNameTest;
|
|
|
|
|
|
|
|
namespace Arabica
|
|
|
|
{
|
|
|
|
namespace XML
|
|
|
|
{
|
2010-02-21 00:24:31 +01:00
|
|
|
namespace impl
|
|
|
|
{
|
|
|
|
|
|
|
|
template<class string_type, class string_adaptor>
|
|
|
|
class MapMapper
|
|
|
|
{
|
|
|
|
typedef typename std::map<string_type, string_type> string_map;
|
|
|
|
typedef typename std::map<string_type, string_type>::const_iterator string_map_iterator;
|
|
|
|
public:
|
|
|
|
MapMapper(const std::map<string_type, string_type>& namespaces) : namespaces_(namespaces) { }
|
|
|
|
MapMapper(const MapMapper& rhs) : namespaces_(rhs.namespaces_) { }
|
|
|
|
|
|
|
|
string_type operator()(const string_type& prefix) const
|
|
|
|
{
|
|
|
|
string_map_iterator ns = namespaces_.find(prefix);
|
|
|
|
if(ns == namespaces_.end())
|
|
|
|
return string_adaptor::empty_string();
|
|
|
|
return ns->second;
|
|
|
|
} //operator()
|
|
|
|
|
|
|
|
private:
|
|
|
|
const string_map& namespaces_;
|
|
|
|
|
|
|
|
bool operator==(const MapMapper&) const;
|
|
|
|
MapMapper& operator=(const MapMapper&);
|
|
|
|
}; // class MapMapper
|
|
|
|
|
|
|
|
} // namespace impl
|
2009-02-24 13:21:35 +01:00
|
|
|
|
2009-02-23 09:38:50 +01:00
|
|
|
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
|
2009-02-18 23:48:35 +01:00
|
|
|
class QualifiedName
|
|
|
|
{
|
2009-02-23 09:38:50 +01:00
|
|
|
typedef string_adaptor SA;
|
|
|
|
|
2009-02-18 23:48:35 +01:00
|
|
|
public:
|
2009-02-23 10:14:35 +01:00
|
|
|
/**
|
|
|
|
* <p>This function processes a raw XML 1.0 name in the current
|
|
|
|
* context by removing the prefix and looking it up among the
|
|
|
|
* prefixes currently declared.
|
|
|
|
*
|
|
|
|
* <p>If the raw name has a prefix that has not been declared,
|
|
|
|
* then the return value will be empty.</p>
|
|
|
|
*
|
|
|
|
* <p>Note that attribute names are processed differently than
|
|
|
|
* element names: an unprefixed element name will received the
|
2009-02-23 20:23:12 +01:00
|
|
|
* default Namespace (if any), while an unprefixed attribute name
|
2009-02-23 10:14:35 +01:00
|
|
|
* will not.</p>
|
|
|
|
*/
|
|
|
|
template<typename UriMapper>
|
|
|
|
static QualifiedName parseQName(const string_type& rawname,
|
|
|
|
bool is_attribute,
|
|
|
|
const UriMapper& mapper)
|
|
|
|
{
|
2009-02-23 20:23:12 +01:00
|
|
|
if(!Arabica::XML::is_qname<string_adaptor>(rawname))
|
2010-01-02 23:20:13 +01:00
|
|
|
throw std::runtime_error("Bad qname : '" + SA::asStdString(rawname) +"'");
|
2009-02-23 20:23:12 +01:00
|
|
|
|
2009-02-23 10:14:35 +01:00
|
|
|
static string_type COLON = SA::construct_from_utf8(":");
|
|
|
|
|
|
|
|
typename string_adaptor::size_type index = string_adaptor::find(rawname, COLON);
|
|
|
|
|
|
|
|
if(index == string_adaptor::npos())
|
2009-02-24 13:21:35 +01:00
|
|
|
return QualifiedName(rawname,
|
|
|
|
is_attribute ? SA::empty_string() : mapper(SA::empty_string()));
|
2009-02-23 10:14:35 +01:00
|
|
|
|
|
|
|
// prefix
|
|
|
|
string_type prefix = string_adaptor::substr(rawname, 0, index);
|
|
|
|
string_type localName = string_adaptor::substr(rawname, index + 1);
|
|
|
|
string_type uri = mapper(prefix);
|
|
|
|
|
2009-02-24 13:21:35 +01:00
|
|
|
return QualifiedName(prefix, localName, uri, rawname);
|
2009-02-23 10:14:35 +01:00
|
|
|
} // parseQName
|
|
|
|
|
2010-02-21 00:24:31 +01:00
|
|
|
static QualifiedName parseQName(const string_type& rawname,
|
|
|
|
bool is_attribute,
|
|
|
|
const std::map<string_type, string_type>& namespaces)
|
|
|
|
{
|
|
|
|
return parseQName(rawname, is_attribute, impl::MapMapper<string_type, string_adaptor>(namespaces));
|
|
|
|
} // parseQName
|
|
|
|
|
|
|
|
public:
|
2009-02-18 23:48:35 +01:00
|
|
|
QualifiedName(const QualifiedName& rhs) :
|
|
|
|
prefix_(rhs.prefix_),
|
|
|
|
localName_(rhs.localName_),
|
2009-02-24 13:21:35 +01:00
|
|
|
namespaceUri_(rhs.namespaceUri_),
|
|
|
|
rawName_(rhs.rawName_)
|
2009-02-18 23:48:35 +01:00
|
|
|
{
|
|
|
|
} // QualifiedName
|
|
|
|
|
|
|
|
QualifiedName& operator=(const QualifiedName& rhs)
|
|
|
|
{
|
|
|
|
QualifiedName qn(rhs);
|
|
|
|
std::swap(prefix_, qn.prefix_);
|
|
|
|
std::swap(localName_, qn.localName_);
|
|
|
|
std::swap(namespaceUri_, qn.namespaceUri_);
|
2009-02-24 13:21:35 +01:00
|
|
|
std::swap(rawName_, qn.rawName_);
|
2009-02-18 23:48:35 +01:00
|
|
|
return *this;
|
|
|
|
} // operator=
|
|
|
|
|
|
|
|
bool operator==(const QualifiedName& rhs) const
|
|
|
|
{
|
|
|
|
return (localName_ == rhs.localName_) &&
|
|
|
|
(namespaceUri_ == rhs.namespaceUri_);
|
|
|
|
} // operator==
|
|
|
|
|
|
|
|
bool operator!=(const QualifiedName& rhs) const
|
|
|
|
{
|
|
|
|
return !(operator==(rhs));
|
|
|
|
} // operator!=
|
|
|
|
|
2009-02-23 09:38:50 +01:00
|
|
|
string_type clarkName() const
|
2009-02-18 23:48:35 +01:00
|
|
|
{
|
2009-02-23 09:38:50 +01:00
|
|
|
if(SA::empty(namespaceUri_))
|
2009-02-18 23:48:35 +01:00
|
|
|
return localName_;
|
2009-02-23 09:38:50 +01:00
|
|
|
|
|
|
|
string_type cn;
|
|
|
|
SA::append(cn, SA::construct_from_utf8("{"));
|
|
|
|
SA::append(cn, namespaceUri_);
|
|
|
|
SA::append(cn, SA::construct_from_utf8("}"));
|
|
|
|
SA::append(cn, localName_);
|
|
|
|
return cn;
|
2009-02-18 23:48:35 +01:00
|
|
|
} // clarkName
|
2009-02-23 08:57:24 +01:00
|
|
|
|
2009-02-23 09:38:50 +01:00
|
|
|
bool has_prefix() const { return !SA::empty(prefix_); }
|
|
|
|
void set_prefix(const string_type& prefix) { prefix_ = prefix; }
|
2009-02-24 13:21:35 +01:00
|
|
|
bool has_namespaceUri() const { return !SA::empty(namespaceUri_); }
|
2009-02-23 08:57:24 +01:00
|
|
|
|
2009-02-23 09:38:50 +01:00
|
|
|
const string_type& prefix() const { return prefix_; }
|
|
|
|
const string_type& localName() const { return localName_; }
|
2009-02-24 13:21:35 +01:00
|
|
|
const string_type& namespaceUri() const { return namespaceUri_; }
|
|
|
|
|
|
|
|
bool has_rawName() const { return !SA::empty(rawName_); }
|
|
|
|
const string_type& rawName() const { return rawName_; }
|
2009-02-18 23:48:35 +01:00
|
|
|
|
|
|
|
private:
|
2009-02-23 09:38:50 +01:00
|
|
|
string_type prefix_;
|
|
|
|
string_type localName_;
|
|
|
|
string_type namespaceUri_;
|
2009-02-24 13:21:35 +01:00
|
|
|
string_type rawName_;
|
|
|
|
|
|
|
|
QualifiedName(const string_type& localName,
|
|
|
|
const string_type& namespaceUri) :
|
|
|
|
prefix_(),
|
|
|
|
localName_(localName),
|
|
|
|
namespaceUri_(namespaceUri),
|
|
|
|
rawName_(localName)
|
|
|
|
{
|
|
|
|
} // QualifiedName
|
|
|
|
|
|
|
|
QualifiedName(const string_type& prefix,
|
|
|
|
const string_type& localName,
|
|
|
|
const string_type& namespaceUri) :
|
|
|
|
prefix_(prefix),
|
|
|
|
localName_(localName),
|
|
|
|
namespaceUri_(namespaceUri),
|
|
|
|
rawName_()
|
|
|
|
{
|
|
|
|
} // QualifiedName
|
|
|
|
|
|
|
|
QualifiedName(const string_type& prefix,
|
|
|
|
const string_type& localName,
|
|
|
|
const string_type& namespaceUri,
|
|
|
|
const string_type& rawName) :
|
|
|
|
prefix_(prefix),
|
|
|
|
localName_(localName),
|
|
|
|
namespaceUri_(namespaceUri),
|
|
|
|
rawName_(rawName)
|
|
|
|
{
|
|
|
|
} // QualifiedName
|
|
|
|
|
2009-02-18 23:48:35 +01:00
|
|
|
|
|
|
|
QualifiedName();
|
2009-02-24 13:21:35 +01:00
|
|
|
|
|
|
|
friend class QualifiedNameTest<string_type, string_adaptor>;
|
2009-02-18 23:48:35 +01:00
|
|
|
}; // class QualifiedName
|
|
|
|
|
2009-02-24 13:21:35 +01:00
|
|
|
} // namespace XML
|
|
|
|
|
|
|
|
} // namespace Arabica
|
2009-02-18 23:48:35 +01:00
|
|
|
#endif
|