mirror of
https://github.com/jezhiggins/arabica
synced 2025-01-30 08:38:15 +01:00
parameterised QualifiedName
This commit is contained in:
parent
32bc0e6597
commit
31b8d99a83
3 changed files with 64 additions and 47 deletions
|
@ -1,20 +1,25 @@
|
|||
#ifndef ARABICA_XML_QNAME_HPP
|
||||
#define ARABICA_XML_QNAME_HPP
|
||||
|
||||
#include <SAX/ArabicaConfig.hpp>
|
||||
#include <Arabica/StringAdaptor.hpp>
|
||||
|
||||
template<class string_type, class string_adaptor = Arabica::default_string_adaptor<string_type> >
|
||||
class QualifiedName
|
||||
{
|
||||
typedef string_adaptor SA;
|
||||
|
||||
public:
|
||||
QualifiedName(const std::string& localName) :
|
||||
QualifiedName(const string_type& localName) :
|
||||
prefix_(),
|
||||
localName_(localName),
|
||||
namespaceUri_()
|
||||
{
|
||||
} // QualifiedName
|
||||
|
||||
QualifiedName(const std::string& prefix,
|
||||
const std::string& localName,
|
||||
const std::string& namespaceUri) :
|
||||
QualifiedName(const string_type& prefix,
|
||||
const string_type& localName,
|
||||
const string_type& namespaceUri) :
|
||||
prefix_(prefix),
|
||||
localName_(localName),
|
||||
namespaceUri_(namespaceUri)
|
||||
|
@ -48,24 +53,30 @@ public:
|
|||
return !(operator==(rhs));
|
||||
} // operator!=
|
||||
|
||||
std::string clarkName() const
|
||||
string_type clarkName() const
|
||||
{
|
||||
if(namespaceUri_.length() == 0)
|
||||
if(SA::empty(namespaceUri_))
|
||||
return localName_;
|
||||
return "{" + namespaceUri_ + "}" + localName_;
|
||||
|
||||
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;
|
||||
} // clarkName
|
||||
|
||||
bool has_prefix() const { return !prefix_.empty(); }
|
||||
void set_prefix(const std::string& prefix) { prefix_ = prefix; }
|
||||
bool has_prefix() const { return !SA::empty(prefix_); }
|
||||
void set_prefix(const string_type& prefix) { prefix_ = prefix; }
|
||||
|
||||
const std::string& prefix() const { return prefix_; }
|
||||
const std::string& namespaceUri() const { return namespaceUri_; }
|
||||
const std::string& localName() const { return localName_; }
|
||||
const string_type& prefix() const { return prefix_; }
|
||||
const string_type& namespaceUri() const { return namespaceUri_; }
|
||||
const string_type& localName() const { return localName_; }
|
||||
|
||||
private:
|
||||
std::string prefix_;
|
||||
std::string localName_;
|
||||
std::string namespaceUri_;
|
||||
string_type prefix_;
|
||||
string_type localName_;
|
||||
string_type namespaceUri_;
|
||||
|
||||
QualifiedName();
|
||||
}; // class QualifiedName
|
||||
|
|
|
@ -5,8 +5,11 @@
|
|||
|
||||
using namespace Arabica::XML;
|
||||
|
||||
template<class string_type, class string_adaptor>
|
||||
class QualifiedNameTest : public TestCase
|
||||
{
|
||||
typedef string_adaptor SA;
|
||||
typedef QualifiedName<string_type, string_adaptor> QN;
|
||||
public:
|
||||
QualifiedNameTest(const std::string& name) :
|
||||
TestCase(name)
|
||||
|
@ -15,8 +18,8 @@ public:
|
|||
|
||||
void testNcNameEquality()
|
||||
{
|
||||
QualifiedName q1("foo");
|
||||
QualifiedName q2("foo");
|
||||
QN q1(SA::construct_from_utf8("foo"));
|
||||
QN q2(SA::construct_from_utf8("foo"));
|
||||
|
||||
assertTrue(q1 == q2);
|
||||
assertTrue(!(q1 != q2));
|
||||
|
@ -24,8 +27,8 @@ public:
|
|||
|
||||
void testNcNameCopy()
|
||||
{
|
||||
QualifiedName q1("foo");
|
||||
QualifiedName q2(q1);
|
||||
QN q1(SA::construct_from_utf8("foo"));
|
||||
QN q2(q1);
|
||||
|
||||
assertTrue(q1 == q2);
|
||||
assertTrue(!(q1 != q2));
|
||||
|
@ -33,8 +36,8 @@ public:
|
|||
|
||||
void testNcNameAssignment()
|
||||
{
|
||||
QualifiedName q1("foo");
|
||||
QualifiedName q2("bar");
|
||||
QN q1(SA::construct_from_utf8("foo"));
|
||||
QN q2(SA::construct_from_utf8("bar"));
|
||||
|
||||
assertFalse(q1 == q2);
|
||||
|
||||
|
@ -45,37 +48,37 @@ public:
|
|||
|
||||
void testNcClarkName()
|
||||
{
|
||||
QualifiedName q("bar");
|
||||
QN q(SA::construct_from_utf8("bar"));
|
||||
|
||||
assertEquals("bar", q.clarkName());
|
||||
assertTrue(SA::construct_from_utf8("bar") == q.clarkName());
|
||||
} // testNcClarkName
|
||||
|
||||
void testEquality()
|
||||
{
|
||||
QualifiedName q1("foo", "bar", "http://test/");
|
||||
QualifiedName q2("foo", "bar", "http://test/");
|
||||
QN q1(SA::construct_from_utf8("foo"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
|
||||
QN q2(SA::construct_from_utf8("foo"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
|
||||
|
||||
assertTrue(q1 == q2);
|
||||
assertTrue(!(q1 != q2));
|
||||
|
||||
// prefix is not significant
|
||||
QualifiedName q3("baz", "bar", "http://test/");
|
||||
QN q3(SA::construct_from_utf8("baz"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
|
||||
assertTrue(q1 == q3);
|
||||
assertTrue(!(q1 != q3));
|
||||
} // testEquality
|
||||
|
||||
void testCopy()
|
||||
{
|
||||
QualifiedName q1("foo", "bar", "http://test/");
|
||||
QualifiedName q2(q1);
|
||||
QN q1(SA::construct_from_utf8("foo"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
|
||||
QN q2(q1);
|
||||
|
||||
assertTrue(q1 == q2);
|
||||
} // testCopy
|
||||
|
||||
void testAssignment()
|
||||
{
|
||||
QualifiedName q1("foo", "bar", "http://test/");
|
||||
QualifiedName q2("foo", "parp", "http://tst/");
|
||||
QN q1(SA::construct_from_utf8("foo"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
|
||||
QN q2(SA::construct_from_utf8("foo"), SA::construct_from_utf8("parp"), SA::construct_from_utf8("http://tst/"));
|
||||
|
||||
assertTrue(q1 != q2);
|
||||
|
||||
|
@ -86,36 +89,39 @@ public:
|
|||
|
||||
void testClarkName()
|
||||
{
|
||||
QualifiedName q("foo", "bar", "http://test/");
|
||||
assertEquals("{http://test/}bar", q.clarkName());
|
||||
QN q(SA::construct_from_utf8("foo"), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
|
||||
assertTrue(SA::construct_from_utf8("{http://test/}bar") == q.clarkName());
|
||||
} // testClarkName
|
||||
|
||||
void testPrefix()
|
||||
{
|
||||
QualifiedName q("", "bar", "http://test/");
|
||||
QN q(SA::construct_from_utf8(""), SA::construct_from_utf8("bar"), SA::construct_from_utf8("http://test/"));
|
||||
assertFalse(q.has_prefix());
|
||||
assertEquals("bar", q.localName());
|
||||
assertEquals("http://test/", q.namespaceUri());
|
||||
assertTrue(SA::construct_from_utf8("bar") == q.localName());
|
||||
assertTrue(SA::construct_from_utf8("http://test/") == q.namespaceUri());
|
||||
|
||||
q.set_prefix("t");
|
||||
q.set_prefix(SA::construct_from_utf8("t"));
|
||||
assertTrue(q.has_prefix());
|
||||
assertEquals("t", q.prefix());
|
||||
assertTrue(SA::construct_from_utf8("t") == q.prefix());
|
||||
} // testPrefix
|
||||
}; // class QualifiedNameTest
|
||||
|
||||
template<class string_type, class string_adaptor>
|
||||
TestSuite* QualifiedNameTest_suite()
|
||||
{
|
||||
typedef QualifiedNameTest<string_type, string_adaptor> QNT;
|
||||
|
||||
TestSuite* suiteOfTests = new TestSuite();
|
||||
|
||||
suiteOfTests->addTest(new TestCaller<QualifiedNameTest>("testNcNameEquality", &QualifiedNameTest::testNcNameEquality));
|
||||
suiteOfTests->addTest(new TestCaller<QualifiedNameTest>("testNcNameCopy", &QualifiedNameTest::testNcNameCopy));
|
||||
suiteOfTests->addTest(new TestCaller<QualifiedNameTest>("testNcNameAssignment", &QualifiedNameTest::testNcNameAssignment));
|
||||
suiteOfTests->addTest(new TestCaller<QualifiedNameTest>("testNcClarkName", &QualifiedNameTest::testNcClarkName));
|
||||
suiteOfTests->addTest(new TestCaller<QualifiedNameTest>("testEquality", &QualifiedNameTest::testEquality));
|
||||
suiteOfTests->addTest(new TestCaller<QualifiedNameTest>("testCopy", &QualifiedNameTest::testCopy));
|
||||
suiteOfTests->addTest(new TestCaller<QualifiedNameTest>("testAssignment", &QualifiedNameTest::testAssignment));
|
||||
suiteOfTests->addTest(new TestCaller<QualifiedNameTest>("testClarkName", &QualifiedNameTest::testClarkName));
|
||||
suiteOfTests->addTest(new TestCaller<QualifiedNameTest>("testPrefix", &QualifiedNameTest::testPrefix));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testNcNameEquality", &QNT::testNcNameEquality));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testNcNameCopy", &QNT::testNcNameCopy));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testNcNameAssignment", &QNT::testNcNameAssignment));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testNcClarkName", &QNT::testNcClarkName));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testEquality", &QNT::testEquality));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testCopy", &QNT::testCopy));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testAssignment", &QNT::testAssignment));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testClarkName", &QNT::testClarkName));
|
||||
suiteOfTests->addTest(new TestCaller<QNT>("testPrefix", &QNT::testPrefix));
|
||||
|
||||
return suiteOfTests;
|
||||
} // QualifiedNameTest_suite
|
||||
|
|
|
@ -20,7 +20,7 @@ bool Util_test_suite(int argc, const char** argv)
|
|||
runner.addTest("Base64Test", Base64Test_suite());
|
||||
runner.addTest("URITest", URITest_suite());
|
||||
runner.addTest("XMLString", XMLStringTest_suite());
|
||||
runner.addTest("QualifiedName", QualifiedNameTest_suite());
|
||||
runner.addTest("QualifiedName", QualifiedNameTest_suite<string_type, string_adaptor>());
|
||||
|
||||
return runner.run(argc, argv);
|
||||
} // main
|
||||
|
|
Loading…
Add table
Reference in a new issue