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.

147 lines
3.8 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/xmlbuilder.h"
  28. #include <vector>
  29. #include <set>
  30. #include "talk/base/common.h"
  31. #include "talk/xmllite/xmlconstants.h"
  32. #include "talk/xmllite/xmlelement.h"
  33. namespace buzz {
  34. XmlBuilder::XmlBuilder() :
  35. pelCurrent_(NULL),
  36. pelRoot_(NULL),
  37. pvParents_(new std::vector<XmlElement *>()) {
  38. }
  39. void
  40. XmlBuilder::Reset() {
  41. pelRoot_.reset();
  42. pelCurrent_ = NULL;
  43. pvParents_->clear();
  44. }
  45. XmlElement *
  46. XmlBuilder::BuildElement(XmlParseContext * pctx,
  47. const char * name, const char ** atts) {
  48. QName tagName(pctx->ResolveQName(name, false));
  49. if (tagName.IsEmpty())
  50. return NULL;
  51. XmlElement * pelNew = new XmlElement(tagName);
  52. if (!*atts)
  53. return pelNew;
  54. std::set<QName> seenNonlocalAtts;
  55. while (*atts) {
  56. QName attName(pctx->ResolveQName(*atts, true));
  57. if (attName.IsEmpty()) {
  58. delete pelNew;
  59. return NULL;
  60. }
  61. // verify that namespaced names are unique
  62. if (!attName.Namespace().empty()) {
  63. if (seenNonlocalAtts.count(attName)) {
  64. delete pelNew;
  65. return NULL;
  66. }
  67. seenNonlocalAtts.insert(attName);
  68. }
  69. pelNew->AddAttr(attName, std::string(*(atts + 1)));
  70. atts += 2;
  71. }
  72. return pelNew;
  73. }
  74. void
  75. XmlBuilder::StartElement(XmlParseContext * pctx,
  76. const char * name, const char ** atts) {
  77. XmlElement * pelNew = BuildElement(pctx, name, atts);
  78. if (pelNew == NULL) {
  79. pctx->RaiseError(XML_ERROR_SYNTAX);
  80. return;
  81. }
  82. if (!pelCurrent_) {
  83. pelCurrent_ = pelNew;
  84. pelRoot_.reset(pelNew);
  85. pvParents_->push_back(NULL);
  86. } else {
  87. pelCurrent_->AddElement(pelNew);
  88. pvParents_->push_back(pelCurrent_);
  89. pelCurrent_ = pelNew;
  90. }
  91. }
  92. void
  93. XmlBuilder::EndElement(XmlParseContext * pctx, const char * name) {
  94. UNUSED(pctx);
  95. UNUSED(name);
  96. pelCurrent_ = pvParents_->back();
  97. pvParents_->pop_back();
  98. }
  99. void
  100. XmlBuilder::CharacterData(XmlParseContext * pctx,
  101. const char * text, int len) {
  102. UNUSED(pctx);
  103. if (pelCurrent_) {
  104. pelCurrent_->AddParsedText(text, len);
  105. }
  106. }
  107. void
  108. XmlBuilder::Error(XmlParseContext * pctx, XML_Error err) {
  109. UNUSED(pctx);
  110. UNUSED(err);
  111. pelRoot_.reset(NULL);
  112. pelCurrent_ = NULL;
  113. pvParents_->clear();
  114. }
  115. XmlElement *
  116. XmlBuilder::CreateElement() {
  117. return pelRoot_.release();
  118. }
  119. XmlElement *
  120. XmlBuilder::BuiltElement() {
  121. return pelRoot_.get();
  122. }
  123. XmlBuilder::~XmlBuilder() {
  124. }
  125. } // namespace buzz