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.

191 lines
5.9 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/xmlprinter.h"
  28. #include <sstream>
  29. #include <string>
  30. #include <vector>
  31. #include "talk/xmllite/xmlconstants.h"
  32. #include "talk/xmllite/xmlelement.h"
  33. #include "talk/xmllite/xmlnsstack.h"
  34. namespace buzz {
  35. class XmlPrinterImpl {
  36. public:
  37. XmlPrinterImpl(std::ostream* pout, XmlnsStack* ns_stack);
  38. void PrintElement(const XmlElement* element);
  39. void PrintQuotedValue(const std::string& text);
  40. void PrintBodyText(const std::string& text);
  41. void PrintCDATAText(const std::string& text);
  42. private:
  43. std::ostream *pout_;
  44. XmlnsStack* ns_stack_;
  45. };
  46. void XmlPrinter::PrintXml(std::ostream* pout, const XmlElement* element) {
  47. XmlnsStack ns_stack;
  48. PrintXml(pout, element, &ns_stack);
  49. }
  50. void XmlPrinter::PrintXml(std::ostream* pout, const XmlElement* element,
  51. XmlnsStack* ns_stack) {
  52. XmlPrinterImpl printer(pout, ns_stack);
  53. printer.PrintElement(element);
  54. }
  55. XmlPrinterImpl::XmlPrinterImpl(std::ostream* pout, XmlnsStack* ns_stack)
  56. : pout_(pout),
  57. ns_stack_(ns_stack) {
  58. }
  59. void XmlPrinterImpl::PrintElement(const XmlElement* element) {
  60. ns_stack_->PushFrame();
  61. // first go through attrs of pel to add xmlns definitions
  62. const XmlAttr* attr;
  63. for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
  64. if (attr->Name() == QN_XMLNS) {
  65. ns_stack_->AddXmlns(STR_EMPTY, attr->Value());
  66. } else if (attr->Name().Namespace() == NS_XMLNS) {
  67. ns_stack_->AddXmlns(attr->Name().LocalPart(),
  68. attr->Value());
  69. }
  70. }
  71. // then go through qnames to make sure needed xmlns definitons are added
  72. std::vector<std::string> new_ns;
  73. std::pair<std::string, bool> prefix;
  74. prefix = ns_stack_->AddNewPrefix(element->Name().Namespace(), false);
  75. if (prefix.second) {
  76. new_ns.push_back(prefix.first);
  77. new_ns.push_back(element->Name().Namespace());
  78. }
  79. for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
  80. prefix = ns_stack_->AddNewPrefix(attr->Name().Namespace(), true);
  81. if (prefix.second) {
  82. new_ns.push_back(prefix.first);
  83. new_ns.push_back(attr->Name().Namespace());
  84. }
  85. }
  86. // print the element name
  87. *pout_ << '<' << ns_stack_->FormatQName(element->Name(), false);
  88. // and the attributes
  89. for (attr = element->FirstAttr(); attr; attr = attr->NextAttr()) {
  90. *pout_ << ' ' << ns_stack_->FormatQName(attr->Name(), true) << "=\"";
  91. PrintQuotedValue(attr->Value());
  92. *pout_ << '"';
  93. }
  94. // and the extra xmlns declarations
  95. std::vector<std::string>::iterator i(new_ns.begin());
  96. while (i < new_ns.end()) {
  97. if (*i == STR_EMPTY) {
  98. *pout_ << " xmlns=\"" << *(i + 1) << '"';
  99. } else {
  100. *pout_ << " xmlns:" << *i << "=\"" << *(i + 1) << '"';
  101. }
  102. i += 2;
  103. }
  104. // now the children
  105. const XmlChild* child = element->FirstChild();
  106. if (child == NULL)
  107. *pout_ << "/>";
  108. else {
  109. *pout_ << '>';
  110. while (child) {
  111. if (child->IsText()) {
  112. if (element->IsCDATA()) {
  113. PrintCDATAText(child->AsText()->Text());
  114. } else {
  115. PrintBodyText(child->AsText()->Text());
  116. }
  117. } else {
  118. PrintElement(child->AsElement());
  119. }
  120. child = child->NextChild();
  121. }
  122. *pout_ << "</" << ns_stack_->FormatQName(element->Name(), false) << '>';
  123. }
  124. ns_stack_->PopFrame();
  125. }
  126. void XmlPrinterImpl::PrintQuotedValue(const std::string& text) {
  127. size_t safe = 0;
  128. for (;;) {
  129. size_t unsafe = text.find_first_of("<>&\"", safe);
  130. if (unsafe == std::string::npos)
  131. unsafe = text.length();
  132. *pout_ << text.substr(safe, unsafe - safe);
  133. if (unsafe == text.length())
  134. return;
  135. switch (text[unsafe]) {
  136. case '<': *pout_ << "&lt;"; break;
  137. case '>': *pout_ << "&gt;"; break;
  138. case '&': *pout_ << "&amp;"; break;
  139. case '"': *pout_ << "&quot;"; break;
  140. }
  141. safe = unsafe + 1;
  142. if (safe == text.length())
  143. return;
  144. }
  145. }
  146. void XmlPrinterImpl::PrintBodyText(const std::string& text) {
  147. size_t safe = 0;
  148. for (;;) {
  149. size_t unsafe = text.find_first_of("<>&", safe);
  150. if (unsafe == std::string::npos)
  151. unsafe = text.length();
  152. *pout_ << text.substr(safe, unsafe - safe);
  153. if (unsafe == text.length())
  154. return;
  155. switch (text[unsafe]) {
  156. case '<': *pout_ << "&lt;"; break;
  157. case '>': *pout_ << "&gt;"; break;
  158. case '&': *pout_ << "&amp;"; break;
  159. }
  160. safe = unsafe + 1;
  161. if (safe == text.length())
  162. return;
  163. }
  164. }
  165. void XmlPrinterImpl::PrintCDATAText(const std::string& text) {
  166. *pout_ << "<![CDATA[" << text << "]]>";
  167. }
  168. } // namespace buzz