OLD | NEW |
(Empty) | |
| 1 // Copyright 2004 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "third_party/xmllite/xmlbuilder.h" |
| 6 |
| 7 #include <set> |
| 8 #include <vector> |
| 9 #include "third_party/xmllite/xmlconstants.h" |
| 10 #include "third_party/xmllite/xmlelement.h" |
| 11 #include "webrtc/base/common.h" |
| 12 |
| 13 namespace buzz { |
| 14 |
| 15 XmlBuilder::XmlBuilder() : |
| 16 pelCurrent_(NULL), |
| 17 pelRoot_(), |
| 18 pvParents_(new std::vector<XmlElement *>()) { |
| 19 } |
| 20 |
| 21 void |
| 22 XmlBuilder::Reset() { |
| 23 pelRoot_.reset(); |
| 24 pelCurrent_ = NULL; |
| 25 pvParents_->clear(); |
| 26 } |
| 27 |
| 28 XmlElement * |
| 29 XmlBuilder::BuildElement(XmlParseContext * pctx, |
| 30 const char * name, const char ** atts) { |
| 31 QName tagName(pctx->ResolveQName(name, false)); |
| 32 if (tagName.IsEmpty()) |
| 33 return NULL; |
| 34 |
| 35 XmlElement * pelNew = new XmlElement(tagName); |
| 36 |
| 37 if (!*atts) |
| 38 return pelNew; |
| 39 |
| 40 std::set<QName> seenNonlocalAtts; |
| 41 |
| 42 while (*atts) { |
| 43 QName attName(pctx->ResolveQName(*atts, true)); |
| 44 if (attName.IsEmpty()) { |
| 45 delete pelNew; |
| 46 return NULL; |
| 47 } |
| 48 |
| 49 // verify that namespaced names are unique |
| 50 if (!attName.Namespace().empty()) { |
| 51 if (seenNonlocalAtts.count(attName)) { |
| 52 delete pelNew; |
| 53 return NULL; |
| 54 } |
| 55 seenNonlocalAtts.insert(attName); |
| 56 } |
| 57 |
| 58 pelNew->AddAttr(attName, std::string(*(atts + 1))); |
| 59 atts += 2; |
| 60 } |
| 61 |
| 62 return pelNew; |
| 63 } |
| 64 |
| 65 void |
| 66 XmlBuilder::StartElement(XmlParseContext * pctx, |
| 67 const char * name, const char ** atts) { |
| 68 XmlElement * pelNew = BuildElement(pctx, name, atts); |
| 69 if (pelNew == NULL) { |
| 70 pctx->RaiseError(XML_ERROR_SYNTAX); |
| 71 return; |
| 72 } |
| 73 |
| 74 if (!pelCurrent_) { |
| 75 pelCurrent_ = pelNew; |
| 76 pelRoot_.reset(pelNew); |
| 77 pvParents_->push_back(NULL); |
| 78 } else { |
| 79 pelCurrent_->AddElement(pelNew); |
| 80 pvParents_->push_back(pelCurrent_); |
| 81 pelCurrent_ = pelNew; |
| 82 } |
| 83 } |
| 84 |
| 85 void |
| 86 XmlBuilder::EndElement(XmlParseContext * pctx, const char * name) { |
| 87 RTC_UNUSED(pctx); |
| 88 RTC_UNUSED(name); |
| 89 pelCurrent_ = pvParents_->back(); |
| 90 pvParents_->pop_back(); |
| 91 } |
| 92 |
| 93 void |
| 94 XmlBuilder::CharacterData(XmlParseContext * pctx, |
| 95 const char * text, int len) { |
| 96 RTC_UNUSED(pctx); |
| 97 if (pelCurrent_) { |
| 98 pelCurrent_->AddParsedText(text, len); |
| 99 } |
| 100 } |
| 101 |
| 102 void |
| 103 XmlBuilder::Error(XmlParseContext * pctx, XML_Error err) { |
| 104 RTC_UNUSED(pctx); |
| 105 RTC_UNUSED(err); |
| 106 pelRoot_.reset(NULL); |
| 107 pelCurrent_ = NULL; |
| 108 pvParents_->clear(); |
| 109 } |
| 110 |
| 111 XmlElement * |
| 112 XmlBuilder::CreateElement() { |
| 113 return pelRoot_.release(); |
| 114 } |
| 115 |
| 116 XmlElement * |
| 117 XmlBuilder::BuiltElement() { |
| 118 return pelRoot_.get(); |
| 119 } |
| 120 |
| 121 XmlBuilder::~XmlBuilder() { |
| 122 } |
| 123 |
| 124 } // namespace buzz |
OLD | NEW |