mirror of
https://github.com/jezhiggins/arabica
synced 2024-11-17 07:48:50 +01:00
initial version of parseQName
This commit is contained in:
parent
31b8d99a83
commit
412a7f2204
2 changed files with 61 additions and 0 deletions
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <SAX/ArabicaConfig.hpp>
|
||||
#include <Arabica/StringAdaptor.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
|
||||
class QualifiedName
|
||||
|
@ -10,6 +11,47 @@ class QualifiedName
|
|||
typedef string_adaptor SA;
|
||||
|
||||
public:
|
||||
/**
|
||||
* <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
|
||||
* default Namespace (if any), while an unprefixed element name
|
||||
* will not.</p>
|
||||
*/
|
||||
template<typename UriMapper>
|
||||
static QualifiedName parseQName(const string_type& rawname,
|
||||
bool is_attribute,
|
||||
const UriMapper& mapper)
|
||||
{
|
||||
static string_type COLON = SA::construct_from_utf8(":");
|
||||
|
||||
typename string_adaptor::size_type index = string_adaptor::find(rawname, COLON);
|
||||
|
||||
if(index == string_adaptor::npos())
|
||||
return QualifiedName(SA::empty_string(),
|
||||
rawname,
|
||||
is_attribute ? SA::empty_string() : mapper(SA::empty_string()));
|
||||
|
||||
// prefix
|
||||
string_type prefix = string_adaptor::substr(rawname, 0, index);
|
||||
string_type localName = string_adaptor::substr(rawname, index + 1);
|
||||
|
||||
if((string_adaptor::length(prefix) == 0) ||
|
||||
(string_adaptor::length(localName) == 0) ||
|
||||
(string_adaptor::find(localName, COLON) != string_adaptor::npos()))
|
||||
throw std::runtime_error("Bad qname : " + SA::asStdString(rawname));
|
||||
|
||||
string_type uri = mapper(prefix);
|
||||
|
||||
return QualifiedName(prefix, localName, uri);
|
||||
} // parseQName
|
||||
|
||||
QualifiedName(const string_type& localName) :
|
||||
prefix_(),
|
||||
localName_(localName),
|
||||
|
|
|
@ -10,6 +10,7 @@ class QualifiedNameTest : public TestCase
|
|||
{
|
||||
typedef string_adaptor SA;
|
||||
typedef QualifiedName<string_type, string_adaptor> QN;
|
||||
typedef QualifiedNameTest<string_type, string_adaptor> QNT;
|
||||
public:
|
||||
QualifiedNameTest(const std::string& name) :
|
||||
TestCase(name)
|
||||
|
@ -104,6 +105,23 @@ public:
|
|||
assertTrue(q.has_prefix());
|
||||
assertTrue(SA::construct_from_utf8("t") == q.prefix());
|
||||
} // testPrefix
|
||||
|
||||
static string_type uri_mapper(const string_type& prefix)
|
||||
{
|
||||
return SA::construct_from_utf8("http://test/");
|
||||
} // uri_mapper
|
||||
|
||||
void testParseBadQName()
|
||||
{
|
||||
try {
|
||||
QN::parseQName(SA::construct_from_utf8("::::"), false, QNT::uri_mapper);
|
||||
assertFalse("oops - should have thrown here");
|
||||
}
|
||||
catch(std::runtime_error&) {
|
||||
// yay
|
||||
}
|
||||
} // testParseBadQName
|
||||
|
||||
}; // class QualifiedNameTest
|
||||
|
||||
template<class string_type, class string_adaptor>
|
||||
|
@ -122,6 +140,7 @@ TestSuite* QualifiedNameTest_suite()
|
|||
suiteOfTests->addTest(new TestCaller<QNT>("testAssignment", &QNT::testAssignment));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testClarkName", &QNT::testClarkName));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testPrefix", &QNT::testPrefix));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testParseBadQName", &QNT::testParseBadQName));
|
||||
|
||||
return suiteOfTests;
|
||||
} // QualifiedNameTest_suite
|
||||
|
|
Loading…
Reference in a new issue