You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
6.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/xmllite/xmlnsstack.h"
  28. #include <sstream>
  29. #include <string>
  30. #include <vector>
  31. #include "talk/xmllite/xmlelement.h"
  32. #include "talk/xmllite/xmlconstants.h"
  33. namespace buzz {
  34. XmlnsStack::XmlnsStack() :
  35. pxmlnsStack_(new std::vector<std::string>),
  36. pxmlnsDepthStack_(new std::vector<size_t>) {
  37. }
  38. XmlnsStack::~XmlnsStack() {}
  39. void XmlnsStack::PushFrame() {
  40. pxmlnsDepthStack_->push_back(pxmlnsStack_->size());
  41. }
  42. void XmlnsStack::PopFrame() {
  43. size_t prev_size = pxmlnsDepthStack_->back();
  44. pxmlnsDepthStack_->pop_back();
  45. if (prev_size < pxmlnsStack_->size()) {
  46. pxmlnsStack_->erase(pxmlnsStack_->begin() + prev_size,
  47. pxmlnsStack_->end());
  48. }
  49. }
  50. std::pair<std::string, bool> XmlnsStack::NsForPrefix(
  51. const std::string& prefix) {
  52. if (prefix.length() >= 3 &&
  53. (prefix[0] == 'x' || prefix[0] == 'X') &&
  54. (prefix[1] == 'm' || prefix[1] == 'M') &&
  55. (prefix[2] == 'l' || prefix[2] == 'L')) {
  56. if (prefix == "xml")
  57. return std::make_pair(NS_XML, true);
  58. if (prefix == "xmlns")
  59. return std::make_pair(NS_XMLNS, true);
  60. // Other names with xml prefix are illegal.
  61. return std::make_pair(STR_EMPTY, false);
  62. }
  63. std::vector<std::string>::iterator pos;
  64. for (pos = pxmlnsStack_->end(); pos > pxmlnsStack_->begin(); ) {
  65. pos -= 2;
  66. if (*pos == prefix)
  67. return std::make_pair(*(pos + 1), true);
  68. }
  69. if (prefix == STR_EMPTY)
  70. return std::make_pair(STR_EMPTY, true); // default namespace
  71. return std::make_pair(STR_EMPTY, false); // none found
  72. }
  73. bool XmlnsStack::PrefixMatchesNs(const std::string& prefix,
  74. const std::string& ns) {
  75. const std::pair<std::string, bool> match = NsForPrefix(prefix);
  76. return match.second && (match.first == ns);
  77. }
  78. std::pair<std::string, bool> XmlnsStack::PrefixForNs(const std::string& ns,
  79. bool isattr) {
  80. if (ns == NS_XML)
  81. return std::make_pair(std::string("xml"), true);
  82. if (ns == NS_XMLNS)
  83. return std::make_pair(std::string("xmlns"), true);
  84. if (isattr ? ns == STR_EMPTY : PrefixMatchesNs(STR_EMPTY, ns))
  85. return std::make_pair(STR_EMPTY, true);
  86. std::vector<std::string>::iterator pos;
  87. for (pos = pxmlnsStack_->end(); pos > pxmlnsStack_->begin(); ) {
  88. pos -= 2;
  89. if (*(pos + 1) == ns &&
  90. (!isattr || !pos->empty()) && PrefixMatchesNs(*pos, ns))
  91. return std::make_pair(*pos, true);
  92. }
  93. return std::make_pair(STR_EMPTY, false); // none found
  94. }
  95. std::string XmlnsStack::FormatQName(const QName& name, bool isAttr) {
  96. std::string prefix(PrefixForNs(name.Namespace(), isAttr).first);
  97. if (prefix == STR_EMPTY)
  98. return name.LocalPart();
  99. else
  100. return prefix + ':' + name.LocalPart();
  101. }
  102. void XmlnsStack::AddXmlns(const std::string & prefix, const std::string & ns) {
  103. pxmlnsStack_->push_back(prefix);
  104. pxmlnsStack_->push_back(ns);
  105. }
  106. void XmlnsStack::RemoveXmlns() {
  107. pxmlnsStack_->pop_back();
  108. pxmlnsStack_->pop_back();
  109. }
  110. static bool IsAsciiLetter(char ch) {
  111. return ((ch >= 'a' && ch <= 'z') ||
  112. (ch >= 'A' && ch <= 'Z'));
  113. }
  114. static std::string AsciiLower(const std::string & s) {
  115. std::string result(s);
  116. size_t i;
  117. for (i = 0; i < result.length(); i++) {
  118. if (result[i] >= 'A' && result[i] <= 'Z')
  119. result[i] += 'a' - 'A';
  120. }
  121. return result;
  122. }
  123. static std::string SuggestPrefix(const std::string & ns) {
  124. size_t len = ns.length();
  125. size_t i = ns.find_last_of('.');
  126. if (i != std::string::npos && len - i <= 4 + 1)
  127. len = i; // chop off ".html" or ".xsd" or ".?{0,4}"
  128. size_t last = len;
  129. while (last > 0) {
  130. last -= 1;
  131. if (IsAsciiLetter(ns[last])) {
  132. size_t first = last;
  133. last += 1;
  134. while (first > 0) {
  135. if (!IsAsciiLetter(ns[first - 1]))
  136. break;
  137. first -= 1;
  138. }
  139. if (last - first > 4)
  140. last = first + 3;
  141. std::string candidate(AsciiLower(ns.substr(first, last - first)));
  142. if (candidate.find("xml") != 0)
  143. return candidate;
  144. break;
  145. }
  146. }
  147. return "ns";
  148. }
  149. std::pair<std::string, bool> XmlnsStack::AddNewPrefix(const std::string& ns,
  150. bool isAttr) {
  151. if (PrefixForNs(ns, isAttr).second)
  152. return std::make_pair(STR_EMPTY, false);
  153. std::string base(SuggestPrefix(ns));
  154. std::string result(base);
  155. int i = 2;
  156. while (NsForPrefix(result).second) {
  157. std::stringstream ss;
  158. ss << base;
  159. ss << (i++);
  160. ss >> result;
  161. }
  162. AddXmlns(result, ns);
  163. return std::make_pair(result, true);
  164. }
  165. void XmlnsStack::Reset() {
  166. pxmlnsStack_->clear();
  167. pxmlnsDepthStack_->clear();
  168. }
  169. }