123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- /*
- * Copyright 2004 The WebRTC Project Authors. All rights reserved.
- *
- * Use of this source code is governed by a BSD-style license
- * that can be found in the LICENSE file in the root of the source
- * tree. An additional intellectual property rights grant can be found
- * in the file PATENTS. All contributing project authors may
- * be found in the AUTHORS file in the root of the source tree.
- */
- #include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
- #include <ostream>
- #include <sstream>
- #include <string>
- #include <vector>
- #include "base/check.h"
- #include "third_party/libjingle_xmpp/xmllite/qname.h"
- #include "third_party/libjingle_xmpp/xmllite/xmlbuilder.h"
- #include "third_party/libjingle_xmpp/xmllite/xmlconstants.h"
- #include "third_party/libjingle_xmpp/xmllite/xmlparser.h"
- #include "third_party/libjingle_xmpp/xmllite/xmlprinter.h"
- namespace jingle_xmpp {
- XmlChild::~XmlChild() {
- }
- bool XmlText::IsTextImpl() const {
- return true;
- }
- XmlElement* XmlText::AsElementImpl() const {
- return NULL;
- }
- XmlText* XmlText::AsTextImpl() const {
- return const_cast<XmlText *>(this);
- }
- void XmlText::SetText(const std::string& text) {
- text_ = text;
- }
- void XmlText::AddParsedText(const char* buf, int len) {
- text_.append(buf, len);
- }
- void XmlText::AddText(const std::string& text) {
- text_ += text;
- }
- XmlText::~XmlText() {
- }
- XmlElement::XmlElement(const QName& name) :
- name_(name),
- first_attr_(NULL),
- last_attr_(NULL),
- first_child_(NULL),
- last_child_(NULL),
- cdata_(false) {
- }
- XmlElement::XmlElement(const XmlElement& elt) :
- XmlChild(),
- name_(elt.name_),
- first_attr_(NULL),
- last_attr_(NULL),
- first_child_(NULL),
- last_child_(NULL),
- cdata_(false) {
- // copy attributes
- XmlAttr* attr;
- XmlAttr ** plast_attr = &first_attr_;
- XmlAttr* newAttr = NULL;
- for (attr = elt.first_attr_; attr; attr = attr->NextAttr()) {
- newAttr = new XmlAttr(*attr);
- *plast_attr = newAttr;
- plast_attr = &(newAttr->next_attr_);
- }
- last_attr_ = newAttr;
- // copy children
- XmlChild* pChild;
- XmlChild ** ppLast = &first_child_;
- XmlChild* newChild = NULL;
- for (pChild = elt.first_child_; pChild; pChild = pChild->NextChild()) {
- if (pChild->IsText()) {
- newChild = new XmlText(*(pChild->AsText()));
- } else {
- newChild = new XmlElement(*(pChild->AsElement()));
- }
- *ppLast = newChild;
- ppLast = &(newChild->next_child_);
- }
- last_child_ = newChild;
- cdata_ = elt.cdata_;
- }
- XmlElement::XmlElement(const QName& name, bool useDefaultNs) :
- name_(name),
- first_attr_(useDefaultNs ? new XmlAttr(QN_XMLNS, name.Namespace()) : NULL),
- last_attr_(first_attr_),
- first_child_(NULL),
- last_child_(NULL),
- cdata_(false) {
- }
- bool XmlElement::IsTextImpl() const {
- return false;
- }
- XmlElement* XmlElement::AsElementImpl() const {
- return const_cast<XmlElement *>(this);
- }
- XmlText* XmlElement::AsTextImpl() const {
- return NULL;
- }
- const std::string XmlElement::BodyText() const {
- if (first_child_ && first_child_->IsText() && last_child_ == first_child_) {
- return first_child_->AsText()->Text();
- }
- return std::string();
- }
- void XmlElement::SetBodyText(const std::string& text) {
- if (text.empty()) {
- ClearChildren();
- } else if (first_child_ == NULL) {
- AddText(text);
- } else if (first_child_->IsText() && last_child_ == first_child_) {
- first_child_->AsText()->SetText(text);
- } else {
- ClearChildren();
- AddText(text);
- }
- }
- const QName XmlElement::FirstElementName() const {
- const XmlElement* element = FirstElement();
- if (element == NULL)
- return QName();
- return element->Name();
- }
- XmlAttr* XmlElement::FirstAttr() {
- return first_attr_;
- }
- const std::string XmlElement::Attr(const StaticQName& name) const {
- XmlAttr* attr;
- for (attr = first_attr_; attr; attr = attr->next_attr_) {
- if (attr->name_ == name)
- return attr->value_;
- }
- return std::string();
- }
- const std::string XmlElement::Attr(const QName& name) const {
- XmlAttr* attr;
- for (attr = first_attr_; attr; attr = attr->next_attr_) {
- if (attr->name_ == name)
- return attr->value_;
- }
- return std::string();
- }
- bool XmlElement::HasAttr(const StaticQName& name) const {
- XmlAttr* attr;
- for (attr = first_attr_; attr; attr = attr->next_attr_) {
- if (attr->name_ == name)
- return true;
- }
- return false;
- }
- bool XmlElement::HasAttr(const QName& name) const {
- XmlAttr* attr;
- for (attr = first_attr_; attr; attr = attr->next_attr_) {
- if (attr->name_ == name)
- return true;
- }
- return false;
- }
- void XmlElement::SetAttr(const QName& name, const std::string& value) {
- XmlAttr* attr;
- for (attr = first_attr_; attr; attr = attr->next_attr_) {
- if (attr->name_ == name)
- break;
- }
- if (!attr) {
- attr = new XmlAttr(name, value);
- if (last_attr_)
- last_attr_->next_attr_ = attr;
- else
- first_attr_ = attr;
- last_attr_ = attr;
- return;
- }
- attr->value_ = value;
- }
- void XmlElement::ClearAttr(const QName& name) {
- XmlAttr* attr;
- XmlAttr* last_attr = NULL;
- for (attr = first_attr_; attr; attr = attr->next_attr_) {
- if (attr->name_ == name)
- break;
- last_attr = attr;
- }
- if (!attr)
- return;
- if (!last_attr)
- first_attr_ = attr->next_attr_;
- else
- last_attr->next_attr_ = attr->next_attr_;
- if (last_attr_ == attr)
- last_attr_ = last_attr;
- delete attr;
- }
- XmlChild* XmlElement::FirstChild() {
- return first_child_;
- }
- XmlElement* XmlElement::FirstElement() {
- XmlChild* pChild;
- for (pChild = first_child_; pChild; pChild = pChild->next_child_) {
- if (!pChild->IsText())
- return pChild->AsElement();
- }
- return NULL;
- }
- XmlElement* XmlElement::NextElement() {
- XmlChild* pChild;
- for (pChild = next_child_; pChild; pChild = pChild->next_child_) {
- if (!pChild->IsText())
- return pChild->AsElement();
- }
- return NULL;
- }
- XmlElement* XmlElement::FirstWithNamespace(const std::string& ns) {
- XmlChild* pChild;
- for (pChild = first_child_; pChild; pChild = pChild->next_child_) {
- if (!pChild->IsText() && pChild->AsElement()->Name().Namespace() == ns)
- return pChild->AsElement();
- }
- return NULL;
- }
- XmlElement *
- XmlElement::NextWithNamespace(const std::string& ns) {
- XmlChild* pChild;
- for (pChild = next_child_; pChild; pChild = pChild->next_child_) {
- if (!pChild->IsText() && pChild->AsElement()->Name().Namespace() == ns)
- return pChild->AsElement();
- }
- return NULL;
- }
- XmlElement *
- XmlElement::FirstNamed(const QName& name) {
- XmlChild* pChild;
- for (pChild = first_child_; pChild; pChild = pChild->next_child_) {
- if (!pChild->IsText() && pChild->AsElement()->Name() == name)
- return pChild->AsElement();
- }
- return NULL;
- }
- XmlElement *
- XmlElement::FirstNamed(const StaticQName& name) {
- XmlChild* pChild;
- for (pChild = first_child_; pChild; pChild = pChild->next_child_) {
- if (!pChild->IsText() && pChild->AsElement()->Name() == name)
- return pChild->AsElement();
- }
- return NULL;
- }
- XmlElement *
- XmlElement::NextNamed(const QName& name) {
- XmlChild* pChild;
- for (pChild = next_child_; pChild; pChild = pChild->next_child_) {
- if (!pChild->IsText() && pChild->AsElement()->Name() == name)
- return pChild->AsElement();
- }
- return NULL;
- }
- XmlElement *
- XmlElement::NextNamed(const StaticQName& name) {
- XmlChild* pChild;
- for (pChild = next_child_; pChild; pChild = pChild->next_child_) {
- if (!pChild->IsText() && pChild->AsElement()->Name() == name)
- return pChild->AsElement();
- }
- return NULL;
- }
- XmlElement* XmlElement::FindOrAddNamedChild(const QName& name) {
- XmlElement* child = FirstNamed(name);
- if (!child) {
- child = new XmlElement(name);
- AddElement(child);
- }
- return child;
- }
- const std::string XmlElement::TextNamed(const QName& name) const {
- XmlChild* pChild;
- for (pChild = first_child_; pChild; pChild = pChild->next_child_) {
- if (!pChild->IsText() && pChild->AsElement()->Name() == name)
- return pChild->AsElement()->BodyText();
- }
- return std::string();
- }
- void XmlElement::InsertChildAfter(XmlChild* predecessor, XmlChild* next) {
- if (predecessor == NULL) {
- next->next_child_ = first_child_;
- first_child_ = next;
- }
- else {
- next->next_child_ = predecessor->next_child_;
- predecessor->next_child_ = next;
- }
- }
- void XmlElement::RemoveChildAfter(XmlChild* predecessor) {
- XmlChild* next;
- if (predecessor == NULL) {
- next = first_child_;
- first_child_ = next->next_child_;
- }
- else {
- next = predecessor->next_child_;
- predecessor->next_child_ = next->next_child_;
- }
- if (last_child_ == next)
- last_child_ = predecessor;
- delete next;
- }
- void XmlElement::AddAttr(const QName& name, const std::string& value) {
- DCHECK(!HasAttr(name));
- XmlAttr ** pprev = last_attr_ ? &(last_attr_->next_attr_) : &first_attr_;
- last_attr_ = (*pprev = new XmlAttr(name, value));
- }
- void XmlElement::AddAttr(const QName& name, const std::string& value,
- int depth) {
- XmlElement* element = this;
- while (depth--) {
- element = element->last_child_->AsElement();
- }
- element->AddAttr(name, value);
- }
- void XmlElement::AddParsedText(const char* cstr, int len) {
- if (len == 0)
- return;
- if (last_child_ && last_child_->IsText()) {
- last_child_->AsText()->AddParsedText(cstr, len);
- return;
- }
- XmlChild ** pprev = last_child_ ? &(last_child_->next_child_) : &first_child_;
- last_child_ = *pprev = new XmlText(cstr, len);
- }
- void XmlElement::AddCDATAText(const char* buf, int len) {
- cdata_ = true;
- AddParsedText(buf, len);
- }
- void XmlElement::AddText(const std::string& text) {
- if (text == STR_EMPTY)
- return;
- if (last_child_ && last_child_->IsText()) {
- last_child_->AsText()->AddText(text);
- return;
- }
- XmlChild ** pprev = last_child_ ? &(last_child_->next_child_) : &first_child_;
- last_child_ = *pprev = new XmlText(text);
- }
- void XmlElement::AddText(const std::string& text, int depth) {
- // note: the first syntax is ambigious for msvc 6
- // XmlElement* pel(this);
- XmlElement* element = this;
- while (depth--) {
- element = element->last_child_->AsElement();
- }
- element->AddText(text);
- }
- void XmlElement::AddElement(XmlElement *child) {
- if (child == NULL)
- return;
- XmlChild ** pprev = last_child_ ? &(last_child_->next_child_) : &first_child_;
- *pprev = child;
- last_child_ = child;
- child->next_child_ = NULL;
- }
- void XmlElement::AddElement(XmlElement *child, int depth) {
- XmlElement* element = this;
- while (depth--) {
- element = element->last_child_->AsElement();
- }
- element->AddElement(child);
- }
- void XmlElement::ClearNamedChildren(const QName& name) {
- XmlChild* prev_child = NULL;
- XmlChild* next_child;
- XmlChild* child;
- for (child = FirstChild(); child; child = next_child) {
- next_child = child->NextChild();
- if (!child->IsText() && child->AsElement()->Name() == name)
- {
- RemoveChildAfter(prev_child);
- continue;
- }
- prev_child = child;
- }
- }
- void XmlElement::ClearAttributes() {
- XmlAttr* attr;
- for (attr = first_attr_; attr; ) {
- XmlAttr* to_delete = attr;
- attr = attr->next_attr_;
- delete to_delete;
- }
- first_attr_ = last_attr_ = NULL;
- }
- void XmlElement::ClearChildren() {
- XmlChild* pchild;
- for (pchild = first_child_; pchild; ) {
- XmlChild* to_delete = pchild;
- pchild = pchild->next_child_;
- delete to_delete;
- }
- first_child_ = last_child_ = NULL;
- }
- std::string XmlElement::Str() const {
- std::stringstream ss;
- XmlPrinter::PrintXml(&ss, this);
- return ss.str();
- }
- XmlElement* XmlElement::ForStr(const std::string& str) {
- XmlBuilder builder;
- XmlParser::ParseXml(&builder, str);
- return builder.CreateElement();
- }
- XmlElement::~XmlElement() {
- XmlAttr* attr;
- for (attr = first_attr_; attr; ) {
- XmlAttr* to_delete = attr;
- attr = attr->next_attr_;
- delete to_delete;
- }
- XmlChild* pchild;
- for (pchild = first_child_; pchild; ) {
- XmlChild* to_delete = pchild;
- pchild = pchild->next_child_;
- delete to_delete;
- }
- }
- } // namespace jingle_xmpp
|