Name.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // Name.cpp
  3. //
  4. // $Id: //poco/1.4/XML/src/Name.cpp#1 $
  5. //
  6. // Library: XML
  7. // Package: XML
  8. // Module: Name
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // Permission is hereby granted, free of charge, to any person or organization
  14. // obtaining a copy of the software and accompanying documentation covered by
  15. // this license (the "Software") to use, reproduce, display, distribute,
  16. // execute, and transmit the Software, and to prepare derivative works of the
  17. // Software, and to permit third-parties to whom the Software is furnished to
  18. // do so, all subject to the following:
  19. //
  20. // The copyright notices in the Software and this entire statement, including
  21. // the above license grant, this restriction and the following disclaimer,
  22. // must be included in all copies of the Software, in whole or in part, and
  23. // all derivative works of the Software, unless such copies or derivative
  24. // works are solely in the form of machine-executable object code generated by
  25. // a source language processor.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  30. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  31. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  32. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. // DEALINGS IN THE SOFTWARE.
  34. //
  35. #include "Poco/XML/Name.h"
  36. #include <algorithm>
  37. namespace Poco {
  38. namespace XML {
  39. const XMLString Name::EMPTY_NAME;
  40. Name::Name()
  41. {
  42. }
  43. Name::Name(const XMLString& qname):
  44. _qname(qname)
  45. {
  46. }
  47. Name::Name(const XMLString& qname, const XMLString& namespaceURI):
  48. _qname(qname),
  49. _namespaceURI(namespaceURI),
  50. _localName(localName(qname))
  51. {
  52. }
  53. Name::Name(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName):
  54. _qname(qname),
  55. _namespaceURI(namespaceURI),
  56. _localName(localName)
  57. {
  58. }
  59. Name::Name(const Name& name):
  60. _qname(name._qname),
  61. _namespaceURI(name._namespaceURI),
  62. _localName(name._localName)
  63. {
  64. }
  65. Name::~Name()
  66. {
  67. }
  68. Name& Name::operator = (const Name& name)
  69. {
  70. if (this != &name)
  71. {
  72. _qname = name._qname;
  73. _namespaceURI = name._namespaceURI;
  74. _localName = name._localName;
  75. }
  76. return *this;
  77. }
  78. void Name::swap(Name& name)
  79. {
  80. std::swap(_qname, name._qname);
  81. std::swap(_namespaceURI, name._namespaceURI);
  82. std::swap(_localName, name._localName);
  83. }
  84. void Name::assign(const XMLString& qname)
  85. {
  86. _qname = qname;
  87. _namespaceURI.clear();
  88. _localName.clear();
  89. }
  90. void Name::assign(const XMLString& qname, const XMLString& namespaceURI)
  91. {
  92. _qname = qname;
  93. _namespaceURI = namespaceURI;
  94. _localName = localName(qname);
  95. }
  96. void Name::assign(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName)
  97. {
  98. _qname = qname;
  99. _namespaceURI = namespaceURI;
  100. _localName = localName;
  101. }
  102. bool Name::equals(const Name& name) const
  103. {
  104. return name._namespaceURI == _namespaceURI && name._localName == _localName && name._qname == _qname;
  105. }
  106. bool Name::equals(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName) const
  107. {
  108. return _namespaceURI == namespaceURI && _localName == localName && _qname == qname;
  109. }
  110. bool Name::equalsWeakly(const XMLString& qname, const XMLString& namespaceURI, const XMLString& localName) const
  111. {
  112. return (_qname == qname && !qname.empty()) || (_namespaceURI == namespaceURI && _localName == localName && !_localName.empty());
  113. }
  114. XMLString Name::prefix() const
  115. {
  116. return prefix(_qname);
  117. }
  118. void Name::split(const XMLString& qname, XMLString& prefix, XMLString& localName)
  119. {
  120. XMLString::size_type pos = qname.find(':');
  121. if (pos != XMLString::npos)
  122. {
  123. prefix.assign(qname, 0, pos);
  124. localName.assign(qname, pos + 1, qname.size() - pos - 1);
  125. }
  126. else
  127. {
  128. prefix.clear();
  129. localName.assign(qname);
  130. }
  131. }
  132. XMLString Name::localName(const XMLString& qname)
  133. {
  134. XMLString::size_type pos = qname.find(':');
  135. if (pos != XMLString::npos)
  136. return XMLString(qname, pos + 1, qname.size() - pos - 1);
  137. else
  138. return qname;
  139. }
  140. XMLString Name::prefix(const XMLString& qname)
  141. {
  142. XMLString::size_type pos = qname.find(':');
  143. if (pos != XMLString::npos)
  144. return XMLString(qname, 0, pos);
  145. else
  146. return EMPTY_NAME;
  147. }
  148. } } // namespace Poco::XML